Groovy exploration Mop Seven method and attribute analysis during runtime

Source: Internet
Author: User
Tags hasproperty reflection

In the groovy language, there are three ways to analyze methods and properties during the runtime, respectively:

First, inherit from the Java language reflection mode.

Second, use the "Respondsto" and "Hasproperty" methods.

Third, use the "Hasmetamethod" and "Hasmetaproperty" methods.

All of the above three methods can be in the runtime to analyze whether a method or attribute exists, I believe we see here, we must think that there is no difference between them?

The discussion of methods and attribute analysis in these three operating periods and the differences between them is exactly what we are going to do in this article.

In the Groovy language class, methods and properties are usually a pair of side-by-side concepts, that is, if there is a method for class methods, then there must be a method of corresponding class attributes. As mentioned above, "Respondsto" and "Hasproperty", "Hasmetamethod" and "Hasmetaproperty". Therefore, the use of the corresponding methods in these groups is broadly similar.

Therefore, in this article, limited to the length of the limit, in the run-time analysis of the method as an example to illustrate the use of the above three groups of methods and the difference between them, and the runtime of the property analysis is not explained. Because the method of attribute analysis in these runtime is used and the difference is generally similar to the usage and difference of the method.

First, let's look at the methods used to analyze the runtime using reflection from the Java language. For example, we have one of the following classes:

class Testor3 {

def test1()
{
println 'test1'
}

}

We can easily use reflection to analyze the method during the runtime, as shown in the following code:

def method = Testor3.class.getMethod('test1',null)

Such code can be compiled and run normally.

However, if we have the following code:

Testor3.metaClass."test2" = {
->
println 'test2'
}
def method = Testor3.class.getMethod('test2',null)

If you run it, you'll report the following error:

Exception in thread "main" java.lang.NoSuchMethodException: Testor3.test2()
at java.lang.Class.getMethod(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

This shows that using reflection does not find a way to add the mop attribute of the groovy language to the class during the run time. Of course, so is the property.

Next, let's take a look at the use of the "Respondsto" method. Suppose we have one of the following classes:

class Testor1 {

def bar()
{
println 'bar'
}

}

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.