• 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/build/SafeVarargsPlugin.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.build;
17

18
import net.bytebuddy.asm.MemberAttributeExtension;
19
import net.bytebuddy.description.method.MethodDescription;
20
import net.bytebuddy.description.type.TypeDescription;
21
import net.bytebuddy.dynamic.ClassFileLocator;
22
import net.bytebuddy.dynamic.DynamicType;
23
import net.bytebuddy.implementation.attribute.AnnotationValueFilter;
24
import net.bytebuddy.implementation.attribute.MethodAttributeAppender;
25
import org.objectweb.asm.AnnotationVisitor;
26
import org.objectweb.asm.MethodVisitor;
27

28
import java.lang.annotation.Documented;
29
import java.lang.annotation.ElementType;
30
import java.lang.annotation.Retention;
31
import java.lang.annotation.RetentionPolicy;
32
import java.lang.annotation.Target;
33

34
import static net.bytebuddy.matcher.ElementMatchers.declaresMethod;
35
import static net.bytebuddy.matcher.ElementMatchers.isAnnotatedWith;
36

37
/**
38
 * A plugin that allows for adding a {@code java.lang.SafeVarargs} annotation even if compiled prior to Java 7
39
 * which introduces this annotation. As the annotation is not present on previous JVM versions, it is ignored
40
 * at runtime for older JVM versions what makes this approach feasible.
41
 */
42
@HashCodeAndEqualsPlugin.Enhance
43
public class SafeVarargsPlugin extends Plugin.ForElementMatcher {
44

45
    /**
46
     * Creates a new plugin for creating repeated annotations.
47
     */
48
    public SafeVarargsPlugin() {
49
        super(declaresMethod(isAnnotatedWith(Enhance.class)));
1✔
50
    }
1✔
51

52
    /**
53
     * {@inheritDoc}
54
     */
55
    public DynamicType.Builder<?> apply(DynamicType.Builder<?> builder, TypeDescription typeDescription, ClassFileLocator classFileLocator) {
56
        return builder.visit(new MemberAttributeExtension.ForMethod()
1✔
57
                .attribute(SafeVarargsAppender.INSTANCE)
1✔
58
                .on(isAnnotatedWith(Enhance.class)));
1✔
59
    }
60

61
    /**
62
     * {@inheritDoc}
63
     */
64
    public void close() {
65
        /* do nothing */
66
    }
×
67

68
    /**
69
     * Defines a safe-varargs annotation.
70
     */
71
    @Documented
72
    @Target({ElementType.CONSTRUCTOR, ElementType.METHOD})
73
    @Retention(RetentionPolicy.RUNTIME)
74
    public @interface Enhance {
75
        /* empty */
76
    }
77

78
    /**
79
     * Appends the varargs annotation.
80
     */
81
    protected enum SafeVarargsAppender implements MethodAttributeAppender, MethodAttributeAppender.Factory {
1✔
82

83
        /**
84
         * The singleton instance.
85
         */
86
        INSTANCE;
1✔
87

88
        /**
89
         * {@inheritDoc}
90
         */
91
        public MethodAttributeAppender make(TypeDescription typeDescription) {
92
            return this;
1✔
93
        }
94

95
        /**
96
         * {@inheritDoc}
97
         */
98
        public void apply(MethodVisitor methodVisitor, MethodDescription methodDescription, AnnotationValueFilter annotationValueFilter) {
99
            if (!methodDescription.isVarArgs()) {
1✔
100
                throw new IllegalStateException(methodDescription + " does not have variable arguments");
1✔
101
            } else if (!methodDescription.isConstructor() && !methodDescription.isStatic() && !methodDescription.isFinal()) {
1✔
102
                throw new IllegalStateException(methodDescription + " is neither a constructor or final and cannot declare safe varargs");
1✔
103
            }
104
            AnnotationVisitor visitor = methodVisitor.visitAnnotation("Ljava/lang/SafeVarargs;", true);
1✔
105
            if (visitor != null) {
1✔
106
                visitor.visitEnd();
1✔
107
            }
108
        }
1✔
109
    }
110
}
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