private boolean hasTypeUseOrTypeParameter(Target targetAnnotation) { // Should only be in cases where Target is not in the classpath return targetAnnotation != null && !Collections.disjoint(FORBIDDEN_ELEMENT_TYPES, Arrays.asList(targetAnnotation.value())); }
/** * 获取注解类可以用来修饰哪些程序元素,如 TYPE, METHOD, CONSTRUCTOR, FIELD, PARAMETER 等 * * @param annotationType 注解类 * @return 注解修饰的程序元素数组 */ public static ElementType[] getTargetType(Class<? extends Annotation> annotationType) { final Target target = annotationType.getAnnotation(Target.class); if (null == target) { return new ElementType[] { ElementType.TYPE, // ElementType.FIELD, // ElementType.METHOD, // ElementType.PARAMETER, // ElementType.CONSTRUCTOR, // ElementType.LOCAL_VARIABLE, // ElementType.ANNOTATION_TYPE, // ElementType.PACKAGE// }; } return target.value(); }
/** * 获取注解类可以用来修饰哪些程序元素,如 TYPE, METHOD, CONSTRUCTOR, FIELD, PARAMETER 等 * * @param annotationType 注解类 * @return 注解修饰的程序元素数组 */ public static ElementType[] getTargetType(Class<? extends Annotation> annotationType) { final Target target = annotationType.getAnnotation(Target.class); if (null == target) { return new ElementType[] { ElementType.TYPE, // ElementType.FIELD, // ElementType.METHOD, // ElementType.PARAMETER, // ElementType.CONSTRUCTOR, // ElementType.LOCAL_VARIABLE, // ElementType.ANNOTATION_TYPE, // ElementType.PACKAGE// }; } return target.value(); }
/** * An annotation which, when found on a type parameter, indicates that the type parameter may * only be instantiated with thread-safe types. */ public Builder typeParameterAnnotation(Class<? extends Annotation> typeParameterAnnotation) { checkNotNull(typeParameterAnnotation); checkArgument( Arrays.stream(typeParameterAnnotation.getAnnotation(Target.class).value()) .anyMatch(ElementType.TYPE_PARAMETER::equals), "%s must be applicable to type parameters", typeParameterAnnotation); this.typeParameterAnnotation = typeParameterAnnotation; return this; }
static boolean annotationMatchesTarget(Element annotationElement, ElementType elementType) { @Nullable Target target = annotationElement.getAnnotation(Target.class); if (target != null) { ElementType[] targetTypes = target.value(); if (targetTypes.length == 0) { return false; } boolean found = false; for (ElementType t : targetTypes) { if (t == elementType) { found = true; } } if (!found) { return false; } } return true; }
/** * Rewrite the annotation with static imports, adding TYPE and METHOD to the @Target annotation * value (and reordering them to their declaration order in ElementType). */ private static Fix replaceTargetAnnotation( Target annotation, AnnotationTree targetAnnotationTree) { Set<ElementType> types = EnumSet.copyOf(REQUIRED_ELEMENT_TYPES); types.addAll(Arrays.asList(annotation.value())); return replaceTargetAnnotation(targetAnnotationTree, types); }
private Fix removeTypeUse(Target targetAnnotation, AnnotationTree tree) { Set<ElementType> elements = EnumSet.copyOf(Arrays.asList(targetAnnotation.value())); elements.removeAll(FORBIDDEN_ELEMENT_TYPES); if (elements.isEmpty()) { return SuggestedFix.delete(tree); } return InvalidTargetingOnScopingAnnotation.replaceTargetAnnotation(tree, elements); } }
private static ImmutableSet<Tree.Kind> supportedTreeTypes(Element whitelistAnnotation) { Target targetAnnotation = whitelistAnnotation.getAnnotation(Target.class); if (targetAnnotation == null) { // in the absence of further information, we assume the annotation is supported on classes and // methods. return TREE_TYPE_UNKNOWN_ANNOTATION; } ImmutableSet.Builder<Tree.Kind> types = ImmutableSet.builder(); for (ElementType t : targetAnnotation.value()) { switch (t) { case TYPE: types.add( Tree.Kind.CLASS, Tree.Kind.ENUM, Tree.Kind.INTERFACE, Tree.Kind.ANNOTATION_TYPE); break; case METHOD: types.add(Tree.Kind.METHOD); break; default: break; } } return types.build(); }
/** * Create a {@link MethodResolver} for the specified Method-level annotation type. * * @param annotationType establish the annotation to be used. */ public AnnotationMethodResolver(Class<? extends Annotation> annotationType) { Assert.notNull(annotationType, "annotationType must not be null"); Assert.isTrue(ObjectUtils.containsElement( annotationType.getAnnotation(Target.class).value(), ElementType.METHOD), "Annotation [" + annotationType + "] is not a Method-level annotation."); this.annotationType = annotationType; }
/** * Create a MethodResolver for the specified Method-level annotation type * @param annotationType the type of the annotation */ public AnnotationMethodResolver(Class<? extends Annotation> annotationType) { Assert.notNull(annotationType, "annotationType must not be null"); Assert.isTrue(ObjectUtils.containsElement( annotationType.getAnnotation(Target.class).value(), ElementType.METHOD), "Annotation [" + annotationType + "] is not a Method-level annotation."); this.annotationType = annotationType; }
ElementType[] elementTypes = target.value();
ElementType[] elementTypes = target.value();
private static boolean isTypeAnnotation(AnnotationTree t) { Symbol annotationSymbol = ASTHelpers.getSymbol(t.getAnnotationType()); if (annotationSymbol == null) { return false; } Target target = annotationSymbol.getAnnotation(Target.class); if (target == null) { return false; } List<ElementType> value = Arrays.asList(target.value()); return value.contains(ElementType.TYPE_USE) || value.contains(ElementType.TYPE_PARAMETER); } }
/** * {@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())); }
Arrays.asList(targetAnnotation == null ? ElementType.values() : targetAnnotation.value());
Collection<ElementType> elementTypes = Arrays.asList(t.value()); final String ERR = "Annotation class " + klass + " not applicable to host of type " + h.getClass() + "."; if (h instanceof SootClass) {
Assert.notNull(target, "Target must not be null"); Assert.notNull(annotationType, "AnnotationType must not be null"); Assert.isTrue(ObjectUtils.containsElement(annotationType.getAnnotation(Target.class).value(), ElementType.METHOD), "Annotation [" + annotationType + "] is not a Method-level annotation."); final Class<?> targetClass = (target instanceof Advised) ? ((Advised) target).getTargetSource()
Assert.notNull(annotationType, "AnnotationType must not be null"); Assert.isTrue(ObjectUtils.containsElement( annotationType.getAnnotation(Target.class).value(), ElementType.METHOD), "Annotation [" + annotationType + "] is not a Method-level annotation."); final Class<?> targetClass = (target instanceof Advised)
@Override public final Description matchClass(ClassTree classTree, VisitorState state) { if (ANNOTATION_WITH_SCOPE_AND_TARGET.matches(classTree, state)) { MultiMatchResult<AnnotationTree> targetAnnotation = HAS_TARGET_ANNOTATION.multiMatchResult(classTree, state); if (targetAnnotation.matches()) { AnnotationTree targetTree = targetAnnotation.onlyMatchingNode(); Target target = getAnnotation(classTree, Target.class); if (target != null && // Unlikely to occur, but just in case Target isn't on the classpath. !Arrays.asList(target.value()).containsAll(REQUIRED_ELEMENT_TYPES)) { return describeMatch(targetTree, replaceTargetAnnotation(target, targetTree)); } } } return Description.NO_MATCH; }
} else if (type == Target.class) { Target t = (Target) annotation; ElementType[] elements = t.value(); ListExpression elementExprs = new ListExpression(); for (ElementType element : elements) {