The reflection of groovy exploration

Source: Internet
Author: User
Tags reflection

We know that in the Java language, we can get the dynamics of the Java language, mainly through its reflection mechanism. And in the groovy language, we'll have a lot of ways to get its dynamics, like mop and so on. So, in the groovy language, we need to call a method during the runtime, and we will not use the reflection mechanism, although we can still use the reflection mechanism in the groovy language to achieve the goal, instead of using the mop mechanism, or using the "duck type". Because these mechanisms are more convenient and flexible than reflection.

Does this mean that we will no longer use the reflection mechanism in the Groovy Language program development process? We say that the reflection mechanism, in addition to allowing us to invoke methods dynamically during the run period, another important function is the introspection function, which allows us to know in the runtime a information about an object itself, such as which class it belongs to, what attributes and methods it has, and which class it inherits. Or what interfaces are implemented, and so on. Of course, another important feature is the ability to instantiate a class during the runtime, which is what we often use.

Therefore, in our groovy language coding process, the reflection mechanism will still play a very important role, the mop mechanism is only the reflection mechanism of some of the very weak features of the expansion.

First, we have a simple class:

package reflect;
class Testor3 {
public a
def b
private String c
}

We've got an object, as follows:


def t = new Testor3()

Now, we want to know the class name of the object "T", in the Java language, we have to get:

println t.getClass().getName()

In the groovy language, because we use Gpath extensively, the above statements can of course be simplified as follows:

println t.class.name

All two of the above statements will print the following results:

reflect.Testor3

If we just want to get the object's class name and don't want it with the package name, it looks like this:

println t.class.simpleName

The results printed are:

Testor3

It's important to note that while using Gpath to get class is fairly straightforward, it's not always possible to get it. Like what:

def map = [:]
println map.class

Its printed results will be:

null

If you have the following statement:

println map.class.simpleName

Well, run it, and you'll get a null pointer for the violation.

At this point, the "getclass" approach will still come in handy:

println map.getClass()
println map.getClass().simpleName
println map.getClass().name

The above statements can be run correctly, and the resulting results are:

class java.util.LinkedHashMap
LinkedHashMap
java.util.LinkedHashMap

After we get the class of the object, we're going to want its properties. There are two ways to get the properties of an object: First, get "fields", and the second is to get "declaredfields". The previous method was able to get the public property of the object, and all the properties of the object acquired by the latter method. Such as:

def t = new Testor3()

def cls = t.class
println cls.fields
println cls.declaredFields

The results of the operation are:

{public java.lang.Object reflect.Testor3.a, public static java.lang.Long reflect.Testor3.__timeStamp}
{public java.lang.Object reflect.Testor3.a, private java.lang.Object reflect.Testor3.b, private java.lang.String reflect.Testor3.c, transient groovy.lang.MetaClass reflect.Testor3.metaClass, public static java.lang.Long reflect.Testor3.__timeStamp, static java.lang.Class reflect.Testor3.class$groovy$lang$MetaClass, static java.lang.Class reflect.Testor3.class$org$codehaus$groovy$runtime$ScriptBytecodeAdapter, static java.lang.Class reflect.Testor3.class$0}

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.