Skip to content

Commit

Permalink
Moves JDT specific workaround code into the tree converter.
Browse files Browse the repository at this point in the history
	Change on 2016/12/23 by kstanger <kstanger@google.com>

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=142854237
  • Loading branch information
kstanger authored and tomball committed Dec 28, 2016
1 parent 30ad89a commit d1120d7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 18 deletions.
Expand Up @@ -1259,11 +1259,31 @@ private static TreeNode convertTypeMethodReference(
org.eclipse.jdt.core.dom.TypeMethodReference node) {
TypeMethodReference newNode = new TypeMethodReference();
convertMethodReference(node, newNode);
if (node.getType().resolveBinding().isArray()) {
// JDT does not provide the correct method on array types, so we find it from
// java.lang.Object.
IMethodBinding functionalInterface = getFunctionalInterface(node.resolveTypeBinding());
ExecutableElement method = getObjectMethod(
node.getAST(), node.getName().getIdentifier(),
functionalInterface.getParameterTypes().length - 1);
newNode.setExecutablePair(new ExecutablePair(method));
}
return newNode
.setName((SimpleName) TreeConverter.convert(node.getName()))
.setType((Type) TreeConverter.convert(node.getType()));
}

private static ExecutableElement getObjectMethod(AST ast, String name, int numParams) {
TypeElement objectType = BindingConverter.getTypeElement(
ast.resolveWellKnownType("java.lang.Object"));
for (ExecutableElement method : ElementUtil.getMethods(objectType)) {
if (ElementUtil.getName(method).equals(name) && method.getParameters().size() == numParams) {
return method;
}
}
throw new AssertionError("Can't find method element for method: " + name);
}

private static TreeNode convertUnionType(org.eclipse.jdt.core.dom.UnionType node) {
UnionType newNode = new UnionType();
convertType(node, newNode);
Expand Down Expand Up @@ -1383,6 +1403,18 @@ private static boolean isAssignable(TypeMirror t1, TypeMirror t2) {
BindingConverter.unwrapTypeMirrorIntoTypeBinding(t2));
}

private static IMethodBinding getFunctionalInterface(ITypeBinding type) {
if (BindingUtil.isIntersectionType(type)) {
for (ITypeBinding i : type.getInterfaces()) {
IMethodBinding functionalInterface = i.getFunctionalInterfaceMethod();
if (functionalInterface != null) {
return functionalInterface;
}
}
}
return type.getFunctionalInterfaceMethod();
}

// Helper class for convertInfixExpression().
private static class StackState {
private final org.eclipse.jdt.core.dom.InfixExpression expression;
Expand Down
Expand Up @@ -253,30 +253,13 @@ private void rewriteSuperMethodReference(SuperMethodReference node) {
}

private void rewriteTypeMethodReference(TypeMethodReference node) {
ExecutablePair method = getExecutablePair(node);
ExecutablePair method = node.getExecutablePair();
Iterator<VariableElement> params = createParameters();
MethodInvocation invocation = new MethodInvocation(
method, ElementUtil.isStatic(method.element()) ? null : new SimpleName(params.next()));
forwardRemainingArgs(params, invocation.getArguments());
setImplementationBody(invocation);
}

private ExecutablePair getExecutablePair(TypeMethodReference node) {
if (!TypeUtil.isArray(node.getType().getTypeMirror())) {
return node.getExecutablePair();
}
// JDT does not provide the correct method on array types, so we find it from
// java.lang.Object.
String name = node.getName().getIdentifier();
int numParams = functionalInterface.getParameters().size() - 1;
for (ExecutableElement method : ElementUtil.getMethods(typeUtil.getJavaObject())) {
if (ElementUtil.getName(method).equals(name)
&& method.getParameters().size() == numParams) {
return new ExecutablePair(method);
}
}
throw new AssertionError("Can't find method element for method: " + name);
}
}

@Override
Expand Down

0 comments on commit d1120d7

Please sign in to comment.