/** * Check for the presence of an annotation with a specific simple name directly on this symbol. * Does *not* consider annotation inheritance. * * @param sym the symbol to check for the presence of the annotation * @param simpleName the simple name of the annotation to look for, e.g. "Nullable" or * "CheckReturnValue" */ public static boolean hasDirectAnnotationWithSimpleName(Symbol sym, String simpleName) { for (AnnotationMirror annotation : sym.getAnnotationMirrors()) { if (annotation.getAnnotationType().asElement().getSimpleName().contentEquals(simpleName)) { return true; } } return false; }
private boolean hasAnnotation(String annotation, Symbol member) { for (Attribute.Compound attribute : member.getAnnotationMirrors()) { if (annotation.equals(attribute.type.toString())) { return true; } } return false; } }
/** * NOTE: this method does not work for getting all annotations of parameters of methods from class * files. For that case, use {@link #getAllAnnotationsForParameter(Symbol.MethodSymbol, int)} * * @param symbol the symbol * @return all annotations on the symbol and on the type of the symbol */ public static Stream<? extends AnnotationMirror> getAllAnnotations(Symbol symbol) { // for methods, we care about annotations on the return type, not on the method type itself Stream<? extends AnnotationMirror> typeUseAnnotations = getTypeUseAnnotations(symbol); return Stream.concat(symbol.getAnnotationMirrors().stream(), typeUseAnnotations); }
@SuppressWarnings("unchecked") public static ImmutableClassToInstanceMap<Annotation> annotationMap(Symbol symbol) { ImmutableClassToInstanceMap.Builder<Annotation> builder = ImmutableClassToInstanceMap.builder(); for (Compound compound : symbol.getAnnotationMirrors()) { Name qualifiedAnnotationType = ((TypeElement) compound.getAnnotationType().asElement()).getQualifiedName(); try { Class<? extends Annotation> annotationClazz = Class.forName(qualifiedAnnotationType.toString()).asSubclass(Annotation.class); builder.put( (Class) annotationClazz, AnnotationProxyMaker.generateAnnotation(compound, annotationClazz)); } catch (ClassNotFoundException e) { throw new IllegalArgumentException("Unrecognized annotation type", e); } } return builder.build(); } }
private static Set<Modifier> getIncompatibleModifiers(AnnotationTree tree, VisitorState state) { for (Attribute.Compound c : ASTHelpers.getSymbol(tree).getAnnotationMirrors()) { if (((TypeElement) c.getAnnotationType().asElement()) .getQualifiedName() .contentEquals(GUAVA_ANNOTATION)) { @SuppressWarnings("unchecked") List<Attribute.Enum> modifiers = (List<Attribute.Enum>) c.member(state.getName("value")).getValue(); return ImmutableSet.copyOf( Iterables.transform( modifiers, (Attribute.Enum input) -> Modifier.valueOf(input.getValue().name.toString()))); } } IncompatibleModifiers annotation = ASTHelpers.getAnnotation(tree, IncompatibleModifiers.class); if (annotation != null) { return ImmutableSet.copyOf(annotation.value()); } return ImmutableSet.of(); }
for (Attribute.Compound attr : sym.getAnnotationMirrors()) { if ((attr.type.tsym == state.getSymtab().suppressWarningsType.tsym) || attr.type.tsym.getQualifiedName().contentEquals("android.annotation.SuppressLint")) {
/** * Gets the symbol's annotation that correspond to the given annotation type * name if exists. */ private static AnnotationMirror getAnnotation(Symbol symbol, String annotationType) { for (Compound a : symbol.getAnnotationMirrors()) { if (annotationType.equals(a.type.toString())) { return a; } } return null; }
/** * Tells if the given symbol is annotated with one of the given annotation type * names. */ private static boolean hasActualAnnotationType(Symbol symbol, String... annotationTypes) { for (Compound a : symbol.getAnnotationMirrors()) { for (String annotationType : annotationTypes) { if (annotationType.equals(a.type.toString())) { return true; } } } return false; }
/** Fetch a particular annotation from a symbol. */ public Attribute.Compound attribute(Symbol anno) { for (Attribute.Compound a : getAnnotationMirrors()) if (a.type.tsym == anno) return a; return null; }
/** Fetch a particular annotation from a symbol. */ public Attribute.Compound attribute(Symbol anno) { for (Attribute.Compound a : getAnnotationMirrors()) if (a.type.tsym == anno) return a; return null; }
/** * Check for the presence of an annotation with a specific simple name directly on this symbol. * Does *not* consider annotation inheritance. * * @param sym the symbol to check for the presence of the annotation * @param simpleName the simple name of the annotation to look for, e.g. "Nullable" or * "CheckReturnValue" */ public static boolean hasDirectAnnotationWithSimpleName(Symbol sym, String simpleName) { for (AnnotationMirror annotation : sym.getAnnotationMirrors()) { if (annotation.getAnnotationType().asElement().getSimpleName().contentEquals(simpleName)) { return true; } } return false; }
private boolean hasAnnotation(String annotation, Symbol member) { for (Attribute.Compound attribute : member.getAnnotationMirrors()) { if (annotation.equals(attribute.type.toString())) { return true; } } return false; } }
public static Map<Symbol.MethodSymbol, Attribute> getElementValues( final Element te, final String annotationClass ) { final Symbol cs = Symbol.class.cast( te ); for( Attribute.Compound c : cs.getAnnotationMirrors() ) { if( annotationClass.equals( c.getAnnotationType().toString() ) ) { return c.getElementValues(); } } return Collections.emptyMap(); }
/** * An internal-use utility that creates a reified annotation. */ public static <A extends Annotation> A getAnnotation(Symbol annotated, Class<A> annoType) { if (!annoType.isAnnotation()) throw new IllegalArgumentException("Not an annotation type: " + annoType); String name = annoType.getName(); for (Attribute.Compound anno : annotated.getAnnotationMirrors()) if (name.equals(anno.type.tsym.flatName().toString())) return AnnotationProxyMaker.generateAnnotation(anno, annoType); return null; }
/** * An internal-use utility that creates a reified annotation. */ public static <A extends Annotation> A getAnnotation(Symbol annotated, Class<A> annoType) { if (!annoType.isAnnotation()) throw new IllegalArgumentException("Not an annotation type: " + annoType); String name = annoType.getName(); for (Attribute.Compound anno : annotated.getAnnotationMirrors()) if (name.equals(anno.type.tsym.flatName().toString())) return AnnotationProxyMaker.generateAnnotation(anno, annoType); return null; }
@SuppressWarnings("unchecked") public static ImmutableClassToInstanceMap<Annotation> annotationMap(Symbol symbol) { ImmutableClassToInstanceMap.Builder<Annotation> builder = ImmutableClassToInstanceMap.builder(); for (Compound compound : symbol.getAnnotationMirrors()) { Name qualifiedAnnotationType = ((TypeElement) compound.getAnnotationType().asElement()).getQualifiedName(); try { Class<? extends Annotation> annotationClazz = Class.forName(qualifiedAnnotationType.toString()).asSubclass(Annotation.class); builder.put( (Class) annotationClazz, AnnotationProxyMaker.generateAnnotation(compound, annotationClazz)); } catch (ClassNotFoundException e) { throw new IllegalArgumentException("Unrecognized annotation type", e); } } return builder.build(); } }
/** * Returns true if the given symbol is annotated with the annotation or if it's a member of a type * annotated with the annotation. */ private boolean isAnnotatedApi(Symbol symbol) { Name name = symbol.getQualifiedName(); if (name != null && isIgnoredType(name.toString())) { return false; } for (AnnotationMirror annotation : symbol.getAnnotationMirrors()) { if (annotation.getAnnotationType().toString().equals(annotationType)) { return true; } } return isMemberOfAnnotatedApi(symbol); }
private static Set<Modifier> getIncompatibleModifiers(AnnotationTree tree, VisitorState state) { for (Attribute.Compound c : ASTHelpers.getSymbol(tree).getAnnotationMirrors()) { if (((TypeElement) c.getAnnotationType().asElement()) .getQualifiedName() .contentEquals(GUAVA_ANNOTATION)) { @SuppressWarnings("unchecked") List<Attribute.Enum> modifiers = (List<Attribute.Enum>) c.member(state.getName("value")).getValue(); return ImmutableSet.copyOf(Iterables.transform(modifiers, TO_MODIFIER)); } } IncompatibleModifiers annotation = ASTHelpers.getAnnotation(tree, IncompatibleModifiers.class); if (annotation != null) { return ImmutableSet.copyOf(annotation.value()); } return ImmutableSet.of(); }
private void addAnnotations( SrcAnnotated<?> srcAnnotated, Symbol symbol ) { for( Attribute.Compound annotationMirror: symbol.getAnnotationMirrors() ) { String fqn = annotationMirror.getAnnotationType().toString(); if( fqn.equals( "jdk.internal.HotSpotIntrinsicCandidate" ) ) { // Since java 10 we have to keep these out of stubbed java source continue; } SrcAnnotationExpression annoExpr = new SrcAnnotationExpression( fqn ); for( Pair<Symbol.MethodSymbol, Attribute> value: annotationMirror.values ) { annoExpr.addArgument( value.fst.flatName().toString(), new SrcType( value.snd.type.toString() ), value.snd.getValue() ); } srcAnnotated.addAnnotation( annoExpr ); } }
/** Write member (field or method) attributes; * return number of attributes written. */ int writeMemberAttrs(Symbol sym) { int acount = writeFlagAttrs(sym.flags()); long flags = sym.flags(); if (source.allowGenerics() && (flags & (SYNTHETIC|BRIDGE)) != SYNTHETIC && (flags & ANONCONSTR) == 0 && (!types.isSameType(sym.type, sym.erasure(types)) || hasTypeVar(sym.type.getThrownTypes()))) { // note that a local class with captured variables // will get a signature attribute int alenIdx = writeAttr(names.Signature); databuf.appendChar(pool.put(typeSig(sym.type))); endAttr(alenIdx); acount++; } acount += writeJavaAnnotations(sym.getAnnotationMirrors()); return acount; }