本文整理汇总了Java中com.sun.source.tree.BlockTree.getStatements方法的典型用法代码示例。如果您正苦于以下问题:Java BlockTree.getStatements方法的具体用法?Java BlockTree.getStatements怎么用?Java BlockTree.getStatements使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.sun.source.tree.BlockTree
的用法示例。
在下文中一共展示了BlockTree.getStatements方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: computeWarning
import com.sun.source.tree.BlockTree; //导入方法依赖的package包/类
@TriggerTreeKind(Tree.Kind.ENHANCED_FOR_LOOP)
@Messages("ERR_ForLoopToFunctionalHint=Can use functional operations")
public static ErrorDescription computeWarning(HintContext ctx) {
if (ctx.getInfo().getElements().getTypeElement("java.util.stream.Streams") == null && !DISABLE_CHECK_FOR_STREAM) return null;
PreconditionsChecker pc = new PreconditionsChecker(ctx.getPath().getLeaf(), ctx.getInfo());
if (pc.isSafeToRefactor()) {
EnhancedForLoopTree eflt = (EnhancedForLoopTree)ctx.getPath().getLeaf();
StatementTree stmt = eflt.getStatement();
if (stmt == null) {
return null;
}
if (stmt.getKind() == Tree.Kind.BLOCK) {
BlockTree bt = (BlockTree)stmt;
if (bt.getStatements() == null || bt.getStatements().isEmpty()) {
return null;
}
}
Fix fix = new FixImpl(ctx.getInfo(), ctx.getPath(), null).toEditorFix();
return ErrorDescriptionFactory.forName(ctx, ctx.getPath(), Bundle.ERR_ForLoopToFunctionalHint(), fix);
}
return null;
}
示例2: visitBlock
import com.sun.source.tree.BlockTree; //导入方法依赖的package包/类
public Boolean visitBlock(BlockTree node, ConstructorData p) {
List<? extends StatementTree> statements = new ArrayList<StatementTree>(node.getStatements());
for (int i = 0; i < statements.size(); i++) {
StatementTree st = statements.get(i);
if (st.getKind() == Kind.IF) {
IfTree it = (IfTree) st;
if (it.getElseStatement() == null && Utilities.exitsFromAllBranchers(info, new TreePath(new TreePath(getCurrentPath(), it), it.getThenStatement()))) {
generalizedIf(it.getCondition(), it.getThenStatement(), statements.subList(i + 1, statements.size()), false);
break;
}
}
scan(st, null);
}
return null;
}
示例3: findExactStatement
import com.sun.source.tree.BlockTree; //导入方法依赖的package包/类
private StatementTree findExactStatement(CompilationInfo info, BlockTree block, int offset, boolean start) {
if (offset == (-1)) return null;
SourcePositions sp = info.getTrees().getSourcePositions();
CompilationUnitTree cut = info.getCompilationUnit();
for (StatementTree t : block.getStatements()) {
long pos = start ? sp.getStartPosition(info.getCompilationUnit(), t) : sp.getEndPosition( cut, t);
if (offset == pos) {
return t;
}
}
return null;
}
示例4: findMatchingMethodInvocation
import com.sun.source.tree.BlockTree; //导入方法依赖的package包/类
private StatementTree findMatchingMethodInvocation(CompilationInfo info, BlockTree block, int offset) {
for (StatementTree t : block.getStatements()) {
if (t.getKind() != Kind.EXPRESSION_STATEMENT) continue;
long statementStart = info.getTrees().getSourcePositions().getStartPosition(info.getCompilationUnit(), t);
if (offset < statementStart) return null;
ExpressionStatementTree est = (ExpressionStatementTree) t;
long statementEnd = info.getTrees().getSourcePositions().getEndPosition(info.getCompilationUnit(), t);
long expressionEnd = info.getTrees().getSourcePositions().getEndPosition(info.getCompilationUnit(), est.getExpression());
if (expressionEnd <= offset && offset < statementEnd) {
return t;
}
}
return null;
}
示例5: visitBlock
import com.sun.source.tree.BlockTree; //导入方法依赖的package包/类
@Override
public Mirror visitBlock(BlockTree arg0, EvaluationContext evaluationContext) {
Mirror lastResult = null;
try {
evaluationContext.pushBlock();
for (StatementTree statementTree : arg0.getStatements()) {
Mirror res = statementTree.accept(this, evaluationContext);
if (res != null) {
lastResult = res;
}
if (res instanceof CommandMirror) {
break;
}
}
} finally {
evaluationContext.popBlock();
}
return lastResult;
}
示例6: getBlockListRepresentation
import com.sun.source.tree.BlockTree; //导入方法依赖的package包/类
private List<ProspectiveOperation> getBlockListRepresentation( StatementTree tree, boolean last) {
List<ProspectiveOperation> ls = new ArrayList<ProspectiveOperation>();
BlockTree blockTree = (BlockTree) tree;
List<? extends StatementTree> statements = blockTree.getStatements();
for ( int i = 0; i < statements.size(); i++) {
StatementTree statement = statements.get(i);
boolean l = last && i == statements.size() - 1;
if (statement.getKind() == Tree.Kind.IF) {
IfTree ifTree = (IfTree) statement;
if (isIfWithContinue(ifTree)) {
ifTree = refactorContinuingIf(ifTree, statements.subList(i + 1, statements.size()));
// the if was refactored, so that all the statements are nested in it, so it became
// the last (and single) statement within the parent
ls.addAll(this.getListRepresentation(ifTree, last));
break;
} else if (l) {
ls.addAll(this.getListRepresentation(ifTree, true));
} else {
if (this.isReturningIf(ifTree)) {
this.untrasformable = true;
}
ls.addAll(ProspectiveOperation.createOperator(ifTree, ProspectiveOperation.OperationType.MAP, preconditionsChecker, workingCopy));
}
} else {
ls.addAll(getListRepresentation(statement, l));
}
}
return ls;
}
示例7: safeInitByCalleeBefore
import com.sun.source.tree.BlockTree; //导入方法依赖的package包/类
/**
* computes those fields always initialized by callee safe init methods before a read operation
* (pathToRead) is invoked. See <a
* href="https://github.com/uber/NullAway/wiki/Error-Messages#initializer-method-does-not-guarantee-nonnull-field-is-initialized--nonnull-field--not-initialized">the
* docs</a> for what is considered a safe initializer method.
*/
private Set<Element> safeInitByCalleeBefore(
TreePath pathToRead, VisitorState state, TreePath enclosingBlockPath) {
Set<Element> result = new LinkedHashSet<>();
Set<Element> safeInitMethods = new LinkedHashSet<>();
Tree enclosingBlockOrMethod = enclosingBlockPath.getLeaf();
if (enclosingBlockOrMethod instanceof VariableTree) {
return Collections.emptySet();
}
BlockTree blockTree =
enclosingBlockOrMethod instanceof BlockTree
? (BlockTree) enclosingBlockOrMethod
: ((MethodTree) enclosingBlockOrMethod).getBody();
List<? extends StatementTree> statements = blockTree.getStatements();
Tree readExprTree = pathToRead.getLeaf();
int readStartPos = getStartPos((JCTree) readExprTree);
Symbol.ClassSymbol classSymbol = enclosingClassSymbol(enclosingBlockPath);
// bound loop at size-1 since the final statement cannot appear before the read
for (int i = 0; i < statements.size() - 1; i++) {
StatementTree curStmt = statements.get(i), nextStmt = statements.get(i + 1);
if (getStartPos((JCTree) nextStmt) <= readStartPos) {
Element privMethodElem = getInvokeOfSafeInitMethod(curStmt, classSymbol, state);
if (privMethodElem != null) {
safeInitMethods.add(privMethodElem);
}
}
}
addGuaranteedNonNullFromInvokes(
state,
Trees.instance(JavacProcessingEnvironment.instance(state.context)),
safeInitMethods,
getNullnessAnalysis(state),
result);
return result;
}
示例8: constructorInvokesAnother
import com.sun.source.tree.BlockTree; //导入方法依赖的package包/类
/** does the constructor invoke another constructor in the same class via this(...)? */
private boolean constructorInvokesAnother(MethodTree constructor, VisitorState state) {
BlockTree body = constructor.getBody();
List<? extends StatementTree> statements = body.getStatements();
if (statements.size() > 0) {
StatementTree statementTree = statements.get(0);
if (isThisCall(statementTree, state)) {
return true;
}
}
return false;
}
示例9: getSafeInitMethods
import com.sun.source.tree.BlockTree; //导入方法依赖的package包/类
/**
* @param blockTree block of statements
* @param state visitor state
* @return Elements of safe init methods that are invoked as top-level statements in the method
*/
private Set<Element> getSafeInitMethods(
BlockTree blockTree, Symbol.ClassSymbol classSymbol, VisitorState state) {
Set<Element> result = new LinkedHashSet<>();
List<? extends StatementTree> statements = blockTree.getStatements();
for (StatementTree stmt : statements) {
Element privMethodElem = getInvokeOfSafeInitMethod(stmt, classSymbol, state);
if (privMethodElem != null) {
result.add(privMethodElem);
}
}
return result;
}
示例10: testBlock
import com.sun.source.tree.BlockTree; //导入方法依赖的package包/类
private boolean testBlock(StringWriter writer, SourcePositions sp, String text, CompilationUnitTree cut, BlockTree blockTree) {
boolean success = true;
for (StatementTree st : blockTree.getStatements()) {
if (isLegal(st)) {
success &= testStatement(writer, sp, text, cut, st);
}
if (st instanceof IfTree) {
IfTree ifTree = (IfTree) st;
success &= testBranch(writer, sp, text, cut, ifTree.getThenStatement());
success &= testBranch(writer, sp, text, cut, ifTree.getElseStatement());
} else if (st instanceof WhileLoopTree) {
WhileLoopTree whileLoopTree = (WhileLoopTree) st;
success &= testBranch(writer, sp, text, cut, whileLoopTree.getStatement());
} else if (st instanceof DoWhileLoopTree) {
DoWhileLoopTree doWhileLoopTree = (DoWhileLoopTree) st;
success &= testBranch(writer, sp, text, cut, doWhileLoopTree.getStatement());
} else if (st instanceof ForLoopTree) {
ForLoopTree forLoopTree = (ForLoopTree) st;
success &= testBranch(writer, sp, text, cut, forLoopTree.getStatement());
} else if (st instanceof LabeledStatementTree) {
LabeledStatementTree labelTree = (LabeledStatementTree) st;
success &= testBranch(writer, sp, text, cut, labelTree.getStatement());
} else if (st instanceof SwitchTree) {
SwitchTree switchTree = (SwitchTree) st;
for (CaseTree caseTree : switchTree.getCases()) {
for (StatementTree statementTree : caseTree.getStatements()) {
success &= testBranch(writer, sp, text, cut, statementTree);
}
}
}
}
return success;
}
示例11: visitTry
import com.sun.source.tree.BlockTree; //导入方法依赖的package包/类
public @Override Void visitTry(TryTree tt, Void p) {
List<CatchTree> catches = new ArrayList<CatchTree>();
catches.addAll(tt.getCatches());
catches.addAll(createCatches(info, make, thandles, statement));
if (!streamAlike) {
info.rewrite(tt, make.Try(tt.getResources(), tt.getBlock(), catches, tt.getFinallyBlock()));
} else {
VariableTree originalDeclaration = (VariableTree) statement.getLeaf();
VariableTree declaration = make.Variable(make.Modifiers(EnumSet.noneOf(Modifier.class)), originalDeclaration.getName(), originalDeclaration.getType(), make.Literal(null));
StatementTree assignment = make.ExpressionStatement(make.Assignment(make.Identifier(originalDeclaration.getName()), originalDeclaration.getInitializer()));
List<StatementTree> finallyStatements = new ArrayList<StatementTree>(tt.getFinallyBlock() != null ? tt.getFinallyBlock().getStatements() : Collections.<StatementTree>emptyList());
finallyStatements.add(createFinallyCloseBlockStatement(originalDeclaration));
BlockTree finallyTree = make.Block(finallyStatements, false);
info.rewrite(originalDeclaration, assignment);
TryTree nueTry = make.Try(tt.getResources(), tt.getBlock(), catches, finallyTree);
TreePath currentBlockCandidate = statement;
while (currentBlockCandidate.getLeaf() != tt) {
currentBlockCandidate = currentBlockCandidate.getParentPath();
}
currentBlockCandidate = currentBlockCandidate.getParentPath();
if (currentBlockCandidate.getLeaf().getKind() == Kind.BLOCK) {
BlockTree originalTree = (BlockTree) currentBlockCandidate.getLeaf();
List<StatementTree> statements = new ArrayList<StatementTree>(originalTree.getStatements());
int index = statements.indexOf(tt);
statements.remove(index);
statements.add(index, nueTry);
statements.add(index, declaration);
info.rewrite(originalTree, make.Block(statements, originalTree.isStatic()));
} else {
BlockTree nueBlock = make.Block(Arrays.asList(declaration, nueTry), false);
info.rewrite(tt, nueBlock);
}
}
return null;
}