[ 
https://issues.apache.org/jira/browse/GROOVY-11237?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17790748#comment-17790748
 ] 

ASF GitHub Bot commented on GROOVY-11237:
-----------------------------------------

eric-milles commented on code in PR #2001:
URL: https://github.com/apache/groovy/pull/2001#discussion_r1408404532


##########
src/test/groovy/InterfaceTest.groovy:
##########
@@ -91,4 +91,19 @@ final class InterfaceTest extends CompilableTestSupport {
             assert impl2.foo() == 'hello from Foo#foo'
         '''
     }
+
+    // GROOVY-11237
+    void testStaticInterfaceMethod() {
+        assertScript '''
+            interface Foo {
+                static hello(where) { "hello $where" }
+                static String BAR = 'bar'
+                String BAA = 'baa' // implicit static
+            }
+
+            assert Foo.hello('world') == 'hello world'
+            assert Foo.BAR == 'bar'
+            assert Foo.BAA == 'baa'
+        '''

Review Comment:
   Can you also check that property methods are not generated:
   ```groovy
               groovy.test.GroovyAssert.shouldFail(MissingMethodException) {
                   Foo.getBAR()
               }
               groovy.test.GroovyAssert.shouldFail(MissingMethodException) {
                   Foo.getBAA()
               }
   ```



##########
src/main/java/org/codehaus/groovy/classgen/ClassCompletionVerifier.java:
##########
@@ -418,10 +418,6 @@ private void checkMethodsForIncorrectModifiers(final 
ClassNode cn) {
                 addError("The " + getDescription(method) + " from " + 
getDescription(cn) +

Review Comment:
   In an interface, is final allowed on private or static methods?  If allowed, 
this check needs a second look.  The message may need to check for method with 
and without body before stating "abstract by definition".



##########
src/main/java/org/codehaus/groovy/classgen/ClassCompletionVerifier.java:
##########
@@ -418,10 +418,6 @@ private void checkMethodsForIncorrectModifiers(final 
ClassNode cn) {
                 addError("The " + getDescription(method) + " from " + 
getDescription(cn) +
                         " must not be final. It is by definition abstract.", 
method);
             }
-            if (method.isStatic() && !method.isStaticConstructor()) {
-                addError("The " + getDescription(method) + " from " + 
getDescription(cn) +
-                        " must not be static. Only fields may be static in an 
interface.", method);
-            }
         }
     }
 

Review Comment:
   `checkInterfaceMethodVisibility` may need some changes.  I rewrote it like 
this:
   ```java
       private void checkInterfaceMethodVisibility(final ClassNode node) {
           if (!node.isInterface()) return;
           for (MethodNode method : node.getMethods()) {
               if (!method.isPublic()) {
                   if (method.isAbstract()) {
                       addError("Method '" + method.getName() + "' from " + 
getDescription(node) +
                               " must be public as it is declared as an 
abstract method.", method);
                   } else if (!method.isPrivate() && 
!method.isStaticConstructor()) {
                       addError("Method '" + method.getName() + "' is " +
                               (method.isProtected() ? "protected" : 
"package-private") +
                               " but should be private in " + 
getDescription(node) + ".", method);
                   }
               }
           }
       }
   ```





> Support static interface methods
> --------------------------------
>
>                 Key: GROOVY-11237
>                 URL: https://issues.apache.org/jira/browse/GROOVY-11237
>             Project: Groovy
>          Issue Type: Sub-task
>          Components: Compiler
>            Reporter: Christopher Smith
>            Assignee: Paul King
>            Priority: Major
>
> Apologies if this is a dupe, but I went back as far as 6000, and GROOVY-8299 
> is the only relevant ticket I saw.
> Since Java 8, interfaces can carry static methods like classes. Groovy still 
> (as of 3.0.8) does not support adding static methods to interfaces.
> {code}
> The method 'java.lang.String dynamoTableNameForType(java.lang.String)' from 
> interface 'com.example.HasDynamo' must not be static. Only fields may be 
> static in an interface.
> {code}



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to