Groovy Quest Mop Three Class, Metaclass and Expandometaclass

Source: Internet
Author: User
Tags reflection

Class is of course the groovy language inherits from the Java language, which means that the groovy language also inherits the reflection mechanism of the Java language. This means that we can write code like the following in the groovy language, as we would use reflection in the Java language:

import java.lang.reflect.Method

class Testor {

def testDelegate()
{
'ok'
}

static void main(args) {

def t = new Testor()

Class cls = t.class

Method method = cls.getMethod('testDelegate',null)

println method.invoke(t,null)
}

}

Of course, we rarely use this in groovy, because the mop mechanism of the groovy language is many times more powerful than the reflection mechanism in the Java language.

In summary, the mop mechanism of groovy language is mainly implemented by the following methods:

The first is the hook for groovy class itself, which includes overloading the "InvokeMethod" and "Set/getproperty" methods, and overloading the "methodmissing" and "propertymissing" methods. These methods have been repeatedly mentioned in different languages and are not restated here.

Secondly, the mop mechanism is realized through metaclass.

In the groovy language, all classes originate from the Groovyobject interface, which has the following two methods:

public MetaClass getMetaClass();
public void      setMetaClass(MetaClass metaClass);

The Metaclass objects of all our objects are based on these two methods.

In the Metaclass object, we can implement some of the following methods:

Object invokeMethod(Object obj, String methodName, Object args)
Object invokeMethod(Object obj, String methodName, Object[] args)
Object invokeStaticMethod(Object obj, String methodName, Object[] args)
Object invokeConstructor(Object[] args)

Used to invoke methods, static methods, and properties, respectively. At the same time, we have some properties and methods for introspection, such as "Properties", "Respondsto" and "Hasproperty" and so on.

Therefore, if we want to use Metaclass to implement the mop mechanism, we need to implement a metaclass of our own, and then set it to the Groovyobject object through the "Setmetaclass" method, thus achieving the purpose of using Metaclass.

Obviously, the mop mechanism of the groovy language in this way is not very flexible and far less useful than the hooks above.

Third, the mop mechanism that is realized through Expandometaclass

It is said that the use of the Expandometaclass class, originating from the Grails project, has been introduced into the core API of the groovy language since Groovy1.1, because of its great ease of implementation of the mop mechanism.

Unlike the Metaclass class above, the Metaclass class is introduced through the "Setmetaclass" and "Getmetaclass" of groovy objects, while the Expandometaclass class is made up of "Java.lang.Class "Metaclass" of the class is introduced.

As in the following example:

class Testor5 {

private delegate

def testDelegate()
{
'ok'
}

static void main(args) {

def t = new Testor5()


t.testDelegate()

Testor5.metaClass.test = {
->
'invoke test function'
}

def t1 = new Testor5()

println t1.test()

}

}

Of course, the Expandometaclass class can also add methods and properties to individual objects, showing great flexibility and dynamism.

All of this will be described in detail in the future, and no longer be detailed here.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.