Skip to content

Commit

Permalink
trait @Implemented method
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Sep 27, 2023
1 parent f4da80e commit a505d80
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,36 @@ public void testTraits1() {

checkGCUDeclaration("T.groovy",
"public @groovy.transform.Trait interface T {\n" +
" public String getFoo();\n" +
" public @org.codehaus.groovy.transform.trait.Traits.Implemented String getFoo();\n" +
"}");
}

@Test
public void testTraits1a() {
//@formatter:off
String[] sources = {
"C.groovy",
"class C implements T {\n" +
"}\n",
"T.groovy",
"trait T {\n" +
" final String foo = 'foo'\n" +
"}\n",
};
//@formatter:on

runNegativeTest(sources, "");

checkGCUDeclaration("C.groovy",
"public class C implements T {\n" +
" public @groovy.transform.Generated C() {\n" +
" }\n" +
//" public @groovy.transform.Generated @org.codehaus.groovy.transform.trait.Traits.TraitBridge String getFoo() {\n" +
//" }\n" +
"}");
checkGCUDeclaration("T.groovy",
"public @groovy.transform.Trait interface T {\n" +
//" public @groovy.transform.Generated @org.codehaus.groovy.transform.trait.Traits.Implemented String getFoo();\n" +
"}");
}

Expand All @@ -69,9 +98,6 @@ public void testTraits2() {
//@formatter:on

runConformTest(sources, "Hello, Bob!");

ClassNode classNode = getCUDeclFor("Greetable.groovy").getCompilationUnit().getClassNode("Greetable");
assertTrue(classNode.isInterface());
}

@Test
Expand All @@ -96,9 +122,6 @@ public void testTraits2a() {
//@formatter:on

runConformTest(sources, "Hello, Bob!");

ClassNode classNode = getCUDeclFor("Greetable.groovy").getCompilationUnit().getClassNode("Greetable");
assertTrue(classNode.isInterface());
}

@Test // Abstract Methods
Expand Down Expand Up @@ -231,10 +254,10 @@ public void testTraits6() {

CompilationUnit unit = getCUDeclFor("Script.groovy").getCompilationUnit();
ClassNode classNode = unit.getClassNode("Person");
ClassNode type = unit.getClassNode("Greetable");
assertTrue(classNode.implementsInterface(type));
type = unit.getClassNode("Named");
assertTrue(classNode.implementsInterface(type));
ClassNode face = unit.getClassNode("Greetable");
assertTrue(classNode.implementsInterface(face));
face = unit.getClassNode("Named");
assertTrue(classNode.implementsInterface(face));
}

@Test // Properties
Expand Down Expand Up @@ -514,7 +537,7 @@ public void testTraits17b() {
"}\n" +
"class D extends C implements T {\n" +
"}\n" +
"print new D().getIdentity()",
"print new D().getIdentity()\n",
};
//@formatter:on

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ protected MethodBinding[] augmentMethodBindings(final MethodBinding[] methodBind
if (superInterfaces.length > 1) Collections.reverse(Arrays.asList(superInterfaces));

boolean implementsGroovyObject = false;
for (ReferenceBinding face : superInterfaces) {
if (CharOperation.equals(face.compoundName, GroovyCompilationUnitScope.GROOVY_LANG_GROOVYOBJECT)) {
for (ReferenceBinding superInterface : superInterfaces) {
if (CharOperation.equals(superInterface.compoundName, GroovyCompilationUnitScope.GROOVY_LANG_GROOVYOBJECT)) {
implementsGroovyObject = true;
break;
}
Expand Down Expand Up @@ -228,6 +228,7 @@ protected MethodBinding[] augmentMethodBindings(final MethodBinding[] methodBind
if (method.isStatic()) {
method = new MethodBinding(method, sourceType);
method.modifiers &= ~Flags.AccPrivate;
method.modifiers |= Flags.AccPublic;
groovyMethods.add(method);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1563,13 +1563,19 @@ private void createMethodDeclarations(ClassNode classNode, GroovyTypeDeclaration
if (!variables.isEmpty()) methodDecl.statements = createStatements(variables.values());
}

if (isTrait && methodNode.isFinal()) {
methodDecl.modifiers ^= Flags.AccFinal | ExtraCompilerModifiers.AccBlankFinal;
} else if (isTrait && methodNode.isStatic()) {
if (unitDeclaration.compilerOptions.targetJDK >= ClassFileConstants.JDK9) {
methodDecl.modifiers ^= Flags.AccPublic | Flags.AccPrivate; // hide from JDT
} else if (unitDeclaration.compilerOptions.targetJDK < ClassFileConstants.JDK1_8) {
methodDecl.modifiers ^= Flags.AccStatic | ExtraCompilerModifiers.AccModifierProblem;
if (isTrait) {
if (methodNode.isFinal()) {
methodDecl.modifiers ^= Flags.AccFinal | ExtraCompilerModifiers.AccBlankFinal;
} else if (methodNode.isStatic()) {
if (unitDeclaration.compilerOptions.targetJDK >= ClassFileConstants.JDK9) {
methodDecl.modifiers ^= Flags.AccPublic | Flags.AccPrivate; // hide from JDT
} else if (unitDeclaration.compilerOptions.targetJDK < ClassFileConstants.JDK1_8) {
methodDecl.modifiers ^= Flags.AccStatic | ExtraCompilerModifiers.AccModifierProblem;
}
}
if (methodNode.isPublic() && !methodNode.isAbstract()) {
Annotation[] implemented = createAnnotations(org.codehaus.groovy.transform.trait.Traits.IMPLEMENTED_CLASSNODE);
methodDecl.annotations = methodDecl.annotations == null ? implemented : ArrayUtils.concat(methodDecl.annotations, implemented);
}
}

Expand Down Expand Up @@ -2610,7 +2616,7 @@ private int getModifiers(FieldNode node) {
private int getModifiers(MethodNode node) {
int modifiers = node.getModifiers() & ~(Flags.AccSynthetic | Flags.AccTransient); // GRECLIPSE-370, GROOVY-10140
if (node.isDefault()) {
modifiers |= Flags.AccDefaultMethod;
modifiers |= ExtraCompilerModifiers.AccDefaultMethod;
}
if (node.getCode() == null) {
modifiers |= ExtraCompilerModifiers.AccSemicolonBody;
Expand Down

0 comments on commit a505d80

Please sign in to comment.