Search in sources :

Example 1 with ASTHelpers.getSymbol

use of com.google.errorprone.util.ASTHelpers.getSymbol in project error-prone by google.

the class LambdaFunctionalInterface method matchMethod.

/**
 * Identifies methods with parameters that have a generic argument with Int, Long, or Double. If
 * pre-conditions are met, it refactors them to the primitive specializations.
 *
 * <pre>PreConditions:
 * (1): The method declaration has to be private (to do a safe refactoring)
 * (2): Its parameters have to meet the following conditions:
 *    2.1 Contain type java.util.function.Function
 *    2.2 At least one argument type of the Function must be subtype of Number
 * (3): All its invocations in the top-level enclosing class have to meet the following
 * conditions as well:
 *    3.1: lambda argument of Kind.LAMBDA_EXPRESSION
 *    3.2: same as 2.1
 *    3.3: same as 2.2
 * </pre>
 *
 * <pre>
 * Refactoring Changes for matched methods:
 * (1) Add the imports
 * (2) Change the method signature to use utility function instead of Function
 * (3) Find and change the 'apply' calls to the corresponding applyAsT
 * </pre>
 */
@Override
public Description matchMethod(MethodTree tree, VisitorState state) {
    MethodSymbol methodSym = ASTHelpers.getSymbol(tree);
    // precondition (1)
    if (!methodSym.getModifiers().contains(Modifier.PRIVATE)) {
        return Description.NO_MATCH;
    }
    ImmutableList<Tree> params = tree.getParameters().stream().filter(param -> hasFunctionAsArg(param, state)).filter(param -> isFunctionArgSubtypeOf(param, 0, state.getTypeFromString(JAVA_LANG_NUMBER), state) || isFunctionArgSubtypeOf(param, 1, state.getTypeFromString(JAVA_LANG_NUMBER), state)).collect(toImmutableList());
    // preconditions (2) and (3)
    if (params.isEmpty() || !methodCallsMeetConditions(methodSym, state)) {
        return Description.NO_MATCH;
    }
    SuggestedFix.Builder fixBuilder = SuggestedFix.builder();
    for (Tree param : params) {
        getMappingForFunctionFromTree(param).ifPresent(mappedFunction -> {
            fixBuilder.addImport(getImportName(mappedFunction));
            fixBuilder.replace(param, getFunctionName(mappedFunction) + " " + ASTHelpers.getSymbol(param).name);
            refactorInternalApplyMethods(tree, fixBuilder, param, mappedFunction);
        });
    }
    return describeMatch(tree, fixBuilder.build());
}
Also used : Optional.empty(java.util.Optional.empty) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) MethodTreeMatcher(com.google.errorprone.bugpatterns.BugChecker.MethodTreeMatcher) MethodTree(com.sun.source.tree.MethodTree) Modifier(javax.lang.model.element.Modifier) VisitorState(com.google.errorprone.VisitorState) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) Kind(com.sun.source.tree.Tree.Kind) ImmutableList(com.google.common.collect.ImmutableList) BugPattern(com.google.errorprone.BugPattern) JDK(com.google.errorprone.BugPattern.Category.JDK) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) Tree(com.sun.source.tree.Tree) ClassTree(com.sun.source.tree.ClassTree) ImmutableMap(com.google.common.collect.ImmutableMap) ASTHelpers.getReceiver(com.google.errorprone.util.ASTHelpers.getReceiver) Optional.ofNullable(java.util.Optional.ofNullable) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ASTHelpers.getSymbol(com.google.errorprone.util.ASTHelpers.getSymbol) Symbol(com.sun.tools.javac.code.Symbol) Streams(com.google.common.collect.Streams) TreeScanner(com.sun.source.util.TreeScanner) Description(com.google.errorprone.matchers.Description) Optional(java.util.Optional) SUGGESTION(com.google.errorprone.BugPattern.SeverityLevel.SUGGESTION) ProvidesFix(com.google.errorprone.BugPattern.ProvidesFix) SuggestedFix(com.google.errorprone.fixes.SuggestedFix) ASTHelpers(com.google.errorprone.util.ASTHelpers) Type(com.sun.tools.javac.code.Type) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) SuggestedFix(com.google.errorprone.fixes.SuggestedFix) MethodTree(com.sun.source.tree.MethodTree) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) Tree(com.sun.source.tree.Tree) ClassTree(com.sun.source.tree.ClassTree)

Aggregations

ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableMultimap (com.google.common.collect.ImmutableMultimap)1 Streams (com.google.common.collect.Streams)1 BugPattern (com.google.errorprone.BugPattern)1 JDK (com.google.errorprone.BugPattern.Category.JDK)1 ProvidesFix (com.google.errorprone.BugPattern.ProvidesFix)1 SUGGESTION (com.google.errorprone.BugPattern.SeverityLevel.SUGGESTION)1 VisitorState (com.google.errorprone.VisitorState)1 MethodTreeMatcher (com.google.errorprone.bugpatterns.BugChecker.MethodTreeMatcher)1 SuggestedFix (com.google.errorprone.fixes.SuggestedFix)1 Description (com.google.errorprone.matchers.Description)1 ASTHelpers (com.google.errorprone.util.ASTHelpers)1 ASTHelpers.getReceiver (com.google.errorprone.util.ASTHelpers.getReceiver)1 ASTHelpers.getSymbol (com.google.errorprone.util.ASTHelpers.getSymbol)1 ClassTree (com.sun.source.tree.ClassTree)1 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)1 MethodTree (com.sun.source.tree.MethodTree)1 Tree (com.sun.source.tree.Tree)1