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

raphw / byte-buddy / #811

03 Nov 2025 10:41AM UTC coverage: 83.677% (-0.3%) from 84.023%
#811

push

raphw
Fix null handling.

0 of 1 new or added line in 1 file covered. (0.0%)

791 existing lines in 12 files now uncovered.

29814 of 35630 relevant lines covered (83.68%)

0.84 hits per line

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

89.29
/byte-buddy-dep/src/main/java/net/bytebuddy/build/RepeatedAnnotationPlugin.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 edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
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.TypeAttributeAppender;
25
import org.objectweb.asm.AnnotationVisitor;
26
import org.objectweb.asm.ClassVisitor;
27
import org.objectweb.asm.Type;
28

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

36
import static net.bytebuddy.matcher.ElementMatchers.isAnnotatedWith;
37
import static net.bytebuddy.matcher.ElementMatchers.named;
38

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

47
    /**
48
     * A description of the {@link Enhance#value()} method.
49
     */
50
    private static final MethodDescription.InDefinedShape VALUE = TypeDescription.ForLoadedType.of(Enhance.class)
1✔
51
            .getDeclaredMethods()
1✔
52
            .filter(named("value"))
1✔
53
            .getOnly();
1✔
54

55
    /**
56
     * Creates a new plugin for creating repeated annotations.
57
     */
58
    public RepeatedAnnotationPlugin() {
59
        super(isAnnotatedWith(Enhance.class));
1✔
60
    }
1✔
61

62
    /**
63
     * {@inheritDoc}
64
     */
65
    public Plugin make() {
UNCOV
66
        return this;
×
67
    }
68

69
    /**
70
     * {@inheritDoc}
71
     */
72
    @SuppressFBWarnings(value = "NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE", justification = "Assuming component type for array type.")
73
    public DynamicType.Builder<?> apply(DynamicType.Builder<?> builder, TypeDescription typeDescription, ClassFileLocator classFileLocator) {
74
        TypeDescription target = typeDescription.getDeclaredAnnotations()
1✔
75
                .ofType(Enhance.class)
1✔
76
                .getValue(VALUE)
1✔
77
                .resolve(TypeDescription.class);
1✔
78
        if (!target.isAnnotation()) {
1✔
79
            throw new IllegalStateException("Expected " + target + " to be an annotation type");
1✔
80
        } else if (target.getDeclaredMethods().size() != 1
1✔
81
                || target.getDeclaredMethods().filter(named("value")).size() != 1
1✔
82
                || !target.getDeclaredMethods().filter(named("value")).getOnly().getReturnType().isArray()
1✔
83
                || !target.getDeclaredMethods().filter(named("value")).getOnly().getReturnType().getComponentType().asErasure().equals(typeDescription)) {
1✔
UNCOV
84
            throw new IllegalStateException("Expected " + target + " to declare exactly one property named value of an array type");
×
85
        }
86
        return builder.attribute(new RepeatedAnnotationAppender(target));
1✔
87
    }
88

89
    /**
90
     * {@inheritDoc}
91
     */
92
    public void close() {
93
        /* do nothing */
UNCOV
94
    }
×
95

96
    /**
97
     * Indicates that the annotated annotation should be repeatable by the supplied annotation.
98
     */
99
    @Documented
100
    @Target(ElementType.ANNOTATION_TYPE)
101
    @Retention(RetentionPolicy.RUNTIME)
102
    public @interface Enhance {
103

104
        /**
105
         * The repeating annotation type.
106
         *
107
         * @return The repeating annotation type.
108
         */
109
        Class<? extends Annotation> value();
110
    }
111

112
    /**
113
     * A type attribute appender that adds a repeated annotation for a target type.
114
     */
115
    @HashCodeAndEqualsPlugin.Enhance
116
    protected static class RepeatedAnnotationAppender implements TypeAttributeAppender {
117

118
        /**
119
         * The repeated type.
120
         */
121
        private final TypeDescription target;
122

123
        /**
124
         * Creates a new appender.
125
         *
126
         * @param target The repeated type.
127
         */
128
        protected RepeatedAnnotationAppender(TypeDescription target) {
1✔
129
            this.target = target;
1✔
130
        }
1✔
131

132
        /**
133
         * {@inheritDoc}
134
         */
135
        public void apply(ClassVisitor classVisitor, TypeDescription instrumentedType, AnnotationValueFilter annotationValueFilter) {
136
            AnnotationVisitor visitor = classVisitor.visitAnnotation("Ljava/lang/annotation/Repeatable;", true);
1✔
137
            if (visitor != null) {
1✔
138
                visitor.visit("value", Type.getType(target.getDescriptor()));
1✔
139
                visitor.visitEnd();
1✔
140
            }
141
        }
1✔
142
    }
143
}
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