Reflection Primer Basics

Source: Internet
Author: User

Testreflect.java

Package com.huawei.reflect;

public class Testreflect {

int a = 0;

Private String name = "Lisi";

protected long B = 2L;

public double c = 3.0d;

public static short e = 20;

static String fav = "read";


public void Add (int a,int b) {
System.out.println (A+B);
}
public void Add (int a,string b) {
System.out.println (A+B);
}

private static String say () {
Return "Hello static say";
}
private string Say (String msg) {
Return "Hello" +MSG;
}

static int Add (int a) {
return a+100;
}

public static int Add (int a,int b, int c) {
return a+b+c;
}

Public Testreflect () {
System.out.println ("Testrefect.testrefect ()");
}
Testreflect (int a) {
System.out.println ("Testrefect.testrefect ()" +a);
}

Public Testreflect (long B, double c) {
Super ();
this.b = b;
THIS.C = C;
System.out.println ("This is a Test");
}

}

Test.java

Package com.huawei.reflect;

Import Java.lang.reflect.Constructor;
Import Java.lang.reflect.Field;
Import Java.lang.reflect.Method;

public class Test {
public static void Testgetclass () throws ClassNotFoundException {

/**
* Get a Class object
*
* There are several ways
*/

Get a class object directly from the class name. class
class<?> clazz = Testreflect.class;

Through the object. GetClass () method to get
class<?> clazz1 = new Testreflect (). GetClass ();

The Forname method provided by class
class<?> clazz2 = Class.forName ("Com.cdsxt.reflect.TestReflect");

System.out.println (Clazz);
System.out.println (CLAZZ1);
System.out.println (CLAZZ2);


}

/**
* Test to get the attributes inside the class
* @throws nosuchfieldexception
* @throws SecurityException
* @throws illegalaccessexception
* @throws IllegalArgumentException
*/
public static void Testgetfield () throws SecurityException, Nosuchfieldexception, IllegalArgumentException, illegalaccessexception{
Get the structure information of a class
class<?> clazz = Testreflect.class;

Get a Public member field
Field C = Clazz.getfield ("E");

Testreflect t = new Testreflect ();
Object oo = new Testreflect ();
Object oo1 = new Testreflect (3l,50.0d);
T.E;
TESTREFLECT.E;

Oo.c
If the attribute is subordinate to the class, the value is not required to be directly worn by the specified object.
Object o = c.get (oo1);
Object o = c.get (null);


System.out.println (o);
Property name
System.out.println (C.getname ());
Get modifier
System.out.println (C.getmodifiers ());
Get the Class object for field
System.out.println (C.getclass ());
Get the type of the property field
System.out.println (C.gettype ());


System.out.println ("--------------------");
Gets the public properties of all public within a given class
Field []fields = Clazz.getfields ();
for (Field f:fields) {
System.out.println (F.getname ());
Get modifier
System.out.println (F.getmodifiers ());
Get the Class object for field
System.out.println (F.getclass ());
Get the type of the property field
System.out.println (F.gettype ());
}


Get an existing property field

Field name = Clazz.getdeclaredfield ("name");
Get the original state
Boolean flag = Name.isaccessible ();
Then in force open
Name.setaccessible (TRUE);
System.out.println (Name.get (oo1));
Set the original state back to
Name.setaccessible (flag);
System.out.println (Name.get (oo1));
}

/**
* Test access method
*/
public static void Testgetmethod () throws exception{
class<?> clazz = Testreflect.class;
O.add (ON)
Get the object of a method

method = Clazz.getmethod ("Add", Int.class,string.class);
The second type of notation

Method add = Clazz.getmethod ("Add", New Class[]{int.class,int.class});
Object o = new Testreflect ();
O.add ();
Method.invoke (o, 1, "Hello");
Add.invoke (o, New object[]{2,10});

Get static methods
Method add1 = Clazz.getmethod ("Add", New Class[]{int.class,int.class,int.class});
Call through Object
Object rs = Add1.invoke (o, 10,20,30);
To invoke by class
Object rs = Add1.invoke (null, 10,20,30);

System.out.println (RS);

System.out.println ("----------------------");
/**
* When you call a method that has no arguments, you don't pass it back.
*
* Can not be transmitted at the time of execution
*
* Be aware of their accessibility in addition to public methods or properties or constructors
*
*/
Method m = Clazz.getdeclaredmethod ("Say");
M.setaccessible (TRUE);
Object msg = M.invoke (null);
SYSTEM.OUT.PRINTLN (msg);

}

/**
* Test Create Instance
*/
public static void Testcreateinstance () throws exception{
class<?> clazz = Testreflect.class;
Create an instance
If you want to use the following method, you must have an parameterless constructor in a given class
Object o = clazz.newinstance ();

System.out.println (o);

class<ttt> C = ttt.class;
C.newinstance ();

Get a specific constructor and then create an instance
constructor<?> Constructor = Clazz.getconstructor (New Class[]{long.class,double.class});

Object O1 = constructor.newinstance (New object[]{20l,30.0d});
System.out.println (O1);

}



public static void Main (string[] args) throws Exception {
Testgetclass ();
Testgetfield ();
Testgetmethod ();

Testcreateinstance ();
}
}

Class ttt{

Public TTT (int a) {
Super ();
}

}

Reflection Primer Basics

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.