Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-2135

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

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 1.1-beta-3
    • 1.5.7, 1.6-beta-2
    • None
    • None
    • Groovy1.1 rc1, jdk6u2, winxp

    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)

      Attachments

        Activity

          People

            blackdrag Jochen Theodorou
            daniel_sun Daniel Sun
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: