• 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

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

82
    /**
83
     * {@inheritDoc}
84
     */
85
    public void close() {
86
        /* do nothing */
87
    }
×
88

89
    /**
90
     * Indicates that the annotated annotation should be repeatable by the supplied annotation.
91
     */
92
    @Documented
93
    @Target(ElementType.ANNOTATION_TYPE)
94
    @Retention(RetentionPolicy.RUNTIME)
95
    public @interface Enhance {
96

97
        /**
98
         * The repeating annotation type.
99
         *
100
         * @return The repeating annotation type.
101
         */
102
        Class<? extends Annotation> value();
103
    }
104

105
    /**
106
     * A type attribute appender that adds a repeated annotation for a target type.
107
     */
108
    @HashCodeAndEqualsPlugin.Enhance
109
    protected static class RepeatedAnnotationAppender implements TypeAttributeAppender {
110

111
        /**
112
         * The repeated type.
113
         */
114
        private final TypeDescription target;
115

116
        /**
117
         * Creates a new appender.
118
         *
119
         * @param target The repeated type.
120
         */
121
        protected RepeatedAnnotationAppender(TypeDescription target) {
1✔
122
            this.target = target;
1✔
123
        }
1✔
124

125
        /**
126
         * {@inheritDoc}
127
         */
128
        public void apply(ClassVisitor classVisitor, TypeDescription instrumentedType, AnnotationValueFilter annotationValueFilter) {
129
            AnnotationVisitor visitor = classVisitor.visitAnnotation("Ljava/lang/annotation/Repeatable;", true);
1✔
130
            if (visitor != null) {
1✔
131
                visitor.visit("value", Type.getType(target.getDescriptor()));
1✔
132
                visitor.visitEnd();
1✔
133
            }
134
        }
1✔
135
    }
136
}
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