Search in sources :

Example 31 with Symbol

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

the class SuggestedFixes method qualifyType.

/**
   * Returns a human-friendly name of the given {@link Symbol.TypeSymbol} for use in fixes.
   *
   * <ul>
   * <li>If the type is already imported, its simple name is used.
   * <li>If an enclosing type is imported, that enclosing type is used as a qualified.
   * <li>Otherwise the outermost enclosing type is imported and used as a qualifier.
   * </ul>
   */
public static String qualifyType(TreeMaker make, SuggestedFix.Builder fix, Symbol.TypeSymbol sym) {
    // let javac figure out whether the type is already imported
    JCTree.JCExpression qual = make.QualIdent(sym);
    if (!selectsPackage(qual)) {
        return qual.toString();
    }
    Deque<String> names = new ArrayDeque<>();
    Symbol curr = sym;
    while (true) {
        names.addFirst(curr.getSimpleName().toString());
        if (curr.owner == null || curr.owner.getKind() == ElementKind.PACKAGE) {
            break;
        }
        curr = curr.owner;
    }
    fix.addImport(curr.toString());
    return Joiner.on('.').join(names);
}
Also used : ASTHelpers.getSymbol(com.google.errorprone.util.ASTHelpers.getSymbol) Symbol(com.sun.tools.javac.code.Symbol) JCTree(com.sun.tools.javac.tree.JCTree) ArrayDeque(java.util.ArrayDeque)

Example 32 with Symbol

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

the class SuggestedFixes method qualifyDocReference.

/**
   * Fully qualifies a javadoc reference, e.g. for replacing {@code {@link List}} with
   * {@code {@link java.util.List}}
   *
   * @param fix the fix builder to add to
   * @param docPath the path to a {@link DCTree.DCReference} element
   */
public static void qualifyDocReference(SuggestedFix.Builder fix, DocTreePath docPath, VisitorState state) {
    DocTree leaf = docPath.getLeaf();
    checkArgument(leaf.getKind() == DocTree.Kind.REFERENCE, "expected a path to a reference, got %s instead", leaf.getKind());
    DCTree.DCReference reference = (DCTree.DCReference) leaf;
    Symbol sym = (Symbol) JavacTrees.instance(state.context).getElement(docPath);
    if (sym == null) {
        return;
    }
    String refString = reference.toString();
    String qualifiedName;
    int idx = refString.indexOf('#');
    if (idx >= 0) {
        qualifiedName = sym.owner.getQualifiedName() + refString.substring(idx, refString.length());
    } else {
        qualifiedName = sym.getQualifiedName().toString();
    }
    replaceDocTree(fix, docPath, qualifiedName);
}
Also used : ASTHelpers.getSymbol(com.google.errorprone.util.ASTHelpers.getSymbol) Symbol(com.sun.tools.javac.code.Symbol) DocTree(com.sun.source.doctree.DocTree) DCTree(com.sun.tools.javac.tree.DCTree)

Example 33 with Symbol

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

the class ConstructorMatcherImpl method getConstructor.

private static MethodSymbol getConstructor(ExpressionTree tree) {
    switch(tree.getKind()) {
        case NEW_CLASS:
        case METHOD_INVOCATION:
            break;
        default:
            return null;
    }
    Symbol sym = ASTHelpers.getSymbol(tree);
    if (!(sym instanceof MethodSymbol)) {
        return null;
    }
    MethodSymbol method = (MethodSymbol) sym;
    if (!method.isConstructor()) {
        return null;
    }
    return method;
}
Also used : MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) Symbol(com.sun.tools.javac.code.Symbol)

Example 34 with Symbol

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

the class Scanner method updateSuppressions.

/**
   * Updates current suppression state with information for the given {@code tree}.  Returns
   * the previous suppression state so that it can be restored when going up the tree.
   */
private SuppressionHelper.SuppressionInfo updateSuppressions(Tree tree, VisitorState state) {
    SuppressionHelper.SuppressionInfo prevSuppressionInfo = new SuppressionHelper.SuppressionInfo(suppressions, customSuppressions, inGeneratedCode);
    initSuppressionHelper(state);
    Symbol sym = ASTHelpers.getSymbol(tree);
    if (sym != null) {
        SuppressionHelper.SuppressionInfo newSuppressions = suppressionHelper.extendSuppressionSets(sym, state.getSymtab().suppressWarningsType, suppressions, customSuppressions, inGeneratedCode, state);
        if (newSuppressions.suppressWarningsStrings != null) {
            suppressions = newSuppressions.suppressWarningsStrings;
        }
        if (newSuppressions.customSuppressions != null) {
            customSuppressions = newSuppressions.customSuppressions;
        }
        inGeneratedCode = newSuppressions.inGeneratedCode;
    }
    return prevSuppressionInfo;
}
Also used : Symbol(com.sun.tools.javac.code.Symbol) SuppressionHelper(com.google.errorprone.SuppressionHelper)

Example 35 with Symbol

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

the class ASTHelpers method findMatchingMethods.

/**
   * Finds all methods in any superclass of {@code startClass} with a certain {@code name} that
   * match the given {@code predicate}.
   *
   * @return The (possibly empty) set of methods in any superclass that match {@code predicate} and
   * have the given {@code name}.
   */
public static Set<MethodSymbol> findMatchingMethods(Name name, final Predicate<MethodSymbol> predicate, Type startClass, Types types) {
    Filter<Symbol> matchesMethodPredicate = sym -> sym instanceof MethodSymbol && predicate.apply((MethodSymbol) sym);
    Set<MethodSymbol> matchingMethods = new HashSet<>();
    // Iterate over all classes and interfaces that startClass inherits from.
    for (Type superClass : types.closure(startClass)) {
        // Iterate over all the methods declared in superClass.
        TypeSymbol superClassSymbol = superClass.tsym;
        Scope superClassSymbols = superClassSymbol.members();
        if (superClassSymbols != null) {
            // Can be null if superClass is a type variable
            for (Symbol symbol : superClassSymbols.getSymbolsByName(name, matchesMethodPredicate, NON_RECURSIVE)) {
                // By definition of the filter, we know that the symbol is a MethodSymbol.
                matchingMethods.add((MethodSymbol) symbol);
            }
        }
    }
    return matchingMethods;
}
Also used : Arrays(java.util.Arrays) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) ClassType(com.sun.tools.javac.code.Type.ClassType) Nullness(com.google.errorprone.dataflow.nullnesspropagation.Nullness) JCFieldAccess(com.sun.tools.javac.tree.JCTree.JCFieldAccess) PackageTree(com.sun.source.tree.PackageTree) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) TypeVar(com.sun.tools.javac.code.Type.TypeVar) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) TypeTag(com.sun.tools.javac.code.TypeTag) IdentifierTree(com.sun.source.tree.IdentifierTree) Log(com.sun.tools.javac.util.Log) Matcher(com.google.errorprone.matchers.Matcher) URI(java.net.URI) JarURLConnection(java.net.JarURLConnection) TypeSymbol(com.sun.tools.javac.code.Symbol.TypeSymbol) Filter(com.sun.tools.javac.util.Filter) TreePath(com.sun.source.util.TreePath) JCLiteral(com.sun.tools.javac.tree.JCTree.JCLiteral) DeferredDiagnosticHandler(com.sun.tools.javac.util.Log.DeferredDiagnosticHandler) Symbol(com.sun.tools.javac.code.Symbol) Set(java.util.Set) MemberSelectTree(com.sun.source.tree.MemberSelectTree) Collectors(java.util.stream.Collectors) TypeKind(javax.lang.model.type.TypeKind) List(java.util.List) CompletionFailure(com.sun.tools.javac.code.Symbol.CompletionFailure) JCMethodDecl(com.sun.tools.javac.tree.JCTree.JCMethodDecl) Predicate(com.google.common.base.Predicate) PackageSymbol(com.sun.tools.javac.code.Symbol.PackageSymbol) Suppliers(com.google.errorprone.suppliers.Suppliers) JCIdent(com.sun.tools.javac.tree.JCTree.JCIdent) Annotation(java.lang.annotation.Annotation) ModifiersTree(com.sun.source.tree.ModifiersTree) MoreObjects.firstNonNull(com.google.common.base.MoreObjects.firstNonNull) JCTypeParameter(com.sun.tools.javac.tree.JCTree.JCTypeParameter) Flags(com.sun.tools.javac.code.Flags) JCClassDecl(com.sun.tools.javac.tree.JCTree.JCClassDecl) Name(com.sun.tools.javac.util.Name) Scope(com.sun.tools.javac.code.Scope) Type(com.sun.tools.javac.code.Type) AnnotationTree(com.sun.source.tree.AnnotationTree) MethodTree(com.sun.source.tree.MethodTree) BinaryTree(com.sun.source.tree.BinaryTree) VariableTree(com.sun.source.tree.VariableTree) Deque(java.util.Deque) TypeParameterTree(com.sun.source.tree.TypeParameterTree) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) VisitorState(com.google.errorprone.VisitorState) JCPackageDecl(com.sun.tools.javac.tree.JCTree.JCPackageDecl) Kind(com.sun.source.tree.Tree.Kind) NewClassTree(com.sun.source.tree.NewClassTree) ParameterizedTypeTree(com.sun.source.tree.ParameterizedTypeTree) TreeInfo(com.sun.tools.javac.tree.TreeInfo) JCNewClass(com.sun.tools.javac.tree.JCTree.JCNewClass) Tree(com.sun.source.tree.Tree) ClassTree(com.sun.source.tree.ClassTree) LinkedHashSet(java.util.LinkedHashSet) Nullable(javax.annotation.Nullable) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) Enter(com.sun.tools.javac.comp.Enter) ElementKind(javax.lang.model.element.ElementKind) ExpressionTree(com.sun.source.tree.ExpressionTree) CharMatcher(com.google.common.base.CharMatcher) IOException(java.io.IOException) JCTree(com.sun.tools.javac.tree.JCTree) AnnotationMirror(javax.lang.model.element.AnnotationMirror) NullnessAnalysis(com.google.errorprone.dataflow.nullnesspropagation.NullnessAnalysis) Types(com.sun.tools.javac.code.Types) JUnitMatchers(com.google.errorprone.matchers.JUnitMatchers) JCMethodInvocation(com.sun.tools.javac.tree.JCTree.JCMethodInvocation) ArrayDeque(java.util.ArrayDeque) NON_RECURSIVE(com.sun.tools.javac.code.Scope.LookupKind.NON_RECURSIVE) Resolve(com.sun.tools.javac.comp.Resolve) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl) ClassType(com.sun.tools.javac.code.Type.ClassType) Type(com.sun.tools.javac.code.Type) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) 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) TypeSymbol(com.sun.tools.javac.code.Symbol.TypeSymbol) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Aggregations

Symbol (com.sun.tools.javac.code.Symbol)195 MethodSymbol (com.sun.tools.javac.code.Symbol.MethodSymbol)56 Type (com.sun.tools.javac.code.Type)54 ClassSymbol (com.sun.tools.javac.code.Symbol.ClassSymbol)53 VarSymbol (com.sun.tools.javac.code.Symbol.VarSymbol)45 TypeSymbol (com.sun.tools.javac.code.Symbol.TypeSymbol)36 PackageSymbol (com.sun.tools.javac.code.Symbol.PackageSymbol)29 JCTree (com.sun.tools.javac.tree.JCTree)28 ClassType (com.sun.tools.javac.code.Type.ClassType)18 Tree (com.sun.source.tree.Tree)17 ExpressionTree (com.sun.source.tree.ExpressionTree)15 DynamicMethodSymbol (com.sun.tools.javac.code.Symbol.DynamicMethodSymbol)15 OperatorSymbol (com.sun.tools.javac.code.Symbol.OperatorSymbol)15 ClassTree (com.sun.source.tree.ClassTree)14 MethodTree (com.sun.source.tree.MethodTree)14 Name (com.sun.tools.javac.util.Name)14 IdentifierTree (com.sun.source.tree.IdentifierTree)13 ArrayType (com.sun.tools.javac.code.Type.ArrayType)12 MethodType (com.sun.tools.javac.code.Type.MethodType)12 UnionClassType (com.sun.tools.javac.code.Type.UnionClassType)12