Skip to content

Commit

Permalink
GROOVY-7984: support withTraits and @SelfType(ClassType)
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Jan 30, 2024
1 parent f18f92f commit 7b2205d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public void visitClass(final ClassNode node) {
checkMethodsForWeakerAccess(node);
checkMethodsForOverridingFinal(node);
checkNoAbstractMethodsNonAbstractClass(node);
checkClassExtendsAllSelfTypes(node);
checkClassExtendsOrImplementsSelfTypes(node);
checkNoStaticMethodWithSameSignatureAsNonStatic(node);
checkGenericsUsage(node, node.getUnresolvedInterfaces());
checkGenericsUsage(node, node.getUnresolvedSuperClass());
Expand Down Expand Up @@ -234,22 +234,16 @@ private void checkNoAbstractMethodsNonAbstractClass(final ClassNode node) {
}
}

private void checkClassExtendsAllSelfTypes(final ClassNode node) {
private void checkClassExtendsOrImplementsSelfTypes(final ClassNode node) {
if (node.isInterface()) return;
for (ClassNode anInterface : GeneralUtils.getInterfacesAndSuperInterfaces(node)) {
if (Traits.isTrait(anInterface)) {
LinkedHashSet<ClassNode> selfTypes = new LinkedHashSet<ClassNode>();
for (ClassNode type : Traits.collectSelfTypes(anInterface, selfTypes, true, false)) {
if (type.isInterface() && !node.implementsInterface(type)) {
addError(getDescription(node)
+ " implements " + getDescription(anInterface)
+ " but does not implement self type " + getDescription(type),
anInterface);
} else if (!type.isInterface() && !node.isDerivedFrom(type)) {
addError(getDescription(node)
+ " implements " + getDescription(anInterface)
+ " but does not extend self type " + getDescription(type),
anInterface);
for (ClassNode selfType : Traits.collectSelfTypes(anInterface, new LinkedHashSet<>(), true, false)) {
ClassNode superClass;
if (selfType.isInterface() ? !node.implementsInterface(selfType) : !(node.isDerivedFrom(selfType)
|| ((superClass = node.getNodeMetaData("super.class")) != null && superClass.isDerivedFrom(selfType)))) {
addError(getDescription(node) + " implements " + getDescription(anInterface) + " but does not " +
(selfType.isInterface() ? "implement" : "extend") + " self type " + getDescription(selfType), anInterface);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,12 +186,12 @@ public ProxyGeneratorAdapter(
}
this.hasWildcard = wildcard;

this.delegateClass = delegateClass;
Class<?> fixedSuperClass = adjustSuperClass(superClass, interfaces);
// if we have to delegate to another object, generate the appropriate delegate field
// and collect the name of the methods for which delegation is active
this.generateDelegateField = delegateClass != null;
this.objectDelegateMethods = generateDelegateField ? createDelegateMethodList(fixedSuperClass, delegateClass, interfaces) : Collections.emptySet();
this.delegateClass = delegateClass;

// a proxy is supposed to be a concrete class, so it cannot extend an interface.
// If the provided superclass is an interface, then we replace the superclass with Object
Expand Down Expand Up @@ -240,6 +240,7 @@ private Class<?> adjustSuperClass(final Class<?> superClass, Class<?>[] interfac
if (!traits.isEmpty()) {
String name = superClass.getName() + "$TraitAdapter";
ClassNode cn = new ClassNode(name, ACC_PUBLIC | ACC_ABSTRACT, ClassHelper.OBJECT_TYPE, traits.toArray(ClassNode.EMPTY_ARRAY), null);
if (delegateClass != null) cn.putNodeMetaData("super.class", ClassHelper.make(delegateClass)); // GROOVY-7984
CompilationUnit cu = new CompilationUnit(innerLoader);
CompilerConfiguration config = new CompilerConfiguration();
SourceUnit su = new SourceUnit(name + "wrapper", "", config, innerLoader, new ErrorCollector(config));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
*/
package org.codehaus.groovy.transform.traitx

import groovy.test.NotYetImplemented
import groovy.transform.SelfType
import org.codehaus.groovy.ast.ClassHelper
import org.codehaus.groovy.ast.expr.ClassExpression
Expand Down Expand Up @@ -626,7 +625,7 @@ final class TraitASTTransformationTest {
}

// GROOVY-7984
@Test @NotYetImplemented
@Test
void testRuntimeWithTraitsDGM2() {
assertScript shell, '''
class C {
Expand Down

0 comments on commit 7b2205d

Please sign in to comment.