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 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(); }
/** * 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; }
/** * 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)); }
@Override public UAnnotation visitAnnotation(AnnotationTree tree, Void v) { return UAnnotation.create( templateType(tree.getAnnotationType()), templateExpressions(tree.getArguments())); }
@Override @Nullable public Choice<Unifier> visitAnnotation(AnnotationTree annotation, Unifier unifier) { return getAnnotationType() .unify(annotation.getAnnotationType(), unifier) .thenChoose(unifications(getArguments(), annotation.getArguments())); }
/** 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; }
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())
Collection<String> newValues, VisitorState state) { if (annotation.getArguments().isEmpty()) { String parameterPrefix = parameterName.equals("value") ? "" : (parameterName + " = "); return SuggestedFix.builder() return SuggestedFix.builder() .prefixWith( annotation.getArguments().get(0), parameterName + " = " + newArgument(newValues) + ", ");
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())
Iterables.getLast(bugPatternAnnotation.getArguments()), ", providesFix = ProvidesFix." + REQUIRES_HUMAN_ATTENTION.name()); } else {
@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; }
@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; }
@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; }
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 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(); }
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; }
@Override public UAnnotation visitAnnotation(AnnotationTree tree, Void v) { return UAnnotation.create( templateType(tree.getAnnotationType()), templateExpressions(tree.getArguments())); }
@Override @Nullable public Choice<Unifier> visitAnnotation(AnnotationTree annotation, Unifier unifier) { return getAnnotationType() .unify(annotation.getAnnotationType(), unifier) .thenChoose(unifications(getArguments(), annotation.getArguments())); }
@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; }