Search in sources :

Example 6 with JavacProcessingEnvironment

use of com.sun.tools.javac.processing.JavacProcessingEnvironment in project error-prone by google.

the class FieldMissingNullable method findDeclaration.

@Nullable
private VariableTree findDeclaration(VisitorState state, Symbol field) {
    JavacProcessingEnvironment javacEnv = JavacProcessingEnvironment.instance(state.context);
    TreePath fieldDeclPath = Trees.instance(javacEnv).getPath(field);
    // Skip fields declared in other compilation units since we can't make a fix for them here.
    if (fieldDeclPath != null && fieldDeclPath.getCompilationUnit() == state.getPath().getCompilationUnit() && (fieldDeclPath.getLeaf() instanceof VariableTree)) {
        return (VariableTree) fieldDeclPath.getLeaf();
    }
    return null;
}
Also used : TreePath(com.sun.source.util.TreePath) JavacProcessingEnvironment(com.sun.tools.javac.processing.JavacProcessingEnvironment) VariableTree(com.sun.source.tree.VariableTree) Nullable(javax.annotation.Nullable)

Example 7 with JavacProcessingEnvironment

use of com.sun.tools.javac.processing.JavacProcessingEnvironment in project error-prone by google.

the class ParameterNotNullable method findDeclaration.

@Nullable
private VariableTree findDeclaration(VisitorState state, Symbol parameter) {
    JavacProcessingEnvironment javacEnv = JavacProcessingEnvironment.instance(state.context);
    TreePath declPath = Trees.instance(javacEnv).getPath(parameter);
    if (declPath != null && declPath.getCompilationUnit() == state.getPath().getCompilationUnit() && (declPath.getLeaf() instanceof VariableTree)) {
        return (VariableTree) declPath.getLeaf();
    }
    return null;
}
Also used : TreePath(com.sun.source.util.TreePath) JavacProcessingEnvironment(com.sun.tools.javac.processing.JavacProcessingEnvironment) VariableTree(com.sun.source.tree.VariableTree) Nullable(javax.annotation.Nullable)

Example 8 with JavacProcessingEnvironment

use of com.sun.tools.javac.processing.JavacProcessingEnvironment in project bazel by bazelbuild.

the class InternalUtils method greatestLowerBound.

/**
     * Returns the greatest lower bound of two {@link TypeMirror}s.
     *
     * @param processingEnv The {@link ProcessingEnvironment} to use.
     * @param tm1 A {@link TypeMirror}.
     * @param tm2 A {@link TypeMirror}.
     * @return The greatest lower bound of {@code tm1} and {@code tm2}.
     */
public static TypeMirror greatestLowerBound(ProcessingEnvironment processingEnv, TypeMirror tm1, TypeMirror tm2) {
    Type t1 = (Type) tm1;
    Type t2 = (Type) tm2;
    JavacProcessingEnvironment javacEnv = (JavacProcessingEnvironment) processingEnv;
    Types types = Types.instance(javacEnv.getContext());
    if (types.isSameType(t1, t2)) {
        // Special case if the two types are equal.
        return t1;
    }
    // Handle the 'null' type manually.
    if (t1.getKind() == TypeKind.NULL) {
        return t1;
    }
    if (t2.getKind() == TypeKind.NULL) {
        return t2;
    }
    // Special case for primitives.
    if (TypesUtils.isPrimitive(t1) || TypesUtils.isPrimitive(t2)) {
        if (types.isAssignable(t1, t2)) {
            return t1;
        } else if (types.isAssignable(t2, t1)) {
            return t2;
        } else {
            // instead.
            return processingEnv.getTypeUtils().getNoType(TypeKind.NONE);
        }
    }
    if (t1.getKind() == TypeKind.WILDCARD) {
        return t2;
    }
    if (t2.getKind() == TypeKind.WILDCARD) {
        return t1;
    }
    return types.glb(t1, t2);
}
Also used : Types(com.sun.tools.javac.code.Types) JCAnnotatedType(com.sun.tools.javac.tree.JCTree.JCAnnotatedType) WildcardType(javax.lang.model.type.WildcardType) Type(com.sun.tools.javac.code.Type) JavacProcessingEnvironment(com.sun.tools.javac.processing.JavacProcessingEnvironment)

Example 9 with JavacProcessingEnvironment

use of com.sun.tools.javac.processing.JavacProcessingEnvironment in project bazel by bazelbuild.

the class TypesUtils method wildUpperBound.

// Version of com.sun.tools.javac.code.Types.wildUpperBound(Type)
// that works with both jdk8 (called upperBound there) and jdk8u.
// TODO: contrast to upperBound.
public static Type wildUpperBound(ProcessingEnvironment env, TypeMirror tm) {
    Type t = (Type) tm;
    if (t.hasTag(TypeTag.WILDCARD)) {
        Context context = ((JavacProcessingEnvironment) env).getContext();
        Type.WildcardType w = (Type.WildcardType) TypeAnnotationUtils.unannotatedType(t);
        if (w.isSuperBound()) {
            Symtab syms = Symtab.instance(context);
            return w.bound == null ? syms.objectType : w.bound.bound;
        } else {
            return wildUpperBound(env, w.type);
        }
    } else {
        return TypeAnnotationUtils.unannotatedType(t);
    }
}
Also used : Context(com.sun.tools.javac.util.Context) Symtab(com.sun.tools.javac.code.Symtab) ArrayType(javax.lang.model.type.ArrayType) DeclaredType(javax.lang.model.type.DeclaredType) WildcardType(javax.lang.model.type.WildcardType) Type(com.sun.tools.javac.code.Type) WildcardType(javax.lang.model.type.WildcardType) JavacProcessingEnvironment(com.sun.tools.javac.processing.JavacProcessingEnvironment)

Example 10 with JavacProcessingEnvironment

use of com.sun.tools.javac.processing.JavacProcessingEnvironment in project error-prone by google.

the class RestrictedApiChecker method anyAnnotation.

private static Matcher<Tree> anyAnnotation(List<? extends TypeMirror> mirrors, VisitorState state) {
    JavacProcessingEnvironment javacEnv = JavacProcessingEnvironment.instance(state.context);
    ArrayList<Matcher<Tree>> matchers = new ArrayList<>(mirrors.size());
    for (TypeMirror mirror : mirrors) {
        TypeElement typeElem = (TypeElement) javacEnv.getTypeUtils().asElement(mirror);
        String name = mirror.toString();
        if (typeElem != null) {
            // Get the binary name if possible ($ to separate nested members). See b/36160747
            name = javacEnv.getElementUtils().getBinaryName(typeElem).toString();
        }
        matchers.add(Matchers.hasAnnotation(name));
    }
    return Matchers.anyOf(matchers);
}
Also used : NewClassTreeMatcher(com.google.errorprone.bugpatterns.BugChecker.NewClassTreeMatcher) Matcher(com.google.errorprone.matchers.Matcher) MethodInvocationTreeMatcher(com.google.errorprone.bugpatterns.BugChecker.MethodInvocationTreeMatcher) TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) JavacProcessingEnvironment(com.sun.tools.javac.processing.JavacProcessingEnvironment) ArrayList(java.util.ArrayList)

Aggregations

JavacProcessingEnvironment (com.sun.tools.javac.processing.JavacProcessingEnvironment)16 Context (com.sun.tools.javac.util.Context)6 VariableTree (com.sun.source.tree.VariableTree)4 TreePath (com.sun.source.util.TreePath)4 Type (com.sun.tools.javac.code.Type)3 Nullable (javax.annotation.Nullable)3 WildcardType (javax.lang.model.type.WildcardType)3 LocalStore (com.google.errorprone.dataflow.LocalStore)2 ExpressionTree (com.sun.source.tree.ExpressionTree)2 Symtab (com.sun.tools.javac.code.Symtab)2 Types (com.sun.tools.javac.code.Types)2 JCAnnotatedType (com.sun.tools.javac.tree.JCTree.JCAnnotatedType)2 ArrayList (java.util.ArrayList)2 Analysis (org.checkerframework.dataflow.analysis.Analysis)2 ControlFlowGraph (org.checkerframework.dataflow.cfg.ControlFlowGraph)2 UnderlyingAST (org.checkerframework.dataflow.cfg.UnderlyingAST)2 MethodInvocationTreeMatcher (com.google.errorprone.bugpatterns.BugChecker.MethodInvocationTreeMatcher)1 NewClassTreeMatcher (com.google.errorprone.bugpatterns.BugChecker.NewClassTreeMatcher)1 Matcher (com.google.errorprone.matchers.Matcher)1 Tree (com.sun.source.tree.Tree)1