Search in sources :

Example 1 with JCMethodInvocation

use of com.sun.tools.javac.tree.JCTree.JCMethodInvocation in project lombok by rzwitserloot.

the class HandleCleanup method preventNullAnalysis.

public JCExpression preventNullAnalysis(JavacTreeMaker maker, JavacNode node, JCExpression expression) {
    if (LombokOptionsFactory.getDelombokOptions(node.getContext()).getFormatPreferences().danceAroundIdeChecks()) {
        JCMethodInvocation singletonList = maker.Apply(List.<JCExpression>nil(), chainDotsString(node, "java.util.Collections.singletonList"), List.of(expression));
        JCMethodInvocation cleanedExpr = maker.Apply(List.<JCExpression>nil(), maker.Select(singletonList, node.toName("get")), List.<JCExpression>of(maker.Literal(CTC_INT, 0)));
        return cleanedExpr;
    } else {
        return expression;
    }
}
Also used : JCMethodInvocation(com.sun.tools.javac.tree.JCTree.JCMethodInvocation)

Example 2 with JCMethodInvocation

use of com.sun.tools.javac.tree.JCTree.JCMethodInvocation in project lombok by rzwitserloot.

the class HandleHelper method handle.

@Override
public void handle(AnnotationValues<Helper> annotation, JCAnnotation ast, JavacNode annotationNode) {
    handleExperimentalFlagUsage(annotationNode, ConfigurationKeys.HELPER_FLAG_USAGE, "@Helper");
    deleteAnnotationIfNeccessary(annotationNode, Helper.class);
    JavacNode annotatedType = annotationNode.up();
    JavacNode containingBlock = annotatedType == null ? null : annotatedType.directUp();
    List<JCStatement> origStatements = getStatementsFromJcNode(containingBlock == null ? null : containingBlock.get());
    if (annotatedType == null || annotatedType.getKind() != Kind.TYPE || origStatements == null) {
        annotationNode.addError("@Helper is legal only on method-local classes.");
        return;
    }
    JCClassDecl annotatedType_ = (JCClassDecl) annotatedType.get();
    Iterator<JCStatement> it = origStatements.iterator();
    while (it.hasNext()) {
        if (it.next() == annotatedType_) {
            break;
        }
    }
    java.util.List<String> knownMethodNames = new ArrayList<String>();
    for (JavacNode ch : annotatedType.down()) {
        if (ch.getKind() != Kind.METHOD)
            continue;
        String n = ch.getName();
        if (n == null || n.isEmpty() || n.charAt(0) == '<')
            continue;
        knownMethodNames.add(n);
    }
    Collections.sort(knownMethodNames);
    final String[] knownMethodNames_ = knownMethodNames.toArray(new String[knownMethodNames.size()]);
    final Name helperName = annotationNode.toName("$" + annotatedType_.name);
    final boolean[] helperUsed = new boolean[1];
    final JavacTreeMaker maker = annotationNode.getTreeMaker();
    TreeVisitor<Void, Void> visitor = new TreeScanner<Void, Void>() {

        @Override
        public Void visitMethodInvocation(MethodInvocationTree node, Void p) {
            JCMethodInvocation jcmi = (JCMethodInvocation) node;
            apply(jcmi);
            return super.visitMethodInvocation(node, p);
        }

        private void apply(JCMethodInvocation jcmi) {
            if (!(jcmi.meth instanceof JCIdent))
                return;
            JCIdent jci = (JCIdent) jcmi.meth;
            if (Arrays.binarySearch(knownMethodNames_, jci.name.toString()) < 0)
                return;
            jcmi.meth = maker.Select(maker.Ident(helperName), jci.name);
            helperUsed[0] = true;
        }
    };
    while (it.hasNext()) {
        JCStatement stat = it.next();
        stat.accept(visitor, null);
    }
    if (!helperUsed[0]) {
        annotationNode.addWarning("No methods of this helper class are ever used.");
        return;
    }
    ListBuffer<JCStatement> newStatements = new ListBuffer<JCStatement>();
    boolean mark = false;
    for (JCStatement stat : origStatements) {
        newStatements.append(stat);
        if (mark || stat != annotatedType_)
            continue;
        mark = true;
        JCExpression init = maker.NewClass(null, List.<JCExpression>nil(), maker.Ident(annotatedType_.name), List.<JCExpression>nil(), null);
        JCExpression varType = maker.Ident(annotatedType_.name);
        JCVariableDecl decl = maker.VarDef(maker.Modifiers(Flags.FINAL), helperName, varType, init);
        newStatements.append(decl);
    }
    setStatementsOfJcNode(containingBlock.get(), newStatements.toList());
}
Also used : JCClassDecl(com.sun.tools.javac.tree.JCTree.JCClassDecl) JCIdent(com.sun.tools.javac.tree.JCTree.JCIdent) JavacTreeMaker(lombok.javac.JavacTreeMaker) ListBuffer(com.sun.tools.javac.util.ListBuffer) ArrayList(java.util.ArrayList) JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl) Name(com.sun.tools.javac.util.Name) JCMethodInvocation(com.sun.tools.javac.tree.JCTree.JCMethodInvocation) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JavacNode(lombok.javac.JavacNode) TreeScanner(com.sun.source.util.TreeScanner) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree)

Example 3 with JCMethodInvocation

use of com.sun.tools.javac.tree.JCTree.JCMethodInvocation in project lombok by rzwitserloot.

the class HandleLog method createField.

private static boolean createField(LoggingFramework framework, JavacNode typeNode, JCFieldAccess loggingType, JCTree source, String logFieldName, boolean useStatic, String loggerTopic) {
    JavacTreeMaker maker = typeNode.getTreeMaker();
    // private static final <loggerType> log = <factoryMethod>(<parameter>);
    JCExpression loggerType = chainDotsString(typeNode, framework.getLoggerTypeName());
    JCExpression factoryMethod = chainDotsString(typeNode, framework.getLoggerFactoryMethodName());
    JCExpression loggerName;
    if (loggerTopic == null || loggerTopic.trim().length() == 0) {
        loggerName = framework.createFactoryParameter(typeNode, loggingType);
    } else {
        loggerName = maker.Literal(loggerTopic);
    }
    JCMethodInvocation factoryMethodCall = maker.Apply(List.<JCExpression>nil(), factoryMethod, List.<JCExpression>of(loggerName));
    JCVariableDecl fieldDecl = recursiveSetGeneratedBy(maker.VarDef(maker.Modifiers(Flags.PRIVATE | Flags.FINAL | (useStatic ? Flags.STATIC : 0)), typeNode.toName(logFieldName), loggerType, factoryMethodCall), source, typeNode.getContext());
    injectFieldAndMarkGenerated(typeNode, fieldDecl);
    return true;
}
Also used : JCMethodInvocation(com.sun.tools.javac.tree.JCTree.JCMethodInvocation) JavacTreeMaker(lombok.javac.JavacTreeMaker) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl)

Example 4 with JCMethodInvocation

use of com.sun.tools.javac.tree.JCTree.JCMethodInvocation in project error-prone by google.

the class ASTHelpers method getRootAssignable.

/**
 * Find the root assignable expression of a chain of field accesses. If there is no root (i.e, a
 * bare method call or a static method call), return null.
 *
 * <p>Examples:
 *
 * <pre>{@code
 * a.trim().intern() ==> a
 * a.b.trim().intern() ==> a.b
 * this.intValue.foo() ==> this.intValue
 * this.foo() ==> this
 * intern() ==> null
 * String.format() ==> null
 * java.lang.String.format() ==> null
 * }</pre>
 */
public static ExpressionTree getRootAssignable(MethodInvocationTree methodInvocationTree) {
    if (!(methodInvocationTree instanceof JCMethodInvocation)) {
        throw new IllegalArgumentException("Expected type to be JCMethodInvocation, but was " + methodInvocationTree.getClass());
    }
    // Check for bare method call, e.g. intern().
    if (((JCMethodInvocation) methodInvocationTree).getMethodSelect() instanceof JCIdent) {
        return null;
    }
    // Unwrap the field accesses until you get to an identifier.
    ExpressionTree expr = methodInvocationTree;
    while (expr instanceof JCMethodInvocation) {
        expr = ((JCMethodInvocation) expr).getMethodSelect();
        if (expr instanceof JCFieldAccess) {
            expr = ((JCFieldAccess) expr).getExpression();
        }
    }
    // We only want assignable identifiers.
    Symbol sym = getSymbol(expr);
    if (sym instanceof VarSymbol) {
        return expr;
    }
    return null;
}
Also used : JCMethodInvocation(com.sun.tools.javac.tree.JCTree.JCMethodInvocation) JCIdent(com.sun.tools.javac.tree.JCTree.JCIdent) JCFieldAccess(com.sun.tools.javac.tree.JCTree.JCFieldAccess) 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) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) LambdaExpressionTree(com.sun.source.tree.LambdaExpressionTree) ConditionalExpressionTree(com.sun.source.tree.ConditionalExpressionTree) ExpressionTree(com.sun.source.tree.ExpressionTree) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol)

Example 5 with JCMethodInvocation

use of com.sun.tools.javac.tree.JCTree.JCMethodInvocation in project error-prone by google.

the class AbstractReturnValueIgnored method mockitoInvocation.

/**
 * Don't match the method that is invoked through {@code Mockito.verify(t)} or {@code
 * doReturn(val).when(t)}.
 */
private static boolean mockitoInvocation(Tree tree, VisitorState state) {
    if (!(tree instanceof JCMethodInvocation)) {
        return false;
    }
    JCMethodInvocation invocation = (JCMethodInvocation) tree;
    if (!(invocation.getMethodSelect() instanceof JCFieldAccess)) {
        return false;
    }
    ExpressionTree receiver = ASTHelpers.getReceiver(invocation);
    return MOCKITO_MATCHER.matches(receiver, state);
}
Also used : JCMethodInvocation(com.sun.tools.javac.tree.JCTree.JCMethodInvocation) JCFieldAccess(com.sun.tools.javac.tree.JCTree.JCFieldAccess) LambdaExpressionTree(com.sun.source.tree.LambdaExpressionTree) ExpressionTree(com.sun.source.tree.ExpressionTree)

Aggregations

JCMethodInvocation (com.sun.tools.javac.tree.JCTree.JCMethodInvocation)26 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)15 JCTree (com.sun.tools.javac.tree.JCTree)10 JCVariableDecl (com.sun.tools.javac.tree.JCTree.JCVariableDecl)8 JCExpressionStatement (com.sun.tools.javac.tree.JCTree.JCExpressionStatement)7 JCNewClass (com.sun.tools.javac.tree.JCTree.JCNewClass)6 JCStatement (com.sun.tools.javac.tree.JCTree.JCStatement)6 JCFieldAccess (com.sun.tools.javac.tree.JCTree.JCFieldAccess)5 JCMethodDecl (com.sun.tools.javac.tree.JCTree.JCMethodDecl)5 ArrayList (java.util.ArrayList)5 JCAnnotation (com.sun.tools.javac.tree.JCTree.JCAnnotation)4 JCBlock (com.sun.tools.javac.tree.JCTree.JCBlock)4 JCIdent (com.sun.tools.javac.tree.JCTree.JCIdent)4 Name (com.sun.tools.javac.util.Name)4 JavacTreeMaker (lombok.javac.JavacTreeMaker)4 ExpressionTree (com.sun.source.tree.ExpressionTree)3 Symbol (com.sun.tools.javac.code.Symbol)3 ClassSymbol (com.sun.tools.javac.code.Symbol.ClassSymbol)3 MethodSymbol (com.sun.tools.javac.code.Symbol.MethodSymbol)3 Type (com.sun.tools.javac.code.Type)3