org.easymock.bytebuddy.build.RepeatedAnnotationPlugin Maven / Gradle / Ivy
/*
* Copyright 2014 - Present Rafael Winterhalter
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.easymock.bytebuddy.build;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.easymock.bytebuddy.description.method.MethodDescription;
import org.easymock.bytebuddy.description.type.TypeDescription;
import org.easymock.bytebuddy.dynamic.ClassFileLocator;
import org.easymock.bytebuddy.dynamic.DynamicType;
import org.easymock.bytebuddy.implementation.attribute.AnnotationValueFilter;
import org.easymock.bytebuddy.implementation.attribute.TypeAttributeAppender;
import org.easymock.bytebuddy.jar.asm.AnnotationVisitor;
import org.easymock.bytebuddy.jar.asm.ClassVisitor;
import org.easymock.bytebuddy.jar.asm.Type;
import java.lang.annotation.*;
import static org.easymock.bytebuddy.matcher.ElementMatchers.isAnnotatedWith;
import static org.easymock.bytebuddy.matcher.ElementMatchers.named;
/**
* A plugin that allows for adding a {@code java.lang.annotation.Repeatable} annotation even if compiled prior to
* Java 8 which introduces this annotation. As the annotation is not present on previous JVM versions, it is ignored
* at runtime for older JVM versions what makes this approach feasible.
*/
@HashCodeAndEqualsPlugin.Enhance
public class RepeatedAnnotationPlugin extends Plugin.ForElementMatcher {
/**
* A description of the {@link Enhance#value()} method.
*/
private static final MethodDescription.InDefinedShape VALUE = TypeDescription.ForLoadedType.of(Enhance.class)
.getDeclaredMethods()
.filter(named("value"))
.getOnly();
/**
* Creates a new plugin for creating repeated annotations.
*/
public RepeatedAnnotationPlugin() {
super(isAnnotatedWith(Enhance.class));
}
/**
* {@inheritDoc}
*/
@SuppressFBWarnings(value = "NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE", justification = "Annotation presence is required by matcher.")
public DynamicType.Builder> apply(DynamicType.Builder> builder, TypeDescription typeDescription, ClassFileLocator classFileLocator) {
TypeDescription target = typeDescription.getDeclaredAnnotations()
.ofType(Enhance.class)
.getValue(VALUE)
.resolve(TypeDescription.class);
if (!target.isAnnotation()) {
throw new IllegalStateException("Expected " + target + " to be an annotation type");
} else if (target.getDeclaredMethods().size() != 1
|| target.getDeclaredMethods().filter(named("value")).size() != 1
|| !target.getDeclaredMethods().filter(named("value")).getOnly().getReturnType().isArray()
|| !target.getDeclaredMethods().filter(named("value")).getOnly().getReturnType().getComponentType().asErasure().equals(typeDescription)) {
throw new IllegalStateException("Expected " + target + " to declare exactly one property named value of an array type");
}
return builder.attribute(new RepeatedAnnotationAppender(target));
}
/**
* {@inheritDoc}
*/
public void close() {
/* do nothing */
}
/**
* Indicates that the annotated annotation should be repeatable by the supplied annotation.
*/
@Documented
@Target(ElementType.ANNOTATION_TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Enhance {
/**
* The repeating annotation type.
*
* @return The repeating annotation type.
*/
Class extends Annotation> value();
}
/**
* A type attribute appender that adds a repeated annotation for a target type.
*/
@HashCodeAndEqualsPlugin.Enhance
protected static class RepeatedAnnotationAppender implements TypeAttributeAppender {
/**
* The repeated type.
*/
private final TypeDescription target;
/**
* Creates a new appender.
*
* @param target The repeated type.
*/
protected RepeatedAnnotationAppender(TypeDescription target) {
this.target = target;
}
/**
* {@inheritDoc}
*/
public void apply(ClassVisitor classVisitor, TypeDescription instrumentedType, AnnotationValueFilter annotationValueFilter) {
AnnotationVisitor visitor = classVisitor.visitAnnotation("Ljava/lang/annotation/Repeatable;", true);
if (visitor != null) {
visitor.visit("value", Type.getType(target.getDescriptor()));
visitor.visitEnd();
}
}
}
}