Search in sources :

Example 26 with JCStatement

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

the class BlockTemplate method replace.

@Override
public Fix replace(BlockTemplateMatch match) {
    checkNotNull(match);
    SuggestedFix.Builder fix = SuggestedFix.builder();
    Inliner inliner = match.createInliner();
    Context context = inliner.getContext();
    if (annotations().containsKey(UseImportPolicy.class)) {
        ImportPolicy.bind(context, annotations().getInstance(UseImportPolicy.class).value());
    } else {
        ImportPolicy.bind(context, ImportPolicy.IMPORT_TOP_LEVEL);
    }
    ImmutableList<JCStatement> targetStatements = match.getStatements();
    try {
        ImmutableList.Builder<JCStatement> inlinedStatementsBuilder = ImmutableList.builder();
        for (UStatement statement : templateStatements()) {
            inlinedStatementsBuilder.addAll(statement.inlineStatements(inliner));
        }
        ImmutableList<JCStatement> inlinedStatements = inlinedStatementsBuilder.build();
        int nInlined = inlinedStatements.size();
        int nTargets = targetStatements.size();
        if (nInlined <= nTargets) {
            for (int i = 0; i < nInlined; i++) {
                fix.replace(targetStatements.get(i), printStatement(context, inlinedStatements.get(i)));
            }
            for (int i = nInlined; i < nTargets; i++) {
                fix.delete(targetStatements.get(i));
            }
        } else {
            for (int i = 0; i < nTargets - 1; i++) {
                fix.replace(targetStatements.get(i), printStatement(context, inlinedStatements.get(i)));
            }
            int last = nTargets - 1;
            ImmutableList<JCStatement> remainingInlined = inlinedStatements.subList(last, nInlined);
            fix.replace(targetStatements.get(last), CharMatcher.whitespace().trimTrailingFrom(printStatements(context, remainingInlined)));
        }
    } catch (CouldNotResolveImportException e) {
        logger.log(SEVERE, "Failure to resolve import in replacement", e);
    }
    return addImports(inliner, fix);
}
Also used : Context(com.sun.tools.javac.util.Context) SuggestedFix(com.google.errorprone.fixes.SuggestedFix) ImmutableList(com.google.common.collect.ImmutableList) JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement)

Example 27 with JCStatement

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

the class BlockTemplate method match.

/**
   * If the tree is a {@link JCBlock}, returns a list of disjoint matches corresponding to the exact
   * list of template statements found consecutively; otherwise, returns an empty list.
   */
@Override
public Iterable<BlockTemplateMatch> match(JCTree tree, Context context) {
    // TODO(lowasser): consider nonconsecutive matches?
    if (tree instanceof JCBlock) {
        JCBlock block = (JCBlock) tree;
        ImmutableList<JCStatement> targetStatements = ImmutableList.copyOf(block.getStatements());
        return matchesStartingAnywhere(block, 0, targetStatements, context).first().or(List.<BlockTemplateMatch>nil());
    }
    return ImmutableList.of();
}
Also used : JCBlock(com.sun.tools.javac.tree.JCTree.JCBlock) JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement)

Example 28 with JCStatement

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

the class BlockTemplate method matchesStartingAtBeginning.

private Choice<List<BlockTemplateMatch>> matchesStartingAtBeginning(final JCBlock block, final int offset, final ImmutableList<? extends StatementTree> statements, final Context context) {
    if (statements.isEmpty()) {
        return Choice.none();
    }
    final JCStatement firstStatement = (JCStatement) statements.get(0);
    Choice<UnifierWithUnconsumedStatements> choice = Choice.of(UnifierWithUnconsumedStatements.create(new Unifier(context), statements));
    for (UStatement templateStatement : templateStatements()) {
        choice = choice.thenChoose(templateStatement);
    }
    return choice.thenChoose(new Function<UnifierWithUnconsumedStatements, Choice<List<BlockTemplateMatch>>>() {

        @Override
        public Choice<List<BlockTemplateMatch>> apply(UnifierWithUnconsumedStatements state) {
            Unifier unifier = state.unifier();
            Inliner inliner = unifier.createInliner();
            try {
                Optional<Unifier> checkedUnifier = typecheck(unifier, inliner, new Warner(firstStatement), expectedTypes(inliner), actualTypes(inliner));
                if (checkedUnifier.isPresent()) {
                    int consumedStatements = statements.size() - state.unconsumedStatements().size();
                    BlockTemplateMatch match = new BlockTemplateMatch(block, checkedUnifier.get(), offset, offset + consumedStatements);
                    return matchesStartingAnywhere(block, offset + consumedStatements, statements.subList(consumedStatements, statements.size()), context).transform(prepend(match));
                }
            } catch (CouldNotResolveImportException e) {
            // fall through
            }
            return Choice.none();
        }
    });
}
Also used : Optional(com.google.common.base.Optional) JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement) UnifierWithUnconsumedStatements(com.google.errorprone.refaster.UStatement.UnifierWithUnconsumedStatements) Warner(com.sun.tools.javac.util.Warner)

Example 29 with JCStatement

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

the class RefasterScanner method visitClass.

@Override
public Void visitClass(ClassTree node, Context context) {
    Symbol sym = ASTHelpers.getSymbol(node);
    if (sym == null || !sym.getQualifiedName().contentEquals(rule().qualifiedTemplateClass())) {
        ListBuffer<JCStatement> statements = new ListBuffer<>();
        for (Tree tree : node.getMembers()) {
            if (tree instanceof JCStatement) {
                statements.append((JCStatement) tree);
            } else {
                tree.accept(this, context);
            }
        }
        scan(TreeMaker.instance(context).Block(0, statements.toList()), context);
    }
    return null;
}
Also used : Symbol(com.sun.tools.javac.code.Symbol) ListBuffer(com.sun.tools.javac.util.ListBuffer) Tree(com.sun.source.tree.Tree) ClassTree(com.sun.source.tree.ClassTree) ParenthesizedTree(com.sun.source.tree.ParenthesizedTree) IfTree(com.sun.source.tree.IfTree) JCTree(com.sun.tools.javac.tree.JCTree) DoWhileLoopTree(com.sun.source.tree.DoWhileLoopTree) SynchronizedTree(com.sun.source.tree.SynchronizedTree) WhileLoopTree(com.sun.source.tree.WhileLoopTree) JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement)

Example 30 with JCStatement

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

the class AbstractTransformer method makeLazyIterable.

/**
     * Makes a lazy iterable literal, for a sequenced argument to a named invocation 
     * (<code>f{foo=""; expr1, expr2, *expr3}</code>) or
     * for an iterable instantiation (<code>{expr1, expr2, *expr3}</code>)
     */
JCExpression makeLazyIterable(Tree.SequencedArgument sequencedArgument, Type seqElemType, Type absentType, int flags) {
    java.util.List<PositionalArgument> list = sequencedArgument.getPositionalArguments();
    int i = 0;
    ListBuffer<JCStatement> returns = new ListBuffer<JCStatement>();
    boolean spread = false;
    boolean old = expressionGen().withinSyntheticClassBody(true);
    try {
        for (Tree.PositionalArgument arg : list) {
            at(arg);
            JCExpression jcExpression;
            // last expression can be an Iterable<seqElemType>
            if (arg instanceof Tree.SpreadArgument || arg instanceof Tree.Comprehension) {
                // make sure we only have spread/comprehension as last
                if (i != list.size() - 1) {
                    jcExpression = makeErroneous(arg, "compiler bug: spread or comprehension argument is not last in sequence literal");
                } else {
                    Type type = typeFact().getIterableType(seqElemType);
                    spread = true;
                    if (arg instanceof Tree.SpreadArgument) {
                        Tree.Expression expr = ((Tree.SpreadArgument) arg).getExpression();
                        // always boxed since it is a sequence
                        jcExpression = expressionGen().transformExpression(expr, BoxingStrategy.BOXED, type);
                    } else {
                        jcExpression = expressionGen().transformComprehension((Comprehension) arg, type);
                    }
                }
            } else if (arg instanceof Tree.ListedArgument) {
                Tree.Expression expr = ((Tree.ListedArgument) arg).getExpression();
                // always boxed since we stuff them into a sequence
                jcExpression = expressionGen().transformExpression(expr, BoxingStrategy.BOXED, seqElemType);
            } else {
                jcExpression = makeErroneous(arg, "compiler bug: " + arg.getNodeType() + " is not a supported sequenced argument");
            }
            at(arg);
            // the last iterable goes first if spread
            returns.add(make().Return(jcExpression));
            i++;
        }
        at(sequencedArgument);
        if (Strategy.preferLazySwitchingIterable(sequencedArgument.getPositionalArguments())) {
            // use a LazySwitchingIterable
            MethodDefinitionBuilder mdb = MethodDefinitionBuilder.systemMethod(this, Unfix.$evaluate$.toString());
            mdb.isOverride(true);
            mdb.modifiers(PROTECTED | FINAL);
            mdb.resultType(null, make().Type(syms().objectType));
            mdb.parameter(ParameterDefinitionBuilder.systemParameter(this, Unfix.$index$.toString()).type(make().Type(syms().intType), null));
            JCSwitch swtch;
            try (SavedPosition sp = noPosition()) {
                ListBuffer<JCCase> cases = ListBuffer.<JCCase>lb();
                i = 0;
                for (JCStatement e : returns) {
                    cases.add(make().Case(make().Literal(i++), List.<JCStatement>of(e)));
                }
                cases.add(make().Case(null, List.<JCStatement>of(make().Return(makeNull()))));
                swtch = make().Switch(naming.makeUnquotedIdent(Unfix.$index$), cases.toList());
            }
            mdb.body(swtch);
            return make().NewClass(null, //of(makeJavaType(seqElemType), makeJavaType(absentType)),
            List.<JCExpression>nil(), make().TypeApply(make().QualIdent(syms.ceylonLazyIterableType.tsym), List.<JCExpression>of(makeJavaType(seqElemType, JT_TYPE_ARGUMENT), makeJavaType(absentType, JT_TYPE_ARGUMENT))), // td, 
            List.of(// td, 
            makeReifiedTypeArgument(seqElemType), //td
            makeReifiedTypeArgument(absentType), // numMethods
            make().Literal(list.size()), // spread), 
            make().Literal(spread)), make().AnonymousClassDef(make().Modifiers(FINAL), List.<JCTree>of(mdb.build())));
        } else {
            // use a LazyInvokingIterable
            ListBuffer<JCTree> methods = new ListBuffer<JCTree>();
            MethodDefinitionBuilder mdb = MethodDefinitionBuilder.systemMethod(this, Unfix.$lookup$.toString());
            mdb.isOverride(true);
            mdb.modifiers(PROTECTED | FINAL);
            mdb.resultType(null, naming.makeQualIdent(make().Type(syms().methodHandlesType), "Lookup"));
            mdb.body(make().Return(make().Apply(List.<JCExpression>nil(), naming.makeQualIdent(make().Type(syms().methodHandlesType), "lookup"), List.<JCExpression>nil())));
            methods.add(mdb.build());
            mdb = MethodDefinitionBuilder.systemMethod(this, Unfix.$invoke$.toString());
            mdb.isOverride(true);
            mdb.modifiers(PROTECTED | FINAL);
            mdb.resultType(null, make().Type(syms().objectType));
            mdb.parameter(ParameterDefinitionBuilder.systemParameter(this, "handle").type(make().Type(syms().methodHandleType), null));
            mdb.body(make().Return(make().Apply(List.<JCExpression>nil(), naming.makeQualIdent(naming.makeUnquotedIdent("handle"), "invokeExact"), List.<JCExpression>of(naming.makeThis()))));
            methods.add(mdb.build());
            i = 0;
            for (JCStatement expr : returns) {
                mdb = MethodDefinitionBuilder.systemMethod(this, "$" + i);
                i++;
                mdb.modifiers(PRIVATE | FINAL);
                mdb.resultType(null, make().Type(syms().objectType));
                mdb.body(expr);
                methods.add(mdb.build());
            }
            return make().NewClass(null, //of(makeJavaType(seqElemType), makeJavaType(absentType)),
            List.<JCExpression>nil(), make().TypeApply(make().QualIdent(syms.ceylonLazyInvokingIterableType.tsym), List.<JCExpression>of(makeJavaType(seqElemType, JT_TYPE_ARGUMENT), makeJavaType(absentType, JT_TYPE_ARGUMENT))), // td, 
            List.of(// td, 
            makeReifiedTypeArgument(seqElemType), //td
            makeReifiedTypeArgument(absentType), // numMethods
            make().Literal(list.size()), // spread), 
            make().Literal(spread)), make().AnonymousClassDef(make().Modifiers(FINAL), methods.toList()));
        }
    } finally {
        expressionGen().withinSyntheticClassBody(old);
    }
}
Also used : ListBuffer(com.sun.tools.javac.util.ListBuffer) PositionalArgument(com.redhat.ceylon.compiler.typechecker.tree.Tree.PositionalArgument) JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement) JCTree(com.sun.tools.javac.tree.JCTree) Tree(com.redhat.ceylon.compiler.typechecker.tree.Tree) JCCase(com.sun.tools.javac.tree.JCTree.JCCase) JCTree(com.sun.tools.javac.tree.JCTree) Comprehension(com.redhat.ceylon.compiler.typechecker.tree.Tree.Comprehension) Type(com.redhat.ceylon.model.typechecker.model.Type) ModelUtil.appliedType(com.redhat.ceylon.model.typechecker.model.ModelUtil.appliedType) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JCSwitch(com.sun.tools.javac.tree.JCTree.JCSwitch) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) Comprehension(com.redhat.ceylon.compiler.typechecker.tree.Tree.Comprehension) PositionalArgument(com.redhat.ceylon.compiler.typechecker.tree.Tree.PositionalArgument)

Aggregations

JCStatement (com.sun.tools.javac.tree.JCTree.JCStatement)112 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)82 JCVariableDecl (com.sun.tools.javac.tree.JCTree.JCVariableDecl)37 JCBlock (com.sun.tools.javac.tree.JCTree.JCBlock)33 JCTree (com.sun.tools.javac.tree.JCTree)32 Name (com.sun.tools.javac.util.Name)32 Type (com.redhat.ceylon.model.typechecker.model.Type)26 JCTypeParameter (com.sun.tools.javac.tree.JCTree.JCTypeParameter)26 ListBuffer (com.sun.tools.javac.util.ListBuffer)25 Tree (com.redhat.ceylon.compiler.typechecker.tree.Tree)23 SyntheticName (com.redhat.ceylon.compiler.java.codegen.Naming.SyntheticName)18 JCMethodDecl (com.sun.tools.javac.tree.JCTree.JCMethodDecl)16 JavacTreeMaker (lombok.javac.JavacTreeMaker)16 TypeParameter (com.redhat.ceylon.model.typechecker.model.TypeParameter)14 JCModifiers (com.sun.tools.javac.tree.JCTree.JCModifiers)14 TypedDeclaration (com.redhat.ceylon.model.typechecker.model.TypedDeclaration)12 JCAnnotation (com.sun.tools.javac.tree.JCTree.JCAnnotation)12 JCPrimitiveTypeTree (com.sun.tools.javac.tree.JCTree.JCPrimitiveTypeTree)12 Parameter (com.redhat.ceylon.model.typechecker.model.Parameter)11 HasErrorException (com.redhat.ceylon.compiler.java.codegen.recovery.HasErrorException)10