<T extends Annotation> AnnotationDescription.Loadable<T> getAnnotation(Class<T> annotationType) { return getAnnotations().ofType( annotationType ); }
/** * {@inheritDoc} */ public Set<ElementType> getElementTypes() { AnnotationDescription.Loadable<Target> target = getAnnotationType().getDeclaredAnnotations().ofType(Target.class); return new HashSet<ElementType>(Arrays.asList(target == null ? DEFAULT_TARGET : target.loadSilent().value())); }
/** * {@inheritDoc} */ public boolean matches(FieldDescription target) { AnnotationDescription.Loadable<ValueHandling> annotation = target.getDeclaredAnnotations().ofType(ValueHandling.class); return annotation != null && annotation.loadSilent().value() == sort; } }
/** * {@inheritDoc} */ public Resolution resolve(MethodDescription source, MethodDelegationBinder.MethodBinding left, MethodDelegationBinder.MethodBinding right) { int leftPriority = resolve(left.getTarget().getDeclaredAnnotations().ofType(BindingPriority.class)); int rightPriority = resolve(right.getTarget().getDeclaredAnnotations().ofType(BindingPriority.class)); if (leftPriority == rightPriority) { return Resolution.AMBIGUOUS; } else if (leftPriority < rightPriority) { return Resolution.RIGHT; } else { return Resolution.LEFT; } } }
/** * {@inheritDoc} */ public int compare(FieldDescription.InDefinedShape left, FieldDescription.InDefinedShape right) { AnnotationDescription.Loadable<Sorted> leftAnnotation = left.getDeclaredAnnotations().ofType(Sorted.class); AnnotationDescription.Loadable<Sorted> rightAnnotation = right.getDeclaredAnnotations().ofType(Sorted.class); int leftValue = leftAnnotation == null ? Sorted.DEFAULT : leftAnnotation.loadSilent().value(); int rightValue = rightAnnotation == null ? Sorted.DEFAULT : rightAnnotation.loadSilent().value(); if (leftValue > rightValue) { return -1; } else if (leftValue < rightValue) { return 1; } else { return 0; } } }
/** * Creates a dispatcher for inlined advice method. * * @param adviceMethod The advice method. */ protected Inlining(MethodDescription.InDefinedShape adviceMethod) { this.adviceMethod = adviceMethod; namedTypes = new HashMap<String, TypeDefinition>(); for (ParameterDescription parameterDescription : adviceMethod.getParameters().filter(isAnnotatedWith(Local.class))) { String name = parameterDescription.getDeclaredAnnotations().ofType(Local.class).loadSilent().value(); TypeDefinition previous = namedTypes.put(name, parameterDescription.getType()); if (previous != null && !previous.equals(parameterDescription.getType())) { throw new IllegalStateException("Local variable for " + name + " is defined with inconsistent types"); } } }
/** * {@inheritDoc} */ public RetentionPolicy getRetention() { AnnotationDescription.Loadable<Retention> retention = getAnnotationType().getDeclaredAnnotations().ofType(Retention.class); return retention == null ? RetentionPolicy.CLASS : retention.loadSilent().value(); }
/** * {@inheritDoc} */ public Dispatcher.Resolved.ForMethodExit asMethodExit(List<? extends OffsetMapping.Factory<?>> userFactories, ClassReader classReader, Unresolved methodEnter) { Map<String, TypeDefinition> namedTypes = methodEnter.getNamedTypes(); for (ParameterDescription parameterDescription : adviceMethod.getParameters().filter(isAnnotatedWith(Local.class))) { String name = parameterDescription.getDeclaredAnnotations().ofType(Local.class).loadSilent().value(); TypeDefinition typeDefinition = namedTypes.get(name); if (typeDefinition == null) { throw new IllegalStateException(adviceMethod + " attempts use of undeclared local variable " + name); } else if (!typeDefinition.equals(parameterDescription.getType())) { throw new IllegalStateException(adviceMethod + " does not read variable " + name + " as " + typeDefinition); } } return Resolved.ForMethodExit.of(adviceMethod, namedTypes, userFactories, methodEnter.getAdviceType()); }
private void createChecks(String fullMethodSignature, AnnotationList declaredAnnotations, TimerNames timerNames) { if (declaredAnnotations.isAnnotationPresent(SLA.class)) { createSlaCheck(declaredAnnotations.ofType(SLA.class).loadSilent(), fullMethodSignature, timerNames); } if (declaredAnnotations.isAnnotationPresent(SLAs.class)) { for (SLA sla : declaredAnnotations.ofType(SLAs.class).loadSilent().value()) { createSlaCheck(sla, fullMethodSignature, timerNames); } } }
/** * Resolves an appropriate offset mapping factory for the {@link Thrown} parameter annotation. * * @param adviceMethod The exit advice method, annotated with {@link OnMethodExit}. * @return An appropriate offset mapping factory. */ @SuppressWarnings("unchecked") // In absence of @SafeVarargs protected static OffsetMapping.Factory<?> of(MethodDescription.InDefinedShape adviceMethod) { return adviceMethod.getDeclaredAnnotations() .ofType(OnMethodExit.class) .getValue(ON_THROWABLE) .resolve(TypeDescription.class) .represents(NoExceptionHandler.class) ? new OffsetMapping.Factory.Illegal<Thrown>(Thrown.class) : Factory.INSTANCE; }
/** * 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); } }
private static TypeDescription.Generic target(AnnotatedFieldDescription persistentField) { AnnotationDescription.Loadable<Access> access = persistentField.getDeclaringType().asErasure().getDeclaredAnnotations().ofType( Access.class ); if ( access != null && access.loadSilent().value() == AccessType.FIELD ) { return persistentField.getType(); } else { Optional<MethodDescription> getter = persistentField.getGetter(); if ( getter.isPresent() ) { return getter.get().getReturnType(); } else { return persistentField.getType(); } } }
@Override protected NamingParameters getNamingParameters(MethodDescription instrumentedMethod) { final ExceptionMetered exceptionMetered = instrumentedMethod.getDeclaredAnnotations().ofType(ExceptionMetered.class).loadSilent(); return new NamingParameters(exceptionMetered.name(), exceptionMetered.absolute()); }
@Override public Target resolve(TypeDescription instrumentedType, MethodDescription instrumentedMethod, Assigner assigner, Advice.ArgumentHandler argumentHandler, Sort sort) { final ExceptionMetered exceptionMetered = instrumentedMethod.getDeclaredAnnotations().ofType(ExceptionMetered.class).loadSilent(); final TypeDescription.ForLoadedType exceptionType = new TypeDescription.ForLoadedType(exceptionMetered.cause()); return Target.ForStackManipulation.of(exceptionType); } };
@Override protected NamingParameters getNamingParameters(MethodDescription instrumentedMethod) { final Metered metered = instrumentedMethod.getDeclaredAnnotations().ofType(Metered.class).loadSilent(); return new NamingParameters(metered.name(), metered.absolute()); }
/** * Resolves exit advice that handles exceptions depending on the specification of the exit advice. * * @param adviceMethod The advice method. * @param namedTypes A mapping of all available local variables by their name to their type. * @param userFactories A list of user-defined factories for offset mappings. * @param enterType The type of the value supplied by the enter advice method or {@code void} if no such value exists. * @return An appropriate exit handler. */ protected static Resolved.ForMethodExit of(MethodDescription.InDefinedShape adviceMethod, Map<String, TypeDefinition> namedTypes, List<? extends OffsetMapping.Factory<?>> userFactories, TypeDefinition enterType) { TypeDescription throwable = adviceMethod.getDeclaredAnnotations() .ofType(OnMethodExit.class) .getValue(ON_THROWABLE) .resolve(TypeDescription.class); return throwable.represents(NoExceptionHandler.class) ? new WithoutExceptionHandler(adviceMethod, namedTypes, userFactories, enterType) : new WithExceptionHandler(adviceMethod, namedTypes, userFactories, enterType, throwable); }
/** * Resolves exit advice that handles exceptions depending on the specification of the exit advice. * * @param adviceMethod The advice method. * @param namedTypes A mapping of all available local variables by their name to their type. * @param userFactories A list of user-defined factories for offset mappings. * @param classReader The class reader for parsing the advice method's class file. * @param enterType The type of the value supplied by the enter advice method or {@code void} if no such value exists. * @return An appropriate exit handler. */ protected static Resolved.ForMethodExit of(MethodDescription.InDefinedShape adviceMethod, Map<String, TypeDefinition> namedTypes, List<? extends OffsetMapping.Factory<?>> userFactories, ClassReader classReader, TypeDefinition enterType) { TypeDescription throwable = adviceMethod.getDeclaredAnnotations() .ofType(OnMethodExit.class) .getValue(ON_THROWABLE).resolve(TypeDescription.class); return throwable.represents(NoExceptionHandler.class) ? new WithoutExceptionHandler(adviceMethod, namedTypes, userFactories, classReader, enterType) : new WithExceptionHandler(adviceMethod, namedTypes, userFactories, classReader, enterType, throwable); }
@Override protected NamingParameters getNamingParameters(MethodDescription instrumentedMethod) { if (instrumentedMethod.getDeclaredAnnotations().isAnnotationPresent(Timed.class)) { final Timed timed = instrumentedMethod.getDeclaredAnnotations().ofType(Timed.class).loadSilent(); return new NamingParameters(timed.name(), timed.absolute()); } else { return new NamingParameters("", false); } }
@Override public boolean matches(MethodDescription targetMethod) { final AnnotationList declaredAnnotationsOfType = targetMethod.getDeclaringType().asErasure().getDeclaredAnnotations(); if (declaredAnnotationsOfType.isAnnotationPresent(Remote.class)) { final Class[] remoteInterfaces = declaredAnnotationsOfType.ofType(Remote.class).loadSilent().value(); if (!new TypeList.ForLoadedTypes(remoteInterfaces).filter(isDeclaredInInterfaceHierarchy(targetMethod)).isEmpty()) { return true; } } return false; } }
/** * {@inheritDoc} */ public DynamicType.Builder<?> apply(DynamicType.Builder<?> builder, TypeDescription typeDescription, ClassFileLocator classFileLocator) { Enhance enhance = typeDescription.getDeclaredAnnotations().ofType(Enhance.class).loadSilent(); if (typeDescription.getDeclaredMethods().filter(isToString()).isEmpty()) { builder = builder.method(isToString()).intercept(ToStringMethod.prefixedBy(enhance.prefix().getPrefixResolver()) .withIgnoredFields(enhance.includeSyntheticFields() ? ElementMatchers.<FieldDescription>none() : ElementMatchers.<FieldDescription>isSynthetic()) .withIgnoredFields(isAnnotatedWith(Exclude.class))); } return builder; }