groovy

Refactor GCL to allow greater extensibility by sub classes

Details

  • Type: Improvement Improvement
  • Status: Closed Closed
  • Priority: Major Major
  • Resolution: Fixed
  • Affects Version/s: 1.0-JSR-5
  • Fix Version/s: 1.0-JSR-6
  • Component/s: class generator
  • Labels:
    None
  • Number of attachments :
    0

Description

In grails we want to implement a mechanism that allows the addition of properties at compilation time, thus allowing an even more concise syntax for GORM (see http://jira.codehaus.org/browse/GRAILS-139)

but currently the dangers in this are too great as the GroovyClassLoader is not extensible enough and needs to be refactored to allow sub-classes to override the necessary methods to provide said functionality.

Activity

Hide
Graeme Rocher added a comment -

Any progress on this?

Cheers!

Show
Graeme Rocher added a comment - Any progress on this? Cheers!
Hide
blackdrag blackdrag added a comment -

there are now various extension point in GCL that can be used to reach the goal. for exmaple this groovy code here adds an additional property to each generated class:

import java.security.CodeSource
import org.codehaus.groovy.control.*
import org.codehaus.groovy.ast.*
import org.codehaus.groovy.classgen.*

class PropertyAdder extends CompilationUnit.PrimaryClassNodeOperation {
void call(SourceUnit source, GeneratorContext context, ClassNode classNode) { classNode.addProperty("id", ClassNode.ACC_PUBLIC, ClassHelper.long_TYPE,null,null,null); }
}

class CustomPhaseOperation extends GroovyClassLoader {
CompilationUnit createCompilationUnit(CompilerConfiguration config, CodeSource source) { def cu = super.createCompilationUnit(config,source) cu.addPhaseOperation(new PropertyAdder (),Phases.CONVERSION) return cu }
}

def loader = new CustomPhaseOperation ()
def ret = loader.parseClass("""class Foo{}""")
def field = ret.declaredFields.find {it.name=="id" && it.type==Long.TYPE}
assert field != null

Show
blackdrag blackdrag added a comment - there are now various extension point in GCL that can be used to reach the goal. for exmaple this groovy code here adds an additional property to each generated class: import java.security.CodeSource import org.codehaus.groovy.control.* import org.codehaus.groovy.ast.* import org.codehaus.groovy.classgen.* class PropertyAdder extends CompilationUnit.PrimaryClassNodeOperation { void call(SourceUnit source, GeneratorContext context, ClassNode classNode) { classNode.addProperty("id", ClassNode.ACC_PUBLIC, ClassHelper.long_TYPE,null,null,null); } } class CustomPhaseOperation extends GroovyClassLoader { CompilationUnit createCompilationUnit(CompilerConfiguration config, CodeSource source) { def cu = super.createCompilationUnit(config,source) cu.addPhaseOperation(new PropertyAdder (),Phases.CONVERSION) return cu } } def loader = new CustomPhaseOperation () def ret = loader.parseClass("""class Foo{}""") def field = ret.declaredFields.find {it.name=="id" && it.type==Long.TYPE} assert field != null

People

Vote (0)
Watch (0)

Dates

  • Created:
    Updated:
    Resolved: