本文整理汇总了Java中com.sun.source.tree.ExpressionStatementTree类的典型用法代码示例。如果您正苦于以下问题:Java ExpressionStatementTree类的具体用法?Java ExpressionStatementTree怎么用?Java ExpressionStatementTree使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ExpressionStatementTree类属于com.sun.source.tree包,在下文中一共展示了ExpressionStatementTree类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: addSyntheticTrees
import com.sun.source.tree.ExpressionStatementTree; //导入依赖的package包/类
private void addSyntheticTrees(DiffContext diffContext, Tree node) {
if (node == null) return ;
if (((JCTree) node).pos == (-1)) {
diffContext.syntheticTrees.add(node);
return ;
}
if (node.getKind() == Kind.EXPRESSION_STATEMENT) {
ExpressionTree est = ((ExpressionStatementTree) node).getExpression();
if (est.getKind() == Kind.METHOD_INVOCATION) {
ExpressionTree select = ((MethodInvocationTree) est).getMethodSelect();
if (select.getKind() == Kind.IDENTIFIER && ((IdentifierTree) select).getName().contentEquals("super")) {
if (getTreeUtilities().isSynthetic(diffContext.origUnit, node)) {
diffContext.syntheticTrees.add(node);
}
}
}
}
}
示例2: testVariableInitWrapped
import com.sun.source.tree.ExpressionStatementTree; //导入依赖的package包/类
public void testVariableInitWrapped() throws Exception {
String code = "package hierbas.del.litoral;\n\n" +
"import java.util.concurrent.atomic.AtomicBoolean;\n\n" +
"public class Test {\n" +
" public void t() {\n" +
" new AtomicBoolean();\n" +
" }\n" +
"}\n";
runWrappingTest(code, 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);
ExpressionStatementTree init = (ExpressionStatementTree) method.getBody().getStatements().get(0);
VariableTree nue = make.Variable(make.Modifiers(EnumSet.noneOf(Modifier.class)), "ab", make.Type("java.util.concurrent.atomic.AtomicBoolean"), init.getExpression());
workingCopy.rewrite(init, nue);
}
}, FmtOptions.wrapAssignOps, WrapStyle.WRAP_IF_LONG.name());
}
示例3: testWrapAssignment
import com.sun.source.tree.ExpressionStatementTree; //导入依赖的package包/类
public void testWrapAssignment() throws Exception {
String code = "package hierbas.del.litoral;\n\n" +
"import java.util.concurrent.atomic.AtomicBoolean;\n\n" +
"public class Test {\n" +
" public void t(AtomicBoolean ab) {\n" +
" new AtomicBoolean();\n" +
" }\n" +
"}\n";
runWrappingTest(code, 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);
ExpressionStatementTree init = (ExpressionStatementTree) method.getBody().getStatements().get(0);
AssignmentTree bt = make.Assignment(make.Identifier("ab"), init.getExpression());
workingCopy.rewrite(init, make.ExpressionStatement(bt));
}
}, FmtOptions.wrapAssignOps, WrapStyle.WRAP_IF_LONG.name());
}
示例4: generateDefMethodBody
import com.sun.source.tree.ExpressionStatementTree; //导入依赖的package包/类
/**
* Returns default body for the test method. The generated body will
* contains the following lines:
* <pre><code>
* // TODO review the generated test code and remove the default call to fail.
* fail("The test case is a prototype.");
* </code></pre>
* @param maker the tree maker
* @return an {@literal ExpressionStatementTree} for the generated body.
* @throws MissingResourceException
* @throws IllegalStateException
*/
private ExpressionStatementTree generateDefMethodBody(TreeMaker maker)
throws MissingResourceException, IllegalStateException {
String failMsg = NbBundle.getMessage(TestCreator.class,
"TestCreator.variantMethods.defaultFailMsg");
MethodInvocationTree failMethodCall =
maker.MethodInvocation(
Collections.<ExpressionTree>emptyList(),
maker.Identifier("fail"),
Collections.<ExpressionTree>singletonList(
maker.Literal(failMsg)));
ExpressionStatementTree exprStatement =
maker.ExpressionStatement(failMethodCall);
if (setup.isGenerateMethodBodyComment()) {
Comment comment =
Comment.create(Comment.Style.LINE, -2, -2, -2,
NbBundle.getMessage(AbstractTestGenerator.class,
"TestCreator.variantMethods.defaultComment"));
maker.addComment(exprStatement, comment, true);
}
return exprStatement;
}
示例5: performRewriteToMemberReference
import com.sun.source.tree.ExpressionStatementTree; //导入依赖的package包/类
public void performRewriteToMemberReference() {
MethodTree methodTree = getMethodFromFunctionalInterface(newClassTree);
if (methodTree.getBody() == null || methodTree.getBody().getStatements().size() != 1)
return;
Tree tree = methodTree.getBody().getStatements().get(0);
if (tree.getKind() == Tree.Kind.EXPRESSION_STATEMENT) {
tree = ((ExpressionStatementTree)tree).getExpression();
} else if (tree.getKind() == Tree.Kind.RETURN) {
tree = ((ReturnTree)tree).getExpression();
} else {
return;
}
Tree changed = null;
if (tree.getKind() == Tree.Kind.METHOD_INVOCATION) {
changed = methodInvocationToMemberReference(copy, tree, pathToNewClassTree, methodTree.getParameters(),
preconditionChecker.needsCastToExpectedType());
} else if (tree.getKind() == Tree.Kind.NEW_CLASS) {
changed = newClassToConstructorReference(copy, tree, pathToNewClassTree, methodTree.getParameters(), preconditionChecker.needsCastToExpectedType());
}
if (changed != null) {
copy.rewrite(newClassTree, changed);
}
}
示例6: beautifyAssignement
import com.sun.source.tree.ExpressionStatementTree; //导入依赖的package包/类
private void beautifyAssignement(Tree currentTree, Set<Name> needed) {
AssignmentTree assigned = (AssignmentTree) ((ExpressionStatementTree) currentTree).getExpression();
ExpressionTree variable = assigned.getVariable();
if (variable.getKind() == Tree.Kind.IDENTIFIER) {
IdentifierTree id = (IdentifierTree) variable;
if (needed.contains(id.getName())) {
this.correspondingTree = treeMaker.ExpressionStatement(assigned.getExpression());
} else {
this.correspondingTree = this.addReturn(castToStatementTree(currentTree), getOneFromSet(needed));
}
} else {
this.correspondingTree = this.addReturn(castToStatementTree(currentTree), getOneFromSet(needed));
}
}
示例7: hint
import com.sun.source.tree.ExpressionStatementTree; //导入依赖的package包/类
@TriggerPattern("$method($params$);")
public static ErrorDescription hint(HintContext ctx) {
Element invoked = ctx.getInfo().getTrees().getElement(new TreePath(ctx.getPath(), ((ExpressionStatementTree) ctx.getPath().getLeaf()).getExpression()));
if (invoked == null || invoked.getKind() != ElementKind.METHOD || ((ExecutableElement) invoked).getReturnType().getKind() == TypeKind.VOID) return null;
boolean found = false;
for (AnnotationMirror am : invoked.getAnnotationMirrors()) {
String simpleName = am.getAnnotationType().asElement().getSimpleName().toString();
if ("CheckReturnValue".equals(simpleName)) {
found = true;
break;
}
}
if (!found && !checkReturnValueForJDKMethods((ExecutableElement) invoked)) return null;
String displayName = NbBundle.getMessage(CheckReturnValueHint.class, "ERR_org.netbeans.modules.java.hints.bugs.CheckReturnValueHint");
return ErrorDescriptionFactory.forName(ctx, ctx.getPath(), displayName);
}
示例8: findMatchingMethodInvocation
import com.sun.source.tree.ExpressionStatementTree; //导入依赖的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;
}
示例9: isFirstStatementThisOrSuperCall
import com.sun.source.tree.ExpressionStatementTree; //导入依赖的package包/类
private static boolean isFirstStatementThisOrSuperCall(@NotNull JCTree.JCBlock body) {
List<JCTree.JCStatement> statements = body.getStatements();
if (statements.isEmpty()) {
return false;
}
JCTree.JCStatement expressionCandidate = statements.get(0);
if (expressionCandidate instanceof ExpressionStatementTree) {
ExpressionStatementTree expression = (ExpressionStatementTree) expressionCandidate;
ExpressionTree methodInvocationCandidate = expression.getExpression();
if (methodInvocationCandidate instanceof MethodInvocationTree) {
MethodInvocationTree methodInvocation = (MethodInvocationTree) methodInvocationCandidate;
ExpressionTree methodSelect = methodInvocation.getMethodSelect();
if (methodSelect != null) {
String select = methodSelect.toString();
return "this".equals(select) || "super".equals(select);
}
}
}
return false;
}
示例10: literalReplacement
import com.sun.source.tree.ExpressionStatementTree; //导入依赖的package包/类
private List<Fix> literalReplacement(
MethodInvocationTree methodInvocationTree, VisitorState state, ExpressionTree lhs) {
Tree parent = state.getPath().getParentPath().getLeaf();
// If the parent is an ExpressionStatement, the expression value is ignored, so we can delete
// the call entirely (or replace removeAll with .clear()). Otherwise, we can't provide a good
// replacement.
if (parent instanceof ExpressionStatementTree) {
Fix fix;
if (instanceMethod().anyClass().named("removeAll").matches(methodInvocationTree, state)) {
fix = SuggestedFix.replace(methodInvocationTree, lhs + ".clear()");
} else {
fix = SuggestedFix.delete(parent);
}
return ImmutableList.of(fix);
}
return ImmutableList.of();
}
示例11: buildFix
import com.sun.source.tree.ExpressionStatementTree; //导入依赖的package包/类
protected static SuggestedFix buildFix(
VisitorState state,
SuggestedFix.Builder fix,
JCExpression expectedException,
List<? extends StatementTree> statements) {
fix.addStaticImport("org.junit.Assert.assertThrows");
StringBuilder prefix = new StringBuilder();
prefix.append(
String.format("assertThrows(%s, () -> ", state.getSourceForNode(expectedException)));
if (statements.size() == 1 && getOnlyElement(statements) instanceof ExpressionStatementTree) {
ExpressionTree expression =
((ExpressionStatementTree) getOnlyElement(statements)).getExpression();
fix.prefixWith(expression, prefix.toString());
fix.postfixWith(expression, ")");
} else {
prefix.append(" {");
fix.prefixWith(statements.iterator().next(), prefix.toString());
fix.postfixWith(getLast(statements), "});");
}
return fix.build();
}
示例12: rethrowFix
import com.sun.source.tree.ExpressionStatementTree; //导入依赖的package包/类
private Optional<Fix> rethrowFix(ImmutableList<CatchTree> catchBlocks, VisitorState state) {
SuggestedFix.Builder fix = SuggestedFix.builder();
catchBlocks.forEach(
c -> {
// e.g.
// fail("message") -> throw new AssertionError("message", cause);
// assertWithMessage("message format %s", 42) ->
// throw new AssertionError(String.format("message format %s", 42), cause);
StatementTree statementTree = getOnlyElement(c.getBlock().getStatements());
MethodInvocationTree methodInvocationTree =
(MethodInvocationTree) ((ExpressionStatementTree) statementTree).getExpression();
String message = null;
if (message == null && !methodInvocationTree.getArguments().isEmpty()) {
message = getMessageOrFormat(methodInvocationTree, state);
}
if (message != null) {
// only catch and rethrow to add additional context, not for raw `fail()` calls
fix.replace(
statementTree,
String.format(
"throw new AssertionError(%s, %s);", message, c.getParameter().getName()));
}
});
return fix.isEmpty() ? Optional.empty() : Optional.of(fix.build());
}
示例13: abortInCatch
import com.sun.source.tree.ExpressionStatementTree; //导入依赖的package包/类
private boolean abortInCatch(CatchTree catchTree, VisitorState state) {
List<? extends StatementTree> statements = catchTree.getBlock().getStatements();
if (statements.isEmpty()) {
return false;
}
StatementTree lastStmt = statements.get(statements.size() - 1);
if (lastStmt.getKind() == EXPRESSION_STATEMENT) {
ExpressionTree et = ((ExpressionStatementTree) lastStmt).getExpression();
Symbol sym = ASTHelpers.getSymbol(et);
if (sym == null || !(sym instanceof MethodSymbol)) {
return false;
}
String methodName = sym.getQualifiedName().toString();
String className = sym.owner.getQualifiedName().toString();
// System.out.println("DEBUG: method: " + methodName + ", className: " + className);
if (methodName.contains("abort") ||
methodName.contains("shutdown") ||
(methodName.equals("exit") && className.equals("java.lang.System"))) {
return true;
}
}
return false;
}
示例14: unhandledExceptionsAreReportedWithoutBugParadeLink
import com.sun.source.tree.ExpressionStatementTree; //导入依赖的package包/类
@Test
public void unhandledExceptionsAreReportedWithoutBugParadeLink() throws Exception {
@BugPattern(name = "", explanation = "", summary = "",
maturity = EXPERIMENTAL, severity = ERROR, category = ONE_OFF)
class Throwing extends BugChecker implements ExpressionStatementTreeMatcher {
@Override
public Description matchExpressionStatement(ExpressionStatementTree tree, VisitorState state)
{
throw new IllegalStateException("test123");
}
}
compilerBuilder.report(new ErrorProneScanner(new Throwing()));
compiler = compilerBuilder.build();
int exitCode = compiler.compile(
sources(getClass(), "MultipleTopLevelClassesWithErrors.java",
"ExtendedMultipleTopLevelClassesWithErrors.java"));
outputStream.flush();
assertThat(outputStream.toString(), exitCode, is(1));
Matcher<Iterable<Diagnostic<JavaFileObject>>> matcher = hasItem(
diagnosticMessage(CoreMatchers.<String>allOf(
containsString("IllegalStateException: test123"),
containsString("unhandled exception was thrown by the Error Prone"))));
assertTrue("Error should be reported. " + diagnosticHelper.describe(),
matcher.matches(diagnosticHelper.getDiagnostics()));
}
示例15: containsThisConstructorInvocation
import com.sun.source.tree.ExpressionStatementTree; //导入依赖的package包/类
/**
* @return true if the first statement in the body is a self constructor
* invocation within a constructor
*/
public static final boolean containsThisConstructorInvocation(MethodTree node) {
if (!TreeUtils.isConstructor(node)
|| node.getBody().getStatements().isEmpty())
return false;
StatementTree st = node.getBody().getStatements().get(0);
if (!(st instanceof ExpressionStatementTree)
|| !(((ExpressionStatementTree)st).getExpression() instanceof MethodInvocationTree))
return false;
MethodInvocationTree invocation = (MethodInvocationTree)
((ExpressionStatementTree)st).getExpression();
return "this".contentEquals(TreeUtils.methodName(invocation));
}