MetaClass property for java classes in groovy

I am studying groovy and I have a question regarding its metaprogramming capabilities. From what I understand, every object in groovy implements the groovy.lang.GroovyObject interface (implicitly added by the groovy compiler). The last interface contains the getMetaClass () method, so I can do the following:

class MyGroovyClass{ } def myGroovyClass = new MyGroovyClass(); println myGroovyClass.metaClass.name 

What I don't understand is how objects compiled by the java compiler, like java.lang.String, get the metaClass property. Is this property defined in the actual MetaClass object that is associated with Java classes through MetaClassRegistry?

+4
source share
2 answers

In addition to the methods added through the metaobject protocol, Groovy also extends classes in the JDK with additional methods defined in groovy.runtime.DefaultGroovyMethods . List and other collections get methods like each and collect . The same mechanism adds the getMetaClass method to java.lang.Object .

+3
source

That's right, POJOs have their metaclasses scanned in the registry.

This presentation provides a high-level overview on how metaclasses are used by Groovy and POJ objects. (With the warning that he is a little old.)

+1
source

Source: https://habr.com/ru/post/1390933/


All Articles