Search in sources :

Example 16 with TypeSymbol

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

the class ASTHelpers method enumValues.

/** @return all values of the given enum type, in declaration order. */
public static LinkedHashSet<String> enumValues(TypeSymbol enumType) {
    if (enumType.getKind() != ElementKind.ENUM) {
        throw new IllegalStateException();
    }
    Scope scope = enumType.members();
    Deque<String> values = new ArrayDeque<>();
    for (Symbol sym : scope.getSymbols()) {
        if (sym instanceof VarSymbol) {
            VarSymbol var = (VarSymbol) sym;
            if ((var.flags() & Flags.ENUM) != 0) {
                /**
           * Javac gives us the members backwards, apparently. It's worth making an effort to
           * preserve declaration order because it's useful for diagnostics (e.g. in
           * {@link MissingCasesInEnumSwitch}).
           */
                values.push(sym.name.toString());
            }
        }
    }
    return new LinkedHashSet<>(values);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Scope(com.sun.tools.javac.code.Scope) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) TypeSymbol(com.sun.tools.javac.code.Symbol.TypeSymbol) Symbol(com.sun.tools.javac.code.Symbol) PackageSymbol(com.sun.tools.javac.code.Symbol.PackageSymbol) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) ArrayDeque(java.util.ArrayDeque)

Example 17 with TypeSymbol

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

the class FragmentInjection method matchClass.

@Override
public Description matchClass(ClassTree tree, VisitorState state) {
    // Only examine classes that extend PreferenceActivity.
    Type preferenceActivityType = state.getTypeFromString("android.preference.PreferenceActivity");
    if (!isSubtype(getType(tree), preferenceActivityType, state)) {
        return NO_MATCH;
    }
    // Examine each method in the class. Complain if isValidFragment not implemented.
    TypeSymbol preferenceActivityTypeSymbol = preferenceActivityType.tsym;
    boolean methodNotImplemented = true;
    try {
        MethodSymbol isValidFragmentMethodSymbol = resolveExistingMethod(state, getSymbol(tree), state.getName("isValidFragment"), ImmutableList.<Type>of(state.getTypeFromString("java.lang.String")), ImmutableList.<Type>of());
        methodNotImplemented = isValidFragmentMethodSymbol.owner.equals(preferenceActivityTypeSymbol);
    } catch (FatalError e) {
    // If isValidFragment method symbol is not found, then we must be compiling against an old SDK
    // version (< 19) in which isValidFragment is not yet implemented, and neither this class nor
    // any of its super classes have implemented it.
    }
    // isValidFragment, and this is not an abstract class, emit warning.
    if (methodNotImplemented && not(hasModifier(Modifier.ABSTRACT)).matches(tree, state)) {
        return buildDescription(tree).setMessage("Class extending PreferenceActivity does not implement isValidFragment.").build();
    }
    // Check the implementation of isValidFragment. Complain if it always returns true.
    MethodTree isValidFragmentMethodTree = getMethod(OVERRIDES_IS_VALID_FRAGMENT, tree, state);
    if (isValidFragmentMethodTree != null) {
        if (isValidFragmentMethodTree.accept(ALWAYS_RETURNS_TRUE, null)) {
            return buildDescription(isValidFragmentMethodTree).setMessage("isValidFragment unconditionally returns true.").build();
        }
    }
    return NO_MATCH;
}
Also used : FatalError(com.sun.tools.javac.util.FatalError) ASTHelpers.getType(com.google.errorprone.util.ASTHelpers.getType) Matchers.isSameType(com.google.errorprone.matchers.Matchers.isSameType) Type(com.sun.tools.javac.code.Type) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) MethodTree(com.sun.source.tree.MethodTree) TypeSymbol(com.sun.tools.javac.code.Symbol.TypeSymbol)

Example 18 with TypeSymbol

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

the class SelfEquals method fieldFix.

@Nullable
protected static Fix fieldFix(Tree toReplace, VisitorState state) {
    TreePath path = state.getPath();
    while (path != null && path.getLeaf().getKind() != Kind.CLASS && path.getLeaf().getKind() != Kind.BLOCK) {
        path = path.getParentPath();
    }
    if (path == null) {
        return null;
    }
    List<? extends JCTree> members;
    // Must be block or class
    if (path.getLeaf().getKind() == Kind.CLASS) {
        members = ((JCClassDecl) path.getLeaf()).getMembers();
    } else {
        members = ((JCBlock) path.getLeaf()).getStatements();
    }
    for (JCTree jcTree : members) {
        if (jcTree.getKind() == Kind.VARIABLE) {
            JCVariableDecl declaration = (JCVariableDecl) jcTree;
            TypeSymbol variableTypeSymbol = state.getTypes().erasure(ASTHelpers.getType(declaration)).tsym;
            if (ASTHelpers.getSymbol(toReplace).isMemberOf(variableTypeSymbol, state.getTypes())) {
                if (toReplace.getKind() == Kind.IDENTIFIER) {
                    return SuggestedFix.prefixWith(toReplace, declaration.getName() + ".");
                } else {
                    return SuggestedFix.replace(((JCFieldAccess) toReplace).getExpression(), declaration.getName().toString());
                }
            }
        }
    }
    return null;
}
Also used : TreePath(com.sun.source.util.TreePath) JCTree(com.sun.tools.javac.tree.JCTree) TypeSymbol(com.sun.tools.javac.code.Symbol.TypeSymbol) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl) Nullable(javax.annotation.Nullable)

Example 19 with TypeSymbol

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

the class UnnecessaryDefaultInEnumSwitch method matchSwitch.

@Override
public Description matchSwitch(SwitchTree tree, VisitorState state) {
    TypeSymbol switchType = ((JCSwitch) tree).getExpression().type.tsym;
    if (switchType.getKind() != ElementKind.ENUM) {
        return NO_MATCH;
    }
    Optional<? extends CaseTree> maybeDefaultCase = tree.getCases().stream().filter(c -> c.getExpression() == null).findFirst();
    if (!maybeDefaultCase.isPresent()) {
        return NO_MATCH;
    }
    CaseTree defaultCase = maybeDefaultCase.get();
    Set<String> handledCases = tree.getCases().stream().map(CaseTree::getExpression).filter(IdentifierTree.class::isInstance).map(p -> ((IdentifierTree) p).getName().toString()).collect(toImmutableSet());
    if (!ASTHelpers.enumValues(switchType).equals(handledCases)) {
        return NO_MATCH;
    }
    Fix fix;
    List<? extends StatementTree> defaultStatements = defaultCase.getStatements();
    if (trivialDefault(defaultStatements)) {
        // deleting `default:` or `default: break;` is a no-op
        fix = SuggestedFix.delete(defaultCase);
    } else if (!canCompleteNormally(tree)) {
        // if the switch statement cannot complete normally, then deleting the default
        // and moving its statements to after the switch statement is a no-op
        String defaultSource = state.getSourceCode().subSequence(((JCTree) defaultStatements.get(0)).getStartPosition(), state.getEndPosition(getLast(defaultStatements))).toString();
        fix = SuggestedFix.builder().delete(defaultCase).postfixWith(tree, defaultSource).build();
    } else {
        return NO_MATCH;
    }
    return describeMatch(defaultCase, fix);
}
Also used : SwitchTreeMatcher(com.google.errorprone.bugpatterns.BugChecker.SwitchTreeMatcher) VisitorState(com.google.errorprone.VisitorState) JCSwitch(com.sun.tools.javac.tree.JCTree.JCSwitch) IdentifierTree(com.sun.source.tree.IdentifierTree) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) BugPattern(com.google.errorprone.BugPattern) JDK(com.google.errorprone.BugPattern.Category.JDK) Fix(com.google.errorprone.fixes.Fix) Tree(com.sun.source.tree.Tree) TypeSymbol(com.sun.tools.javac.code.Symbol.TypeSymbol) ElementKind(javax.lang.model.element.ElementKind) Set(java.util.Set) Iterables.getLast(com.google.common.collect.Iterables.getLast) Iterables.getOnlyElement(com.google.common.collect.Iterables.getOnlyElement) NO_MATCH(com.google.errorprone.matchers.Description.NO_MATCH) CaseTree(com.sun.source.tree.CaseTree) JCTree(com.sun.tools.javac.tree.JCTree) SwitchTree(com.sun.source.tree.SwitchTree) List(java.util.List) Description(com.google.errorprone.matchers.Description) StatementTree(com.sun.source.tree.StatementTree) Optional(java.util.Optional) WARNING(com.google.errorprone.BugPattern.SeverityLevel.WARNING) SuggestedFix(com.google.errorprone.fixes.SuggestedFix) Reachability.canCompleteNormally(com.google.errorprone.util.Reachability.canCompleteNormally) ASTHelpers(com.google.errorprone.util.ASTHelpers) CaseTree(com.sun.source.tree.CaseTree) JCSwitch(com.sun.tools.javac.tree.JCTree.JCSwitch) Fix(com.google.errorprone.fixes.Fix) SuggestedFix(com.google.errorprone.fixes.SuggestedFix) IdentifierTree(com.sun.source.tree.IdentifierTree) TypeSymbol(com.sun.tools.javac.code.Symbol.TypeSymbol)

Example 20 with TypeSymbol

use of com.sun.tools.javac.code.Symbol.TypeSymbol in project lombok by rzwitserloot.

the class JavacResolution method typeToJCTree0.

private static JCExpression typeToJCTree0(Type type, JavacAST ast, boolean allowCompound, boolean allowVoid) throws TypeNotConvertibleException {
    // NB: There's such a thing as maker.Type(type), but this doesn't work very well; it screws up anonymous classes, captures, and adds an extra prefix dot for some reason too.
    //  -- so we write our own take on that here.
    JavacTreeMaker maker = ast.getTreeMaker();
    if (CTC_BOT.equals(typeTag(type)))
        return createJavaLangObject(ast);
    if (CTC_VOID.equals(typeTag(type)))
        return allowVoid ? primitiveToJCTree(type.getKind(), maker) : createJavaLangObject(ast);
    if (type.isPrimitive())
        return primitiveToJCTree(type.getKind(), maker);
    if (type.isErroneous())
        throw new TypeNotConvertibleException("Type cannot be resolved");
    TypeSymbol symbol = type.asElement();
    List<Type> generics = type.getTypeArguments();
    JCExpression replacement = null;
    if (symbol == null)
        throw new TypeNotConvertibleException("Null or compound type");
    if (symbol.name.length() == 0) {
        // Anonymous inner class
        if (type instanceof ClassType) {
            List<Type> ifaces = ((ClassType) type).interfaces_field;
            Type supertype = ((ClassType) type).supertype_field;
            if (ifaces != null && ifaces.length() == 1) {
                return typeToJCTree(ifaces.get(0), ast, allowCompound, allowVoid);
            }
            if (supertype != null)
                return typeToJCTree(supertype, ast, allowCompound, allowVoid);
        }
        throw new TypeNotConvertibleException("Anonymous inner class");
    }
    if (type instanceof CapturedType || type instanceof WildcardType) {
        Type lower, upper;
        if (type instanceof WildcardType) {
            upper = ((WildcardType) type).getExtendsBound();
            lower = ((WildcardType) type).getSuperBound();
        } else {
            lower = type.getLowerBound();
            upper = type.getUpperBound();
        }
        if (allowCompound) {
            if (lower == null || CTC_BOT.equals(typeTag(lower))) {
                if (upper == null || upper.toString().equals("java.lang.Object")) {
                    return maker.Wildcard(maker.TypeBoundKind(BoundKind.UNBOUND), null);
                }
                if (upper.getTypeArguments().contains(type)) {
                    return maker.Wildcard(maker.TypeBoundKind(BoundKind.UNBOUND), null);
                }
                return maker.Wildcard(maker.TypeBoundKind(BoundKind.EXTENDS), typeToJCTree(upper, ast, false, false));
            } else {
                return maker.Wildcard(maker.TypeBoundKind(BoundKind.SUPER), typeToJCTree(lower, ast, false, false));
            }
        }
        if (upper != null) {
            if (upper.getTypeArguments().contains(type)) {
                return maker.Wildcard(maker.TypeBoundKind(BoundKind.UNBOUND), null);
            }
            return typeToJCTree(upper, ast, allowCompound, allowVoid);
        }
        return createJavaLangObject(ast);
    }
    String qName;
    if (symbol.isLocal()) {
        qName = symbol.getSimpleName().toString();
    } else if (symbol.type != null && symbol.type.getEnclosingType() != null && typeTag(symbol.type.getEnclosingType()).equals(typeTag("CLASS"))) {
        replacement = typeToJCTree0(type.getEnclosingType(), ast, false, false);
        qName = symbol.getSimpleName().toString();
    } else {
        qName = symbol.getQualifiedName().toString();
    }
    if (qName.isEmpty())
        throw new TypeNotConvertibleException("unknown type");
    if (qName.startsWith("<"))
        throw new TypeNotConvertibleException(qName);
    String[] baseNames = qName.split("\\.");
    int i = 0;
    if (replacement == null) {
        replacement = maker.Ident(ast.toName(baseNames[0]));
        i = 1;
    }
    for (; i < baseNames.length; i++) {
        replacement = maker.Select(replacement, ast.toName(baseNames[i]));
    }
    return genericsToJCTreeNodes(generics, ast, replacement);
}
Also used : ClassType(com.sun.tools.javac.code.Type.ClassType) CapturedType(com.sun.tools.javac.code.Type.CapturedType) ArrayType(com.sun.tools.javac.code.Type.ArrayType) WildcardType(com.sun.tools.javac.code.Type.WildcardType) Type(com.sun.tools.javac.code.Type) WildcardType(com.sun.tools.javac.code.Type.WildcardType) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) CapturedType(com.sun.tools.javac.code.Type.CapturedType) TypeSymbol(com.sun.tools.javac.code.Symbol.TypeSymbol) ClassType(com.sun.tools.javac.code.Type.ClassType)

Aggregations

TypeSymbol (com.sun.tools.javac.code.Symbol.TypeSymbol)21 Symbol (com.sun.tools.javac.code.Symbol)11 MethodSymbol (com.sun.tools.javac.code.Symbol.MethodSymbol)11 Type (com.sun.tools.javac.code.Type)11 MethodTree (com.sun.source.tree.MethodTree)6 ClassSymbol (com.sun.tools.javac.code.Symbol.ClassSymbol)6 PackageSymbol (com.sun.tools.javac.code.Symbol.PackageSymbol)4 JCTree (com.sun.tools.javac.tree.JCTree)4 VisitorState (com.google.errorprone.VisitorState)3 ClassTree (com.sun.source.tree.ClassTree)3 Tree (com.sun.source.tree.Tree)3 CompletionFailure (com.sun.tools.javac.code.Symbol.CompletionFailure)3 ArrayList (java.util.ArrayList)3 UnknownType (com.redhat.ceylon.model.typechecker.model.UnknownType)2 IdentifierTree (com.sun.source.tree.IdentifierTree)2 MemberSelectTree (com.sun.source.tree.MemberSelectTree)2 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)2 TreePath (com.sun.source.util.TreePath)2 Scope (com.sun.tools.javac.code.Scope)2 ClassType (com.sun.tools.javac.code.Type.ClassType)2