/** {@inheritDoc} */ public boolean isRelevant(AnnotationDescription annotationDescription, MethodDescription.InDefinedShape methodDescription) { Object defaultValue = methodDescription.getDefaultValue(); return defaultValue == null || !defaultValue.equals(annotationDescription.getValue(methodDescription)); } },
@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; }
/** * 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); } }
/** * 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(); }
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; }
@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; }