下面列出了com.sun.source.tree.ThrowTree#com.sun.source.tree.MemberReferenceTree 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public Boolean visitMemberReference(MemberReferenceTree node, TreePath p) {
if (p == null)
return super.visitMemberReference(node, p);
MemberReferenceTree t = (MemberReferenceTree) p.getLeaf();
if (!node.getMode().equals(t.getMode()))
return false;
if (!scan(node.getQualifierExpression(), t.getQualifierExpression(), p))
return false;
String ident = t.getName().toString();
if (ident.startsWith("$")) { //XXX: there should be a utility method for this check
if (bindState.variables2Names.containsKey(ident)) {
return node.getName().contentEquals(bindState.variables2Names.get(ident));
} else {
bindState.variables2Names.put(ident, node.getName().toString());
}
return true;
}
return node.getName().contentEquals(t.getName());
}
@Override
public Void visitMemberReference(MemberReferenceTree node, Void p) {
Element el = info.getTrees().getElement(getCurrentPath());
if (toFind.equals(el)) {
try {
int[] span = treeUtils.findNameSpan(node);
if(span != null) {
MutablePositionRegion region = createRegion(doc, span[0], span[1]);
usages.add(region);
}
} catch (BadLocationException ex) {
Exceptions.printStackTrace(ex);
}
}
return super.visitMemberReference(node, p);
}
@Override
public Void visitMemberReference(MemberReferenceTree node, Void unused) {
sync(node);
builder.open(plusFour);
scan(node.getQualifierExpression(), null);
builder.breakOp();
builder.op("::");
addTypeArguments(node.getTypeArguments(), plusFour);
switch (node.getMode()) {
case INVOKE:
visit(node.getName());
break;
case NEW:
token("new");
break;
default:
throw new AssertionError(node.getMode());
}
builder.close();
return null;
}
@Override
public CodeModel visitMemberReference(MemberReferenceTree node, VisitContext p) {
if (node.getMode() == MemberReferenceTree.ReferenceMode.INVOKE) {
JCTree.JCMemberReference refTree = (JCTree.JCMemberReference) node;
ExecutableElement method = (ExecutableElement) refTree.sym;
MethodSignature signature = createMethodSignature(method, false);
ExpressionModel expression = scan(node.getQualifierExpression(), p);
if (expression instanceof ThisModel) {
p.getReferencedMethods().add(node.getName().toString());
}
ExpressionModel methodReferenceExpression = expression.onMethodReference(signature);
return methodReferenceExpression;
} else {
throw new UnsupportedOperationException("New reference not implemented yet");
}
}
public static KindName kindName(MemberReferenceTree.ReferenceMode mode) {
switch (mode) {
case INVOKE: return KindName.METHOD;
case NEW: return KindName.CONSTRUCTOR;
default : throw new AssertionError("Unexpected mode: "+ mode);
}
}
public static KindName kindName(MemberReferenceTree.ReferenceMode mode) {
switch (mode) {
case INVOKE: return KindName.METHOD;
case NEW: return KindName.CONSTRUCTOR;
default : throw new AssertionError("Unexpected mode: "+ mode);
}
}
public static KindName kindName(MemberReferenceTree.ReferenceMode mode) {
switch (mode) {
case INVOKE: return KindName.METHOD;
case NEW: return KindName.CONSTRUCTOR;
default : throw new AssertionError("Unexpected mode: "+ mode);
}
}
public static KindName kindName(MemberReferenceTree.ReferenceMode mode) {
switch (mode) {
case INVOKE: return KindName.METHOD;
case NEW: return KindName.CONSTRUCTOR;
default : throw new AssertionError("Unexpected mode: "+ mode);
}
}
@Override
public Tree visitMemberReference(MemberReferenceTree tree, Void p) {
MemberReferenceTree n = make.MemberReference(tree.getMode(), tree.getName(), tree.getQualifierExpression(), (List<ExpressionTree>)tree.getTypeArguments());
model.setType(n, model.getType(tree));
comments.copyComments(tree, n);
model.setPos(n, model.getPos(tree));
return n;
}
public void testMethodReferenceDiff() throws Exception {
testFile = new File(getWorkDir(), "Test.java");
TestUtilities.copyStringToFile(testFile,
"package hierbas.del.litoral;\n\n" +
"public class Test {\n" +
" public static void taragui() {\n" +
" Runnable r = hierbas.del.litoral.Test :: taragui;\n" +
" }\n" +
"}\n"
);
String golden =
"package hierbas.del.litoral;\n\n" +
"public class Test {\n" +
" public static void taragui() {\n" +
" Runnable r = Test :: taragui;\n" +
" }\n" +
"}\n";
JavaSource src = getJavaSource(testFile);
Task<WorkingCopy> task = new Task<WorkingCopy>() {
public void run(final WorkingCopy workingCopy) throws IOException {
workingCopy.toPhase(Phase.RESOLVED);
final TreeMaker make = workingCopy.getTreeMaker();
new ErrorAwareTreeScanner<Void, Void>() {
@Override public Void visitMemberReference(MemberReferenceTree node, Void p) {
workingCopy.rewrite(node, make.MemberReference(node.getMode(), make.Identifier("Test"), node.getName(), node.getTypeArguments()));
return super.visitMemberReference(node, p);
}
}.scan(workingCopy.getCompilationUnit(), null);
}
};
src.runModificationTask(task).commit();
String res = TestUtilities.copyFileToString(testFile);
//System.err.println(res);
assertEquals(golden, res);
}
public void testMethodReferenceNameDiff() throws Exception {
testFile = new File(getWorkDir(), "Test.java");
TestUtilities.copyStringToFile(testFile,
"package hierbas.del.litoral;\n\n" +
"public class Test {\n" +
" public static void taragui() {\n" +
" Runnable r = Test :: taragui;\n" +
" }\n" +
"}\n"
);
String golden =
"package hierbas.del.litoral;\n\n" +
"public class Test {\n" +
" public static void taragui() {\n" +
" Runnable r = Test :: taragui2;\n" +
" }\n" +
"}\n";
JavaSource src = getJavaSource(testFile);
Task<WorkingCopy> task = new Task<WorkingCopy>() {
public void run(final WorkingCopy workingCopy) throws IOException {
workingCopy.toPhase(Phase.RESOLVED);
final TreeMaker make = workingCopy.getTreeMaker();
new ErrorAwareTreeScanner<Void, Void>() {
@Override public Void visitMemberReference(MemberReferenceTree node, Void p) {
workingCopy.rewrite(node, make.setLabel(node, "taragui2"));
return super.visitMemberReference(node, p);
}
}.scan(workingCopy.getCompilationUnit(), null);
}
};
src.runModificationTask(task).commit();
String res = TestUtilities.copyFileToString(testFile);
//System.err.println(res);
assertEquals(golden, res);
}
public void testMethodReferenceFirstTypeParam() throws Exception {
testFile = new File(getWorkDir(), "Test.java");
TestUtilities.copyStringToFile(testFile,
"package hierbas.del.litoral;\n\n" +
"public class Test {\n" +
" public static void taragui() {\n" +
" Runnable r = Test::taragui;\n" +
" }\n" +
"}\n"
);
String golden =
"package hierbas.del.litoral;\n\n" +
"public class Test {\n" +
" public static void taragui() {\n" +
" Runnable r = Test::<String>taragui;\n" +
" }\n" +
"}\n";
JavaSource src = getJavaSource(testFile);
Task<WorkingCopy> task = new Task<WorkingCopy>() {
public void run(final WorkingCopy workingCopy) throws IOException {
workingCopy.toPhase(Phase.RESOLVED);
final TreeMaker make = workingCopy.getTreeMaker();
new ErrorAwareTreeScanner<Void, Void>() {
@Override public Void visitMemberReference(MemberReferenceTree node, Void p) {
workingCopy.rewrite(node, make.MemberReference(node.getMode(), node.getQualifierExpression(), node.getName(), Collections.singletonList(make.Identifier("String"))));
return super.visitMemberReference(node, p);
}
}.scan(workingCopy.getCompilationUnit(), null);
}
};
src.runModificationTask(task).commit();
String res = TestUtilities.copyFileToString(testFile);
//System.err.println(res);
assertEquals(golden, res);
}
public void testMethodReferenceLastTypeParam() throws Exception {
testFile = new File(getWorkDir(), "Test.java");
TestUtilities.copyStringToFile(testFile,
"package hierbas.del.litoral;\n\n" +
"public class Test {\n" +
" public static void taragui() {\n" +
" Runnable r = Test::<String>taragui;\n" +
" }\n" +
"}\n"
);
String golden =
"package hierbas.del.litoral;\n\n" +
"public class Test {\n" +
" public static void taragui() {\n" +
" Runnable r = Test::taragui;\n" +
" }\n" +
"}\n";
JavaSource src = getJavaSource(testFile);
Task<WorkingCopy> task = new Task<WorkingCopy>() {
public void run(final WorkingCopy workingCopy) throws IOException {
workingCopy.toPhase(Phase.RESOLVED);
final TreeMaker make = workingCopy.getTreeMaker();
new ErrorAwareTreeScanner<Void, Void>() {
@Override public Void visitMemberReference(MemberReferenceTree node, Void p) {
workingCopy.rewrite(node, make.MemberReference(node.getMode(), node.getQualifierExpression(), node.getName(), null));
return super.visitMemberReference(node, p);
}
}.scan(workingCopy.getCompilationUnit(), null);
}
};
src.runModificationTask(task).commit();
String res = TestUtilities.copyFileToString(testFile);
//System.err.println(res);
assertEquals(golden, res);
}
@Override
public Void visitMemberReference(MemberReferenceTree node, Void p) {
scan(node.getQualifierExpression(), p);
tl.moveToEnd(node.getQualifierExpression());
scan(node.getTypeArguments(), null);
tl.moveToEnd(node.getTypeArguments());
handlePossibleIdentifier(getCurrentPath(), false);
firstIdentifier(node.getName().toString());
return null;
}
private static Token<JavaTokenId> findIdentifierSpanImpl(CompilationInfo info, MemberReferenceTree tree, CompilationUnitTree cu, SourcePositions positions) {
int start = (int)positions.getStartPosition(cu, tree);
int endPosition = (int)positions.getEndPosition(cu, tree);
if (start == (-1) || endPosition == (-1))
return null;
String member = tree.getName().toString();
TokenHierarchy<?> th = info.getTokenHierarchy();
TokenSequence<JavaTokenId> ts = th.tokenSequence(JavaTokenId.language());
if (ts.move(endPosition) == Integer.MAX_VALUE) {
return null;
}
if (ts.moveNext()) {
while (ts.offset() >= start) {
Token<JavaTokenId> t = ts.token();
if (t.id() == JavaTokenId.IDENTIFIER && member.equals(info.getTreeUtilities().decodeIdentifier(t.text()).toString())) {
return t;
}
if (!ts.movePrevious())
break;
}
}
return null;
}
@Override
public Void visitMemberReference(MemberReferenceTree tree, List<Node> d) {
List<Node> below = new ArrayList<Node>();
addCorrespondingElement(below);
addCorrespondingType(below);
addCorrespondingComments(below);
super.visitMemberReference(tree, below);
d.add(new TreeNode(info, getCurrentPath(), below));
return null;
}
public static Tree newClassToConstructorReference(WorkingCopy copy, Tree tree, TreePath contextPath, List<? extends VariableTree> passedParameters, boolean addTypeCast) {
NewClassTree nct = (NewClassTree)tree;
if (passedParameters.size() != nct.getArguments().size()) {
return null;
}
Element e = copy.getTrees().getElement(new TreePath(contextPath, tree));
if (e == null || e.getKind() != ElementKind.CONSTRUCTOR) {
return null;
}
TreeMaker make = copy.getTreeMaker();
return possiblyCast(copy,
make.MemberReference(MemberReferenceTree.ReferenceMode.NEW, nct.getIdentifier(), "new", (List<? extends ExpressionTree>)nct.getTypeArguments()),
contextPath, addTypeCast);
}
public static Tree methodInvocationToMemberReference(WorkingCopy copy, Tree tree, TreePath contextPath, List<? extends VariableTree> passedParameters, boolean addTypeCast) {
if (tree.getKind() != Tree.Kind.METHOD_INVOCATION)
return null;
ExpressionTree ms = ((MethodInvocationTree)tree).getMethodSelect();
Element e = copy.getTrees().getElement(new TreePath(contextPath, ms));
if (e == null || e.getKind() != ElementKind.METHOD) {
return null;
}
Name name = null;
ExpressionTree expr = null;
TreeMaker make = copy.getTreeMaker();
if (ms.getKind() == Tree.Kind.IDENTIFIER) {
name = ((IdentifierTree)ms).getName();
expr = e.getModifiers().contains(Modifier.STATIC) ?
make.Identifier(e.getEnclosingElement()) :
make.Identifier("this"); //NOI18N
} else if (ms.getKind() == Tree.Kind.MEMBER_SELECT) {
name = ((MemberSelectTree)ms).getIdentifier();
if (passedParameters.size() == ((MethodInvocationTree)tree).getArguments().size()) {
expr = ((MemberSelectTree)ms).getExpression();
} else {
expr = make.Identifier(e.getEnclosingElement());
}
}
if (name == null || expr == null) {
return null;
}
return possiblyCast(copy,
make.MemberReference(MemberReferenceTree.ReferenceMode.INVOKE, expr, name, Collections.<ExpressionTree>emptyList()),
contextPath, addTypeCast
);
}
public void testMethodReferenceDiff() throws Exception {
testFile = new File(getWorkDir(), "Test.java");
TestUtilities.copyStringToFile(testFile,
"package hierbas.del.litoral;\n\n" +
"public class Test {\n" +
" public static void taragui() {\n" +
" Runnable r = hierbas.del.litoral.Test :: taragui;\n" +
" }\n" +
"}\n"
);
String golden =
"package hierbas.del.litoral;\n\n" +
"public class Test {\n" +
" public static void taragui() {\n" +
" Runnable r = Test :: taragui;\n" +
" }\n" +
"}\n";
JavaSource src = getJavaSource(testFile);
Task<WorkingCopy> task = new Task<WorkingCopy>() {
public void run(final WorkingCopy workingCopy) throws IOException {
workingCopy.toPhase(Phase.RESOLVED);
final TreeMaker make = workingCopy.getTreeMaker();
new ErrorAwareTreeScanner<Void, Void>() {
@Override public Void visitMemberReference(MemberReferenceTree node, Void p) {
workingCopy.rewrite(node, make.MemberReference(node.getMode(), make.Identifier("Test"), node.getName(), node.getTypeArguments()));
return super.visitMemberReference(node, p);
}
}.scan(workingCopy.getCompilationUnit(), null);
}
};
src.runModificationTask(task).commit();
String res = TestUtilities.copyFileToString(testFile);
System.err.println(res);
assertEquals(golden, res);
}
public void testMethodReferenceNameDiff() throws Exception {
testFile = new File(getWorkDir(), "Test.java");
TestUtilities.copyStringToFile(testFile,
"package hierbas.del.litoral;\n\n" +
"public class Test {\n" +
" public static void taragui() {\n" +
" Runnable r = Test :: taragui;\n" +
" }\n" +
"}\n"
);
String golden =
"package hierbas.del.litoral;\n\n" +
"public class Test {\n" +
" public static void taragui() {\n" +
" Runnable r = Test :: taragui2;\n" +
" }\n" +
"}\n";
JavaSource src = getJavaSource(testFile);
Task<WorkingCopy> task = new Task<WorkingCopy>() {
public void run(final WorkingCopy workingCopy) throws IOException {
workingCopy.toPhase(Phase.RESOLVED);
final TreeMaker make = workingCopy.getTreeMaker();
new ErrorAwareTreeScanner<Void, Void>() {
@Override public Void visitMemberReference(MemberReferenceTree node, Void p) {
workingCopy.rewrite(node, make.setLabel(node, "taragui2"));
return super.visitMemberReference(node, p);
}
}.scan(workingCopy.getCompilationUnit(), null);
}
};
src.runModificationTask(task).commit();
String res = TestUtilities.copyFileToString(testFile);
System.err.println(res);
assertEquals(golden, res);
}
public void testMethodReferenceFirstTypeParam() throws Exception {
testFile = new File(getWorkDir(), "Test.java");
TestUtilities.copyStringToFile(testFile,
"package hierbas.del.litoral;\n\n" +
"public class Test {\n" +
" public static void taragui() {\n" +
" Runnable r = Test::taragui;\n" +
" }\n" +
"}\n"
);
String golden =
"package hierbas.del.litoral;\n\n" +
"public class Test {\n" +
" public static void taragui() {\n" +
" Runnable r = Test::<String>taragui;\n" +
" }\n" +
"}\n";
JavaSource src = getJavaSource(testFile);
Task<WorkingCopy> task = new Task<WorkingCopy>() {
public void run(final WorkingCopy workingCopy) throws IOException {
workingCopy.toPhase(Phase.RESOLVED);
final TreeMaker make = workingCopy.getTreeMaker();
new ErrorAwareTreeScanner<Void, Void>() {
@Override public Void visitMemberReference(MemberReferenceTree node, Void p) {
workingCopy.rewrite(node, make.MemberReference(node.getMode(), node.getQualifierExpression(), node.getName(), Collections.singletonList(make.Identifier("String"))));
return super.visitMemberReference(node, p);
}
}.scan(workingCopy.getCompilationUnit(), null);
}
};
src.runModificationTask(task).commit();
String res = TestUtilities.copyFileToString(testFile);
System.err.println(res);
assertEquals(golden, res);
}
public void testMethodReferenceLastTypeParam() throws Exception {
testFile = new File(getWorkDir(), "Test.java");
TestUtilities.copyStringToFile(testFile,
"package hierbas.del.litoral;\n\n" +
"public class Test {\n" +
" public static void taragui() {\n" +
" Runnable r = Test::<String>taragui;\n" +
" }\n" +
"}\n"
);
String golden =
"package hierbas.del.litoral;\n\n" +
"public class Test {\n" +
" public static void taragui() {\n" +
" Runnable r = Test::taragui;\n" +
" }\n" +
"}\n";
JavaSource src = getJavaSource(testFile);
Task<WorkingCopy> task = new Task<WorkingCopy>() {
public void run(final WorkingCopy workingCopy) throws IOException {
workingCopy.toPhase(Phase.RESOLVED);
final TreeMaker make = workingCopy.getTreeMaker();
new ErrorAwareTreeScanner<Void, Void>() {
@Override public Void visitMemberReference(MemberReferenceTree node, Void p) {
workingCopy.rewrite(node, make.MemberReference(node.getMode(), node.getQualifierExpression(), node.getName(), null));
return super.visitMemberReference(node, p);
}
}.scan(workingCopy.getCompilationUnit(), null);
}
};
src.runModificationTask(task).commit();
String res = TestUtilities.copyFileToString(testFile);
System.err.println(res);
assertEquals(golden, res);
}
/**
* finds the corresponding functional interface method for a lambda expression or method reference
*
* @param tree the lambda expression or method reference
* @return the functional interface method
*/
public static Symbol.MethodSymbol getFunctionalInterfaceMethod(ExpressionTree tree, Types types) {
Preconditions.checkArgument(
(tree instanceof LambdaExpressionTree) || (tree instanceof MemberReferenceTree));
Type funcInterfaceType = ((JCTree.JCFunctionalExpression) tree).type;
return (Symbol.MethodSymbol) types.findDescriptorSymbol(funcInterfaceType.tsym);
}
@Override
public void onMatchMethodReference(
NullAway analysis,
MemberReferenceTree tree,
VisitorState state,
Symbol.MethodSymbol methodSymbol) {
for (Handler h : handlers) {
h.onMatchMethodReference(analysis, tree, state, methodSymbol);
}
}
@Override
public void onMatchMethodReference(
NullAway analysis,
MemberReferenceTree tree,
VisitorState state,
Symbol.MethodSymbol methodSymbol) {
// NoOp
}
/**
* for method references, we check that the referenced method correctly overrides the
* corresponding functional interface method
*/
@Override
public Description matchMemberReference(MemberReferenceTree tree, VisitorState state) {
if (!matchWithinClass) {
return Description.NO_MATCH;
}
Symbol.MethodSymbol referencedMethod = ASTHelpers.getSymbol(tree);
Symbol.MethodSymbol funcInterfaceSymbol =
NullabilityUtil.getFunctionalInterfaceMethod(tree, state.getTypes());
handler.onMatchMethodReference(this, tree, state, referencedMethod);
return checkOverriding(funcInterfaceSymbol, referencedMethod, tree, state);
}
public static KindName kindName(MemberReferenceTree.ReferenceMode mode) {
switch (mode) {
case INVOKE: return KindName.METHOD;
case NEW: return KindName.CONSTRUCTOR;
default : throw new AssertionError("Unexpected mode: "+ mode);
}
}
public static KindName kindName(MemberReferenceTree.ReferenceMode mode) {
switch (mode) {
case INVOKE: return KindName.METHOD;
case NEW: return KindName.CONSTRUCTOR;
default : throw new AssertionError("Unexpected mode: "+ mode);
}
}
public static KindName kindName(MemberReferenceTree.ReferenceMode mode) {
switch (mode) {
case INVOKE: return KindName.METHOD;
case NEW: return KindName.CONSTRUCTOR;
default : throw new AssertionError("Unexpected mode: "+ mode);
}
}
public static KindName kindName(MemberReferenceTree.ReferenceMode mode) {
switch (mode) {
case INVOKE: return KindName.METHOD;
case NEW: return KindName.CONSTRUCTOR;
default : throw new AssertionError("Unexpected mode: "+ mode);
}
}