Search in sources :

Example 31 with ClassSymbol

use of com.sun.tools.javac.code.Symbol.ClassSymbol in project j2objc by google.

the class JavacEnvironment method resolve.

@Override
public Element resolve(String name) {
    Name className = javacNames.fromString(name);
    // Check first if compiler already created or loaded the class.
    ClassSymbol symbol = symbolTable.classes.get(className);
    if (symbol == null) {
        // Not available, read it from a class file.
        // Note: the enterName(Name) method moved from ClassReader to
        // Symtab in Java 9. Reflection is used to support both locations.
        symbol = enterClassJavac(className);
        if (symbol != null) {
            symbolTable.classes.put(className, symbol);
        } else {
            symbol = enterClassJavac9(className);
        // The symbolTable is already updated in Java 9.
        }
    }
    return symbol;
}
Also used : ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) Name(com.sun.tools.javac.util.Name)

Example 32 with ClassSymbol

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

the class VisitorState method getTypeFromStringInternal.

private Type getTypeFromStringInternal(String typeStr) {
    validateTypeStr(typeStr);
    if (isPrimitiveType(typeStr)) {
        return getPrimitiveType(typeStr);
    }
    Name typeName = getName(typeStr);
    try {
        ClassSymbol typeSymbol = getSymtab().classes.get(typeName);
        if (typeSymbol == null) {
            JavaCompiler compiler = JavaCompiler.instance(context);
            Symbol sym = compiler.resolveIdent(typeStr);
            if (!(sym instanceof ClassSymbol)) {
                return null;
            }
            typeSymbol = (ClassSymbol) sym;
        }
        Type type = typeSymbol.asType();
        // Throws CompletionFailure if the source/class file for this type is not available.
        // This is hacky but the best way I can think of to handle this case.
        type.complete();
        if (type.isErroneous()) {
            return null;
        }
        return type;
    } catch (CompletionFailure failure) {
        return null;
    }
}
Also used : ClassType(com.sun.tools.javac.code.Type.ClassType) ArrayType(com.sun.tools.javac.code.Type.ArrayType) Type(com.sun.tools.javac.code.Type) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) Symbol(com.sun.tools.javac.code.Symbol) CompletionFailure(com.sun.tools.javac.code.Symbol.CompletionFailure) JavaCompiler(com.sun.tools.javac.main.JavaCompiler) Name(com.sun.tools.javac.util.Name)

Example 33 with ClassSymbol

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

the class IncompatibleArgumentType method resolveTypeFromClass.

// class Foo<X> { void something(@CW("X") Object x); }
// new Foo<String>().something(123);
@Nullable
private RequiredType resolveTypeFromClass(Type calledType, ClassSymbol clazzSymbol, String typeArgName, VisitorState state) {
    // Try on the class
    int tyargIndex = findTypeArgInList(clazzSymbol, typeArgName);
    if (tyargIndex != -1) {
        return RequiredType.create(extractTypeArgAsMemberOfSupertype(calledType, clazzSymbol, tyargIndex, state.getTypes()));
    }
    while (clazzSymbol.isInner()) {
        // class Foo<T> {
        //    class Bar {
        //      void something(@CW("T") Object o));
        //    }
        // }
        // new Foo<String>().new Bar().something(123); // should fail, 123 needs to match String
        ClassSymbol encloser = clazzSymbol.owner.enclClass();
        calledType = calledType.getEnclosingType();
        tyargIndex = findTypeArgInList(encloser, typeArgName);
        if (tyargIndex != -1) {
            if (calledType.getTypeArguments().isEmpty()) {
                // bar.something(123); // Nope (this call would be unchecked if arg was T)
                return null;
            }
            return RequiredType.create(calledType.getTypeArguments().get(tyargIndex));
        }
        clazzSymbol = encloser;
    }
    return null;
}
Also used : ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) Nullable(javax.annotation.Nullable)

Example 34 with ClassSymbol

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

the class CheckReturnValue method checkEnclosingClasses.

private static Optional<Boolean> checkEnclosingClasses(MethodSymbol method, VisitorState state) {
    Symbol enclosingClass = enclosingClass(method);
    while (enclosingClass instanceof ClassSymbol) {
        Optional<Boolean> result = shouldCheckReturnValue(enclosingClass, state);
        if (result.isPresent()) {
            return result;
        }
        enclosingClass = enclosingClass.owner;
    }
    return Optional.absent();
}
Also used : MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) Symbol(com.sun.tools.javac.code.Symbol) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol)

Example 35 with ClassSymbol

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

the class FunctionalInterfaceClash method matchClass.

@Override
public Description matchClass(ClassTree tree, VisitorState state) {
    ClassSymbol origin = getSymbol(tree);
    Types types = state.getTypes();
    // collect declared and inherited methods whose signature contains a functional interface
    Multimap<String, MethodSymbol> methods = HashMultimap.create();
    for (Symbol sym : types.membersClosure(getType(tree), /*skipInterface=*/
    false).getSymbols()) {
        if (!(sym instanceof MethodSymbol)) {
            continue;
        }
        if (isBugCheckerSuppressed((MethodSymbol) sym)) {
            continue;
        }
        MethodSymbol msym = (MethodSymbol) sym;
        if (msym.getParameters().stream().noneMatch(p -> maybeFunctionalInterface(p.type, types))) {
            continue;
        }
        if (msym.isConstructor() && !msym.owner.equals(origin)) {
            continue;
        }
        methods.put(functionalInterfaceSignature(state, msym), msym);
    }
    // (don't report clashes between inherited members)
    for (Tree member : tree.getMembers()) {
        if (!(member instanceof MethodTree)) {
            continue;
        }
        MethodSymbol msym = getSymbol((MethodTree) member);
        if (msym.getParameters().stream().noneMatch(p -> maybeFunctionalInterface(p.type, types))) {
            continue;
        }
        Collection<MethodSymbol> clash = new ArrayList<>(methods.removeAll(functionalInterfaceSignature(state, msym)));
        clash.remove(msym);
        // ignore inherited methods that are overridden in the original class
        clash.removeIf(m -> msym.overrides(m, origin, types, false));
        if (!clash.isEmpty()) {
            String message = "When passing lambda arguments to this function, callers will need a cast to" + " disambiguate with: " + clash.stream().map(m -> Signatures.prettyMethodSignature(origin, m)).collect(joining("\n    "));
            state.reportMatch(buildDescription(member).setMessage(message).build());
        }
    }
    return NO_MATCH;
}
Also used : Types(com.sun.tools.javac.code.Types) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) MethodTree(com.sun.source.tree.MethodTree) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) ASTHelpers.getSymbol(com.google.errorprone.util.ASTHelpers.getSymbol) Symbol(com.sun.tools.javac.code.Symbol) ArrayList(java.util.ArrayList) MethodTree(com.sun.source.tree.MethodTree) Tree(com.sun.source.tree.Tree) ClassTree(com.sun.source.tree.ClassTree)

Aggregations

ClassSymbol (com.sun.tools.javac.code.Symbol.ClassSymbol)49 Symbol (com.sun.tools.javac.code.Symbol)20 MethodSymbol (com.sun.tools.javac.code.Symbol.MethodSymbol)20 Type (com.sun.tools.javac.code.Type)17 VarSymbol (com.sun.tools.javac.code.Symbol.VarSymbol)10 PackageSymbol (com.sun.tools.javac.code.Symbol.PackageSymbol)9 ClassType (com.sun.tools.javac.code.Type.ClassType)9 ClassTree (com.sun.source.tree.ClassTree)8 TypeSymbol (com.sun.tools.javac.code.Symbol.TypeSymbol)8 MethodTree (com.sun.source.tree.MethodTree)6 Tree (com.sun.source.tree.Tree)6 ArrayType (com.sun.tools.javac.code.Type.ArrayType)5 CompilationUnitTree (com.sun.source.tree.CompilationUnitTree)4 VariableTree (com.sun.source.tree.VariableTree)4 Name (com.sun.tools.javac.util.Name)4 ArrayList (java.util.ArrayList)4 Violation (com.google.errorprone.bugpatterns.threadsafety.ImmutableAnalysis.Violation)3 BlockTree (com.sun.source.tree.BlockTree)3 EnhancedForLoopTree (com.sun.source.tree.EnhancedForLoopTree)3 ForLoopTree (com.sun.source.tree.ForLoopTree)3