@Override public String apply(VariableTree variableTree) { for (Compound c : ASTHelpers.getSymbol(variableTree).getAnnotationMirrors()) { if (((TypeElement) c.getAnnotationType().asElement()) .getQualifiedName() .contentEquals(ASSISTED_ANNOTATION)) { // Assisted only has 'value', and value can only contain 1 element. Collection<Attribute> valueEntries = c.getElementValues().values(); if (!valueEntries.isEmpty()) { return Iterables.getOnlyElement(valueEntries).getValue().toString(); } } } return ""; } };
/** Converts the given attribute to an integer value. */ public static Optional<Integer> asIntegerValue(Attribute a) { class Visitor extends SimpleAnnotationValueVisitor8<Integer, Void> { @Override public Integer visitInt(int i, Void unused) { return i; } } return Optional.ofNullable(a.accept(new Visitor(), null)); }
@Override public TypeAnnotationPosition getPosition() { if (values.length != 0) return values[0].getPosition(); else return null; } }
@Override public TypeAnnotationPosition getPosition() { if (values.length != 0) return values[0].getPosition(); else return null; } }
/** * Returns if the variable has a {@code @Mock} annotation that specifies an answer that does not * handle generics. */ static boolean answerHandlesGenerics(VarSymbol varSym, VisitorState state) { Compound attribute = varSym.attribute(state.getSymbolFromString(MOCK_ANNOTATION)); String answer = null; for (Entry<MethodSymbol, Attribute> e : attribute.getElementValues().entrySet()) { if (e.getKey().getSimpleName().contentEquals("answer")) { answer = e.getValue().getValue().toString(); break; } } return !BAD_ANSWER_STRATEGIES.contains(answer); } }
/** Converts the given attribute to an enum value. */ public static <T extends Enum<T>> Optional<T> asEnumValue(Class<T> clazz, Attribute a) { class Visitor extends SimpleAnnotationValueVisitor8<T, Void> { @Override public T visitEnumConstant(VariableElement c, Void unused) { return Enum.valueOf(clazz, c.getSimpleName().toString()); } } return Optional.ofNullable(a.accept(new Visitor(), null)); }
@Override public TypeAnnotationPosition getPosition() { if (values.size() != 0) { Name valueName = values.head.fst.name.table.names.value; Pair<MethodSymbol, Attribute> res = getElemPair(valueName); return res == null ? null : res.snd.getPosition(); } return null; }
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(); }
/** Converts the given attribute to an string value. */ public static Optional<String> asStringValue(Attribute a) { class Visitor extends SimpleAnnotationValueVisitor8<String, Void> { @Override public String visitString(String s, Void unused) { return s; } } return Optional.ofNullable(a.accept(new Visitor(), null)); }
@Override public TypeAnnotationPosition getPosition() { if (values.size() != 0) { Name valueName = values.head.fst.name.table.names.value; Pair<MethodSymbol, Attribute> res = getElemPair(valueName); return res == null ? null : res.snd.getPosition(); } return null; }
instanceof Attribute.Array) { // SuppressWarnings/SuppressLint take an array for (Attribute suppress : ((Attribute.Array) value.snd).values) { String suppressedWarning = (String) suppress.getValue(); if (!suppressWarningsStrings.contains(suppressedWarning)) { anyModification = true;
private static Stream<String> asStrings(Attribute v) { return MoreObjects.firstNonNull( v.accept( new SimpleAnnotationValueVisitor8<Stream<String>, Void>() { @Override public Stream<String> visitString(String s, Void aVoid) { return Stream.of(s); } @Override public Stream<String> visitArray(List<? extends AnnotationValue> list, Void aVoid) { return list.stream().flatMap(a -> a.accept(this, null)).filter(x -> x != null); } }, null), Stream.empty()); }
/** * Gets the first value of the 'value' property. */ @SuppressWarnings("unchecked") private static <T> T getFirstAnnotationValue(AnnotationMirror annotation, String propertyName, Class<T> propertyClass, T defaultValue) { for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> valueEntry : annotation .getElementValues().entrySet()) { if (propertyName == null || propertyName.equals(valueEntry.getKey().getSimpleName().toString())) { if (propertyClass.isArray()) { List<Object> l = (List<Object>) valueEntry.getValue().getValue(); Object array = Array.newInstance(propertyClass.getComponentType(), l.size()); for (int i = 0; i < l.size(); i++) { Array.set(array, i, ((Attribute) l.get(i)).getValue()); } return (T) array; } else { return (T) valueEntry.getValue().getValue(); } } } return defaultValue; }
/** Converts the given attribute to one or more strings. */ public static Stream<String> asStrings(Attribute v) { return MoreObjects.firstNonNull( v.accept( new SimpleAnnotationValueVisitor8<Stream<String>, Void>() { @Override public Stream<String> visitString(String s, Void unused) { return Stream.of(s); } @Override public Stream<String> visitArray(List<? extends AnnotationValue> list, Void unused) { return list.stream().flatMap(a -> a.accept(this, null)).filter(x -> x != null); } }, null), Stream.empty()); }
public static String[] getAnnotationClassValuesAsFQCN(VariableElement p, Class annotationClazz, String methodName) { List<? extends AnnotationMirror> annotationMirrors = p.getAnnotationMirrors(); for(AnnotationMirror annotationMirror : annotationMirrors){ if(annotationMirror.getAnnotationType().toString().equals(annotationClazz.getCanonicalName())){ for(Map.Entry<? extends ExecutableElement,? extends AnnotationValue> entry : annotationMirror.getElementValues().entrySet()){ if(entry.getKey().getSimpleName().contentEquals(methodName)){ Attribute.Array array = (Attribute.Array) entry.getValue(); List<String> fqcns = new ArrayList<>(); for(Attribute attribute : array.getValue()) { DeclaredType type = (DeclaredType) attribute.getValue(); fqcns.add(type.toString()); } return fqcns.toArray(new String[fqcns.size()]); } } } } return null; } }
private static ImmutableList<String> containerOf(VisitorState state, Compound attr) { Attribute m = attr.member(state.getName("containerOf")); if (m == null) { return ImmutableList.of(); } ImmutableList.Builder<String> containerOf = ImmutableList.builder(); m.accept( new SimpleAnnotationValueVisitor8<Void, Void>() { @Override public Void visitString(String s, Void unused) { containerOf.add(s); return null; } @Override public Void visitArray(List<? extends AnnotationValue> list, Void unused) { for (AnnotationValue value : list) { value.accept(this, null); } return null; } }, null); return containerOf.build(); }
public static String getAttributeString( final Element te, final Class<? extends Annotation> annotationClass, final String value, final String defaultValue ) { final Attribute attr = getAttribute( te, annotationClass, value ); return attr == null ? defaultValue : attr.getValue().toString(); }
values .get() .accept( new SimpleAnnotationValueVisitor8<Void, Void>() { @Override
private void getTypesToCompile( JCTree.JCAnnotation precompileAnno, Map<String, Set<String>> typeNames ) { Attribute.Compound attribute = precompileAnno.attribute; if( attribute == null ) { return; } String typeManifoldClassName = null; String regex = ".*"; String ext = "*"; for( com.sun.tools.javac.util.Pair<Symbol.MethodSymbol, Attribute> pair: attribute.values ) { Name argName = pair.fst.getSimpleName(); switch( argName.toString() ) { case "typeManifold": typeManifoldClassName = pair.snd.getValue().toString(); break; case "fileExtension": ext = pair.snd.getValue().toString(); break; case "typeNames": regex = pair.snd.getValue().toString(); break; } } addToPrecompile( typeNames, typeManifoldClassName, ext, regex ); }
JCExpression translate(Attribute a) { a.accept(this); return result; } JCAnnotation translate(Attribute.Compound a) {