• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

raphw / byte-buddy / #801

27 Oct 2025 09:37AM UTC coverage: 84.715% (-0.4%) from 85.118%
#801

push

raphw
Fix imports.

29586 of 34924 relevant lines covered (84.72%)

0.85 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

81.82
/byte-buddy-dep/src/main/java/net/bytebuddy/implementation/bytecode/constant/SerializedConstant.java
1
/*
2
 * Copyright 2014 - Present Rafael Winterhalter
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *     http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
package net.bytebuddy.implementation.bytecode.constant;
17

18
import net.bytebuddy.build.HashCodeAndEqualsPlugin;
19
import net.bytebuddy.description.method.MethodDescription;
20
import net.bytebuddy.description.type.TypeDescription;
21
import net.bytebuddy.implementation.Implementation;
22
import net.bytebuddy.implementation.bytecode.Duplication;
23
import net.bytebuddy.implementation.bytecode.StackManipulation;
24
import net.bytebuddy.implementation.bytecode.TypeCreation;
25
import net.bytebuddy.implementation.bytecode.member.MethodInvocation;
26
import net.bytebuddy.utility.nullability.MaybeNull;
27
import org.objectweb.asm.MethodVisitor;
28

29
import java.io.ByteArrayInputStream;
30
import java.io.ByteArrayOutputStream;
31
import java.io.IOException;
32
import java.io.InputStream;
33
import java.io.ObjectInputStream;
34
import java.io.ObjectOutputStream;
35
import java.io.Serializable;
36

37
/**
38
 * A constant that represents a value in its serialized form.
39
 */
40
@HashCodeAndEqualsPlugin.Enhance
41
public class SerializedConstant extends StackManipulation.AbstractBase {
42

43
    /**
44
     * A charset that does not change the supplied byte array upon encoding or decoding.
45
     */
46
    private static final String CHARSET = "ISO-8859-1";
47

48
    /**
49
     * The serialized value.
50
     */
51
    private final String serialization;
52

53
    /**
54
     * Creates a new constant for a serialized value.
55
     *
56
     * @param serialization The serialized value.
57
     */
58
    protected SerializedConstant(String serialization) {
1✔
59
        this.serialization = serialization;
1✔
60
    }
1✔
61

62
    /**
63
     * Creates a new stack manipulation to load the supplied value onto the stack.
64
     *
65
     * @param value The value to serialize or {@code null}.
66
     * @return A stack manipulation to serialize the supplied value.
67
     */
68
    public static StackManipulation of(@MaybeNull Serializable value) {
69
        if (value == null) {
1✔
70
            return NullConstant.INSTANCE;
1✔
71
        }
72
        try {
73
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
1✔
74
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
1✔
75
            try {
76
                objectOutputStream.writeObject(value);
1✔
77
            } finally {
78
                objectOutputStream.close();
1✔
79
            }
80
            return new SerializedConstant(byteArrayOutputStream.toString(CHARSET));
1✔
81
        } catch (IOException exception) {
×
82
            throw new IllegalStateException("Cannot serialize " + value, exception);
×
83
        }
84
    }
85

86
    /**
87
     * {@inheritDoc}
88
     */
89
    public Size apply(MethodVisitor methodVisitor, Implementation.Context implementationContext) {
90
        try {
91
            return new StackManipulation.Compound(
1✔
92
                    TypeCreation.of(TypeDescription.ForLoadedType.of(ObjectInputStream.class)),
1✔
93
                    Duplication.SINGLE,
94
                    TypeCreation.of(TypeDescription.ForLoadedType.of(ByteArrayInputStream.class)),
1✔
95
                    Duplication.SINGLE,
96
                    new TextConstant(serialization),
97
                    new TextConstant(CHARSET),
98
                    MethodInvocation.invoke(new MethodDescription.ForLoadedMethod(String.class.getMethod("getBytes", String.class))),
1✔
99
                    MethodInvocation.invoke(new MethodDescription.ForLoadedConstructor(ByteArrayInputStream.class.getConstructor(byte[].class))),
1✔
100
                    MethodInvocation.invoke(new MethodDescription.ForLoadedConstructor(ObjectInputStream.class.getConstructor(InputStream.class))),
1✔
101
                    MethodInvocation.invoke(new MethodDescription.ForLoadedMethod(ObjectInputStream.class.getMethod("readObject")))
1✔
102
            ).apply(methodVisitor, implementationContext);
1✔
103
        } catch (NoSuchMethodException exception) {
×
104
            throw new IllegalStateException("Could not locate Java API method", exception);
×
105
        }
106
    }
107
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc