Skip to content

Commit

Permalink
Some migration of OuterReferenceFixer.
Browse files Browse the repository at this point in the history
	Change on 2016/08/02 by nbraswell <nbraswell@google.com>

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=129111788
  • Loading branch information
Limvot authored and tomball committed Sep 7, 2016
1 parent 927ce5a commit 283d511
Show file tree
Hide file tree
Showing 10 changed files with 295 additions and 34 deletions.
1 change: 1 addition & 0 deletions translator/Makefile
Expand Up @@ -264,6 +264,7 @@ JAVA_SOURCES = \
translate/VariableRenamer.java \
types/AbstractTypeBinding.java \
types/FunctionBinding.java \
types/GeneratedExecutableElement.java \
types/GeneratedMethodBinding.java \
types/GeneratedPackageBinding.java \
types/GeneratedTypeBinding.java \
Expand Down
Expand Up @@ -89,6 +89,10 @@ public ExecutableElement getExecutableElement() {
return method;
}

public void setExecutableElement(ExecutableElement element) {
method = element;
}

@Override
public TypeMirror getTypeMirror() {
return method != null
Expand Down
Expand Up @@ -1183,6 +1183,12 @@ private void printAnnotations(List<Annotation> annotations) {
}
}

public static void printModifiers(int modifiers, StringBuilder builder) {
DebugASTPrinter temp = new DebugASTPrinter();
temp.printModifiers(modifiers);
builder.append(temp.sb.toString());
}

private void printModifiers(int modifiers) {
if (Modifier.isPublic(modifiers)) {
sb.print("public ");
Expand Down
Expand Up @@ -22,6 +22,7 @@

import java.util.List;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.type.ExecutableType;
import javax.lang.model.type.TypeMirror;

/**
Expand All @@ -30,6 +31,7 @@
public class MethodInvocation extends Expression {

private ExecutableElement method = null;
private ExecutableType methodType = null;
// The context-specific known type of this expression.
private TypeMirror typeMirror = null;
private ChildLink<Expression> expression = ChildLink.create(Expression.class, this);
Expand All @@ -40,6 +42,7 @@ public MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation jdtNode) {
super(jdtNode);
IMethodBinding methodBinding = BindingConverter.wrapBinding(jdtNode.resolveMethodBinding());
method = BindingConverter.getExecutableElement(methodBinding);
methodType = BindingConverter.getType(methodBinding);
ITypeBinding typeBinding = BindingConverter.wrapBinding(jdtNode.resolveTypeBinding());
typeMirror = BindingConverter.getType(typeBinding);
expression.set((Expression) TreeConverter.convert(jdtNode.getExpression()));
Expand All @@ -52,6 +55,7 @@ public MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation jdtNode) {
public MethodInvocation(MethodInvocation other) {
super(other);
method = other.getExecutableElement();
methodType = other.getExecutableType();
typeMirror = other.getTypeMirror();
expression.copyFrom(other.getExpression());
name.copyFrom(other.getName());
Expand All @@ -60,6 +64,7 @@ public MethodInvocation(MethodInvocation other) {

public MethodInvocation(IMethodBinding binding, ITypeBinding typeBinding, Expression expression) {
method = BindingConverter.getExecutableElement(binding);
methodType = BindingConverter.getType(binding);
typeMirror = BindingConverter.getType(typeBinding);
this.expression.set(expression);
name.set(new SimpleName(binding));
Expand All @@ -75,17 +80,22 @@ public Kind getKind() {
}

public IMethodBinding getMethodBinding() {
return (IMethodBinding) BindingConverter.unwrapElement(method);
return (IMethodBinding) BindingConverter.unwrapTypeMirrorIntoBinding(methodType);
}

public void setMethodBinding(IMethodBinding newMethodBinding) {
method = BindingConverter.getExecutableElement(newMethodBinding);
methodType = BindingConverter.getType(newMethodBinding);
}

public ExecutableElement getExecutableElement() {
return method;
}

public ExecutableType getExecutableType() {
return methodType;
}

@Override
public TypeMirror getTypeMirror() {
return typeMirror;
Expand Down
Expand Up @@ -68,6 +68,10 @@ public ExecutableElement getExecutableElement() {
return method;
}

public void setExecutableElement(ExecutableElement element) {
method = element;
}

public Expression getExpression() {
return expression.get();
}
Expand Down
Expand Up @@ -14,12 +14,14 @@

package com.google.devtools.j2objc.jdt;

import com.google.devtools.j2objc.types.GeneratedExecutableElement;
import com.google.devtools.j2objc.types.GeneratedMethodBinding;
import com.google.devtools.j2objc.types.GeneratedVariableBinding;
import com.google.devtools.j2objc.types.GeneratedVariableElement;
import com.google.devtools.j2objc.types.NativeType;
import com.google.devtools.j2objc.types.NativeTypeBinding;
import com.google.devtools.j2objc.util.BindingUtil;

import com.google.devtools.j2objc.util.ElementUtil;
import org.eclipse.jdt.core.dom.IAnnotationBinding;
import org.eclipse.jdt.core.dom.IBinding;
import org.eclipse.jdt.core.dom.IExtendedModifier;
Expand Down Expand Up @@ -263,10 +265,10 @@ public static JdtTypeMirror getType(ITypeBinding binding) {
return type;
}

public static JdtTypeMirror getType(IMethodBinding binding) {
public static JdtExecutableType getType(IMethodBinding binding) {
JdtTypeMirror type = getTypeMirror(binding);
if (type != null) {
return type;
return (JdtExecutableType) type;
}
JdtMethodBinding wrappedBinding = wrapBinding(binding);
JdtExecutableType executableType = new JdtExecutableType(wrappedBinding);
Expand Down Expand Up @@ -340,6 +342,19 @@ public static IBinding unwrapElement(Element element) {
element.getKind() == ElementKind.FIELD, element.getKind() == ElementKind.PARAMETER,
null, null);
}
if (element instanceof GeneratedExecutableElement) {
GeneratedExecutableElement gElement = (GeneratedExecutableElement) element;
GeneratedMethodBinding newOne = new GeneratedMethodBinding(
null, gElement.getSimpleName().toString(),
ElementUtil.fromModifierSet(gElement.getModifiers()),
BindingConverter.unwrapTypeMirrorIntoTypeBinding(gElement.getReturnType()), null,
BindingConverter.unwrapTypeElement((TypeElement) gElement.getEnclosingElement()),
element.getKind() == ElementKind.CONSTRUCTOR, gElement.isVarArgs());
for (VariableElement p : gElement.getParameters()) {
newOne.getParameters().add(BindingConverter.unwrapTypeMirrorIntoTypeBinding(p.asType()));
}
return newOne;
}
return element != null ? ((JdtElement) element).binding : null;
}

Expand Down
Expand Up @@ -34,7 +34,8 @@
class JdtExecutableElement extends JdtElement implements ExecutableElement {

public JdtExecutableElement(IMethodBinding binding) {
super(binding, binding.getName(), binding.getModifiers());
super(binding.getMethodDeclaration(), binding.getMethodDeclaration().getName(),
binding.getMethodDeclaration().getModifiers());
}

@Override
Expand Down
Expand Up @@ -75,7 +75,9 @@ public Object getConstantValue() {

@Override
public TypeMirror asType() {
return BindingConverter.getType(((IVariableBinding) binding).getType());
return BindingConverter.getType(binding instanceof ITypeBinding
? ((ITypeBinding) binding)
: ((IVariableBinding) binding).getType());
}

@Override
Expand Down
Expand Up @@ -28,13 +28,14 @@
import com.google.devtools.j2objc.ast.TreeUtil;
import com.google.devtools.j2objc.ast.TreeVisitor;
import com.google.devtools.j2objc.jdt.BindingConverter;
import com.google.devtools.j2objc.types.GeneratedMethodBinding;
import com.google.devtools.j2objc.util.BindingUtil;
import com.google.devtools.j2objc.types.GeneratedExecutableElement;
import com.google.devtools.j2objc.util.ElementUtil;
import java.util.ArrayList;
import java.util.List;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;
import org.eclipse.jdt.core.dom.IMethodBinding;
import org.eclipse.jdt.core.dom.ITypeBinding;
import org.eclipse.jdt.core.dom.Modifier;
import javax.lang.model.type.TypeMirror;

/**
* Updates variable references outside an inner class to the new fields
Expand All @@ -53,8 +54,7 @@ public OuterReferenceFixer(OuterReferenceResolver outerResolver) {

@Override
public boolean visit(MethodDeclaration node) {
IMethodBinding binding = node.getMethodBinding();
if (binding.isConstructor()) {
if (node.getMethodElement().getKind() == ElementKind.CONSTRUCTOR) {
List<SingleVariableDeclaration> params = node.getParameters();
if (params.size() > 0) {
VariableElement firstParam = params.get(0).getVariableElement();
Expand All @@ -73,34 +73,34 @@ public void endVisit(MethodDeclaration node) {

@Override
public boolean visit(ClassInstanceCreation node) {
ITypeBinding newType = node.getTypeBinding().getTypeDeclaration();
ITypeBinding declaringClass = newType.getDeclaringClass();
if (Modifier.isStatic(newType.getModifiers()) || declaringClass == null) {
TypeElement newType = (TypeElement) node.getExecutableElement().getEnclosingElement();
TypeElement declaringClass = ElementUtil.getDeclaringClass(newType);
if (newType.getModifiers().contains(javax.lang.model.element.Modifier.STATIC)
|| declaringClass == null) {
return true;
}

GeneratedMethodBinding binding =
new GeneratedMethodBinding(node.getMethodBinding().getMethodDeclaration());
List<TypeMirror> parameterTypes = new ArrayList<>();
GeneratedExecutableElement element =
new GeneratedExecutableElement(node.getExecutableElement());

List<Expression> captureArgs = node.getArguments().subList(0, 0);
List<ITypeBinding> captureParams = binding.getParameters().subList(0, 0);
if (outerResolver.needsOuterParam(newType)) {
captureArgs.add(getOuterArg(node, declaringClass));
captureParams.add(declaringClass);
if (outerResolver.needsOuterParam(BindingConverter.unwrapTypeElement(newType))) {
captureArgs.add(getOuterArg(node, declaringClass.asType()));
parameterTypes.add(declaringClass.asType());
}

for (List<VariableElement> captureArgPath : outerResolver.getCaptureArgPaths(node)) {
captureArgPath = fixPath(captureArgPath);
captureArgs.add(Name.newName(captureArgPath));
captureParams.add(BindingConverter.unwrapTypeMirrorIntoTypeBinding(
captureArgPath.get(captureArgPath.size() - 1).asType()));
parameterTypes.add(captureArgPath.get(captureArgPath.size() - 1).asType());
}
node.setMethodBinding(binding);
assert binding.isVarargs() || node.getArguments().size() == binding.getParameterTypes().length;
element.addParametersPlaceholderFront(parameterTypes);
node.setExecutableElement(element);
assert element.isVarArgs() || node.getArguments().size() == element.getParameters().size();
return true;
}

private Expression getOuterArg(ClassInstanceCreation node, ITypeBinding declaringClass) {
private Expression getOuterArg(ClassInstanceCreation node, TypeMirror declaringClass) {
Expression outerExpr = node.getExpression();
if (outerExpr != null) {
node.setExpression(null);
Expand All @@ -125,7 +125,7 @@ public boolean visit(MethodInvocation node) {
@Override
public void endVisit(SuperMethodInvocation node) {
// Ignore default methods, they do not have outer paths.
if (BindingUtil.isDefault(node.getMethodBinding())) {
if (ElementUtil.isDefault(node.getExecutableElement())) {
return;
}
List<VariableElement> path = outerResolver.getPath(node);
Expand Down Expand Up @@ -169,12 +169,12 @@ public void endVisit(SuperConstructorInvocation node) {
return;
}
node.setExpression(null);
ITypeBinding outerExpressionType = outerExpression.getTypeBinding();
GeneratedMethodBinding binding =
new GeneratedMethodBinding(node.getMethodBinding().getMethodDeclaration());
node.setMethodBinding(binding);
TypeMirror outerExpressionType = outerExpression.getTypeMirror();
GeneratedExecutableElement element =
new GeneratedExecutableElement(node.getExecutableElement());
node.setExecutableElement(element);
node.addArgument(0, outerExpression);
binding.addParameter(0, outerExpressionType);
element.addParameterPlaceholderFront(outerExpressionType);
}

private List<VariableElement> fixPath(List<VariableElement> path) {
Expand Down

0 comments on commit 283d511

Please sign in to comment.