Search in sources :

Example 6 with Symtab

use of com.sun.tools.javac.code.Symtab in project error-prone by google.

the class Template method infer.

/**
   * Returns the inferred method type of the template based on the given actual argument types.
   *
   * @throws InferException if no instances of the specified type variables would allow the
   *         {@code actualArgTypes} to match the {@code expectedArgTypes}
   */
private Type infer(Warner warner, Inliner inliner, List<Type> freeTypeVariables, List<Type> expectedArgTypes, Type returnType, List<Type> actualArgTypes) throws InferException {
    Symtab symtab = inliner.symtab();
    Type methodType = new MethodType(expectedArgTypes, returnType, List.<Type>nil(), symtab.methodClass);
    if (!freeTypeVariables.isEmpty()) {
        methodType = new ForAll(freeTypeVariables, methodType);
    }
    Enter enter = inliner.enter();
    MethodSymbol methodSymbol = new MethodSymbol(0, inliner.asName("__m__"), methodType, symtab.unknownSymbol);
    Type site = symtab.methodClass.type;
    Env<AttrContext> env = enter.getTopLevelEnv(TreeMaker.instance(inliner.getContext()).TopLevel(List.<JCTree>nil()));
    // Set up the resolution phase:
    try {
        Field field = AttrContext.class.getDeclaredField("pendingResolutionPhase");
        field.setAccessible(true);
        field.set(env.info, newMethodResolutionPhase(autoboxing()));
    } catch (ReflectiveOperationException e) {
        throw new LinkageError(e.getMessage(), e);
    }
    Object resultInfo;
    try {
        Class<?> resultInfoClass = Class.forName("com.sun.tools.javac.comp.Attr$ResultInfo");
        Constructor<?> resultInfoCtor = resultInfoClass.getDeclaredConstructor(Attr.class, KindSelector.class, Type.class);
        resultInfoCtor.setAccessible(true);
        resultInfo = resultInfoCtor.newInstance(Attr.instance(inliner.getContext()), KindSelector.PCK, Type.noType);
    } catch (ReflectiveOperationException e) {
        throw new LinkageError(e.getMessage(), e);
    }
    // Type inference sometimes produces diagnostics, so we need to catch them to avoid interfering
    // with the enclosing compilation.
    Log.DeferredDiagnosticHandler handler = new Log.DeferredDiagnosticHandler(Log.instance(inliner.getContext()));
    try {
        MethodType result = callCheckMethod(warner, inliner, resultInfo, actualArgTypes, methodSymbol, site, env);
        if (!handler.getDiagnostics().isEmpty()) {
            throw new InferException(handler.getDiagnostics());
        }
        return result;
    } finally {
        Log.instance(inliner.getContext()).popDiagnosticHandler(handler);
    }
}
Also used : MethodType(com.sun.tools.javac.code.Type.MethodType) Log(com.sun.tools.javac.util.Log) JCTree(com.sun.tools.javac.tree.JCTree) AttrContext(com.sun.tools.javac.comp.AttrContext) Symtab(com.sun.tools.javac.code.Symtab) Field(java.lang.reflect.Field) MethodType(com.sun.tools.javac.code.Type.MethodType) Type(com.sun.tools.javac.code.Type) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) Enter(com.sun.tools.javac.comp.Enter) ForAll(com.sun.tools.javac.code.Type.ForAll)

Example 7 with Symtab

use of com.sun.tools.javac.code.Symtab in project ceylon-compiler by ceylon.

the class TestSymtabItems method run.

void run() throws Exception {
    Context c = new Context();
    JavacFileManager.preRegister(c);
    Symtab syms = Symtab.instance(c);
    JavacTypes types = JavacTypes.instance(c);
    // will init ClassReader.sourceCompleter
    JavaCompiler.instance(c);
    for (Field f : Symtab.class.getDeclaredFields()) {
        // during API evolution
        if (f.getName().toLowerCase().contains("methodhandle"))
            continue;
        Class<?> ft = f.getType();
        if (TypeMirror.class.isAssignableFrom(ft))
            print(f.getName(), (TypeMirror) f.get(syms), types);
        else if (Element.class.isAssignableFrom(ft))
            print(f.getName(), (Element) f.get(syms));
    }
    if (errors > 0)
        throw new Exception(errors + " errors occurred");
}
Also used : Context(com.sun.tools.javac.util.Context) Symtab(com.sun.tools.javac.code.Symtab) JavacTypes(com.sun.tools.javac.model.JavacTypes) Field(java.lang.reflect.Field) TypeMirror(javax.lang.model.type.TypeMirror) PackageElement(javax.lang.model.element.PackageElement) ExecutableElement(javax.lang.model.element.ExecutableElement) VariableElement(javax.lang.model.element.VariableElement) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) TypeParameterElement(javax.lang.model.element.TypeParameterElement) UnknownTypeException(javax.lang.model.type.UnknownTypeException) UnknownElementException(javax.lang.model.element.UnknownElementException)

Example 8 with Symtab

use of com.sun.tools.javac.code.Symtab in project ceylon-compiler by ceylon.

the class MakeLiteralTest method run.

void run() throws Exception {
    Context context = new Context();
    JavacFileManager.preRegister(context);
    Symtab syms = Symtab.instance(context);
    maker = TreeMaker.instance(context);
    types = Types.instance(context);
    test("abc", CLASS, syms.stringType, "abc");
    test(Boolean.FALSE, BOOLEAN, syms.booleanType, Integer.valueOf(0));
    test(Boolean.TRUE, BOOLEAN, syms.booleanType, Integer.valueOf(1));
    test(Byte.valueOf((byte) 1), BYTE, syms.byteType, Byte.valueOf((byte) 1));
    test(Character.valueOf('a'), CHAR, syms.charType, Integer.valueOf('a'));
    test(Double.valueOf(1d), DOUBLE, syms.doubleType, Double.valueOf(1d));
    test(Float.valueOf(1f), FLOAT, syms.floatType, Float.valueOf(1f));
    test(Integer.valueOf(1), INT, syms.intType, Integer.valueOf(1));
    test(Long.valueOf(1), LONG, syms.longType, Long.valueOf(1));
    test(Short.valueOf((short) 1), SHORT, syms.shortType, Short.valueOf((short) 1));
    if (errors > 0)
        throw new Exception(errors + " errors found");
}
Also used : Context(com.sun.tools.javac.util.Context) Symtab(com.sun.tools.javac.code.Symtab)

Example 9 with Symtab

use of com.sun.tools.javac.code.Symtab 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 Symtab

use of com.sun.tools.javac.code.Symtab in project error-prone by google.

the class BoxedPrimitiveConstructor method matchNewClass.

@Override
public Description matchNewClass(NewClassTree tree, VisitorState state) {
    Symbol sym = ASTHelpers.getSymbol(tree.getIdentifier());
    if (sym == null) {
        return NO_MATCH;
    }
    Types types = state.getTypes();
    Symtab symtab = state.getSymtab();
    // TODO(cushon): consider handling String also
    if (sym.equals(types.boxedClass(symtab.byteType)) || sym.equals(types.boxedClass(symtab.charType)) || sym.equals(types.boxedClass(symtab.shortType)) || sym.equals(types.boxedClass(symtab.intType)) || sym.equals(types.boxedClass(symtab.longType)) || sym.equals(types.boxedClass(symtab.doubleType)) || sym.equals(types.boxedClass(symtab.floatType)) || sym.equals(types.boxedClass(symtab.booleanType))) {
        return describeMatch(tree, buildFix(tree, state));
    }
    return NO_MATCH;
}
Also used : Symtab(com.sun.tools.javac.code.Symtab) Types(com.sun.tools.javac.code.Types) Symbol(com.sun.tools.javac.code.Symbol)

Aggregations

Symtab (com.sun.tools.javac.code.Symtab)16 Type (com.sun.tools.javac.code.Type)6 Types (com.sun.tools.javac.code.Types)5 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)3 JCStatement (com.sun.tools.javac.tree.JCTree.JCStatement)3 Context (com.sun.tools.javac.util.Context)3 JavacTreeMaker (lombok.javac.JavacTreeMaker)3 ExpressionTree (com.sun.source.tree.ExpressionTree)2 MethodTree (com.sun.source.tree.MethodTree)2 Tree (com.sun.source.tree.Tree)2 MethodSymbol (com.sun.tools.javac.code.Symbol.MethodSymbol)2 ClassType (com.sun.tools.javac.code.Type.ClassType)2 JavacProcessingEnvironment (com.sun.tools.javac.processing.JavacProcessingEnvironment)2 JCTree (com.sun.tools.javac.tree.JCTree)2 Field (java.lang.reflect.Field)2 ArrayList (java.util.ArrayList)2 VisitorState (com.google.errorprone.VisitorState)1 Builder (com.google.errorprone.fixes.SuggestedFix.Builder)1 Matchers.toType (com.google.errorprone.matchers.Matchers.toType)1 ASTHelpers.getType (com.google.errorprone.util.ASTHelpers.getType)1