/** * {@inheritDoc} */ public boolean isAnnotationPresent(TypeDescription annotationType) { for (AnnotationDescription annotation : this) { if (annotation.getAnnotationType().equals(annotationType)) { return true; } } return false; }
@Override public boolean equals(Object other) { if (this == other) { return true; } else if (!(other instanceof AnnotationDescription)) { return false; } AnnotationDescription annotationDescription = ((AnnotationDescription) other); TypeDescription annotationType = getAnnotationType(); if (!annotationDescription.getAnnotationType().equals(annotationType)) { return false; } for (MethodDescription.InDefinedShape methodDescription : annotationType.getDeclaredMethods()) { if (!getValue(methodDescription).equals(annotationDescription.getValue(methodDescription))) { return false; } } return true; }
/** * Validates the type annotations on a formal type variable but not on its bounds.. * * @param typeVariable The type variable to validate. * @return {@code true} if the formal type variable declares invalid type annotations. */ public static boolean ofFormalTypeVariable(Generic typeVariable) { Set<TypeDescription> annotationTypes = new HashSet<TypeDescription>(); for (AnnotationDescription annotationDescription : typeVariable.getDeclaredAnnotations()) { if (!annotationDescription.getElementTypes().contains(INSTANCE.typeParameter) || !annotationTypes.add(annotationDescription.getAnnotationType())) { return false; } } return true; }
/** * {@inheritDoc} */ @SuppressWarnings("unchecked") public <T extends Annotation> AnnotationDescription.Loadable<T> ofType(Class<T> annotationType) { for (AnnotationDescription annotation : this) { if (annotation.getAnnotationType().represents(annotationType)) { return annotation.prepare(annotationType); } } return (AnnotationDescription.Loadable<T>) AnnotationDescription.UNDEFINED; }
/** * {@inheritDoc} */ public AnnotationList inherited(Set<? extends TypeDescription> ignoredTypes) { List<AnnotationDescription> inherited = new ArrayList<AnnotationDescription>(size()); for (AnnotationDescription annotation : this) { if (!ignoredTypes.contains(annotation.getAnnotationType()) && annotation.isInherited()) { inherited.add(annotation); } } return wrap(inherited); }
/** {@inheritDoc} */ public boolean isRelevant(AnnotationDescription annotationDescription, MethodDescription.InDefinedShape methodDescription) { Object defaultValue = methodDescription.getDefaultValue(); return defaultValue == null || !defaultValue.equals(annotationDescription.getValue(methodDescription)); } },
/** * {@inheritDoc} */ public AnnotationAppender append(AnnotationDescription annotationDescription, AnnotationValueFilter annotationValueFilter) { switch (annotationDescription.getRetention()) { case RUNTIME: doAppend(annotationDescription, true, annotationValueFilter); break; case CLASS: doAppend(annotationDescription, false, annotationValueFilter); break; case SOURCE: break; default: throw new IllegalStateException("Unexpected retention policy: " + annotationDescription.getRetention()); } return this; }
/** * Creates a handler for a given annotation. * * @param target The target parameter being handled. * @param parameterBinder The parameter binder that should process an annotation. * @param annotation An annotation instance that can be understood by this parameter binder. * @param typing The typing to apply. * @return A handler for processing the given annotation. */ @SuppressWarnings("unchecked") protected static Handler of(ParameterDescription target, ParameterBinder<?> parameterBinder, AnnotationDescription annotation, Assigner.Typing typing) { return new Bound<Annotation>(target, (ParameterBinder<Annotation>) parameterBinder, (AnnotationDescription.Loadable<Annotation>) annotation.prepare(parameterBinder.getHandledType()), typing); }
/** * {@inheritDoc} */ public AnnotationValue.Loaded<U> load(ClassLoader classLoader) throws ClassNotFoundException { @SuppressWarnings("unchecked") Class<U> annotationType = (Class<U>) Class.forName(annotationDescription.getAnnotationType().getName(), false, classLoader); return new Loaded<U>(annotationDescription.prepare(annotationType).load()); }
/** * Locates a dispatcher for the method if available. * * @param type The annotation type that indicates a given form of advice that is currently resolved. * @param property An annotation property that indicates if the advice method should be inlined. * @param dispatcher Any previous dispatcher that was discovered or {@code null} if no such dispatcher was yet found. * @param methodDescription The method description that is considered as an advice method. * @return A resolved dispatcher or {@code null} if no dispatcher was resolved. */ private static Dispatcher.Unresolved locate(Class<? extends Annotation> type, MethodDescription.InDefinedShape property, Dispatcher.Unresolved dispatcher, MethodDescription.InDefinedShape methodDescription) { AnnotationDescription annotation = methodDescription.getDeclaredAnnotations().ofType(type); if (annotation == null) { return dispatcher; } else if (dispatcher.isAlive()) { throw new IllegalStateException("Duplicate advice for " + dispatcher + " and " + methodDescription); } else if (!methodDescription.isStatic()) { throw new IllegalStateException("Advice for " + methodDescription + " is not static"); } else { return annotation.getValue(property).resolve(Boolean.class) ? new Dispatcher.Inlining(methodDescription) : new Dispatcher.Delegating(methodDescription); } }
/** * {@inheritDoc} */ public AnnotationAppender append(AnnotationDescription annotationDescription, AnnotationValueFilter annotationValueFilter, int typeReference, String typePath) { switch (annotationDescription.getRetention()) { case RUNTIME: doAppend(annotationDescription, true, annotationValueFilter, typeReference, typePath); break; case CLASS: doAppend(annotationDescription, false, annotationValueFilter, typeReference, typePath); break; case SOURCE: break; default: throw new IllegalStateException("Unexpected retention policy: " + annotationDescription.getRetention()); } return this; }
/** * {@inheritDoc} */ public AnnotationDescription ofType(TypeDescription annotationType) { for (AnnotationDescription annotation : this) { if (annotation.getAnnotationType().equals(annotationType)) { return annotation; } } return AnnotationDescription.UNDEFINED; }
/** * Handles the writing of a single annotation to an annotation visitor. * * @param annotationVisitor The annotation visitor the write process is to be applied on. * @param annotation The annotation to be written. * @param annotationValueFilter The value filter to apply for discovering which values of an annotation should be written. */ private static void handle(AnnotationVisitor annotationVisitor, AnnotationDescription annotation, AnnotationValueFilter annotationValueFilter) { for (MethodDescription.InDefinedShape methodDescription : annotation.getAnnotationType().getDeclaredMethods()) { if (annotationValueFilter.isRelevant(annotation, methodDescription)) { apply(annotationVisitor, methodDescription.getReturnType().asErasure(), methodDescription.getName(), annotation.getValue(methodDescription).resolve()); } } annotationVisitor.visitEnd(); }
OffsetMapping offsetMapping = null; for (AnnotationDescription annotationDescription : parameterDescription.getDeclaredAnnotations()) { OffsetMapping.Factory<?> factory = offsetMappings.get(annotationDescription.getAnnotationType()); if (factory != null) { @SuppressWarnings("unchecked") OffsetMapping current = factory.make(parameterDescription, (AnnotationDescription.Loadable) annotationDescription.prepare(factory.getAnnotationType()), adviceType); if (offsetMapping == null) {
/** * Checks if the supplied type's type annotations are valid. * * @param typeDescription The type to validate. * @return {@code true} if the supplied type's type annotations are valid. */ private boolean isValid(Generic typeDescription) { Set<TypeDescription> annotationTypes = new HashSet<TypeDescription>(); for (AnnotationDescription annotationDescription : typeDescription.getDeclaredAnnotations()) { if (!annotationDescription.getElementTypes().contains(typeUse) || !annotationTypes.add(annotationDescription.getAnnotationType())) { return false; } } return true; } }
@Nullable private Object getAnnotationValue(MethodDescription instrumentedMethod, AnnotationValueExtractor annotationValueExtractor) { for (TypeDescription typeDescription : instrumentedMethod.getDeclaredAnnotations().asTypeList()) { if (named(annotationValueExtractor.annotationClassName()).matches(typeDescription)) { for (MethodDescription.InDefinedShape annotationMethod : typeDescription.getDeclaredMethods()) { if (annotationMethod.getName().equals(annotationValueExtractor.method())) { return instrumentedMethod.getDeclaredAnnotations().ofType(typeDescription).getValue(annotationMethod).resolve(); } } } } return null; }
/** * {@inheritDoc} */ public AnnotationList visibility(ElementMatcher<? super RetentionPolicy> matcher) { List<AnnotationDescription> annotationDescriptions = new ArrayList<AnnotationDescription>(size()); for (AnnotationDescription annotation : this) { if (matcher.matches(annotation.getRetention())) { annotationDescriptions.add(annotation); } } return wrap(annotationDescriptions); }
/** * Checks if the supplied enumeration descriptions describe the correct annotation type. * * @param annotationType The annotation type to check for. * @param annotationDescription The annotation descriptions to check. * @return {@code true} if all annotation descriptions represent the annotation type in question. */ private static boolean isAnnotationType(TypeDescription annotationType, AnnotationDescription... annotationDescription) { for (AnnotationDescription anAnnotationDescription : annotationDescription) { if (!anAnnotationDescription.getAnnotationType().equals(annotationType)) { return false; } } return true; }
private AnnotationProperties buildAnnotationProperties(AnnotationDescription annotation) { AnnotationProperties props = new AnnotationProperties(annotation.getAnnotationType().getCanonicalName()); annotation.getAnnotationType().getDeclaredMethods().forEach(m -> { Object val = annotation.getValue(m).resolve(); if (val.getClass().isArray() && !val.getClass().getComponentType().isPrimitive()) { Object[] array = (Object[])val; Set<String> values = new HashSet<>(); for (Object obj : array) { if (obj instanceof TypeDescription) { values.add(((TypeDescription)obj).getName()); } else { values.add(obj.toString()); } } props.putArrayValues(m.getName(), values); } }); return props; }
if (!annotationDescription.getElementTypes().contains(ElementType.TYPE) && !(isAnnotation() && annotationDescription.getElementTypes().contains(ElementType.ANNOTATION_TYPE)) && !(isPackageType() && annotationDescription.getElementTypes().contains(ElementType.PACKAGE))) { throw new IllegalStateException("Cannot add " + annotationDescription + " on " + this); } else if (!typeAnnotationTypes.add(annotationDescription.getAnnotationType())) { throw new IllegalStateException("Duplicate annotation " + annotationDescription + " for " + this); if (!annotationDescription.getElementTypes().contains(ElementType.FIELD)) { throw new IllegalStateException("Cannot add " + annotationDescription + " on " + fieldDescription); } else if (!fieldAnnotationTypes.add(annotationDescription.getAnnotationType())) { throw new IllegalStateException("Duplicate annotation " + annotationDescription + " for " + fieldDescription); if (!annotationDescription.getElementTypes().contains(ElementType.PARAMETER)) { throw new IllegalStateException("Cannot add " + annotationDescription + " on " + parameterDescription); } else if (!parameterAnnotationTypes.add(annotationDescription.getAnnotationType())) { throw new IllegalStateException("Duplicate annotation " + annotationDescription + " of " + parameterDescription + " for " + methodDescription); if (!annotationDescription.getElementTypes().contains(methodDescription.isMethod() ? ElementType.METHOD : ElementType.CONSTRUCTOR)) { throw new IllegalStateException("Cannot add " + annotationDescription + " on " + methodDescription); } else if (!methodAnnotationTypes.add(annotationDescription.getAnnotationType())) { throw new IllegalStateException("Duplicate annotation " + annotationDescription + " for " + methodDescription);