[
https://issues.apache.org/jira/browse/GROOVY-10392?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17464664#comment-17464664
]
William Woodman edited comment on GROOVY-10392 at 12/23/21, 4:04 PM:
---------------------------------------------------------------------
yes i suppose so - but its about programmer expectations for consistency i
guess. I was trying to add some features whilst writing some scripts, and you
see examples that show how to embellish the class MetaClass or per instance
metaMethod. but the experience with metaClass.static just feels weird
you might be tempted (as i had been) to add a static property - then finding it
seems to do nothing. I then tried to add a prop getMethod like
{code:java}
metaClass.static.getStaticProp = {"return some value"} {code}
but this doesn't allow you to access this.
Writing some tests - demonstrates some of the inconsistency - so getProperties
wont find it but hasProperty will.
as a programmer you're expectations are not met, you then spend time trying to
figure it out (or give up) and decide you cant trust the features
{code:java}
@Test
void testMetaClassStatic () {
Integer num = 1
num.metaClass.static.getStatProp = {"dynamic static property"}
shouldFail (MissingPropertyException) {num.statProp == "dynamic static
property"}
def mm = num.metaClass.getStaticMetaMethod('getStatProp', Object[])
assertTrue mm.isStatic()
assertEquals mm.invoke(num, []), "dynamic static property"
//ask for hasProperty - says there is a prop called statProp
MetaProperty mp = num.hasProperty('statProp')
assertTrue Modifier.isStatic (mp.modifiers)
assertTrue num.hasProperty ('statProp').getProperty(num) == "dynamic static
property"
// but cant'ask for a list of staticMetaMethods -
// you have to know the name in advance
// num.metaClass.getStaticMetaProperty ('statProp') - method doesnt exist
//try and get properties - and statProp is not there
Map props = num.getProperties()
assert props.size() == 1
assert props.get ('class') == Integer
assert props.get ('statProp') == null
mp = props.find{it.key == 'statProp'}
assert mp == null
} {code}
was (Author: woodmawa):
yes i suppose so - but its about programmer expectations for consistency i
guess. I was trying to add some features whilst writing some scripts, and you
see examples that show how to embellish the class MetaClass or per instance
metaMethod. but the experience with metaClass.static just feels weird
you might be tempted (as i had been) to add a static property - then finding it
seems to do nothing. I then tried to add a prop getMethod like
{code:java}
metaClass.static.getStaticProp = {"return some value"} {code}
but this doesn't allow you to access this.
Writing some tests - demonstrates some of the inconsistency - so getProperties
wont find it but hasProperty will.
as a programmer you're expectations are not met, you then spend time trying to
figure it out (or give up) and decide you cant trust the features
{code:java}
@Test
void testMetaClassStatic () {
Integer num = 1
num.metaClass.static.getStatProp = {"dynamic static property"}
shouldFail (MissingPropertyException) {num.statProp == "dynamic static
property"}
def mm = num.metaClass.getStaticMetaMethod('getStatProp', Object[])
assertTrue mm.isStatic()
assertEquals mm.invoke(num, []), "dynamic static property"
//ask for hasProperty - says there is a prop called statProp
MetaProperty mp = num.hasProperty('statProp')
assertTrue Modifier.isStatic (mp.modifiers)
assertTrue num.hasProperty ('statProp').getProperty(num) == "dynamic static
property"
// num.metaClass.getStaticMetaProperty ('statProp') - method doesnt exist
//try and get properties - and statProp is not there
Map props = num.getProperties()
assert props.size() == 1
assert props.get ('class') == Integer
assert props.get ('statProp') == null
mp = props.find{it.key == 'statProp'}
assert mp == null
} {code}
> 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)