Search in sources :

Example 21 with JavacProcessingEnvironment

use of com.sun.tools.javac.processing.JavacProcessingEnvironment in project checker-framework by typetools.

the class ElementUtils method getAllSupertypes.

/**
 * Get all the supertypes of a given type, including the type itself. The result includes both
 * superclasses and implemented interfaces.
 *
 * @param type a type
 * @param env the processing environment
 * @return list including the type and all its supertypes, with a guarantee that direct supertypes
 *     (i.e. those that appear in extends or implements clauses) appear before indirect supertypes
 */
public static List<TypeElement> getAllSupertypes(TypeElement type, ProcessingEnvironment env) {
    Context ctx = ((JavacProcessingEnvironment) env).getContext();
    com.sun.tools.javac.code.Types javacTypes = com.sun.tools.javac.code.Types.instance(ctx);
    return CollectionsPlume.<Type, TypeElement>mapList(t -> (TypeElement) t.tsym, javacTypes.closure(((Symbol) type).type));
}
Also used : Context(com.sun.tools.javac.util.Context) DeclaredType(javax.lang.model.type.DeclaredType) Type(com.sun.tools.javac.code.Type) TypeElement(javax.lang.model.element.TypeElement) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) Symbol(com.sun.tools.javac.code.Symbol) JavacProcessingEnvironment(com.sun.tools.javac.processing.JavacProcessingEnvironment)

Example 22 with JavacProcessingEnvironment

use of com.sun.tools.javac.processing.JavacProcessingEnvironment in project checker-framework by typetools.

the class DefaultReflectionResolver method getMethodSymbolsfor.

/**
 * Get set of MethodSymbols based on class name, method name, and parameter length.
 *
 * @param className the class that contains the method
 * @param methodName the method's name
 * @param paramLength the number of parameters
 * @param env the environment
 * @return the (potentially empty) set of corresponding method Symbol(s)
 */
private List<Symbol> getMethodSymbolsfor(String className, String methodName, int paramLength, Env<AttrContext> env) {
    Context context = ((JavacProcessingEnvironment) processingEnv).getContext();
    Resolve resolve = Resolve.instance(context);
    Names names = Names.instance(context);
    Symbol sym = getSymbol(className, env, names, resolve);
    if (!sym.exists()) {
        debugReflection("Unable to resolve class: " + className);
        return Collections.emptyList();
    }
    // The common case is probably that `result` is a singleton at method exit.
    List<Symbol> result = new ArrayList<>();
    ClassSymbol classSym = (ClassSymbol) sym;
    while (classSym != null) {
        for (Symbol s : classSym.getEnclosedElements()) {
            // check all member methods
            if (s.getKind() == ElementKind.METHOD) {
                // Check for method name and number of arguments
                if (names.fromString(methodName) == s.name && ((MethodSymbol) s).getParameters().size() == paramLength) {
                    result.add(s);
                }
            }
        }
        if (!result.isEmpty()) {
            break;
        }
        Type t = classSym.getSuperclass();
        if (!t.hasTag(TypeTag.CLASS) || t.isErroneous()) {
            break;
        }
        classSym = (ClassSymbol) t.tsym;
    }
    if (result.isEmpty()) {
        debugReflection("Unable to resolve method: " + className + "@" + methodName);
    }
    return result;
}
Also used : Context(com.sun.tools.javac.util.Context) AttrContext(com.sun.tools.javac.comp.AttrContext) Names(com.sun.tools.javac.util.Names) AnnotatedArrayType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedArrayType) ParameterizedExecutableType(org.checkerframework.framework.type.AnnotatedTypeFactory.ParameterizedExecutableType) Type(com.sun.tools.javac.code.Type) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) Symbol(com.sun.tools.javac.code.Symbol) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) JavacProcessingEnvironment(com.sun.tools.javac.processing.JavacProcessingEnvironment) ArrayList(java.util.ArrayList) Resolve(com.sun.tools.javac.comp.Resolve)

Example 23 with JavacProcessingEnvironment

use of com.sun.tools.javac.processing.JavacProcessingEnvironment in project checker-framework by typetools.

the class AnnotatedTypeFactory method getCheckerNames.

/**
 * Returns the names of the annotation processors that are being run.
 *
 * @return the names of the annotation processors that are being run
 */
// ClassLoader.getResources returns an Enumeration
@SuppressWarnings("JdkObsolete")
public String[] getCheckerNames() {
    com.sun.tools.javac.util.Context context = ((JavacProcessingEnvironment) processingEnv).getContext();
    String processorArg = Options.instance(context).get("-processor");
    if (processorArg != null) {
        return processorArg.split(",");
    }
    try {
        String filename = "META-INF/services/javax.annotation.processing.Processor";
        List<String> lines = new ArrayList<>();
        Enumeration<URL> urls = getClass().getClassLoader().getResources(filename);
        while (urls.hasMoreElements()) {
            URL url = urls.nextElement();
            BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
            lines.addAll(in.lines().collect(Collectors.toList()));
        }
        String[] result = lines.toArray(new String[0]);
        return result;
    } catch (IOException e) {
        throw new BugInCF(e);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) ArrayList(java.util.ArrayList) IOException(java.io.IOException) BugInCF(org.checkerframework.javacutil.BugInCF) URL(java.net.URL) JavacProcessingEnvironment(com.sun.tools.javac.processing.JavacProcessingEnvironment) BufferedReader(java.io.BufferedReader)

Example 24 with JavacProcessingEnvironment

use of com.sun.tools.javac.processing.JavacProcessingEnvironment in project checker-framework by typetools.

the class BoundsInitializer method createAndSetLowerBound.

/**
 * Creates the lower bound type for {@code typeVar} and sets it. If the type variable does not
 * have a lower bound, then a null type is created.
 *
 * @param typeVar type variable
 * @return the newly created lower bound
 */
private static AnnotatedTypeMirror createAndSetLowerBound(AnnotatedTypeVariable typeVar) {
    TypeMirror lb = typeVar.getUnderlyingType().getLowerBound();
    if (lb == null) {
        // Use bottom type to ensure there is a lower bound.
        Context context = ((JavacProcessingEnvironment) typeVar.atypeFactory.processingEnv).getContext();
        Symtab syms = Symtab.instance(context);
        lb = syms.botType;
    }
    AnnotatedTypeMirror lowerBound = AnnotatedTypeMirror.createType(lb, typeVar.atypeFactory, false);
    typeVar.setLowerBound(lowerBound);
    return lowerBound;
}
Also used : Context(com.sun.tools.javac.util.Context) Symtab(com.sun.tools.javac.code.Symtab) TypeMirror(javax.lang.model.type.TypeMirror) JavacProcessingEnvironment(com.sun.tools.javac.processing.JavacProcessingEnvironment)

Example 25 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)

Aggregations

JavacProcessingEnvironment (com.sun.tools.javac.processing.JavacProcessingEnvironment)49 Context (com.sun.tools.javac.util.Context)30 Type (com.sun.tools.javac.code.Type)13 WildcardType (javax.lang.model.type.WildcardType)11 DeclaredType (javax.lang.model.type.DeclaredType)9 ArrayList (java.util.ArrayList)7 ArrayType (javax.lang.model.type.ArrayType)7 TreePath (com.sun.source.util.TreePath)6 Symtab (com.sun.tools.javac.code.Symtab)6 CapturedType (com.sun.tools.javac.code.Type.CapturedType)6 ClassType (com.sun.tools.javac.code.Type.ClassType)6 PrimitiveType (javax.lang.model.type.PrimitiveType)6 UnionType (javax.lang.model.type.UnionType)6 Symbol (com.sun.tools.javac.code.Symbol)5 ClassSymbol (com.sun.tools.javac.code.Symbol.ClassSymbol)5 MethodSymbol (com.sun.tools.javac.code.Symbol.MethodSymbol)5 Types (com.sun.tools.javac.code.Types)5 VariableTree (com.sun.source.tree.VariableTree)4 AttrContext (com.sun.tools.javac.comp.AttrContext)4 Log (com.sun.tools.javac.util.Log)3