groovy
  1. groovy
  2. GROOVY-2135

failed to reference an instance defined in enum class with the syntax like Planet.MERCURY

    Details

    • Type: Bug Bug
    • Status: Closed Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 1.1-beta-3
    • Fix Version/s: 1.5.7, 1.6-beta-2
    • Component/s: None
    • Labels:
      None
    • Environment:
      Groovy1.1 rc1, jdk6u2, winxp
    • Number of attachments :
      0

      Description

      I failed to run the following code with groovy1.1 rc1

      Planet.groovy
      public enum Planet {
          MERCURY (3.303e+23, 2.4397e6),
          VENUS   (4.869e+24, 6.0518e6),
          EARTH   (5.976e+24, 6.37814e6),
          MARS    (6.421e+23, 3.3972e6),
          JUPITER (1.9e+27,   7.1492e7),
          SATURN  (5.688e+26, 6.0268e7),
          URANUS  (8.686e+25, 2.5559e7),
          NEPTUNE (1.024e+26, 2.4746e7),
          PLUTO   (1.27e+22,  1.137e6);
      
          private final double mass;   // in kilograms
          private final double radius; // in meters
          Planet(double mass, double radius) {
              this.mass = mass;
              this.radius = radius;
          }
          public double mass()   { return mass; }
          public double radius() { return radius; }
      
          // universal gravitational constant  (m3 kg-1 s-2)
          public static final double G = 6.67300E-11;
      
          public double surfaceGravity() {
              return G * mass / (radius * radius);
          }
          public double surfaceWeight(double otherMass) {
              return otherMass * surfaceGravity();
          }
          public String toString() {
              return mass + ", " + radius;
          }
          
          public static void main(String[] args) {
          	System.out.println(Planet.MERCURY);
          }
      }
      
      

      Groovy complains:
      Exception thrown: java.lang.InstantiationException: Planet

      java.lang.InstantiationException: Planet
      at java.lang.Class.newInstance0(Class.java:340)
      at java.lang.Class.newInstance(Class.java:308)
      at org.codehaus.groovy.runtime.InvokerHelper.createScript(InvokerHelper.java:366)
      at groovy.lang.GroovyShell.parse(GroovyShell.java:500)
      at groovy.lang.GroovyShell.parse(GroovyShell.java:480)
      at groovy.lang.GroovyShell.evaluate(GroovyShell.java:458)
      at groovy.lang.GroovyShell.evaluate(GroovyShell.java:400)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:597)
      at org.codehaus.groovy.runtime.metaclass.ReflectionMetaMethod.invoke(ReflectionMetaMethod.java:64)
      at org.codehaus.groovy.runtime.MetaClassHelper.doMethodInvoke(MetaClassHelper.java:537)
      at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:772)
      at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:614)
      at org.codehaus.groovy.runtime.Invoker.invokePogoMethod(Invoker.java:98)
      at org.codehaus.groovy.runtime.Invoker.invokeMethod(Invoker.java:79)
      at org.codehaus.groovy.runtime.InvokerHelper.invokeMethod(InvokerHelper.java:74)
      at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodN(ScriptBytecodeAdapter.java:158)
      at groovy.ui.Console$_runScript_closure10.doCall(Console.groovy:681)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:597)
      at org.codehaus.groovy.runtime.metaclass.ReflectionMetaMethod.invoke(ReflectionMetaMethod.java:64)
      at org.codehaus.groovy.runtime.MetaClassHelper.doMethodInvoke(MetaClassHelper.java:537)
      at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:224)
      at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodOnCurrentN(ScriptBytecodeAdapter.java:66)
      at groovy.ui.Console$_runScript_closure10.doCall(Console.groovy)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:597)
      at org.codehaus.groovy.runtime.metaclass.ReflectionMetaMethod.invoke(ReflectionMetaMethod.java:64)
      at org.codehaus.groovy.runtime.MetaClassHelper.doMethodInvoke(MetaClassHelper.java:537)
      at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:224)
      at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:614)
      at groovy.lang.Closure.call(Closure.java:292)
      at groovy.lang.Closure.call(Closure.java:287)
      at groovy.lang.Closure.run(Closure.java:368)
      at java.lang.Thread.run(Thread.java:619)

        Activity

        Hide
        Daniel.Sun added a comment -

        I ran the code again in the groovy console with groovy1.6beta1.
        the issue still exists

        groovy> public enum Planet {
        groovy> MERCURY (3.303e+23, 2.4397e6),
        groovy> VENUS (4.869e+24, 6.0518e6),
        groovy> EARTH (5.976e+24, 6.37814e6),
        groovy> MARS (6.421e+23, 3.3972e6),
        groovy> JUPITER (1.9e+27, 7.1492e7),
        groovy> SATURN (5.688e+26, 6.0268e7),
        groovy> URANUS (8.686e+25, 2.5559e7),
        groovy> NEPTUNE (1.024e+26, 2.4746e7),
        groovy> PLUTO (1.27e+22, 1.137e6);
        groovy> private final double mass; // in kilograms
        groovy> private final double radius; // in meters
        groovy> Planet(double mass, double radius)

        { groovy> this.mass = mass; groovy> this.radius = radius; groovy> }

        groovy> public double mass()

        { return mass; }

        groovy> public double radius()

        { return radius; }

        groovy> // universal gravitational constant (m3 kg-1 s-2)
        groovy> public static final double G = 6.67300E-11;
        groovy> public double surfaceGravity()

        { groovy> return G * mass / (radius * radius); groovy> }

        groovy> public double surfaceWeight(double otherMass)

        { groovy> return otherMass * surfaceGravity(); groovy> }

        groovy> public String toString()

        { groovy> return mass + ", " + radius; groovy> }

        groovy>
        groovy> public static void main(String[] args)

        { groovy> System.out.println(Planet.MERCURY); groovy> }

        groovy> }

        Exception thrown: java.lang.InstantiationException: Planet

        java.lang.InstantiationException: Planet

        Show
        Daniel.Sun added a comment - I ran the code again in the groovy console with groovy1.6beta1. the issue still exists groovy> public enum Planet { groovy> MERCURY (3.303e+23, 2.4397e6), groovy> VENUS (4.869e+24, 6.0518e6), groovy> EARTH (5.976e+24, 6.37814e6), groovy> MARS (6.421e+23, 3.3972e6), groovy> JUPITER (1.9e+27, 7.1492e7), groovy> SATURN (5.688e+26, 6.0268e7), groovy> URANUS (8.686e+25, 2.5559e7), groovy> NEPTUNE (1.024e+26, 2.4746e7), groovy> PLUTO (1.27e+22, 1.137e6); groovy> private final double mass; // in kilograms groovy> private final double radius; // in meters groovy> Planet(double mass, double radius) { groovy> this.mass = mass; groovy> this.radius = radius; groovy> } groovy> public double mass() { return mass; } groovy> public double radius() { return radius; } groovy> // universal gravitational constant (m3 kg-1 s-2) groovy> public static final double G = 6.67300E-11; groovy> public double surfaceGravity() { groovy> return G * mass / (radius * radius); groovy> } groovy> public double surfaceWeight(double otherMass) { groovy> return otherMass * surfaceGravity(); groovy> } groovy> public String toString() { groovy> return mass + ", " + radius; groovy> } groovy> groovy> public static void main(String[] args) { groovy> System.out.println(Planet.MERCURY); groovy> } groovy> } Exception thrown: java.lang.InstantiationException: Planet java.lang.InstantiationException: Planet
        Hide
        Paul King added a comment -

        Removed groovysh as component as I think this is a GroovyShell not groovysh issue. I still get the following on 1.5.6 and 1.6-beta1 when running the original example in GroovyConsole: Exception thrown: java.lang.InstantiationException: Planet.

        Show
        Paul King added a comment - Removed groovysh as component as I think this is a GroovyShell not groovysh issue. I still get the following on 1.5.6 and 1.6-beta1 when running the original example in GroovyConsole: Exception thrown: java.lang.InstantiationException: Planet .
        Hide
        blackdrag blackdrag added a comment -

        doesn't groovysh use GroovyShell? I added that component, because of the comment from November 3rd... I would also like to reassign this issue to someone else

        Show
        blackdrag blackdrag added a comment - doesn't groovysh use GroovyShell? I added that component, because of the comment from November 3rd... I would also like to reassign this issue to someone else
        Hide
        Paul King added a comment -

        I just tried this again on trunk in both groovyConsole and groovysh and the problem seems to have gome away. Anyone object to closing this?

        Show
        Paul King added a comment - I just tried this again on trunk in both groovyConsole and groovysh and the problem seems to have gome away. Anyone object to closing this?
        Hide
        Paul King added a comment -

        Please reopen if you find any issues.

        Show
        Paul King added a comment - Please reopen if you find any issues.

          People

          • Assignee:
            blackdrag blackdrag
            Reporter:
            Daniel.Sun
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: