• 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

94.12
/byte-buddy-dep/src/main/java/net/bytebuddy/implementation/ExceptionMethod.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;
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.dynamic.scaffold.InstrumentedType;
22
import net.bytebuddy.implementation.bytecode.ByteCodeAppender;
23
import net.bytebuddy.implementation.bytecode.Duplication;
24
import net.bytebuddy.implementation.bytecode.StackManipulation;
25
import net.bytebuddy.implementation.bytecode.Throw;
26
import net.bytebuddy.implementation.bytecode.TypeCreation;
27
import net.bytebuddy.implementation.bytecode.constant.TextConstant;
28
import net.bytebuddy.implementation.bytecode.member.MethodInvocation;
29
import org.objectweb.asm.MethodVisitor;
30

31
import static net.bytebuddy.matcher.ElementMatchers.isConstructor;
32
import static net.bytebuddy.matcher.ElementMatchers.takesArguments;
33

34
/**
35
 * This implementation causes a {@link java.lang.Throwable} to be thrown when the instrumented method is invoked.
36
 * Be aware that the Java Virtual machine does not care about exception declarations and will throw any
37
 * {@link java.lang.Throwable} from any method even if the method does not declared a checked exception.
38
 */
39
@HashCodeAndEqualsPlugin.Enhance
40
public class ExceptionMethod implements Implementation, ByteCodeAppender {
41

42
    /**
43
     * The construction delegation which is responsible for creating the exception to be thrown.
44
     */
45
    private final ConstructionDelegate constructionDelegate;
46

47
    /**
48
     * Creates a new instance of an implementation for throwing throwables.
49
     *
50
     * @param constructionDelegate A delegate that is responsible for calling the {@link Throwable}'s constructor.
51
     */
52
    public ExceptionMethod(ConstructionDelegate constructionDelegate) {
1✔
53
        this.constructionDelegate = constructionDelegate;
1✔
54
    }
1✔
55

56
    /**
57
     * Creates an implementation that creates a new instance of the given {@link Throwable} type on each method invocation
58
     * which is then thrown immediately. For this to be possible, the given type must define a default constructor
59
     * which is visible from the instrumented type.
60
     *
61
     * @param throwableType The type of the {@link Throwable}.
62
     * @return An implementation that will throw an instance of the {@link Throwable} on each method invocation of the
63
     * instrumented methods.
64
     */
65
    public static Implementation throwing(Class<? extends Throwable> throwableType) {
66
        return throwing(TypeDescription.ForLoadedType.of(throwableType));
1✔
67
    }
68

69
    /**
70
     * Creates an implementation that creates a new instance of the given {@link Throwable} type on each method invocation
71
     * which is then thrown immediately. For this to be possible, the given type must define a default constructor
72
     * which is visible from the instrumented type.
73
     *
74
     * @param throwableType The type of the {@link Throwable}.
75
     * @return An implementation that will throw an instance of the {@link Throwable} on each method invocation of the
76
     * instrumented methods.
77
     */
78
    public static Implementation throwing(TypeDescription throwableType) {
79
        if (!throwableType.isAssignableTo(Throwable.class)) {
1✔
80
            throw new IllegalArgumentException(throwableType + " does not extend throwable");
×
81
        }
82
        return new ExceptionMethod(new ConstructionDelegate.ForDefaultConstructor(throwableType));
1✔
83
    }
84

85
    /**
86
     * Creates an implementation that creates a new instance of the given {@link Throwable} type on each method
87
     * invocation which is then thrown immediately. For this to be possible, the given type must define a
88
     * constructor that takes a single {@link java.lang.String} as its argument.
89
     *
90
     * @param throwableType The type of the {@link Throwable}.
91
     * @param message       The string that is handed to the constructor. Usually an exception message.
92
     * @return An implementation that will throw an instance of the {@link Throwable} on each method invocation
93
     * of the instrumented methods.
94
     */
95
    public static Implementation throwing(Class<? extends Throwable> throwableType, String message) {
96
        return throwing(TypeDescription.ForLoadedType.of(throwableType), message);
1✔
97
    }
98

99
    /**
100
     * Creates an implementation that creates a new instance of the given {@link Throwable} type on each method
101
     * invocation which is then thrown immediately. For this to be possible, the given type must define a
102
     * constructor that takes a single {@link java.lang.String} as its argument.
103
     *
104
     * @param throwableType The type of the {@link Throwable}.
105
     * @param message       The string that is handed to the constructor. Usually an exception message.
106
     * @return An implementation that will throw an instance of the {@link Throwable} on each method invocation
107
     * of the instrumented methods.
108
     */
109
    public static Implementation throwing(TypeDescription throwableType, String message) {
110
        if (!throwableType.isAssignableTo(Throwable.class)) {
1✔
111
            throw new IllegalArgumentException(throwableType + " does not extend throwable");
×
112
        }
113
        return new ExceptionMethod(new ConstructionDelegate.ForStringConstructor(throwableType, message));
1✔
114
    }
115

116
    /**
117
     * {@inheritDoc}
118
     */
119
    public InstrumentedType prepare(InstrumentedType instrumentedType) {
120
        return instrumentedType;
1✔
121
    }
122

123
    /**
124
     * {@inheritDoc}
125
     */
126
    public ByteCodeAppender appender(Target implementationTarget) {
127
        return this;
1✔
128
    }
129

130
    /**
131
     * {@inheritDoc}
132
     */
133
    public Size apply(MethodVisitor methodVisitor, Context implementationContext, MethodDescription instrumentedMethod) {
134
        StackManipulation.Size stackSize = new StackManipulation.Compound(
1✔
135
                constructionDelegate.make(),
1✔
136
                Throw.INSTANCE
137
        ).apply(methodVisitor, implementationContext);
1✔
138
        return new Size(stackSize.getMaximalSize(), instrumentedMethod.getStackSize());
1✔
139
    }
140

141
    /**
142
     * A construction delegate is responsible for calling a {@link Throwable}'s constructor.
143
     */
144
    public interface ConstructionDelegate {
145

146
        /**
147
         * Creates a stack manipulation that creates pushes all constructor arguments onto the operand stack
148
         * and subsequently calls the constructor.
149
         *
150
         * @return A stack manipulation for constructing a {@link Throwable}.
151
         */
152
        StackManipulation make();
153

154
        /**
155
         * A construction delegate that calls the default constructor.
156
         */
157
        @HashCodeAndEqualsPlugin.Enhance
158
        class ForDefaultConstructor implements ConstructionDelegate {
159

160
            /**
161
             * The type of the exception that is to be thrown.
162
             */
163
            private final TypeDescription throwableType;
164

165
            /**
166
             * The constructor that is used for creating the exception.
167
             */
168
            private final MethodDescription targetConstructor;
169

170
            /**
171
             * Creates a new construction delegate that calls a default constructor.
172
             *
173
             * @param throwableType The type of the {@link Throwable}.
174
             */
175
            public ForDefaultConstructor(TypeDescription throwableType) {
1✔
176
                this.throwableType = throwableType;
1✔
177
                this.targetConstructor = throwableType.getDeclaredMethods()
1✔
178
                        .filter(isConstructor().and(takesArguments(0))).getOnly();
1✔
179
            }
1✔
180

181
            /**
182
             * {@inheritDoc}
183
             */
184
            public StackManipulation make() {
185
                return new StackManipulation.Compound(
1✔
186
                        TypeCreation.of(throwableType),
1✔
187
                        Duplication.SINGLE,
188
                        MethodInvocation.invoke(targetConstructor));
1✔
189
            }
190
        }
191

192
        /**
193
         * A construction delegate that calls a constructor that takes a single string as its argument.
194
         */
195
        @HashCodeAndEqualsPlugin.Enhance
196
        class ForStringConstructor implements ConstructionDelegate {
197

198
            /**
199
             * The type of the exception that is to be thrown.
200
             */
201
            private final TypeDescription throwableType;
202

203
            /**
204
             * The constructor that is used for creating the exception.
205
             */
206
            private final MethodDescription targetConstructor;
207

208
            /**
209
             * The {@link java.lang.String} that is to be passed to the exception's constructor.
210
             */
211
            private final String message;
212

213
            /**
214
             * Creates a new construction delegate that calls a constructor by handing it the given string.
215
             *
216
             * @param throwableType The type of the {@link Throwable}.
217
             * @param message       The string that is handed to the constructor.
218
             */
219
            public ForStringConstructor(TypeDescription throwableType, String message) {
1✔
220
                this.throwableType = throwableType;
1✔
221
                this.targetConstructor = throwableType.getDeclaredMethods()
1✔
222
                        .filter(isConstructor().and(takesArguments(String.class))).getOnly();
1✔
223
                this.message = message;
1✔
224
            }
1✔
225

226
            /**
227
             * {@inheritDoc}
228
             */
229
            public StackManipulation make() {
230
                return new StackManipulation.Compound(
1✔
231
                        TypeCreation.of(throwableType),
1✔
232
                        Duplication.SINGLE,
233
                        new TextConstant(message),
234
                        MethodInvocation.invoke(targetConstructor));
1✔
235
            }
236
        }
237
    }
238
}
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