[ 
https://issues.apache.org/jira/browse/GROOVY-10392?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17470123#comment-17470123
 ] 

William Woodman commented on GROOVY-10392:
------------------------------------------

Eric, 

 

painful day - but started to get some progress.  I have copied existing 
ExpandoMetaClass into my local direct and renamed 

to WillsMetaClass2.  (tried to do originally as groovy and got caught with 
strange dynamic dispatch problems - so this is in straight java 

had to create a new StaticMetaBean as well 

 

[https://github.com/woodmawa/GroovyV4sample/blob/master/src/main/groovy/MOP/StaticExpandoProperty.groovy]
 

 

[https://github.com/woodmawa/GroovyV4sample/blob/master/src/main/groovy/MOP/WillsMetaClass2.java]
 

 

i've changed some of the internal classes as well so i can see what i'm doing 

 

 

this sort of works using this simple driver script and looking at the results - 

 

still needs tidying, tests, bahvaioural checks and consistency of expectations 
checks - but should give you the idea i was shooting for 

 
{code:java}
class WillsClass {
String prop = "class property"

static String sprop = "static class property "
}

WillsClass wo = WillsClass::new()

WillsMetaClass2 wmc = new WillsMetaClass2(WillsClass, true, true)
wmc.dynProp = "wmc dynamic prop"

//wmc.initialize()
wo.setMetaClass(wmc) //set new metaclass

def newMetaClass = wo.metaClass
assert wo.metaClass.class == WillsMetaClass2

def dynProp = wo.dynProp
//def unknown = wo.unknown

String modType (int mod) {
if (Modifier.isStatic(mod))
return "static"
if (Modifier.isPublic(mod))
return "public"
if (Modifier.isProtected(mod))
return "protected"
if (Modifier.isPrivate(mod))
return "private"
}


Collection props = 
wo.metaClass.getExpandoProperties().collect{"name:${it.name}, 
mod:${modType(it.modifiers)}"}
Collection meths = wo.metaClass.getMethods().collect{"name:${it.name}, 
mod:${modType(it.modifiers)}"}

def womc = wo.metaClass
def stat = womc.'static'


stat.myStaticProp = 10
def result = stat.myStaticProp
assert wo.hasProperty( "dynProp")

def sprops = wo.metaClass.'static'.getProperties()

def allSprops = wo.metaClass.getStaticProperties()
 
  {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)

Reply via email to