@Override public UAnnotation visitAnnotation(AnnotationTree tree, Void v) { return UAnnotation.create( templateType(tree.getAnnotationType()), templateExpressions(tree.getArguments())); }
@Override public Void visitAnnotation(AnnotationTree at, Void p) { String annType = at.getAnnotationType().toString(); Integer i = annotationHisto.get(annType); if (i == null) { annotationHisto.put(annType, 0); } else { annotationHisto.put(annType, i + 1); } return super.visitAnnotation(at, p); } }, null);
private String valueArgumentFromCompatibleWithAnnotation(AnnotationTree tree) { ExpressionTree argumentValue = Iterables.getOnlyElement(tree.getArguments()); if (argumentValue.getKind() != Kind.ASSIGNMENT) { // :-| Annotation symbol broken. Punt? return null; } return ASTHelpers.constValue(((AssignmentTree) argumentValue).getExpression(), String.class); }
private AnnotationTree modifyAnnotationAttrValue(AnnotationTree annotation, int index, ExpressionTree attrValue, Operation op) { AnnotationTree copy = annotation.getKind() == Kind.ANNOTATION ? Annotation(annotation.getAnnotationType(), c(annotation.getArguments(), index, attrValue, op)) : TypeAnnotation(annotation.getAnnotationType(), c(annotation.getArguments(), index, attrValue, op)); return copy; }
boolean isOverridden = false; for (AnnotationTree annotation : node.getAnnotations()) { ann = annotation.accept(this, v); if ("override".equals(ann)) { isOverridden = true;
private static boolean hasSimpleName(AnnotationTree annotation, String name) { Tree annotationType = annotation.getAnnotationType(); javax.lang.model.element.Name simpleName; if (annotationType instanceof IdentifierTree) { simpleName = ((IdentifierTree) annotationType).getName(); } else if (annotationType instanceof MemberSelectTree) { simpleName = ((MemberSelectTree) annotationType).getIdentifier(); } else { return false; } return simpleName.contentEquals(name); }
private static Optional<ExpressionTree> findArgument( AnnotationTree annotation, String parameter) { for (ExpressionTree argument : annotation.getArguments()) { if (argument.getKind().equals(ASSIGNMENT)) { AssignmentTree assignment = (AssignmentTree) argument; if (assignment.getVariable().toString().equals(parameter)) { return Optional.of(ASTHelpers.stripParentheses(assignment.getExpression())); } } } return Optional.empty(); }
@Override public Tree visitAnnotation(AnnotationTree tree, Void p) { AnnotationTree n = tree.getKind() == Tree.Kind.ANNOTATION ? make.Annotation(tree.getAnnotationType(), tree.getArguments()) : make.TypeAnnotation(tree.getAnnotationType(), tree.getArguments()); model.setType(n, model.getType(tree)); comments.copyComments(tree, n); model.setPos(n, model.getPos(tree)); return n; }
for( AnnotationTree annotation : node.getAnnotations() ) ann = annotation.accept( this, null ); if( "override".equals( ann ) )
@Override @Nullable public Choice<Unifier> visitAnnotation(AnnotationTree annotation, Unifier unifier) { return getAnnotationType() .unify(annotation.getAnnotationType(), unifier) .thenChoose(unifications(getArguments(), annotation.getArguments())); }
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); } }
/** * Gets the value for an argument, or null if the argument does not exist. * * @param annotationTree the AST node for the annotation * @param name the name of the argument whose value to get * @return the value of the argument, or null if the argument does not exist */ @Nullable public static ExpressionTree getArgument(AnnotationTree annotationTree, String name) { for (ExpressionTree argumentTree : annotationTree.getArguments()) { if (argumentTree.getKind() != Tree.Kind.ASSIGNMENT) { continue; } AssignmentTree assignmentTree = (AssignmentTree) argumentTree; if (!assignmentTree.getVariable().toString().equals(name)) { continue; } ExpressionTree expressionTree = assignmentTree.getExpression(); return expressionTree; } return null; }
protected final AnnotationTree rewriteChildren(AnnotationTree tree) { Tree annotationType = translate(tree.getAnnotationType()); List<? extends ExpressionTree> args = translate(tree.getArguments()); if (annotationType!=tree.getAnnotationType() || !args.equals(tree.getArguments())) { if (args != tree.getArguments()) args = optimize(args); AnnotationTree n = tree.getKind() == Kind.ANNOTATION ? make.Annotation(annotationType, args) : make.TypeAnnotation(annotationType, args); model.setType(n, model.getType(tree)); copyCommentTo(tree,n); tree = n; if (tree.getArguments().size() != args.size()) model.setPos(tree, NOPOS); else copyPosTo(tree,n); } return tree; }
/** Detects if the class is annotated as @BTrace(trusted=true). */ private boolean hasTrustedAnnotation(ClassTree ct, Element topElement) { for (AnnotationTree at : ct.getModifiers().getAnnotations()) { String annFqn = ((JCTree)at.getAnnotationType()).type.tsym.getQualifiedName().toString(); if (!annFqn.equals(BTrace.class.getName())) { continue; } // now we have @BTrace, look for unsafe = xxx or trusted = xxx for (ExpressionTree ext : at.getArguments()) { if (!(ext instanceof JCAssign)) { continue; } JCAssign assign = (JCAssign) ext; String name = ((JCIdent)assign.lhs).name.toString(); if (!"unsafe".equals(name) && !"trusted".equals(name)) { continue; } // now rhs is the value of @BTrace.unsafe. // The value can be complex (!!true, 1 == 2, etc.) - we support only booleans String val = assign.rhs.toString(); if ("true".equals(val)) { return true; // bingo! } else if (!"false".equals(val)) { processingEnv.getMessager().printMessage(Kind.WARNING, Messages.get("no.complex.unsafe.value"), topElement); } } } return false; }
@Override public boolean matches(AnnotationTree annotationTree, VisitorState state) { return symbolHasAnnotation(GUICE_BINDING_ANNOTATION) .matches(annotationTree.getAnnotationType(), state); } };
/** * Returns a fix that updates {@code newValues} to the {@code parameterName} argument for {@code * annotation}, regardless of whether there is already an argument. * * <p>N.B.: {@code newValues} are source-code strings, not string literal values. */ public static Builder updateAnnotationArgumentValues( AnnotationTree annotation, String parameterName, Collection<String> newValues) { if (annotation.getArguments().isEmpty()) { String parameterPrefix = parameterName.equals("value") ? "" : (parameterName + " = "); return SuggestedFix.builder() .replace( annotation, annotation .toString() .replaceFirst("\\(\\)", "(" + parameterPrefix + newArgument(newValues) + ")")); } Optional<ExpressionTree> maybeExistingArgument = findArgument(annotation, parameterName); if (!maybeExistingArgument.isPresent()) { return SuggestedFix.builder() .prefixWith( annotation.getArguments().get(0), parameterName + " = " + newArgument(newValues) + ", "); } ExpressionTree existingArgument = maybeExistingArgument.get(); return SuggestedFix.builder().replace(existingArgument, newArgument(newValues)); }
JCIdent ident = (JCIdent) annotationTree.getAnnotationType(); String annotationClassName = ident.sym.getQualifiedName().toString(); if ("org.robolectric.annotation.Implements".equals(annotationClassName)) { for (ExpressionTree expressionTree : annotationTree.getArguments()) { JCAssign jcAnnotation = (JCAssign) expressionTree; if ("isInAndroidSdk".equals(jcAnnotation.lhs.toString())
/** Returns the annotation on {@code classTree} whose type's FQCN is {@code annotationName}. */ static Optional<AnnotationTree> findAnnotation(String annotationName, ClassTree classTree) { for (AnnotationTree annotationTree : classTree.getModifiers().getAnnotations()) { ClassSymbol annotationClass = (ClassSymbol) getSymbol(annotationTree.getAnnotationType()); if (annotationClass.fullname.contentEquals(annotationName)) { return Optional.of(annotationTree); } } return Optional.absent(); }
protected final Fix getSuggestedFix(AnnotationTree annotationTree) { List<String> values = new ArrayList<>(); for (ExpressionTree argumentTree : annotationTree.getArguments()) { AssignmentTree assignmentTree = (AssignmentTree) argumentTree; if (ASTHelpers.getSymbol(assignmentTree.getVariable())
@Override public Void visitAnnotation(AnnotationTree expected, Tree actual) { Optional<AnnotationTree> other = checkTypeAndCast(expected, actual); if (!other.isPresent()) { addTypeMismatch(expected, actual); return null; } scan(expected.getAnnotationType(), other.get().getAnnotationType()); parallelScan(expected.getArguments(), other.get().getArguments()); return null; }