本文整理汇总了Java中com.sun.source.tree.LambdaExpressionTree.getBody方法的典型用法代码示例。如果您正苦于以下问题:Java LambdaExpressionTree.getBody方法的具体用法?Java LambdaExpressionTree.getBody怎么用?Java LambdaExpressionTree.getBody使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.sun.source.tree.LambdaExpressionTree
的用法示例。
在下文中一共展示了LambdaExpressionTree.getBody方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: matchLambdaExpression
import com.sun.source.tree.LambdaExpressionTree; //导入方法依赖的package包/类
@Override
public Description matchLambdaExpression(LambdaExpressionTree tree, VisitorState state) {
Symbol.MethodSymbol methodSymbol =
NullabilityUtil.getFunctionalInterfaceMethod(tree, state.getTypes());
handler.onMatchLambdaExpression(this, tree, state, methodSymbol);
Description description = checkParamOverriding(tree, tree.getParameters(), methodSymbol, true);
if (description != Description.NO_MATCH) {
return description;
}
if (tree.getBodyKind() == LambdaExpressionTree.BodyKind.EXPRESSION
&& methodSymbol.getReturnType().getKind() != TypeKind.VOID) {
ExpressionTree resExpr = (ExpressionTree) tree.getBody();
return checkReturnExpression(tree, resExpr, methodSymbol, state);
}
return Description.NO_MATCH;
}
示例2: computeLambdaRetrun
import com.sun.source.tree.LambdaExpressionTree; //导入方法依赖的package包/类
private static List<? extends TypeMirror> computeLambdaRetrun(Set<ElementKind> types, CompilationInfo info, TreePath parent, Tree error, int offset) {
LambdaExpressionTree let = (LambdaExpressionTree)parent.getLeaf();
if (let.getBody() != error) {
return null;
}
TreePath parentParent = parent.getParentPath();
TypeMirror m = info.getTrees().getTypeMirror(parentParent);
if (org.netbeans.modules.java.hints.errors.Utilities.isValidType(m)) {
return Collections.singletonList(m);
}
return resolveType(types, info, parentParent, let, offset, null, null);
}
示例3: performRewrite
import com.sun.source.tree.LambdaExpressionTree; //导入方法依赖的package包/类
@Override
protected void performRewrite(TransformationContext ctx) throws Exception {
final WorkingCopy copy = ctx.getWorkingCopy();
TypeMirror samType = copy.getTrees().getTypeMirror(ctx.getPath());
if (samType == null || samType.getKind() != TypeKind.DECLARED) {
// FIXME: report
return ;
}
LambdaExpressionTree lambda = (LambdaExpressionTree) ctx.getPath().getLeaf();
Tree tree = lambda.getBody();
if (tree.getKind() == Tree.Kind.BLOCK) {
if (((BlockTree)tree).getStatements().size() == 1) {
tree = ((BlockTree)tree).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;
}
} else {
return;
}
}
Tree changed = null;
if (tree.getKind() == Tree.Kind.METHOD_INVOCATION) {
changed = ConvertToLambdaConverter.methodInvocationToMemberReference(copy, tree, ctx.getPath(), lambda.getParameters(), false);
} else if (tree.getKind() == Tree.Kind.NEW_CLASS) {
changed = ConvertToLambdaConverter.newClassToConstructorReference(copy, tree, ctx.getPath(), lambda.getParameters(), false);
}
if (changed != null) {
copy.rewrite(lambda, changed);
}
}
示例4: run
import com.sun.source.tree.LambdaExpressionTree; //导入方法依赖的package包/类
@Override
public List<Fix> run(CompilationInfo compilationInfo, String diagnosticKey, int offset, TreePath treePath, Data<Void> data) {
TreePath method = null;
if (diagnosticKey != null && diagnosticKey.contains("/compiler.misc.incompatible.ret.type.in.lambda/")) { // NOI18N
// PENDING: when issue #258201 is implemented, use the new method instead of this HACK
offset++;
}
TreePath tp = compilationInfo.getTreeUtilities().pathFor(offset);
while (tp != null && !TreeUtilities.CLASS_TREE_KINDS.contains(tp.getLeaf().getKind())) {
Kind kind = tp.getLeaf().getKind();
if (kind == Kind.METHOD || kind == Kind.LAMBDA_EXPRESSION) {
method = tp;
break;
}
tp = tp.getParentPath();
}
if (method == null) {
return null;
}
if (method.getLeaf().getKind() == Kind.METHOD) {
MethodTree mt = (MethodTree) tp.getLeaf();
if (mt.getReturnType() == null) {
return null;
}
} else if (method.getLeaf().getKind() == Kind.LAMBDA_EXPRESSION) {
LambdaExpressionTree let = (LambdaExpressionTree)method.getLeaf();
TreePath bodyPath = new TreePath(method, let.getBody());
if (let.getBodyKind() == LambdaExpressionTree.BodyKind.EXPRESSION) {
TypeMirror m = compilationInfo.getTrees().getTypeMirror(
bodyPath);
if (m == null) {
return null;
}
if (m.getKind() == TypeKind.ERROR) {
m = compilationInfo.getTrees().getOriginalType((ErrorType)m);
}
if (m.getKind() != TypeKind.VOID) {
// do not offer to add return for something, which already has
// some type
return null;
}
} else if (Utilities.exitsFromAllBranchers(compilationInfo, bodyPath)) {
// do not add return, returns are already there.
return null;
}
}
List<Fix> result = new ArrayList<Fix>(2);
result.add(new FixImpl(compilationInfo.getSnapshot().getSource(), TreePathHandle.create(tp, compilationInfo)));
if (method.getLeaf().getKind() == Kind.METHOD) {
result.add(new ChangeMethodReturnType.FixImpl(compilationInfo, tp, TypeMirrorHandle.create(compilationInfo.getTypes().getNoType(TypeKind.VOID)), "void").toEditorFix());
}
return result;
}
示例5: lambda2MemberReference
import com.sun.source.tree.LambdaExpressionTree; //导入方法依赖的package包/类
@Hint(displayName="#DN_lambda2MemberReference", description="#DESC_lambda2MemberReference", category="suggestions", hintKind=Hint.Kind.ACTION,
minSourceVersion = "8")
@Messages({
"DN_lambda2MemberReference=Convert Lambda Expression to Member Reference",
"DESC_lambda2MemberReference=Converts lambda expressions to member references",
"ERR_lambda2MemberReference=Member reference can be used",
"FIX_lambda2MemberReference=Use member reference"
})
@TriggerTreeKind(Kind.LAMBDA_EXPRESSION)
public static ErrorDescription lambda2MemberReference(HintContext ctx) {
TypeMirror samType = ctx.getInfo().getTrees().getTypeMirror(ctx.getPath());
if (samType == null || samType.getKind() != TypeKind.DECLARED) {
return null;
}
LambdaExpressionTree lambda = (LambdaExpressionTree) ctx.getPath().getLeaf();
Tree tree = lambda.getBody();
if (tree == null) {
return null;
}
if (tree.getKind() == Tree.Kind.BLOCK) {
if (((BlockTree)tree).getStatements().size() == 1) {
tree = ((BlockTree)tree).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 null;
}
if (tree == null) {
return null;
}
} else {
return null;
}
}
if (tree.getKind() != Tree.Kind.METHOD_INVOCATION) {
return null;
}
boolean check = true;
Iterator<? extends VariableTree> paramsIt = lambda.getParameters().iterator();
ExpressionTree methodSelect = ((MethodInvocationTree)tree).getMethodSelect();
if (paramsIt.hasNext() && methodSelect.getKind() == Tree.Kind.MEMBER_SELECT) {
ExpressionTree expr = ((MemberSelectTree) methodSelect).getExpression();
if (expr.getKind() == Tree.Kind.IDENTIFIER) {
if (!((IdentifierTree)expr).getName().contentEquals(paramsIt.next().getName())) {
paramsIt = lambda.getParameters().iterator();
}
}
}
Iterator<? extends ExpressionTree> argsIt = ((MethodInvocationTree)tree).getArguments().iterator();
while (check && argsIt.hasNext() && paramsIt.hasNext()) {
ExpressionTree arg = argsIt.next();
if (arg.getKind() != Tree.Kind.IDENTIFIER || !paramsIt.next().getName().contentEquals(((IdentifierTree)arg).getName())) {
check = false;
}
}
if (!check || paramsIt.hasNext() || argsIt.hasNext()) {
return null;
}
return ErrorDescriptionFactory.forTree(ctx, ctx.getPath(), Bundle.ERR_lambda2MemberReference(), new Lambda2MemberReference(ctx.getInfo(), ctx.getPath()).toEditorFix());
}
示例6: getFix
import com.sun.source.tree.LambdaExpressionTree; //导入方法依赖的package包/类
private SuggestedFix getFix(Tree releaseStatement, Symbol wakelockSymbol, VisitorState state) {
// Wrap the release call line in a try/catch(RuntimeException) block.
String before = "\ntry {\n";
String after =
"\n} catch (RuntimeException unused) {\n"
+ "// Ignore: already released by timeout.\n"
+ "// TODO: Log this exception.\n"
+ "}\n";
// Lambda expressions are special. If the release call is in a one-expression lambda,
// only wrap body (not args) and convert to block lambda.
if (releaseStatement.getKind() == Kind.LAMBDA_EXPRESSION) {
LambdaExpressionTree enclosingLambda = (LambdaExpressionTree) releaseStatement;
if (enclosingLambda.getBodyKind() == BodyKind.EXPRESSION) {
releaseStatement = enclosingLambda.getBody();
before = "{" + before;
after = ";" + after + "}";
}
}
// Remove `if (wakelock.isHeld())` check.
// TODO(epmjohnston): can avoid this if no isHeld check in class (check call map).
IfTree enclosingIfHeld = findEnclosingNode(state.getPath(), IfTree.class);
if (enclosingIfHeld != null) {
ExpressionTree condition = ASTHelpers.stripParentheses(enclosingIfHeld.getCondition());
if (enclosingIfHeld.getElseStatement() == null
&& instanceMethod()
.onExactClass(WAKELOCK_CLASS_NAME)
.named("isHeld")
.matches(condition, state)
&& wakelockSymbol.equals(getSymbol(getReceiver(condition)))) {
String ifBody = state.getSourceForNode(enclosingIfHeld.getThenStatement()).trim();
// Remove leading and trailing `{}`
ifBody = ifBody.startsWith("{") ? ifBody.substring(1) : ifBody;
ifBody = ifBody.endsWith("}") ? ifBody.substring(0, ifBody.length() - 1) : ifBody;
ifBody = ifBody.trim();
String releaseStatementSource = state.getSourceForNode(releaseStatement);
return SuggestedFix.replace(
enclosingIfHeld,
ifBody.replace(releaseStatementSource, before + releaseStatementSource + after));
}
}
return SuggestedFix.builder()
.prefixWith(releaseStatement, before)
.postfixWith(releaseStatement, after)
.build();
}