Skip to content

Commit

Permalink
Removes method bindings from the LambdaExpression node because the bi…
Browse files Browse the repository at this point in the history
…ndings provided by JDT are not useful. The binding provided by typeBinding.getFunctionalInterfaceMethod() is much more useful.

	Change on 2016/05/24 by kstanger <kstanger@google.com>

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=123096843
  • Loading branch information
kstanger authored and tomball committed May 27, 2016
1 parent cc97b92 commit 378b14e
Show file tree
Hide file tree
Showing 6 changed files with 4 additions and 62 deletions.
Expand Up @@ -13,11 +13,6 @@
*/
package com.google.devtools.j2objc.ast;

import com.google.devtools.j2objc.types.GeneratedMethodBinding;
import com.google.devtools.j2objc.util.NameTable;

import org.eclipse.jdt.core.dom.IMethodBinding;

/**
* Creation reference expression AST node type (added in JLS8, section 15.13).
*/
Expand Down Expand Up @@ -58,21 +53,4 @@ protected void acceptInner(TreeVisitor visitor) {
public CreationReference copy() {
return new CreationReference(this);
}

@Override
public IMethodBinding getMethodBinding() {
if (methodBinding == null) {
// Workaround for JDT 4.5.2 bug. A method reference's type binding has a
// single method, so generate an equivalent constructor as that binding.
IMethodBinding[] methods = typeBinding.getDeclaredMethods();
assert methods.length == 1;
IMethodBinding m = methods[0];
methodBinding = new GeneratedMethodBinding(null, NameTable.INIT_NAME, m.getModifiers(),
m.getReturnType(), null, m.getDeclaringClass(), true,
// References to array types are always vararg, as that's the only way to use them.
m.isVarargs() || m.getReturnType().isArray());
((GeneratedMethodBinding) methodBinding).addParameter(m.getReturnType());
}
return methodBinding;
}
}
Expand Up @@ -606,11 +606,7 @@ public boolean visit(LabeledStatement node) {

@Override
public boolean visit(LambdaExpression node) {
IMethodBinding methodBinding = node.getMethodBinding();
sb.print(methodBinding.getReturnType().getName());
sb.print(" ");
sb.print(methodBinding.getName());
sb.print(" (");
sb.print("(");
boolean delimiterFlag = false;
for (VariableDeclaration x : node.getParameters()) {
IVariableBinding variableBinding = x.getVariableBinding();
Expand Down
Expand Up @@ -15,7 +15,6 @@

import com.google.devtools.j2objc.types.LambdaTypeBinding;

import org.eclipse.jdt.core.dom.IMethodBinding;
import org.eclipse.jdt.core.dom.ITypeBinding;

import java.util.List;
Expand All @@ -28,7 +27,6 @@ public class LambdaExpression extends Expression {
private final ITypeBinding typeBinding;
// Unique type binding that can be used as a key.
private final ITypeBinding lambdaTypeBinding;
private final IMethodBinding methodBinding;
private ChildList<VariableDeclaration> parameters = ChildList.create(VariableDeclaration.class,
this);
protected ChildLink<TreeNode> body = ChildLink.create(TreeNode.class, this);
Expand All @@ -39,7 +37,6 @@ public class LambdaExpression extends Expression {
public LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression jdtNode) {
super(jdtNode);
typeBinding = jdtNode.resolveTypeBinding();
methodBinding = jdtNode.resolveMethodBinding();
for (Object x : jdtNode.parameters()) {
parameters.add((VariableDeclaration) TreeConverter.convert(x));
}
Expand All @@ -54,16 +51,14 @@ public LambdaExpression(LambdaExpression other) {
super(other);
typeBinding = other.getTypeBinding();
lambdaTypeBinding = other.getLambdaTypeBinding();
methodBinding = other.getMethodBinding();
parameters.copyFrom(other.getParameters());
body.copyFrom(other.getBody());
uniqueName = other.getUniqueName();
isCapturing = other.isCapturing();
}

public LambdaExpression(String name, ITypeBinding typeBinding, IMethodBinding methodBinding) {
public LambdaExpression(String name, ITypeBinding typeBinding) {
this.typeBinding = typeBinding;
this.methodBinding = methodBinding;
lambdaTypeBinding = new LambdaTypeBinding(name);
}

Expand All @@ -89,10 +84,6 @@ public ITypeBinding getLambdaTypeBinding() {
return lambdaTypeBinding;
}

public IMethodBinding getMethodBinding() {
return methodBinding;
}

public List<VariableDeclaration> getParameters() {
return parameters;
}
Expand Down
Expand Up @@ -119,24 +119,6 @@ public static Expression trimParentheses(Expression node) {
return node;
}

/**
* Returns the method binding which is the parent of the specified node, as a node may be parented
* by a lambda, method reference or a method.
*/
public static IMethodBinding getOwningMethodBinding(TreeNode node) {
while (node != null) {
if (node instanceof MethodDeclaration) {
return ((MethodDeclaration) node).getMethodBinding();
} else if (node instanceof LambdaExpression) {
return ((LambdaExpression) node).getMethodBinding();
} else if (node instanceof MethodReference) {
return ((MethodReference) node).getMethodBinding();
}
node = node.getParent();
}
return null;
}

/**
* With lambdas and methodbindings, the return type of the method binding does not necessarily
* match the return type of the functional interface, which enforces the type contracts. To get
Expand Down
Expand Up @@ -1012,13 +1012,9 @@ public boolean visit(QualifiedType node) {
public boolean visit(ReturnStatement node) {
buffer.append("return");
Expression expr = node.getExpression();
IMethodBinding methodBinding = TreeUtil.getOwningMethodBinding(node);
if (expr != null) {
buffer.append(' ');
expr.accept(this);
} else if (methodBinding != null && methodBinding.isConstructor()) {
// A return statement without any expression is allowed in constructors.
buffer.append(" self");
}
buffer.append(";\n");
return false;
Expand Down
Expand Up @@ -42,7 +42,6 @@ public class MethodReferenceRewriter extends TreeVisitor {

@Override
public void endVisit(CreationReference node) {
IMethodBinding methodBinding = node.getMethodBinding();
ITypeBinding exprBinding = node.getTypeBinding();
ITypeBinding creationType = node.getType().getTypeBinding();
IMethodBinding functionalInterface = exprBinding.getFunctionalInterfaceMethod();
Expand All @@ -54,12 +53,12 @@ public void endVisit(CreationReference node) {
invocationArguments = arrayCreation.getDimensions();
} else {
ClassInstanceCreation classCreation =
new ClassInstanceCreation(methodBinding, Type.newType(creationType));
new ClassInstanceCreation(node.getMethodBinding(), Type.newType(creationType));
invocation = classCreation;
invocationArguments = classCreation.getArguments();
}
LambdaExpression lambda = new LambdaExpression(
"CreationReference:" + node.getLineNumber(), exprBinding, methodBinding);
"CreationReference:" + node.getLineNumber(), exprBinding);
lambda.setBody(invocation);
addRemainingLambdaParams(
Arrays.asList(functionalInterface.getParameterTypes()), invocationArguments, lambda, null);
Expand Down

0 comments on commit 378b14e

Please sign in to comment.