groovy
  1. groovy
  2. GROOVY-3971

adding method to class in ASTBuilder not working

    Details

    • Type: Bug Bug
    • Status: In Progress In Progress
    • Priority: Major Major
    • Resolution: Unresolved
    • Affects Version/s: 1.7-rc-2
    • Fix Version/s: None
    • Component/s: ast builder
    • Labels:
      None
    • Environment:
      Groovy 1.7-RC-2 on jre1.6.0_04 on Windows Vista SP2
    • Number of attachments :
      1

      Description

      The following code produces class MyClass OK, but running the class produces error:
      Exception in thread "main" java.lang.NoSuchMethodError: main

      Here's the ASTBuilder code I used:

      import static org.objectweb.asm.Opcodes.ACC_PUBLIC
      import static org.objectweb.asm.Opcodes.ACC_STATIC
      import org.codehaus.groovy.ast.builder.AstBuilder
      import org.codehaus.groovy.control.CompilerConfiguration
      import org.codehaus.groovy.control.CompilationUnit
      
      def classes= new AstBuilder().buildFromSpec{
        classNode 'MyClass', ACC_PUBLIC, {
          classNode Object //superclass
          interfaces{
            classNode GroovyObject
          }
          mixins{}
          genericsTypes{}
          method('main', ACC_PUBLIC & ACC_STATIC, Void.class) {
            parameters{
              parameter 'args': String[].class
            }
            exceptions{}
            block{
              methodCall {
                variable "this"
                constant "println"
                argumentList {
                  constant "Hello, world!"
                }
              }
            }
          }
        }
      }
      
      def gcl= new GroovyClassLoader()
      def sec= null
      def ccfg= CompilerConfiguration.DEFAULT
      def cu= new CompilationUnit(ccfg, sec, gcl)
      cu.addClassNode(classes[0])
      cu.compile() 
      

      Also tried this but same result:

      import static org.objectweb.asm.Opcodes.ACC_PUBLIC
      import static org.objectweb.asm.Opcodes.ACC_STATIC
      import org.codehaus.groovy.ast.*
      import java.security.CodeSource
      import org.codehaus.groovy.ast.builder.AstBuilder
      import org.codehaus.groovy.control.CompilerConfiguration
      import org.codehaus.groovy.control.CompilationUnit
      import org.codehaus.groovy.control.CompilePhase
      
      def classes= new AstBuilder().buildFromString(CompilePhase.CONVERSION, """\
      public class MyClass{
        public static void main(String[] args){
          println "Hello, world!"
        }
      }
      """)
      
      def gcl= new GroovyClassLoader()
      def sec= null //new CodeSource()
      def ccfg= CompilerConfiguration.DEFAULT
      def cu= new CompilationUnit(ccfg, sec, gcl)
      cu.addClassNode(classes[1])
      cu.compile()
      

        Activity

          People

          • Assignee:
            Unassigned
            Reporter:
            Gavin Grover
          • Votes:
            1 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated: