Search in sources :

Example 1 with AnnotationTree

use of com.sun.source.tree.AnnotationTree in project error-prone by google.

the class AssistedInjectScoping method matchClass.

@Override
public final Description matchClass(ClassTree classTree, VisitorState state) {
    MultiMatchResult<AnnotationTree> hasScopeAnnotations = CLASS_TO_SCOPE_ANNOTATIONS.multiMatchResult(classTree, state);
    if (!hasScopeAnnotations.matches() || !HAS_ASSISTED_CONSTRUCTOR.matches(classTree, state)) {
        return Description.NO_MATCH;
    }
    AnnotationTree annotationWithScopeAnnotation = hasScopeAnnotations.matchingNodes().get(0);
    if (annotationWithScopeAnnotation == null) {
        throw new IllegalStateException("Expected to find an annotation that was annotated with @ScopeAnnotation");
    }
    return describeMatch(annotationWithScopeAnnotation, SuggestedFix.delete(annotationWithScopeAnnotation));
}
Also used : AnnotationTree(com.sun.source.tree.AnnotationTree)

Example 2 with AnnotationTree

use of com.sun.source.tree.AnnotationTree in project error-prone by google.

the class ParameterNotNullable method matchDereference.

private Description matchDereference(ExpressionTree dereferencedExpression, VisitorState state) {
    Symbol dereferenced = ASTHelpers.getSymbol(dereferencedExpression);
    if (dereferenced == null || dereferenced.getKind() != ElementKind.PARAMETER || dereferenced.type.isPrimitive()) {
        // not a parameter dereference
        return Description.NO_MATCH;
    }
    if (!TrustingNullnessAnalysis.hasNullableAnnotation(dereferenced)) {
        return Description.NO_MATCH;
    }
    Nullness nullness = TrustingNullnessAnalysis.instance(state.context).getNullness(new TreePath(state.getPath(), dereferencedExpression), state.context);
    if (nullness != Nullness.NULLABLE) {
        return Description.NO_MATCH;
    }
    for (AnnotationTree anno : findDeclaration(state, dereferenced).getModifiers().getAnnotations()) {
        if (ASTHelpers.getSymbol(anno).type.toString().endsWith(".Nullable")) {
            return buildDescription(dereferencedExpression).setMessage("Nullable parameter not checked for null").addFix(SuggestedFix.delete(anno)).build();
        }
    }
    // Shouldn't get here
    return Description.NO_MATCH;
}
Also used : TreePath(com.sun.source.util.TreePath) Symbol(com.sun.tools.javac.code.Symbol) AnnotationTree(com.sun.source.tree.AnnotationTree) Nullness(com.google.errorprone.dataflow.nullnesspropagation.Nullness)

Example 3 with AnnotationTree

use of com.sun.source.tree.AnnotationTree in project error-prone by google.

the class QualifierOnMethodWithoutProvides method matchMethod.

@Override
public Description matchMethod(MethodTree tree, VisitorState state) {
    MultiMatchResult<AnnotationTree> qualifierAnnotations = QUALIFIER_ANNOTATION_FINDER.multiMatchResult(tree, state);
    if (qualifierAnnotations.matches() && NOT_ABSTRACT.matches(tree, state) && NOT_PROVIDES_METHOD.matches(tree, state)) {
        // Otherwise, suggest removing the qualifier annotation
        if (not(methodReturns(anyOf(isSameType(Suppliers.VOID_TYPE), isSameType(Suppliers.JAVA_LANG_VOID_TYPE)))).matches(tree, state)) {
            if (INSIDE_GUICE_MODULE.matches(tree, state)) {
                // Guice Module
                SuggestedFix fix = SuggestedFix.builder().addStaticImport(GUICE_PROVIDES_ANNOTATION).prefixWith(tree, "@Provides ").build();
                return describeMatch(tree, fix);
            }
            if (enclosingClass(IS_DAGGER_COMPONENT_OR_MODULE).matches(tree, state)) {
                // Dagger component
                SuggestedFix fix = SuggestedFix.builder().addStaticImport(DAGGER_PROVIDES_ANNOTATION).prefixWith(tree, "@Provides ").build();
                return describeMatch(tree, fix);
            }
        }
        List<AnnotationTree> matchingNodes = qualifierAnnotations.matchingNodes();
        Builder fixBuilder = SuggestedFix.builder();
        matchingNodes.forEach(fixBuilder::delete);
        return describeMatch(matchingNodes.get(0), fixBuilder.build());
    }
    return Description.NO_MATCH;
}
Also used : SuggestedFix(com.google.errorprone.fixes.SuggestedFix) Builder(com.google.errorprone.fixes.SuggestedFix.Builder) AnnotationTree(com.sun.source.tree.AnnotationTree)

Example 4 with AnnotationTree

use of com.sun.source.tree.AnnotationTree in project error-prone by google.

the class QualifierWithTypeUse method matchClass.

@Override
public Description matchClass(ClassTree tree, VisitorState state) {
    if (IS_QUALIFIER_WITH_TARGET.matches(tree, state)) {
        MultiMatchResult<AnnotationTree> targetAnnotation = HAS_TARGET_ANNOTATION.multiMatchResult(tree, state);
        if (targetAnnotation.matches()) {
            AnnotationTree annotationTree = targetAnnotation.onlyMatchingNode();
            Target target = ASTHelpers.getAnnotation(tree, Target.class);
            if (hasTypeUseOrTypeParameter(target)) {
                return describeMatch(annotationTree, removeTypeUse(target, annotationTree));
            }
        }
    }
    return Description.NO_MATCH;
}
Also used : Target(java.lang.annotation.Target) AnnotationTree(com.sun.source.tree.AnnotationTree)

Example 5 with AnnotationTree

use of com.sun.source.tree.AnnotationTree in project checker-framework by typetools.

the class DependentTypesHelper method checkType.

/**
 * Checks all expression in the given annotated type to see if the expression string is an error
 * string as specified by {@link DependentTypesError#isExpressionError}. If the annotated type
 * has any errors, a flowexpr.parse.error is issued at the errorTree.
 *
 * @param atm annotated type to check for expression errors
 * @param errorTree the tree at which to report any found errors
 */
public void checkType(AnnotatedTypeMirror atm, Tree errorTree) {
    List<DependentTypesError> errors = new ExpressionErrorChecker().visit(atm);
    if (errors == null || errors.isEmpty()) {
        return;
    }
    if (errorTree.getKind() == Kind.VARIABLE) {
        ModifiersTree modifiers = ((VariableTree) errorTree).getModifiers();
        errorTree = ((VariableTree) errorTree).getType();
        for (AnnotationTree annoTree : modifiers.getAnnotations()) {
            for (Class<?> annoClazz : annoToElements.keySet()) {
                if (annoTree.toString().contains(annoClazz.getSimpleName())) {
                    errorTree = annoTree;
                    break;
                }
            }
        }
    }
    reportErrors(errorTree, errors);
}
Also used : ModifiersTree(com.sun.source.tree.ModifiersTree) VariableTree(com.sun.source.tree.VariableTree) AnnotationTree(com.sun.source.tree.AnnotationTree)

Aggregations

AnnotationTree (com.sun.source.tree.AnnotationTree)32 Tree (com.sun.source.tree.Tree)11 ExpressionTree (com.sun.source.tree.ExpressionTree)10 ClassTree (com.sun.source.tree.ClassTree)9 MethodTree (com.sun.source.tree.MethodTree)8 VariableTree (com.sun.source.tree.VariableTree)8 ArrayList (java.util.ArrayList)7 SuggestedFix (com.google.errorprone.fixes.SuggestedFix)6 IdentifierTree (com.sun.source.tree.IdentifierTree)6 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)6 ModifiersTree (com.sun.source.tree.ModifiersTree)6 AssignmentTree (com.sun.source.tree.AssignmentTree)5 AnnotatedTypeTree (com.sun.source.tree.AnnotatedTypeTree)4 ArrayAccessTree (com.sun.source.tree.ArrayAccessTree)4 CompoundAssignmentTree (com.sun.source.tree.CompoundAssignmentTree)4 ConditionalExpressionTree (com.sun.source.tree.ConditionalExpressionTree)4 LambdaExpressionTree (com.sun.source.tree.LambdaExpressionTree)4 MemberReferenceTree (com.sun.source.tree.MemberReferenceTree)4 MemberSelectTree (com.sun.source.tree.MemberSelectTree)4 NewClassTree (com.sun.source.tree.NewClassTree)4