本文整理汇总了Java中com.sun.source.tree.LambdaExpressionTree类的典型用法代码示例。如果您正苦于以下问题:Java LambdaExpressionTree类的具体用法?Java LambdaExpressionTree怎么用?Java LambdaExpressionTree使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
LambdaExpressionTree类属于com.sun.source.tree包,在下文中一共展示了LambdaExpressionTree类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: visitLambdaExpression
import com.sun.source.tree.LambdaExpressionTree; //导入依赖的package包/类
@Override
public Tree visitLambdaExpression(LambdaExpressionTree node, Element elementToFind) {
if (workingCopy.getTreeUtilities().isSynthetic(getCurrentPath())) {
return super.visitLambdaExpression(node, elementToFind);
}
Trees trees = workingCopy.getTrees();
Types types = workingCopy.getTypes();
TypeMirror type1 = trees.getTypeMirror(getCurrentPath());
if(type1 == null) {
return super.visitLambdaExpression(node, elementToFind);
}
type1 = types.erasure(type1);
TypeMirror type2 = elementToFind.asType();
type2 = types.erasure(type2);
if (types.isSameType(type1, type2) || (recursive && isSubtype(type1, type2))) {
addUsage(getCurrentPath());
}
return super.visitLambdaExpression(node, elementToFind);
}
示例2: performRewriteToLambda
import com.sun.source.tree.LambdaExpressionTree; //导入依赖的package包/类
public void performRewriteToLambda() {
LambdaExpressionTree lambdaTree = getLambdaTreeFromAnonymous(newClassTree, copy);
if (lambdaTree == null) {
return;
}
if (preconditionChecker.foundShadowedVariable()) {
TreePath pathToLambda = new TreePath(pathToNewClassTree, lambdaTree);
renameShadowedVariables(pathToLambda);
}
ExpressionTree convertedTree = lambdaTree;
if (preconditionChecker.needsCastToExpectedType()) {
convertedTree = getTreeWithCastPrepended(lambdaTree, newClassTree.getIdentifier());
}
copy.rewrite(newClassTree, convertedTree);
}
示例3: expression2Return
import com.sun.source.tree.LambdaExpressionTree; //导入依赖的package包/类
@Hint(displayName="#DN_expression2Return", description="#DESC_expression2Return", category="suggestions", hintKind=Hint.Kind.ACTION,
minSourceVersion = "8")
@Messages({
"DN_expression2Return=Convert Lambda Body to Use a Block",
"DESC_expression2Return=Converts lambda bodies to use blocks rather than expressions",
"ERR_expression2Return=",
"FIX_expression2Return=Use block as the lambda's body"
})
@TriggerPattern("($args$) -> $lambdaExpression")
public static ErrorDescription expression2Return(HintContext ctx) {
if (((LambdaExpressionTree) ctx.getPath().getLeaf()).getBodyKind() != BodyKind.EXPRESSION) {
return null;
}
TypeMirror lambdaExpressionType = ctx.getInfo().getTrees().getTypeMirror(ctx.getVariables().get("$lambdaExpression"));
String target = lambdaExpressionType == null || lambdaExpressionType.getKind() != TypeKind.VOID
? "($args$) -> { return $lambdaExpression; }"
: "($args$) -> { $lambdaExpression; }";
return ErrorDescriptionFactory.forTree(ctx, ctx.getPath(), Bundle.ERR_expression2Return(), JavaFixUtilities.rewriteFix(ctx, Bundle.FIX_expression2Return(), ctx.getPath(), target));
}
示例4: explicitParameterTypes
import com.sun.source.tree.LambdaExpressionTree; //导入依赖的package包/类
@Hint(displayName="#DN_addExplicitLambdaParameters", description="#DESC_addExplicitLambdaParameters", category="suggestions", hintKind=Hint.Kind.ACTION)
@Messages({
"DN_addExplicitLambdaParameters=Convert Lambda to Use Explicit Parameter Types",
"DESC_addExplicitLambdaParameters=Converts lambdas to use explicit parameter types",
"ERR_addExplicitLambdaParameters=",
"FIX_addExplicitLambdaParameters=Use explicit parameter types"
})
@TriggerTreeKind(Kind.LAMBDA_EXPRESSION)
public static ErrorDescription explicitParameterTypes(HintContext ctx) {
LambdaExpressionTree let = (LambdaExpressionTree) ctx.getPath().getLeaf();
boolean hasSyntheticParameterName = false;
for (VariableTree var : let.getParameters()) {
hasSyntheticParameterName |= var.getType() == null || ctx.getInfo().getTreeUtilities().isSynthetic(TreePath.getPath(ctx.getPath(), var.getType()));
}
if (!hasSyntheticParameterName) {
return null;
}
return ErrorDescriptionFactory.forName(ctx, ctx.getPath(), Bundle.ERR_addExplicitLambdaParameters(), new AddExplicitLambdaParameterTypes(ctx.getInfo(), ctx.getPath()).toEditorFix());
}
示例5: findEnclosingMethodOrLambdaOrInitializer
import com.sun.source.tree.LambdaExpressionTree; //导入依赖的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;
}
示例6: onMatchLambdaExpression
import com.sun.source.tree.LambdaExpressionTree; //导入依赖的package包/类
@Override
public void onMatchLambdaExpression(
NullAway analysis,
LambdaExpressionTree tree,
VisitorState state,
Symbol.MethodSymbol methodSymbol) {
if (filterMethodOrLambdaSet.contains(tree)
&& tree.getBodyKind().equals(LambdaExpressionTree.BodyKind.EXPRESSION)) {
expressionBodyToFilterLambda.put((ExpressionTree) tree.getBody(), tree);
// Single expression lambda, onMatchReturn will not be triggered, force the dataflow analysis
// here
AccessPathNullnessAnalysis nullnessAnalysis = analysis.getNullnessAnalysis(state);
nullnessAnalysis.forceRunOnMethod(state.getPath(), state.context);
}
if (mapToFilterMap.containsKey(tree)) {
bodyToMethodOrLambda.put(tree.getBody(), tree);
}
}
示例7: onMatchReturn
import com.sun.source.tree.LambdaExpressionTree; //导入依赖的package包/类
@Override
public void onMatchReturn(NullAway analysis, ReturnTree tree, VisitorState state) {
// Figure out the enclosing method node
TreePath enclosingMethodOrLambda =
NullabilityUtil.findEnclosingMethodOrLambdaOrInitializer(state.getPath());
if (enclosingMethodOrLambda == null) {
throw new RuntimeException("no enclosing method, lambda or initializer!");
}
if (!(enclosingMethodOrLambda.getLeaf() instanceof MethodTree
|| enclosingMethodOrLambda.getLeaf() instanceof LambdaExpressionTree)) {
throw new RuntimeException(
"return statement outside of a method or lambda! (e.g. in an initializer block)");
}
Tree leaf = enclosingMethodOrLambda.getLeaf();
if (filterMethodOrLambdaSet.contains(leaf)) {
returnToEnclosingMethodOrLambda.put(tree, leaf);
// We need to manually trigger the dataflow analysis to run on the filter method,
// this ensures onDataflowVisitReturn(...) gets called for all return statements in this
// method before
// onDataflowInitialStore(...) is called for all successor methods in the observable chain.
// Caching should prevent us from re-analyzing any given method.
AccessPathNullnessAnalysis nullnessAnalysis = analysis.getNullnessAnalysis(state);
nullnessAnalysis.forceRunOnMethod(new TreePath(state.getPath(), leaf), state.context);
}
}
示例8: onDataflowVisitReturn
import com.sun.source.tree.LambdaExpressionTree; //导入依赖的package包/类
@Override
public void onDataflowVisitReturn(
ReturnTree tree, NullnessStore<Nullness> thenStore, NullnessStore<Nullness> elseStore) {
if (returnToEnclosingMethodOrLambda.containsKey(tree)) {
Tree filterTree = returnToEnclosingMethodOrLambda.get(tree);
assert (filterTree instanceof MethodTree || filterTree instanceof LambdaExpressionTree);
ExpressionTree retExpression = tree.getExpression();
if (canBooleanExpressionEvalToTrue(retExpression)) {
if (filterToNSMap.containsKey(filterTree)) {
filterToNSMap.put(filterTree, filterToNSMap.get(filterTree).leastUpperBound(thenStore));
} else {
filterToNSMap.put(filterTree, thenStore);
}
}
}
}
示例9: 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;
}
示例10: relevantInitializerMethodOrBlock
import com.sun.source.tree.LambdaExpressionTree; //导入依赖的package包/类
private boolean relevantInitializerMethodOrBlock(
TreePath enclosingBlockPath, VisitorState state) {
Tree methodLambdaOrBlock = enclosingBlockPath.getLeaf();
if (methodLambdaOrBlock instanceof LambdaExpressionTree) {
return false;
} else if (methodLambdaOrBlock instanceof MethodTree) {
MethodTree methodTree = (MethodTree) methodLambdaOrBlock;
if (isConstructor(methodTree) && !constructorInvokesAnother(methodTree, state)) return true;
if (ASTHelpers.getSymbol(methodTree).isStatic()) {
Set<MethodTree> staticInitializerMethods =
class2Entities.get(enclosingClassSymbol(enclosingBlockPath)).staticInitializerMethods();
return staticInitializerMethods.size() == 1
&& staticInitializerMethods.contains(methodTree);
} else {
Set<MethodTree> instanceInitializerMethods =
class2Entities
.get(enclosingClassSymbol(enclosingBlockPath))
.instanceInitializerMethods();
return instanceInitializerMethods.size() == 1
&& instanceInitializerMethods.contains(methodTree);
}
} else {
// initializer or field declaration
return true;
}
}
示例11: findEnclosingMethodOrLambdaOrInitializer
import com.sun.source.tree.LambdaExpressionTree; //导入依赖的package包/类
private static <T> TreePath findEnclosingMethodOrLambdaOrInitializer(TreePath path) {
while (path != null) {
if (path.getLeaf() instanceof MethodTree) {
return path;
}
TreePath parent = path.getParentPath();
if (parent != null) {
if (parent.getLeaf() instanceof ClassTree) {
if (path.getLeaf() instanceof BlockTree) {
// this is a class or instance initializer block
return path;
}
if (path.getLeaf() instanceof VariableTree
&& ((VariableTree) path.getLeaf()).getInitializer() != null) {
// this is a field with an inline initializer
return path;
}
}
if (parent.getLeaf() instanceof LambdaExpressionTree) {
return parent;
}
}
path = parent;
}
return null;
}
示例12: renderBlock
import com.sun.source.tree.LambdaExpressionTree; //导入依赖的package包/类
/**
* Renders a ruby block with curly brace syntax.
*/
private void renderBlock(LambdaExpressionTree.BodyKind bodyKind, List<TypeInfo> parameterTypes, List<String> parameterNames, CodeModel body, CodeWriter writer) {
append("{");
if (parameterNames.size() > 0) {
append(" |");
for (int i = 0; i < parameterNames.size(); i++) {
if (i > 0) {
append(",");
}
append(parameterNames.get(i));
}
append("|");
}
append("\n");
indent();
body.render(this);
if (bodyKind == LambdaExpressionTree.BodyKind.EXPRESSION) {
append("\n");
}
unindent();
append("}");
}
示例13: renderLambda
import com.sun.source.tree.LambdaExpressionTree; //导入依赖的package包/类
@Override
public void renderLambda(LambdaExpressionTree.BodyKind bodyKind, List<TypeInfo> parameterTypes, List<String> parameterNames, CodeModel body){
append("(");
IntStream.range(0, parameterNames.size()).forEach(i -> {
if(i > 0) append(", ");
append(parameterNames.get(i));
append(": ");
append(parameterTypes.get(i).translateName("scala"));
});
append(") => {\n");
indent();
body.render(this);
if (bodyKind == LambdaExpressionTree.BodyKind.EXPRESSION) append("\n");
unindent();
append("}");
}
示例14: renderLambda
import com.sun.source.tree.LambdaExpressionTree; //导入依赖的package包/类
@Override
public void renderLambda(LambdaExpressionTree.BodyKind bodyKind, List<TypeInfo> parameterTypes, List<String> parameterNames, CodeModel body) {
append("{");
for (int i = 0; i < parameterNames.size(); i++) {
if (i == 0) {
append(" ");
} else {
append(", ");
}
append(parameterNames.get(i));
}
append(" ->\n");
indent();
body.render(this);
if (bodyKind == LambdaExpressionTree.BodyKind.EXPRESSION) {
append("\n");
}
unindent();
append("}");
}
示例15: renderLambda
import com.sun.source.tree.LambdaExpressionTree; //导入依赖的package包/类
@Override
public void renderLambda(LambdaExpressionTree.BodyKind bodyKind, List<TypeInfo> parameterTypes, List<String> parameterNames, CodeModel body) {
append("function (");
for (int i = 0; i < parameterNames.size(); i++) {
if (i > 0) {
append(", ");
}
append(parameterNames.get(i));
}
append(") {\n");
indent();
body.render(this);
if (bodyKind == LambdaExpressionTree.BodyKind.EXPRESSION) {
append(";\n");
}
unindent();
append("}");
}