本文整理汇总了Java中com.sun.source.tree.BlockTree类的典型用法代码示例。如果您正苦于以下问题:Java BlockTree类的具体用法?Java BlockTree怎么用?Java BlockTree使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
BlockTree类属于com.sun.source.tree包,在下文中一共展示了BlockTree类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: visitBlock
import com.sun.source.tree.BlockTree; //导入依赖的package包/类
public Boolean visitBlock(BlockTree node, TreePath p) {
if (p == null) {
super.visitBlock(node, p);
return false;
}
if (p.getLeaf().getKind() != Kind.BLOCK) {
//single-statement blocks are considered to be equivalent to statements
//TODO: some parents may need to be more strict, esp. synchronized and do-while
assert node.getStatements().size() == 1;
assert !node.isStatic();
if (p.getLeaf() == searchingFor.getLeaf())
return false;
return checkLists(node.getStatements(), Collections.singletonList(p.getLeaf()), p.getParentPath());
}
BlockTree at = (BlockTree) p.getLeaf();
if (node.isStatic() != at.isStatic()) {
return false;
}
return checkLists(node.getStatements(), at.getStatements(), p);
}
示例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: createHashCodeMethod
import com.sun.source.tree.BlockTree; //导入依赖的package包/类
private static MethodTree createHashCodeMethod(WorkingCopy wc, Iterable<? extends VariableElement> hashCodeFields, Scope scope) {
TreeMaker make = wc.getTreeMaker();
Set<Modifier> mods = EnumSet.of(Modifier.PUBLIC);
int startNumber = generatePrimeNumber(2, 10);
int multiplyNumber = generatePrimeNumber(10, 100);
List<StatementTree> statements = new ArrayList<>();
//int hash = <startNumber>;
statements.add(make.Variable(make.Modifiers(EnumSet.noneOf(Modifier.class)), "hash", make.PrimitiveType(TypeKind.INT), make.Literal(startNumber))); //NOI18N
for (VariableElement ve : hashCodeFields) {
TypeMirror tm = ve.asType();
ExpressionTree variableRead = prepareExpression(wc, HASH_CODE_PATTERNS, tm, ve, scope);
statements.add(make.ExpressionStatement(make.Assignment(make.Identifier("hash"), make.Binary(Tree.Kind.PLUS, make.Binary(Tree.Kind.MULTIPLY, make.Literal(multiplyNumber), make.Identifier("hash")), variableRead)))); //NOI18N
}
statements.add(make.Return(make.Identifier("hash"))); //NOI18N
BlockTree body = make.Block(statements, false);
ModifiersTree modifiers = prepareModifiers(wc, mods,make);
return make.Method(modifiers, "hashCode", make.PrimitiveType(TypeKind.INT), Collections.<TypeParameterTree> emptyList(), Collections.<VariableTree>emptyList(), Collections.<ExpressionTree>emptyList(), body, null); //NOI18N
}
示例4: generateMethodContents
import com.sun.source.tree.BlockTree; //导入依赖的package包/类
private void generateMethodContents(List<StatementTree> methodStatements) {
Iterator<TypeMirrorHandle> additionalType = additionalLocalTypes.iterator();
Iterator<String> additionalName = additionalLocalNames.iterator();
while (additionalType.hasNext() && additionalName.hasNext()) {
TypeMirror tm = additionalType.next().resolve(copy);
if (tm == null) {
//XXX:
return;
}
Tree type = make.Type(tm);
methodStatements.add(make.Variable(make.Modifiers(EnumSet.noneOf(Modifier.class)), additionalName.next(), type, null));
}
if (from == to && statements.get(from).getKind() == Tree.Kind.BLOCK) {
methodStatements.addAll(((BlockTree) statements.get(from)).getStatements());
} else {
methodStatements.addAll(statements.subList(from, to + 1));
}
}
示例5: getBounds
import com.sun.source.tree.BlockTree; //导入依赖的package包/类
private int[] getBounds(TreeUtilities tu, SourcePositions sp, CompilationUnitTree cut, Tree tree) {
int[] bounds = {-1, -1};
if (tree != null) {
if (tree.getKind() == Tree.Kind.BLOCK) {
List<? extends StatementTree> stats = ((BlockTree) tree).getStatements();
if (stats != null && !stats.isEmpty()) {
bounds[0] = getStart(tu, sp, cut, stats.get(0));
bounds[1] = getEnd(tu, sp, cut, stats.get(stats.size() - 1));
}
} else {
bounds[0] = getStart(tu, sp, cut, tree);
bounds[1] = getEnd(tu, sp, cut, tree);
}
}
return bounds;
}
示例6: 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;
}
示例7: composeNewTestMethod
import com.sun.source.tree.BlockTree; //导入依赖的package包/类
/**
*/
protected MethodTree composeNewTestMethod(String testMethodName,
BlockTree testMethodBody,
List<ExpressionTree> throwsList,
WorkingCopy workingCopy) {
TreeMaker maker = workingCopy.getTreeMaker();
return maker.Method(
maker.Modifiers(createModifierSet(PUBLIC)),
testMethodName,
maker.PrimitiveType(TypeKind.VOID),
Collections.<TypeParameterTree>emptyList(),
Collections.<VariableTree>emptyList(),
throwsList,
testMethodBody,
null); //default value - used by annotations
}
示例8: createGetter
import com.sun.source.tree.BlockTree; //导入依赖的package包/类
private MethodTree createGetter(ModifiersTree mods, TypeMirror valueType) {
StringBuilder getterName = GeneratorUtils.getCapitalizedName(config.getName());
getterName.insert(0, valueType.getKind() == TypeKind.BOOLEAN ? "is" : "get");
ReturnTree returnTree = make.Return(make.MethodInvocation(Collections.EMPTY_LIST, make.MemberSelect(make.Identifier(config.getName()), hasGet ? "get" : "getValue"), Collections.EMPTY_LIST));
BlockTree getterBody = make.Block(Collections.singletonList(returnTree), false);
Tree valueTree;
if (valueType.getKind() == TypeKind.DECLARED) {
valueTree = make.QualIdent(((DeclaredType) valueType).asElement());
} else if (valueType.getKind().isPrimitive()) {
valueTree = make.PrimitiveType(valueType.getKind());
} else {
valueTree = make.Identifier(valueType.toString());
}
MethodTree getter = make.Method(mods, getterName, valueTree, Collections.EMPTY_LIST, Collections.EMPTY_LIST, Collections.EMPTY_LIST, getterBody, null);
return getter;
}
示例9: 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;
}
示例10: 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;
}
示例11: isLastInControlFlow
import com.sun.source.tree.BlockTree; //导入依赖的package包/类
private boolean isLastInControlFlow(TreePath pathToInstruction) {
Tree currentTree = pathToInstruction.getLeaf();
Tree parentTree = pathToInstruction.getParentPath().getLeaf();
if (parentTree.equals(this.loop)) {
return true;
} else if (parentTree.getKind() == Tree.Kind.BLOCK) {
List<? extends StatementTree> ls = ((BlockTree) parentTree).getStatements();
if (ls.get(ls.size() - 1).equals(currentTree)) {
return isLastInControlFlow(pathToInstruction.getParentPath());
} else {
return false;
}
} else if (parentTree.getKind() == Tree.Kind.AND.IF && ((IfTree) parentTree).getElseStatement() != null) {
return false;
} else {
return this.isLastInControlFlow(pathToInstruction.getParentPath());
}
}
示例12: findEnclosingMethodOrLambdaOrInitializer
import com.sun.source.tree.BlockTree; //导入依赖的package包/类
/**
* find the enclosing method, lambda expression or initializer block for the leaf of some tree
* path
*
* @param path the tree path
* @return the closest enclosing method / lambda
*/
@Nullable
public static TreePath findEnclosingMethodOrLambdaOrInitializer(TreePath path) {
while (path != null) {
if (path.getLeaf() instanceof MethodTree || path.getLeaf() instanceof LambdaExpressionTree) {
return path;
}
TreePath parent = path.getParentPath();
if (parent != null && parent.getLeaf() instanceof ClassTree) {
if (path.getLeaf() instanceof BlockTree) {
// found initializer block
return path;
}
if (path.getLeaf() instanceof VariableTree
&& ((VariableTree) path.getLeaf()).getInitializer() != null) {
// found field with an inline initializer
return path;
}
}
path = parent;
}
return null;
}
示例13: performRewrite
import com.sun.source.tree.BlockTree; //导入依赖的package包/类
@Override
protected void performRewrite(TransformationContext ctx) throws Exception {
Tree t = ctx.getPath().getLeaf();
if (t.getKind() != Tree.Kind.BLOCK) {
return;
}
BlockTree bl = (BlockTree)t;
WorkingCopy wc = ctx.getWorkingCopy();
GeneratorUtilities gu = GeneratorUtilities.get(wc);
gu.importComments(bl, wc.getCompilationUnit());
TreeMaker mk = wc.getTreeMaker();
BlockTree nbl = mk.Block(bl.getStatements(), true);
gu.copyComments(bl, nbl, true);
gu.copyComments(bl, nbl, false);
wc.rewrite(bl, nbl);
}
示例14: findOuterIf
import com.sun.source.tree.BlockTree; //导入依赖的package包/类
private static TreePath findOuterIf(HintContext ctx, TreePath treePath) {
while (!ctx.isCanceled()) {
treePath = treePath.getParentPath();
if (treePath == null) {
break;
}
Tree leaf = treePath.getLeaf();
if (leaf.getKind() == Kind.IF) {
return treePath;
}
if (leaf.getKind() == Kind.BLOCK) {
BlockTree b = (BlockTree)leaf;
if (b.getStatements().size() == 1) {
// ok, empty blocks can be around synchronized(this)
// statements
continue;
}
}
return null;
}
return null;
}
示例15: needsStaticRelativeTo
import com.sun.source.tree.BlockTree; //导入依赖的package包/类
static boolean needsStaticRelativeTo(CompilationInfo info, TreePath targetClass, TreePath occurrence) {
while (occurrence != null && targetClass.getLeaf() != occurrence.getLeaf()) {
switch (occurrence.getLeaf().getKind()) {
case METHOD:
if (((MethodTree) occurrence.getLeaf()).getModifiers().getFlags().contains(Modifier.STATIC)) {
return true;
}
break;
case BLOCK:
if (((BlockTree) occurrence.getLeaf()).isStatic()) {
return true;
}
break;
case INTERFACE:
return true;
}
occurrence = occurrence.getParentPath();
}
return false;
}