本文整理汇总了Java中com.sun.source.tree.ForLoopTree.getStatement方法的典型用法代码示例。如果您正苦于以下问题:Java ForLoopTree.getStatement方法的具体用法?Java ForLoopTree.getStatement怎么用?Java ForLoopTree.getStatement使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.sun.source.tree.ForLoopTree
的用法示例。
在下文中一共展示了ForLoopTree.getStatement方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: visitForLoop
import com.sun.source.tree.ForLoopTree; //导入方法依赖的package包/类
@Override
public Mirror visitForLoop(ForLoopTree arg0, EvaluationContext evaluationContext) {
try {
evaluationContext.pushBlock();
for (StatementTree st : arg0.getInitializer()) {
st.accept(this, evaluationContext);
}
Mirror result = null;
ExpressionTree condition = arg0.getCondition();
List<? extends ExpressionStatementTree> updateList = arg0.getUpdate();
StatementTree statement = arg0.getStatement();
while (condition == null || evaluateCondition(arg0, evaluationContext, condition)) {
Mirror value = null;
try {
evaluationContext.pushBlock();
value = statement.accept(this, evaluationContext);
if (value instanceof Break) {
break;
} else if (value instanceof Continue) {
continue;
}
if (value != null) {
result = value;
}
} finally {
evaluationContext.popBlock();
if ((value instanceof Continue) || !(value instanceof CommandMirror)) {
for (Tree tree : updateList) {
tree.accept(this, evaluationContext);
} // for
} // if
} // finally
} // while
return result;
} finally {
evaluationContext.popBlock();
}
}
示例2: visitForLoop
import com.sun.source.tree.ForLoopTree; //导入方法依赖的package包/类
@Override
public Void visitForLoop(ForLoopTree forLoopTree, List<ReformatOption> optionsToReformat) {
StatementTree statement = forLoopTree.getStatement();
if (statement instanceof BlockTree) {
addLeftBraceToList(optionsToReformat, ((BlockTree) statement), PreferencesFormatOptions.BRACES_IN_OTHER_DECLARATION);
addRightBraceToList(optionsToReformat, ((CompoundTree) statement), PreferencesFormatOptions.AFTER_OTHER_DECLARATION);
}
return null;
}
示例3: matchForLoop
import com.sun.source.tree.ForLoopTree; //导入方法依赖的package包/类
@Override
public Description matchForLoop(ForLoopTree forLoopTree, VisitorState visitorState) {
List<? extends ExpressionStatementTree> updates = forLoopTree.getUpdate();
// keep track of all the symbols that are updated in the for loop header
final Set<Symbol> incrementedSymbols =
updates
.stream()
.filter(expStateTree -> expStateTree.getExpression() instanceof UnaryTree)
.map(
expStateTree ->
ASTHelpers.getSymbol(
((UnaryTree) expStateTree.getExpression()).getExpression()))
.collect(Collectors.toCollection(HashSet::new));
// track if they are updated in the body without a conditional surrounding them
StatementTree body = forLoopTree.getStatement();
List<? extends StatementTree> statementTrees =
body instanceof BlockTree ? ((BlockTree) body).getStatements() : ImmutableList.of(body);
for (StatementTree s : statementTrees) {
if (!CONDITIONALS.contains(s.getKind())) {
Optional<Symbol> opSymbol = returnUnarySym(s);
if (opSymbol.isPresent() && incrementedSymbols.contains(opSymbol.get())) {
// both ++ and --
return describeMatch(forLoopTree);
}
}
}
return Description.NO_MATCH;
}
示例4: testAddCastInForWithoutSteps
import com.sun.source.tree.ForLoopTree; //导入方法依赖的package包/类
public void testAddCastInForWithoutSteps() throws Exception {
testFile = new File(getWorkDir(), "Test.java");
TestUtilities.copyStringToFile(testFile,
"package hierbas.del.litoral;\n\n" +
"import java.util.*;\n\n" +
"public class Test<E> {\n" +
" public void cast() {\n" +
" Object o = null;\n" +
" for (int i = 0; i < 5; ) {\n" +
" String s = o;\n" +
" }\n" +
" }\n" +
"}\n"
);
String golden =
"package hierbas.del.litoral;\n\n" +
"import java.util.*;\n\n" +
"public class Test<E> {\n" +
" public void cast() {\n" +
" Object o = null;\n" +
" for (int i = 0; i < 5; ) {\n" +
" String s = (String) o;\n" +
" }\n" +
" }\n" +
"}\n";
JavaSource src = getJavaSource(testFile);
Task<WorkingCopy> task = new Task<WorkingCopy>() {
public void run(WorkingCopy workingCopy) throws IOException {
workingCopy.toPhase(Phase.RESOLVED);
CompilationUnitTree cut = workingCopy.getCompilationUnit();
TreeMaker make = workingCopy.getTreeMaker();
ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
MethodTree method = (MethodTree) clazz.getMembers().get(1);
ForLoopTree forLoop = (ForLoopTree) method.getBody().getStatements().get(1);
BlockTree block = (BlockTree) forLoop.getStatement();
VariableTree vt = (VariableTree) block.getStatements().get(0);
ExpressionTree init = vt.getInitializer();
ExpressionTree cast = make.TypeCast(make.Identifier("String"), init);
workingCopy.rewrite(init, cast);
}
};
src.runModificationTask(task).commit();
String res = TestUtilities.copyFileToString(testFile);
System.err.println(res);
assertEquals(golden, res);
}
示例5: performRewrite
import com.sun.source.tree.ForLoopTree; //导入方法依赖的package包/类
@Override
protected void performRewrite(TransformationContext ctx) {
WorkingCopy copy = ctx.getWorkingCopy();
TreePath path = ctx.getPath();
if ( path != null ) {
TreeMaker make = copy.getTreeMaker();
Tree oldTree = path.getLeaf();
oldTree = GeneratorUtilities.get(copy).importComments(oldTree, copy.getCompilationUnit());
switch( oldTree.getKind() ) {
case FOR_LOOP:
ForLoopTree oldFor = (ForLoopTree)oldTree;
StatementTree oldBlock = oldFor.getStatement();
BlockTree newBlock = make.Block(Collections.<StatementTree>singletonList(oldBlock), false);
copy.rewrite(oldBlock, newBlock);
break;
case ENHANCED_FOR_LOOP:
EnhancedForLoopTree oldEnhancedFor = (EnhancedForLoopTree)oldTree;
oldBlock = oldEnhancedFor.getStatement();
newBlock = make.Block(Collections.<StatementTree>singletonList(oldBlock), false);
copy.rewrite(oldBlock, newBlock);
break;
case WHILE_LOOP:
WhileLoopTree oldWhile = (WhileLoopTree)oldTree;
oldBlock = oldWhile.getStatement();
newBlock = make.Block(Collections.<StatementTree>singletonList(oldBlock), false);
copy.rewrite(oldBlock, newBlock);
break;
case DO_WHILE_LOOP:
DoWhileLoopTree oldDoWhile = (DoWhileLoopTree)oldTree;
oldBlock = oldDoWhile.getStatement();
newBlock = make.Block(Collections.<StatementTree>singletonList(oldBlock), false);
copy.rewrite(oldBlock, newBlock);
break;
case IF:
IfTree oldIf = (IfTree)oldTree;
if ( fixThen ) {
oldBlock = oldIf.getThenStatement();
newBlock = make.Block(Collections.<StatementTree>singletonList(oldBlock), false);
copy.rewrite(oldBlock, newBlock);
}
if ( fixElse ) {
oldBlock = oldIf.getElseStatement();
newBlock = make.Block(Collections.<StatementTree>singletonList(oldBlock), false);
copy.rewrite(oldBlock, newBlock);
}
}
}
}
示例6: visitContinue
import com.sun.source.tree.ForLoopTree; //导入方法依赖的package包/类
public Boolean visitContinue(ContinueTree node, ConstructorData p) {
StatementTree loop = info.getTreeUtilities().getBreakContinueTarget(getCurrentPath());
if (loop == null) {
super.visitContinue(node, p);
return null;
}
Tree resumePoint;
if (loop.getKind() == Kind.LABELED_STATEMENT) {
loop = ((LabeledStatementTree) loop).getStatement();
}
switch (loop.getKind()) {
case WHILE_LOOP:
resumePoint = ((WhileLoopTree) loop).getCondition();
break;
case FOR_LOOP: {
ForLoopTree flt = (ForLoopTree)loop;
resumePoint = null;
if (flt.getUpdate() != null && !flt.getUpdate().isEmpty()) {
// resume will react on the 1st Tree of the update statement (always processed left to right)
resumePoint = flt.getUpdate().get(0);
}
if (resumePoint == null) {
resumePoint = flt.getCondition();
}
if (resumePoint == null) {
resumePoint = flt.getStatement();
}
}
break;
case DO_WHILE_LOOP:
resumePoint = ((DoWhileLoopTree) loop).getCondition();
break;
case ENHANCED_FOR_LOOP:
resumePoint = ((EnhancedForLoopTree) loop).getStatement();
break;
default:
resumePoint = null;
break;
}
if (resumePoint != null) {
recordResume(resumeBefore, resumePoint, variable2State);
}
variable2State = new HashMap< Element, State>();
super.visitContinue(node, p);
return null;
}
示例7: interestingPartOfLoop
import com.sun.source.tree.ForLoopTree; //导入方法依赖的package包/类
@Override
Object interestingPartOfLoop(ForLoopTree loop) {
return loop.getStatement();
}
示例8: visitForLoop
import com.sun.source.tree.ForLoopTree; //导入方法依赖的package包/类
@Override
public Node visitForLoop(ForLoopTree tree, Void p) {
Name parentLabel = getLabel(getCurrentPath());
Label conditionStart = new Label();
Label loopEntry = new Label();
Label loopExit = new Label();
// If the loop is a labeled statement, then its continue
// target is identical for continues with no label and
// continues with the loop's label.
Label updateStart;
if (parentLabel != null) {
updateStart = continueLabels.get(parentLabel);
} else {
updateStart = new Label();
}
Label oldBreakTargetL = breakTargetL;
breakTargetL = loopExit;
Label oldContinueTargetL = continueTargetL;
continueTargetL = updateStart;
// Initializer
for (StatementTree init : tree.getInitializer()) {
scan(init, p);
}
// Condition
addLabelForNextNode(conditionStart);
if (tree.getCondition() != null) {
unbox(scan(tree.getCondition(), p));
ConditionalJump cjump = new ConditionalJump(loopEntry, loopExit);
extendWithExtendedNode(cjump);
}
// Loop body
addLabelForNextNode(loopEntry);
if (tree.getStatement() != null) {
scan(tree.getStatement(), p);
}
// Update
addLabelForNextNode(updateStart);
for (ExpressionStatementTree update : tree.getUpdate()) {
scan(update, p);
}
extendWithExtendedNode(new UnconditionalJump(conditionStart));
// Loop exit
addLabelForNextNode(loopExit);
breakTargetL = oldBreakTargetL;
continueTargetL = oldContinueTargetL;
return null;
}