

![]() | Start a set with this search |
![]() | Include this search in one of my sets |
![]() | Exclude this search from one of my sets |
![]() | Permalink to these results Paste this link in email or IM: |
| Atom feed for tracking future search results Paste this URL into your reader: |
7 messages in org.codehaus.groovy.userRe: [groovy-user] ordered bean proper...| From | Sent On | Attachments |
|---|---|---|
| David Durham, Jr. | Oct 8, 2008 1:27 pm | |
| Guillaume Laforge | Oct 8, 2008 1:39 pm | |
| Guillaume Laforge | Oct 8, 2008 2:09 pm | |
| David Durham, Jr. | Oct 8, 2008 4:32 pm | |
| Jose Noheda | Oct 9, 2008 12:16 am | |
| David Durham, Jr. | Oct 9, 2008 7:38 am | |
| Guillaume Laforge | Oct 9, 2008 7:42 am |

![]() | Permalink for this message Paste this link in email or IM: |
![]() | Permalink for this thread Paste this link in email or IM: |
| Atom feed for this thread Paste this URL into your reader: |
| Subject: | Re: [groovy-user] ordered bean properties | Actions... |
|---|---|---|
| From: | Guillaume Laforge (glaf...@gmail.com) | |
| Date: | Oct 8, 2008 2:09:16 pm | |
| List: | org.codehaus.groovy.user | |
By "a bit more complicated", I meant something like the following example. It's based on a blog post I wrote recently: http://glaforge.free.fr/weblog/index.php?itemid=247
With some modifications, here what you can get to have an ordered list of properties defined in a class:
import org.codehaus.groovy.ast.expr.* import org.codehaus.groovy.ast.stmt.* import org.codehaus.groovy.ast.* import org.codehaus.groovy.control.* import org.codehaus.groovy.classgen.* import java.security.CodeSource
def scriptText = """ class Customer { def name def phone def address1 def address2 } """
// we define a custom visitor that traverses the AST just to look at class properties class PropertyVisitor extends ClassCodeVisitorSupport { def orderedProperties = [] void visitProperty(PropertyNode node) { orderedProperties << node.name }
protected SourceUnit getSourceUnit() { return source; } }
// we define our custom PrimaryClassNodeOperation // to be able to hook our code visitor class CustomSourceOperation extends CompilationUnit.PrimaryClassNodeOperation { CodeVisitorSupport visitor void call(SourceUnit source, GeneratorContext context, ClassNode classNode) throws CompilationFailedException { classNode.visitContents(visitor) } }
// we use our own class loader to add our phase operation class MyClassLoader extends GroovyClassLoader { CodeVisitorSupport visitor protected CompilationUnit createCompilationUnit(CompilerConfiguration config, CodeSource source) { CompilationUnit cu = super.createCompilationUnit(config, source) cu.addPhaseOperation(new CustomSourceOperation(visitor: visitor), Phases.CLASS_GENERATION) return cu } }
def visitor = new PropertyVisitor() def myCL = new MyClassLoader(visitor: visitor) // simply by parsing the script with our classloader // our visitor will be called and will visit the properties of the class in order def script = myCL.parseClass(scriptText)
assert ['name', 'phone', 'address1', 'address2'] == visitor.orderedProperties
On Wed, Oct 8, 2008 at 10:39 PM, Guillaume Laforge <glaf...@gmail.com> wrote:
There are ways to get the list of properties, for instance with Customer.metaClass.properties, but it's not necessarily in order. Then, there may be a way to visit the AST to visit the properties in order, but it's a bit more complicated, and you need to have the source code of your classes available. Out of curiosity, what do you need to find properties with their order?
On Wed, Oct 8, 2008 at 10:28 PM, David Durham, Jr. <davi...@gmail.com> wrote:
Hi all,
I've got a bean with properties like so:
class Customer extends Model { def name def phone def address1 def address2 }
And I'd like to retrieve them in the order they are declared in with something like this:
properties.each { property -> it}
Is there a known easy way to do this?
Thanks, Dave
--------------------------------------------------------------------- To unsubscribe from this list, please visit:
-- Guillaume Laforge Groovy Project Manager G2One, Inc. Vice-President Technology http://www.g2one.com
-- Guillaume Laforge Groovy Project Manager G2One, Inc. Vice-President Technology http://www.g2one.com
--------------------------------------------------------------------- To unsubscribe from this list, please visit:







