[
https://issues.apache.org/jira/browse/GROOVY-10392?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17463919#comment-17463919
]
William Woodman commented on GROOVY-10392:
------------------------------------------
I was thinking about the MetaClass and lopsided feel to MOP. MetaClass is an
internal class for groovy and little opaque to understand all that its doing
however one of its features is that you can dynamically add properties and
closures to a classes metaclass or per instance metaclass. This is great and
can be useful as a point of adding capability dynamically in your code.
however trying to add static properties doesnt seem to work (groovy lets you
right the code - but just seems to silently swallow and do nothing (e.f.
<class>.metaClass.static.someProperty = "hi there" seems to do nothing and you
cant retrieve this). you can add a static closure to .static - but you cant
query for staticMethods. there is no in metaclass.
res = myc.metaClass.getStaticMetaMethods() //this method doesnt exist in
MOP apires = myc.metaClass.getStaticProperties() //this method doesnt
exist in MOP api either
when i came to look to use the metaClass expansion capability for static props,
and closures the current API is not symmetric with that of ordinary
props/methods, which provide methods on metaClass to query these at runtime and
work with them.
as ever in life there are alternative approaches etc - but i cam away feeling
that MOP was not holistically complete for static props/methods.
My attempt above was to see what these would feel like. I tried to copy
metaClass into my own directory space but it didnt like that when you try and
build it, hence i ended up creating my own sample dynamically expandable class
a bit like Expando but with more feature methods.
What i'm looking for is make metaClass feel consistent for 'statics' as well as
ordinary props/methods, and feed this back into future releases.
so whilst the class metaClass is inherited by default into a per instance
metaClass this doesnt feel the same as having static methods /props of the
metaClass itself
I hope that makes some sense to you.
> Inconsistent metaClass capability for .static properties and methods vs
> ordinary dynamic properties and methods
> -----------------------------------------------------------------------------------------------------------------
>
> Key: GROOVY-10392
> URL: https://issues.apache.org/jira/browse/GROOVY-10392
> Project: Groovy
> Issue Type: Improvement
> Components: Compiler
> Affects Versions: 3.0.9, 4.0.0-beta-2
> Reporter: William Woodman
> Priority: Minor
> Fix For: 3.x, 4.x
>
>
> runtime addition and working with static dynamic properties and methods is
> inconsistent and doesnt work the same as the equivalent capability for
> ordinary dynamic properties and methods.
> I've tried to show the problem in the script code below
> {code:groovy}
> package script
> class SomeClass {
> static String classDeclaredStatName = "in class definition static name"
> String name = "instance level name"
> }
> //add a new property to metaclass - works fine
> SomeClass.metaClass.dynProp = "dynamic property added"
> SomeClass.metaClass.dynMethod = \{"dynamic method added as closure"}
> SomeClass.metaClass.static.dynStaticProp = "dynamic static property added"
> SomeClass.metaClass.static.dynStaticMethod = \{"dynamic static method added"}
> assert SomeClass.classDeclaredStatName == "in class definition static name"
> assert SomeClass.dynStaticMethod() == "dynamic static method added"
> //this forces conversion of metaClassImpl to expandoMetaClass
> SomeClass myc = new SomeClass()
> assert myc.name == "instance level name"
> assert myc.classDeclaredStatName == "in class definition static name"
> assert myc.dynProp == "dynamic property added"
> assert myc.dynMethod() == "dynamic method added as closure"
> assert myc.dynStaticMethod() == "dynamic static method added"
> def res
> res = myc.metaClass.properties //returns list of metaBeanProperty
> res = myc.metaClass.getMetaMethods() //works and returns list of metaMethods
> //This is the only method for static's in MOP - this works but you have to
> know the name of the method in advance
> res = myc.metaClass.getStaticMetaMethod("dynStaticMethod", [] as ArrayList)
> //these functions are missing from MOP and would enable you to query for
> static props/methods
> res = myc.metaClass.getStaticMetaMethods() //this method doesnt exist in
> MOP api
> res = myc.metaClass.getStaticProperties() //this method doesnt
> exist in MOP api either
> {code}
--
This message was sent by Atlassian Jira
(v8.20.1#820001)