JCExpression memberReferenceSuffix(int pos1, JCExpression t) { checkMethodReferences(); mode = EXPR; List<JCExpression> typeArgs = null; if (token.kind == LT) { typeArgs = typeArguments(false); } Name refName; ReferenceMode refMode; if (token.kind == NEW) { refMode = ReferenceMode.NEW; refName = names.init; nextToken(); } else { refMode = ReferenceMode.INVOKE; refName = ident(); } return toP(F.at(t.getStartPosition()).Reference(refMode, refName, t, typeArgs)); }
JCLambda lambda() { int prevPos = make.pos; try { make.at(tree); //body generation - this can be either a method call or a //new instance creation expression, depending on the member reference kind VarSymbol rcvr = addParametersReturnReceiver(); JCExpression expr = (tree.getMode() == ReferenceMode.INVOKE) ? expressionInvoke(rcvr) : expressionNew(); JCLambda slam = make.Lambda(params.toList(), expr); slam.targets = tree.targets; slam.type = tree.type; slam.pos = tree.pos; return slam; } finally { make.at(prevPos); } }
/** * Detect member references that implement an interface that return Object, but resolve to a * method that returns Future. */ @Override public Description matchMemberReference(MemberReferenceTree tree, VisitorState state) { Description description = super.matchMemberReference(tree, state); if (Description.NO_MATCH == description) { if (allOf( (t, s) -> t.getMode() == ReferenceMode.INVOKE, FutureReturnValueIgnored::isObjectReturningMethodReferenceExpression, not((t, s) -> isWhitelistedInterfaceType(((JCMemberReference) t).type, s)), not((t, s) -> isThrowingFunctionalInterface(s, ((JCMemberReference) t).type)), specializedMatcher()) .matches(tree, state)) { return describeMatch(tree); } } return description; }
@Override public Description matchMemberReference(MemberReferenceTree tree, VisitorState state) { if (allOf( (t, s) -> t.getMode() == ReferenceMode.INVOKE, AbstractReturnValueIgnored::isVoidReturningMethodReferenceExpression, // Skip cases where the method we're referencing really does return void. We're only // looking for cases where the referenced method does not return void, but it's being // used on a void-returning functional interface. not((t, s) -> ASTHelpers.isVoidType(ASTHelpers.getSymbol(tree).getReturnType(), s)), not((t, s) -> isThrowingFunctionalInterface(s, ((JCMemberReference) t).type)), specializedMatcher()) .matches(tree, state)) { return describeMatch(tree); } return Description.NO_MATCH; }
@Override public void visitReference(JCMemberReference tree) { printExpr(tree.expr); print(cs.spaceAroundMethodReferenceDoubleColon() ? " :: " : "::"); if (tree.typeargs != null && !tree.typeargs.isEmpty()) { print("<"); printExprs(tree.typeargs); print(">"); } if (tree.getMode() == ReferenceMode.INVOKE) print(tree.name); else print("new"); }
public MemberReferenceTree MemberReference(ReferenceMode refMode, CharSequence name, ExpressionTree expression, List<? extends ExpressionTree> typeArguments) { ListBuffer<JCExpression> targs; if (typeArguments != null) { targs = new ListBuffer<JCExpression>(); for (ExpressionTree t : typeArguments) targs.append((JCExpression)t); } else { targs = null; } return make.at(NOPOS).Reference(refMode, names.fromString(name.toString()), (JCExpression) expression, targs != null ? targs.toList() : null); }
public void testPrintMemberReference() 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 = null;\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); CompilationUnitTree cut = workingCopy.getCompilationUnit(); final TreeMaker make = workingCopy.getTreeMaker(); new ErrorAwareTreeScanner<Void, Void>() { @Override public Void visitLiteral(LiteralTree node, Void p) { workingCopy.rewrite(node, make.MemberReference(ReferenceMode.INVOKE, make.Identifier("Test"), "taragui", Collections.<ExpressionTree>emptyList())); return super.visitLiteral(node, p); } }.scan(workingCopy.getCompilationUnit(), null); } }; src.runModificationTask(task).commit(); String res = TestUtilities.copyFileToString(testFile); System.err.println(res); assertEquals(golden, res); }
public void visitReference(JCMemberReference tree) { try { printExpr(tree.expr); print("::"); if (tree.typeargs != null) { print("<"); printExprs(tree.typeargs); print(">"); } print(tree.getMode() == ReferenceMode.INVOKE ? tree.name : "new"); } catch (IOException e) { throw new UncheckedIOException(e); } }
/** * Does this reference need to be converted to a lambda * (i.e. var args need to be expanded or "super" is used) */ final boolean needsConversionToLambda() { return interfaceParameterIsIntersectionType() || isSuper || needsVarArgsConversion() || isArrayOp() || isPrivateInOtherClass() || !receiverAccessible() || (tree.getMode() == ReferenceMode.NEW && tree.kind != ReferenceKind.ARRAY_CTOR && (tree.sym.owner.isLocal() || tree.sym.owner.isInner())); }
/** * Does this reference need to be converted to a lambda * (i.e. var args need to be expanded or "super" is used) */ final boolean needsConversionToLambda() { return interfaceParameterIsIntersectionType() || isSuper || needsVarArgsConversion() || isArrayOp() || isPrivateInOtherClass() || isProtectedInSuperClassOfEnclosingClassInOtherPackage() || !receiverAccessible() || (tree.getMode() == ReferenceMode.NEW && tree.kind != ReferenceKind.ARRAY_CTOR && (tree.sym.owner.isLocal() || tree.sym.owner.isInner())); }
@Override public void visitReference(JCMemberReference tree) { super.visitReference(tree); if (tree.getMode() != ReferenceMode.NEW) { return; } if (memberOfEnclosing(owner, state, tree.expr.type.tsym)) { referencesOuter = true; } }
private ReferenceKind(ReferenceMode mode, boolean unbound) { this.mode = mode; this.unbound = unbound; }
protected JCMemberReference(ReferenceMode mode, Name name, JCExpression expr, List<JCExpression> typeargs) { this.mode = mode; this.name = name; this.expr = expr; this.typeargs = typeargs; }
@Override public ReferenceMode getMode() { return mode; }
ResultInfo memberReferenceQualifierResult(JCMemberReference tree) { //if this is a constructor reference, the expected kind must be a type return new ResultInfo(tree.getMode() == ReferenceMode.INVOKE ? VAL | TYP : TYP, Type.noType); }
ResultInfo memberReferenceQualifierResult(JCMemberReference tree) { //if this is a constructor reference, the expected kind must be a type return new ResultInfo(tree.getMode() == ReferenceMode.INVOKE ? KindSelector.VAL_TYP : KindSelector.TYP, Type.noType); }