Search in sources :

Example 1 with JCEnhancedForLoop

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

the class ElementsCountedInLoop method matchEnhancedForLoop.

@Override
public Description matchEnhancedForLoop(EnhancedForLoopTree tree, VisitorState state) {
    JCEnhancedForLoop enhancedForLoop = (JCEnhancedForLoop) tree;
    IdentifierTree identifier = getIncrementedIdentifer(extractSingleStatement(enhancedForLoop.body));
    if (identifier != null) {
        ExpressionTree expression = tree.getExpression();
        Fix fix;
        if (isSubtypeOf("java.util.Collection").matches(expression, state)) {
            String replacement = identifier + " += " + expression + ".size();";
            fix = SuggestedFix.replace(tree, replacement);
        } else if (isArrayType().matches(expression, state)) {
            String replacement = identifier + " += " + expression + ".length;";
            fix = SuggestedFix.replace(tree, replacement);
        } else {
            String replacement = identifier + " += Iterables.size(" + expression + ");";
            fix = SuggestedFix.builder().replace(tree, replacement).addImport("com.google.common.collect.Iterables").build();
        }
        return describeMatch(tree, fix);
    }
    return Description.NO_MATCH;
}
Also used : Fix(com.google.errorprone.fixes.Fix) SuggestedFix(com.google.errorprone.fixes.SuggestedFix) JCEnhancedForLoop(com.sun.tools.javac.tree.JCTree.JCEnhancedForLoop) IdentifierTree(com.sun.source.tree.IdentifierTree) ExpressionTree(com.sun.source.tree.ExpressionTree)

Example 2 with JCEnhancedForLoop

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

the class HandleVal method visitLocal.

@SuppressWarnings("deprecation")
@Override
public void visitLocal(JavacNode localNode, JCVariableDecl local) {
    JCTree typeTree = local.vartype;
    if (typeTree == null)
        return;
    String typeTreeToString = typeTree.toString();
    if (!(eq(typeTreeToString, "val") || eq(typeTreeToString, "var")))
        return;
    boolean isVal = typeMatches(val.class, localNode, typeTree);
    boolean isVar = typeMatches(var.class, localNode, typeTree);
    if (!(isVal || isVar))
        return;
    if (isVal)
        handleFlagUsage(localNode, ConfigurationKeys.VAL_FLAG_USAGE, "val");
    if (isVar)
        handleFlagUsage(localNode, ConfigurationKeys.VAR_FLAG_USAGE, "var");
    JCTree parentRaw = localNode.directUp().get();
    if (isVal && parentRaw instanceof JCForLoop) {
        localNode.addError("'val' is not allowed in old-style for loops");
        return;
    }
    if (parentRaw instanceof JCForLoop && ((JCForLoop) parentRaw).getInitializer().size() > 1) {
        localNode.addError("'var' is not allowed in old-style for loops if there is more than 1 initializer");
        return;
    }
    JCExpression rhsOfEnhancedForLoop = null;
    if (local.init == null) {
        if (parentRaw instanceof JCEnhancedForLoop) {
            JCEnhancedForLoop efl = (JCEnhancedForLoop) parentRaw;
            if (efl.var == local)
                rhsOfEnhancedForLoop = efl.expr;
        }
    }
    final String annotation = typeTreeToString;
    if (rhsOfEnhancedForLoop == null && local.init == null) {
        localNode.addError("'" + annotation + "' on a local variable requires an initializer expression");
        return;
    }
    if (local.init instanceof JCNewArray && ((JCNewArray) local.init).elemtype == null) {
        localNode.addError("'" + annotation + "' is not compatible with array initializer expressions. Use the full form (new int[] { ... } instead of just { ... })");
        return;
    }
    if (localNode.shouldDeleteLombokAnnotations()) {
        JavacHandlerUtil.deleteImportFromCompilationUnit(localNode, val.class.getName());
        JavacHandlerUtil.deleteImportFromCompilationUnit(localNode, lombok.experimental.var.class.getName());
        JavacHandlerUtil.deleteImportFromCompilationUnit(localNode, var.class.getName());
    }
    if (isVal)
        local.mods.flags |= Flags.FINAL;
    if (!localNode.shouldDeleteLombokAnnotations()) {
        JCAnnotation valAnnotation = recursiveSetGeneratedBy(localNode.getTreeMaker().Annotation(local.vartype, List.<JCExpression>nil()), typeTree, localNode.getContext());
        local.mods.annotations = local.mods.annotations == null ? List.of(valAnnotation) : local.mods.annotations.append(valAnnotation);
    }
    if (JavacResolution.platformHasTargetTyping()) {
        local.vartype = localNode.getAst().getTreeMaker().Ident(localNode.getAst().toName("___Lombok_VAL_Attrib__"));
    } else {
        local.vartype = JavacResolution.createJavaLangObject(localNode.getAst());
    }
    Type type;
    try {
        if (rhsOfEnhancedForLoop == null) {
            if (local.init.type == null) {
                if (isVar && local.init instanceof JCLiteral && ((JCLiteral) local.init).value == null) {
                    localNode.addError("variable initializer is 'null'");
                }
                JavacResolution resolver = new JavacResolution(localNode.getContext());
                try {
                    type = ((JCExpression) resolver.resolveMethodMember(localNode).get(local.init)).type;
                } catch (RuntimeException e) {
                    System.err.println("Exception while resolving: " + localNode + "(" + localNode.getFileName() + ")");
                    throw e;
                }
            } else {
                type = local.init.type;
                if (type.isErroneous()) {
                    try {
                        JavacResolution resolver = new JavacResolution(localNode.getContext());
                        local.type = Symtab.instance(localNode.getContext()).unknownType;
                        type = ((JCExpression) resolver.resolveMethodMember(localNode).get(local.init)).type;
                    } catch (RuntimeException e) {
                        System.err.println("Exception while resolving: " + localNode + "(" + localNode.getFileName() + ")");
                        throw e;
                    }
                }
            }
        } else {
            if (rhsOfEnhancedForLoop.type == null) {
                JavacResolution resolver = new JavacResolution(localNode.getContext());
                type = ((JCExpression) resolver.resolveMethodMember(localNode.directUp()).get(rhsOfEnhancedForLoop)).type;
            } else {
                type = rhsOfEnhancedForLoop.type;
            }
        }
        try {
            JCExpression replacement;
            if (rhsOfEnhancedForLoop != null) {
                Type componentType = JavacResolution.ifTypeIsIterableToComponent(type, localNode.getAst());
                if (componentType == null)
                    replacement = JavacResolution.createJavaLangObject(localNode.getAst());
                else
                    replacement = JavacResolution.typeToJCTree(componentType, localNode.getAst(), false);
            } else {
                replacement = JavacResolution.typeToJCTree(type, localNode.getAst(), false);
            }
            if (replacement != null) {
                local.vartype = replacement;
            } else {
                local.vartype = JavacResolution.createJavaLangObject(localNode.getAst());
            }
            localNode.getAst().setChanged();
        } catch (JavacResolution.TypeNotConvertibleException e) {
            localNode.addError("Cannot use '" + annotation + "' here because initializer expression does not have a representable type: " + e.getMessage());
            local.vartype = JavacResolution.createJavaLangObject(localNode.getAst());
        }
    } catch (RuntimeException e) {
        local.vartype = JavacResolution.createJavaLangObject(localNode.getAst());
        throw e;
    } finally {
        recursiveSetGeneratedBy(local.vartype, typeTree, localNode.getContext());
    }
}
Also used : lombok.val(lombok.val) JavacResolution(lombok.javac.JavacResolution) lombok.var(lombok.var) JCTree(com.sun.tools.javac.tree.JCTree) JCForLoop(com.sun.tools.javac.tree.JCTree.JCForLoop) Type(com.sun.tools.javac.code.Type) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JCEnhancedForLoop(com.sun.tools.javac.tree.JCTree.JCEnhancedForLoop) JCLiteral(com.sun.tools.javac.tree.JCTree.JCLiteral) JCNewArray(com.sun.tools.javac.tree.JCTree.JCNewArray) JCAnnotation(com.sun.tools.javac.tree.JCTree.JCAnnotation)

Example 3 with JCEnhancedForLoop

use of com.sun.tools.javac.tree.JCTree.JCEnhancedForLoop in project ceylon-compiler by ceylon.

the class JavaPositionsRetriever method getJavaSourceCodeWithCeylonPositions.

public String getJavaSourceCodeWithCeylonPositions() {
    final CharArrayWriter writer = new CharArrayWriter();
    Pretty printer = new Pretty(writer, true) {

        int previousCeylonPosition = -1;

        int previousPositionInString = 0;

        private void outputCeylonPosition(JCTree tree) {
            try {
                int currentCeylonPosition = tree.getPreferredPosition();
                int currentPositionInString = writer.size();
                if (previousCeylonPosition != currentCeylonPosition || previousPositionInString != currentPositionInString) {
                    if (currentCeylonPosition != -1 && currentCeylonPosition != 0) {
                        writer.write("/* " + formatCeylonPosition(currentCeylonPosition) + " */");
                    }
                    previousCeylonPosition = currentCeylonPosition;
                    previousPositionInString = writer.size();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void visitTopLevel(JCCompilationUnit tree) {
            outputCeylonPosition(tree);
            super.visitTopLevel(tree);
        }

        @Override
        public void visitImport(JCImport tree) {
            outputCeylonPosition(tree);
            super.visitImport(tree);
        }

        @Override
        public void visitClassDef(JCClassDecl tree) {
            outputCeylonPosition(tree);
            super.visitClassDef(tree);
        }

        @Override
        public void visitMethodDef(JCMethodDecl tree) {
            outputCeylonPosition(tree);
            super.visitMethodDef(tree);
        }

        @Override
        public void visitVarDef(JCVariableDecl tree) {
            outputCeylonPosition(tree);
            super.visitVarDef(tree);
        }

        @Override
        public void visitSkip(JCSkip tree) {
            outputCeylonPosition(tree);
            super.visitSkip(tree);
        }

        @Override
        public void visitBlock(JCBlock tree) {
            outputCeylonPosition(tree);
            super.visitBlock(tree);
            tree.endpos = currentPosition - 1;
        }

        @Override
        public void visitDoLoop(JCDoWhileLoop tree) {
            outputCeylonPosition(tree);
            super.visitDoLoop(tree);
        }

        @Override
        public void visitWhileLoop(JCWhileLoop tree) {
            outputCeylonPosition(tree);
            super.visitWhileLoop(tree);
        }

        @Override
        public void visitForLoop(JCForLoop tree) {
            outputCeylonPosition(tree);
            super.visitForLoop(tree);
        }

        @Override
        public void visitForeachLoop(JCEnhancedForLoop tree) {
            outputCeylonPosition(tree);
            super.visitForeachLoop(tree);
        }

        @Override
        public void visitLabelled(JCLabeledStatement tree) {
            outputCeylonPosition(tree);
            super.visitLabelled(tree);
        }

        @Override
        public void visitSwitch(JCSwitch tree) {
            outputCeylonPosition(tree);
            super.visitSwitch(tree);
        }

        @Override
        public void visitCase(JCCase tree) {
            outputCeylonPosition(tree);
            super.visitCase(tree);
        }

        @Override
        public void visitSynchronized(JCSynchronized tree) {
            outputCeylonPosition(tree);
            super.visitSynchronized(tree);
        }

        @Override
        public void visitTry(JCTry tree) {
            outputCeylonPosition(tree);
            super.visitTry(tree);
        }

        @Override
        public void visitCatch(JCCatch tree) {
            outputCeylonPosition(tree);
            super.visitCatch(tree);
        }

        @Override
        public void visitConditional(JCConditional tree) {
            outputCeylonPosition(tree);
            super.visitConditional(tree);
        }

        @Override
        public void visitIf(JCIf tree) {
            outputCeylonPosition(tree);
            super.visitIf(tree);
        }

        @Override
        public void visitExec(JCExpressionStatement tree) {
            outputCeylonPosition(tree);
            super.visitExec(tree);
        }

        @Override
        public void visitBreak(JCBreak tree) {
            outputCeylonPosition(tree);
            super.visitBreak(tree);
        }

        @Override
        public void visitContinue(JCContinue tree) {
            outputCeylonPosition(tree);
            super.visitContinue(tree);
        }

        @Override
        public void visitReturn(JCReturn tree) {
            outputCeylonPosition(tree);
            super.visitReturn(tree);
        }

        @Override
        public void visitThrow(JCThrow tree) {
            outputCeylonPosition(tree);
            super.visitThrow(tree);
        }

        @Override
        public void visitAssert(JCAssert tree) {
            outputCeylonPosition(tree);
            super.visitAssert(tree);
        }

        @Override
        public void visitApply(JCMethodInvocation tree) {
            outputCeylonPosition(tree);
            super.visitApply(tree);
        }

        @Override
        public void visitNewClass(JCNewClass tree) {
            outputCeylonPosition(tree);
            super.visitNewClass(tree);
        }

        @Override
        public void visitNewArray(JCNewArray tree) {
            outputCeylonPosition(tree);
            super.visitNewArray(tree);
        }

        @Override
        public void visitParens(JCParens tree) {
            outputCeylonPosition(tree);
            super.visitParens(tree);
        }

        @Override
        public void visitAssign(JCAssign tree) {
            outputCeylonPosition(tree);
            super.visitAssign(tree);
        }

        @Override
        public void visitAssignop(JCAssignOp tree) {
            outputCeylonPosition(tree);
            super.visitAssignop(tree);
        }

        @Override
        public void visitUnary(JCUnary tree) {
            outputCeylonPosition(tree);
            super.visitUnary(tree);
        }

        @Override
        public void visitBinary(JCBinary tree) {
            outputCeylonPosition(tree);
            super.visitBinary(tree);
        }

        @Override
        public void visitTypeCast(JCTypeCast tree) {
            outputCeylonPosition(tree);
            super.visitTypeCast(tree);
        }

        @Override
        public void visitTypeTest(JCInstanceOf tree) {
            outputCeylonPosition(tree);
            super.visitTypeTest(tree);
        }

        @Override
        public void visitIndexed(JCArrayAccess tree) {
            outputCeylonPosition(tree);
            super.visitIndexed(tree);
        }

        @Override
        public void visitSelect(JCFieldAccess tree) {
            outputCeylonPosition(tree);
            super.visitSelect(tree);
        }

        @Override
        public void visitIdent(JCIdent tree) {
            outputCeylonPosition(tree);
            super.visitIdent(tree);
        }

        @Override
        public void visitLiteral(JCLiteral tree) {
            outputCeylonPosition(tree);
            super.visitLiteral(tree);
        }

        @Override
        public void visitTypeIdent(JCPrimitiveTypeTree tree) {
            outputCeylonPosition(tree);
            super.visitTypeIdent(tree);
        }

        @Override
        public void visitTypeArray(JCArrayTypeTree tree) {
            outputCeylonPosition(tree);
            super.visitTypeArray(tree);
        }

        @Override
        public void visitTypeApply(JCTypeApply tree) {
            outputCeylonPosition(tree);
            super.visitTypeApply(tree);
        }

        @Override
        public void visitTypeParameter(JCTypeParameter tree) {
            outputCeylonPosition(tree);
            super.visitTypeParameter(tree);
        }

        @Override
        public void visitWildcard(JCWildcard tree) {
            outputCeylonPosition(tree);
            super.visitWildcard(tree);
        }

        @Override
        public void visitTypeBoundKind(TypeBoundKind tree) {
            outputCeylonPosition(tree);
            super.visitTypeBoundKind(tree);
        }

        @Override
        public void visitErroneous(JCErroneous tree) {
            outputCeylonPosition(tree);
            super.visitErroneous(tree);
        }

        @Override
        public void visitLetExpr(LetExpr tree) {
            outputCeylonPosition(tree);
            super.visitLetExpr(tree);
        }

        @Override
        public void visitModifiers(JCModifiers mods) {
            outputCeylonPosition(mods);
            super.visitModifiers(mods);
        }

        @Override
        public void visitAnnotation(JCAnnotation tree) {
            outputCeylonPosition(tree);
            super.visitAnnotation(tree);
        }

        @Override
        public void visitTree(JCTree tree) {
            outputCeylonPosition(tree);
            super.visitTree(tree);
        }
    };
    printer.visitTopLevel(unit);
    return writer.toString();
}
Also used : JCFieldAccess(com.sun.tools.javac.tree.JCTree.JCFieldAccess) JCTypeApply(com.sun.tools.javac.tree.JCTree.JCTypeApply) JCAssert(com.sun.tools.javac.tree.JCTree.JCAssert) JCPrimitiveTypeTree(com.sun.tools.javac.tree.JCTree.JCPrimitiveTypeTree) JCIf(com.sun.tools.javac.tree.JCTree.JCIf) JCEnhancedForLoop(com.sun.tools.javac.tree.JCTree.JCEnhancedForLoop) JCNewClass(com.sun.tools.javac.tree.JCTree.JCNewClass) JCNewArray(com.sun.tools.javac.tree.JCTree.JCNewArray) JCAnnotation(com.sun.tools.javac.tree.JCTree.JCAnnotation) JCCase(com.sun.tools.javac.tree.JCTree.JCCase) JCThrow(com.sun.tools.javac.tree.JCTree.JCThrow) JCImport(com.sun.tools.javac.tree.JCTree.JCImport) JCWildcard(com.sun.tools.javac.tree.JCTree.JCWildcard) JCClassDecl(com.sun.tools.javac.tree.JCTree.JCClassDecl) JCIdent(com.sun.tools.javac.tree.JCTree.JCIdent) JCMethodDecl(com.sun.tools.javac.tree.JCTree.JCMethodDecl) LetExpr(com.sun.tools.javac.tree.JCTree.LetExpr) JCErroneous(com.sun.tools.javac.tree.JCTree.JCErroneous) JCSynchronized(com.sun.tools.javac.tree.JCTree.JCSynchronized) JCParens(com.sun.tools.javac.tree.JCTree.JCParens) JCDoWhileLoop(com.sun.tools.javac.tree.JCTree.JCDoWhileLoop) JCContinue(com.sun.tools.javac.tree.JCTree.JCContinue) JCInstanceOf(com.sun.tools.javac.tree.JCTree.JCInstanceOf) TypeBoundKind(com.sun.tools.javac.tree.JCTree.TypeBoundKind) JCTypeParameter(com.sun.tools.javac.tree.JCTree.JCTypeParameter) JCMethodInvocation(com.sun.tools.javac.tree.JCTree.JCMethodInvocation) JCUnary(com.sun.tools.javac.tree.JCTree.JCUnary) JCModifiers(com.sun.tools.javac.tree.JCTree.JCModifiers) JCCatch(com.sun.tools.javac.tree.JCTree.JCCatch) JCCompilationUnit(com.sun.tools.javac.tree.JCTree.JCCompilationUnit) JCWhileLoop(com.sun.tools.javac.tree.JCTree.JCWhileLoop) JCReturn(com.sun.tools.javac.tree.JCTree.JCReturn) JCLabeledStatement(com.sun.tools.javac.tree.JCTree.JCLabeledStatement) JCAssign(com.sun.tools.javac.tree.JCTree.JCAssign) JCSkip(com.sun.tools.javac.tree.JCTree.JCSkip) JCConditional(com.sun.tools.javac.tree.JCTree.JCConditional) JCExpressionStatement(com.sun.tools.javac.tree.JCTree.JCExpressionStatement) CharArrayWriter(java.io.CharArrayWriter) JCTypeCast(com.sun.tools.javac.tree.JCTree.JCTypeCast) JCArrayTypeTree(com.sun.tools.javac.tree.JCTree.JCArrayTypeTree) JCBlock(com.sun.tools.javac.tree.JCTree.JCBlock) JCTree(com.sun.tools.javac.tree.JCTree) JCBinary(com.sun.tools.javac.tree.JCTree.JCBinary) JCArrayAccess(com.sun.tools.javac.tree.JCTree.JCArrayAccess) IOException(java.io.IOException) JCForLoop(com.sun.tools.javac.tree.JCTree.JCForLoop) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl) Pretty(com.sun.tools.javac.tree.Pretty) JCTry(com.sun.tools.javac.tree.JCTree.JCTry) JCSwitch(com.sun.tools.javac.tree.JCTree.JCSwitch) JCLiteral(com.sun.tools.javac.tree.JCTree.JCLiteral) JCAssignOp(com.sun.tools.javac.tree.JCTree.JCAssignOp) JCBreak(com.sun.tools.javac.tree.JCTree.JCBreak)

Aggregations

JCEnhancedForLoop (com.sun.tools.javac.tree.JCTree.JCEnhancedForLoop)3 JCTree (com.sun.tools.javac.tree.JCTree)2 JCAnnotation (com.sun.tools.javac.tree.JCTree.JCAnnotation)2 JCForLoop (com.sun.tools.javac.tree.JCTree.JCForLoop)2 JCLiteral (com.sun.tools.javac.tree.JCTree.JCLiteral)2 JCNewArray (com.sun.tools.javac.tree.JCTree.JCNewArray)2 Fix (com.google.errorprone.fixes.Fix)1 SuggestedFix (com.google.errorprone.fixes.SuggestedFix)1 ExpressionTree (com.sun.source.tree.ExpressionTree)1 IdentifierTree (com.sun.source.tree.IdentifierTree)1 Type (com.sun.tools.javac.code.Type)1 JCArrayAccess (com.sun.tools.javac.tree.JCTree.JCArrayAccess)1 JCArrayTypeTree (com.sun.tools.javac.tree.JCTree.JCArrayTypeTree)1 JCAssert (com.sun.tools.javac.tree.JCTree.JCAssert)1 JCAssign (com.sun.tools.javac.tree.JCTree.JCAssign)1 JCAssignOp (com.sun.tools.javac.tree.JCTree.JCAssignOp)1 JCBinary (com.sun.tools.javac.tree.JCTree.JCBinary)1 JCBlock (com.sun.tools.javac.tree.JCTree.JCBlock)1 JCBreak (com.sun.tools.javac.tree.JCTree.JCBreak)1 JCCase (com.sun.tools.javac.tree.JCTree.JCCase)1