Skip to content

Commit

Permalink
Retain original method linkage for generated methods from default params
Browse files Browse the repository at this point in the history
GroovyCompUnitDecl maps MethodNode -> AbstractMethodDeclaration
MethodScope.createMethod maps AbstractMethodDeclaration -> MethodBinding
JDTClassNode.methodBindingToMethodNode maps MethodBinding -> MethodNode
  • Loading branch information
eric-milles committed Jan 15, 2019
1 parent 586ef97 commit 823faf7
Show file tree
Hide file tree
Showing 16 changed files with 329 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@
* @see org.codehaus.groovy.ast.ClassHelper
*/
public class ClassNode extends AnnotatedNode implements Opcodes {
// GRECLIPSE private->package
static class MapOfLists {
// GRECLIPSE private->protected
protected static class MapOfLists {
// GRECLIPSE private->protected
protected Map<Object, List<MethodNode>> map;

Expand Down Expand Up @@ -147,7 +147,8 @@ public void remove(Object key, MethodNode value) {
private boolean syntheticPublic;
private ClassNode[] interfaces;
private MixinNode[] mixins;
private List<ConstructorNode> constructors;
// GRECLIPSE private->protected
protected List<ConstructorNode> constructors;
private List<Statement> objectInitializers;
// GRECLIPSE private->protected
protected MapOfLists methods;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@
* @see org.codehaus.groovy.ast.ClassHelper
*/
public class ClassNode extends AnnotatedNode implements Opcodes {
// GRECLIPSE private->package
static class MapOfLists {
// GRECLIPSE private->protected
protected static class MapOfLists {
// GRECLIPSE private->protected
protected Map<Object, List<MethodNode>> map;

Expand Down Expand Up @@ -146,7 +146,8 @@ public void remove(Object key, MethodNode value) {
private boolean syntheticPublic;
private ClassNode[] interfaces;
private MixinNode[] mixins;
private List<ConstructorNode> constructors;
// GRECLIPSE private->protected
protected List<ConstructorNode> constructors;
private List<Statement> objectInitializers;
// GRECLIPSE private->protected
protected MapOfLists methods;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@
* @see org.codehaus.groovy.ast.ClassHelper
*/
public class ClassNode extends AnnotatedNode implements Opcodes {
// GRECLIPSE private->package
static class MapOfLists {
// GRECLIPSE private->protected
protected static class MapOfLists {
// GRECLIPSE private->protected
protected Map<Object, List<MethodNode>> map;

Expand Down Expand Up @@ -146,7 +146,8 @@ public void remove(Object key, MethodNode value) {
private boolean syntheticPublic;
private ClassNode[] interfaces;
private MixinNode[] mixins;
private List<ConstructorNode> constructors;
// GRECLIPSE private->protected
protected List<ConstructorNode> constructors;
private List<Statement> objectInitializers;
// GRECLIPSE private->protected
protected MapOfLists methods;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2018 the original author or authors.
* Copyright 2009-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -83,7 +83,7 @@ protected MethodBinding[] augmentMethodBindings(MethodBinding[] methodBindings)
return methodBindings;
}

ReferenceBinding[] superInterfaces = (typeBinding.superInterfaces != null ? typeBinding.superInterfaces : Binding.NO_SUPERINTERFACES);
ReferenceBinding[] superInterfaces = Optional.ofNullable(typeBinding.superInterfaces).orElse(Binding.NO_SUPERINTERFACES);

boolean implementsGroovyLangObject = false;
for (ReferenceBinding face : superInterfaces) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2018 the original author or authors.
* Copyright 2009-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -66,6 +66,7 @@
import org.eclipse.jdt.internal.compiler.lookup.Binding;
import org.eclipse.jdt.internal.compiler.lookup.ClassScope;
import org.eclipse.jdt.internal.compiler.lookup.CompilationUnitScope;
import org.eclipse.jdt.internal.compiler.lookup.DelegateMethodBinding;
import org.eclipse.jdt.internal.compiler.lookup.FieldBinding;
import org.eclipse.jdt.internal.compiler.lookup.LazilyResolvedMethodBinding;
import org.eclipse.jdt.internal.compiler.lookup.LookupEnvironment;
Expand Down Expand Up @@ -328,6 +329,7 @@ private MethodNode methodBindingToMethodNode(MethodBinding methodBinding) {
MethodNode methodNode = new JDTMethodNode(methodBinding, resolver, String.valueOf(methodBinding.selector), modifiers, returnType, parameters, exceptions, null);
methodNode.setGenericsTypes(new JDTClassNodeBuilder(resolver).configureTypeVariables(methodBinding.typeVariables()));
methodNode.setSynthetic(methodBinding instanceof LazilyResolvedMethodBinding); // see GroovyClassScope
populateOriginal(methodBinding, methodNode);
return methodNode;
} catch (AbortCompilation e) {
throw e;
Expand All @@ -353,6 +355,8 @@ private ConstructorNode constructorBindingToConstructorNode(MethodBinding method
ctorNode.addAnnotation(new JDTAnnotationNode(annotationBinding, resolver));
}
ctorNode.setGenericsTypes(new JDTClassNodeBuilder(resolver).configureTypeVariables(methodBinding.typeVariables()));
ctorNode.putNodeMetaData("JdtBinding", methodBinding);
populateOriginal(methodBinding, ctorNode);
return ctorNode;
}

Expand Down Expand Up @@ -450,6 +454,18 @@ private Parameter[] makeParameters(TypeBinding[] parameterTypes, char[][] parame
return parameters;
}

private void populateOriginal(MethodBinding methodBinding, MethodNode methodNode) {
if (methodBinding instanceof DelegateMethodBinding) {
MethodBinding target = ((DelegateMethodBinding) methodBinding).delegateMethod.binding;
for (MethodNode candidate : methodNode instanceof ConstructorNode ? constructors : methods.getNotNull(methodNode.getName())) {
Binding binding = methodNode instanceof JDTNode ? ((JDTNode) candidate).getJdtBinding() : candidate.getNodeMetaData("JdtBinding");
if (binding == target) {
methodNode.setOriginal(candidate);
}
}
}
}

//--------------------------------------------------------------------------

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2018 the original author or authors.
* Copyright 2009-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -655,14 +655,13 @@ private void appendUniqueKeyForMethod(StringBuilder sb, MethodNode node, ClassNo
Parameter[] parameters = node.getOriginal().getParameters();
if (parameters != null) {
for (Parameter param : parameters) {
ClassNode paramType = param.getType();
appendUniqueKeyForClass(sb, paramType, resolvedDeclaringType);
appendUniqueKeyForClass(sb, param.getType(), resolvedDeclaringType);
}
}
sb.append(Signature.C_PARAM_END);

// return type
appendUniqueKeyForClass(sb, node.getOriginal().getReturnType(), resolvedDeclaringType);
appendUniqueKeyForClass(sb, node.getReturnType(), resolvedDeclaringType);

// generic type resolution
if (generics.length > 0) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2009-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.eclipse.jdt.internal.compiler.lookup;

import org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration;

public class DelegateMethodBinding extends MethodBinding {

public final AbstractMethodDeclaration delegateMethod;

public DelegateMethodBinding(int modifiers, ReferenceBinding declaringClass, AbstractMethodDeclaration delegateMethod) {
super(modifiers, null, null, declaringClass);
this.delegateMethod = delegateMethod;
}

public DelegateMethodBinding(int modifiers, char[] selector, ReferenceBinding declaringClass, AbstractMethodDeclaration delegateMethod) {
super(modifiers, selector, null, null, null, declaringClass);
this.delegateMethod = delegateMethod;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,18 @@ MethodBinding createMethod(AbstractMethodDeclaration method) {
if (method.isConstructor()) {
if (method.isDefaultConstructor())
modifiers |= ExtraCompilerModifiers.AccIsDefaultConstructor;
// GROOVY add
if (method.sourceStart > 0 && method.declarationSourceStart <= 0) {
TypeDeclaration type = enclosingClassScope().referenceContext;
for (AbstractMethodDeclaration m : type.methods) {
if (m != method && m.sourceStart == method.sourceStart) {
method.binding = new DelegateMethodBinding(modifiers, declaringClass, m);
break;
}
}
}
if (method.binding == null)
// GROOVY end
method.binding = new MethodBinding(modifiers, null, null, declaringClass);
checkAndSetModifiersForConstructor(method.binding);
} else {
Expand All @@ -387,6 +399,18 @@ MethodBinding createMethod(AbstractMethodDeclaration method) {
modifiers |= ClassFileConstants.AccPublic | ClassFileConstants.AccAbstract;
}
}
// GROOVY add
if (method.sourceStart > 0 && method.declarationSourceStart <= 0) {
TypeDeclaration type = enclosingClassScope().referenceContext;
for (AbstractMethodDeclaration m : type.methods) {
if (m != method && m.sourceStart == method.sourceStart) {
method.binding = new DelegateMethodBinding(modifiers, method.selector, declaringClass, m);
break;
}
}
}
if (method.binding == null)
// GROOVY end
method.binding =
new MethodBinding(modifiers, method.selector, null, null, null, declaringClass);
checkAndSetModifiersForMethod(method.binding);
Expand Down Expand Up @@ -431,8 +455,8 @@ MethodBinding createMethod(AbstractMethodDeclaration method) {

TypeParameter[] typeParameters = method.typeParameters();
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=324850, If they exist at all, process type parameters irrespective of source level.
if (typeParameters == null || typeParameters.length == 0) {
method.binding.typeVariables = Binding.NO_TYPE_VARIABLES;
if (typeParameters == null || typeParameters.length == 0) {
method.binding.typeVariables = Binding.NO_TYPE_VARIABLES;
} else {
method.binding.typeVariables = createTypeVariables(typeParameters, method.binding);
method.binding.modifiers |= ExtraCompilerModifiers.AccGenericSignature;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2009-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.eclipse.jdt.internal.compiler.lookup;

import org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration;

public class DelegateMethodBinding extends MethodBinding {

public final AbstractMethodDeclaration delegateMethod;

public DelegateMethodBinding(int modifiers, ReferenceBinding declaringClass, AbstractMethodDeclaration delegateMethod) {
super(modifiers, null, null, declaringClass);
this.delegateMethod = delegateMethod;
}

public DelegateMethodBinding(int modifiers, char[] selector, ReferenceBinding declaringClass, AbstractMethodDeclaration delegateMethod) {
super(modifiers, selector, null, null, null, declaringClass);
this.delegateMethod = delegateMethod;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,18 @@ MethodBinding createMethod(AbstractMethodDeclaration method) {
if (method.isConstructor()) {
if (method.isDefaultConstructor())
modifiers |= ExtraCompilerModifiers.AccIsDefaultConstructor;
// GROOVY add
if (method.sourceStart > 0 && method.declarationSourceStart <= 0) {
TypeDeclaration type = enclosingClassScope().referenceContext;
for (AbstractMethodDeclaration m : type.methods) {
if (m != method && m.sourceStart == method.sourceStart) {
method.binding = new DelegateMethodBinding(modifiers, declaringClass, m);
break;
}
}
}
if (method.binding == null)
// GROOVY end
method.binding = new MethodBinding(modifiers, null, null, declaringClass);
checkAndSetModifiersForConstructor(method.binding);
} else {
Expand All @@ -387,6 +399,18 @@ MethodBinding createMethod(AbstractMethodDeclaration method) {
modifiers |= ClassFileConstants.AccPublic | ClassFileConstants.AccAbstract;
}
}
// GROOVY add
if (method.sourceStart > 0 && method.declarationSourceStart <= 0) {
TypeDeclaration type = enclosingClassScope().referenceContext;
for (AbstractMethodDeclaration m : type.methods) {
if (m != method && m.sourceStart == method.sourceStart) {
method.binding = new DelegateMethodBinding(modifiers, method.selector, declaringClass, m);
break;
}
}
}
if (method.binding == null)
// GROOVY end
method.binding =
new MethodBinding(modifiers, method.selector, null, null, null, declaringClass);
checkAndSetModifiersForMethod(method.binding);
Expand Down Expand Up @@ -431,8 +455,8 @@ MethodBinding createMethod(AbstractMethodDeclaration method) {

TypeParameter[] typeParameters = method.typeParameters();
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=324850, If they exist at all, process type parameters irrespective of source level.
if (typeParameters == null || typeParameters.length == 0) {
method.binding.typeVariables = Binding.NO_TYPE_VARIABLES;
if (typeParameters == null || typeParameters.length == 0) {
method.binding.typeVariables = Binding.NO_TYPE_VARIABLES;
} else {
method.binding.typeVariables = createTypeVariables(typeParameters, method.binding);
method.binding.modifiers |= ExtraCompilerModifiers.AccGenericSignature;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2009-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.eclipse.jdt.internal.compiler.lookup;

import org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration;

public class DelegateMethodBinding extends MethodBinding {

public final AbstractMethodDeclaration delegateMethod;

public DelegateMethodBinding(int modifiers, ReferenceBinding declaringClass, AbstractMethodDeclaration delegateMethod) {
super(modifiers, null, null, declaringClass);
this.delegateMethod = delegateMethod;
}

public DelegateMethodBinding(int modifiers, char[] selector, ReferenceBinding declaringClass, AbstractMethodDeclaration delegateMethod) {
super(modifiers, selector, null, null, null, declaringClass);
this.delegateMethod = delegateMethod;
}
}
Loading

1 comment on commit 823faf7

@eric-milles
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.