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

Eric Milles commented on GROOVY-10746:
--------------------------------------

I don't have a recreation script/project yet, so I am going to speculate a bit. 
 To make "super.keys()" work in dynamic groovy, it creates a MOP method 
"super$n$keys()" in {{SortedProperties}}.  The "n" is based on the distance 
between {{SortedProperties}} and {{Object}}.  Anyways, we have seen cases where 
the "n" changes from release to release.  You can use javap on your 
{{SortedProperties.class}} to see which was chosen at compile time.  The other 
half of this is the selection of "keys()" or "super$n$keys()" in the 
{{MetaMethodIndex}}.  You can see in the debugger 
{{MetaClassImpl#invokeMethod}} read the method index and pick from what's there 
for "super.keys()" aka "invokeMethodOnSuper0(...)".

You can also put {{@groovy.transform.CompileStatic}} on your method which makes 
the "super.keys()" method call to get static (aka compile-time) selection like 
java provides.

> Calling method in Super class causes java.lang.StackOverflowError with Java 
> 1.8
> -------------------------------------------------------------------------------
>
>                 Key: GROOVY-10746
>                 URL: https://issues.apache.org/jira/browse/GROOVY-10746
>             Project: Groovy
>          Issue Type: Bug
>    Affects Versions: 2.5.17, 3.0.12, 4.0.4
>            Reporter: Troy Harris
>            Priority: Major
>              Labels: StackOverflowError
>
> Consider the class SortedProperties below which extends java.util.Properties 
> and overrides Properties.keys().
> {code:java}
> import java.util.Enumeration;
> import java.util.Collections;
> class SortedProperties extends Properties {
>     
>   @Override 
>   public synchronized Enumeration keys() {
>      Enumeration keysEnum = super.keys();
>      Vector keyList = new Vector();
>      while(keysEnum.hasMoreElements()){
>        keyList.add(keysEnum.nextElement());
>      }
>      Collections.sort(keyList);
>      return keyList.elements();
>   }
> }   {code}
> In this scenario the Groovy class above is compiled with Groovy 3.0.12 and 
> JDK 11 but using the flag "--release 1.8" for JDK 1.8 compatibility.
> Attempting to call the overridden keys() method from the following TestGroovy 
> class (compiled the same way) will yield a StackOverflowException when 
> running with Java 1.8.  This error is not reproduceable when running with 
> Java 11.
> {code:java}
> import java.util.Properties;
> public class TestGroovy {
>   public static void main(String[] args) {
>         SortedProperties properties = new SortedProperties();
>         properties.setProperty("FirstName", "John");
>         properties.setProperty("LastName", "Doe");
>         properties.keys();
>     }
> } {code}
> Truncated Stacktrace:
> {noformat}
> Exception in thread "main" java.lang.StackOverflowError
>         at 
> org.codehaus.groovy.runtime.metaclass.MetaMethodIndex.getMethods(MetaMethodIndex.java:210)
>         at 
> groovy.lang.MetaClassImpl.getMethodWithCaching(MetaClassImpl.java:1371)
>         at groovy.lang.MetaClassImpl.getMetaMethod(MetaClassImpl.java:1280)
>         at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1132)
>         at 
> org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodOnSuperN(ScriptBytecodeAdapter.java:144)
>         at 
> org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodOnSuper0(ScriptBytecodeAdapter.java:164)
>         at com.foo.SortedProperties.keys(SortedProperties.groovy:12)
>         at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
>         at 
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
>         at java.lang.reflect.Method.invoke(Method.java:498)
>         at 
> org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:107)
>         at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:323)
>         at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1268)
>         at 
> org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodOnSuperN(ScriptBytecodeAdapter.java:144)
>         at 
> org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodOnSuper0(ScriptBytecodeAdapter.java:164)
>         at com.foo.SortedProperties.keys(SortedProperties.groovy:12)
>         at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
>         at 
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
>         at java.lang.reflect.Method.invoke(Method.java:498)
>         at 
> org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:107)
>         at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:323)
>         at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1268)
>         at 
> org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodOnSuperN(ScriptBytecodeAdapter.java:144)
>         at 
> org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodOnSuper0(ScriptBytecodeAdapter.java:164)
>         at com.foo.SortedProperties.keys(SortedProperties.groovy:12){noformat}
>  
>  



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

Reply via email to