And obtaining reflection method dynamic operation _ _ _ builder properties

Acquiring attributes of the relevant code example:

package ReflectProject;

java.lang.reflect.Constructor Import;
Import as java.lang.reflect.Field;
Import the java.lang.reflect.Method;
/ **
* Get property class, method, constructor
* @author Administrator
*
* /
public class Test2 {
public static void main (String [] args) throws a ClassNotFoundException, a NoSuchFieldException, a SecurityException, a NoSuchMethodException {
String path = "ReflectEntity.User";
.. 1 // Get the name of the class
class C = the Class.forName (path);
the System.out .println ( "full class name:" + c.getName ());
( "class name:" + c.getSimpleName ()) System.out.println;


// Get parent class object
class csuper = c .getSuperclass ();
System.out.println ( "full class name:" + csuper.getName ());
System.out.println ( "Class Name:" + csuper.getSimpleName ());


// attribute acquisition class 2.
// c.getField Field, F = ( "ID"); // error, can only get disclosed attribute
// System.out.println (F);
Field, [] = c.getFields Fields ();
System.out.println (fields.length); // 0, can still obtain the properties disclosed in

Field, [] = c.getDeclaredFields fields2 ();
System.out.println (fields2.length); // is. 3
for (Field, Field: fields2) {
System.out.println (Field); // call tostring method
System.out. println (field.getModifiers () + "\ t" + field.getType () + "\ t" + field.getName ()); // 2 on behalf of a private property
}

// 3 the method of acquiring the class information.
method, [] = c.getDeclaredMethods Methods ();
System.out.println (methods.length);
for (Method, Method: Methods) {
// System.out.println (Method);
System.out.println ( "Access:" + method.getModifiers () + " \ t return value type:" + method.getReturnType () + " \ t Method name:" + method.getName ());
// parameter acquisition method
Class [] = Cpara Method.getParameterTypes ();
for (Class C1: Cpara) {
System.out.println ( "method of parameter types:" + c1.getTypeName ());
}
System.out.println ( "----------------------------");
}

// Get 4 class's constructor (the constructor).
the constructor [] = c.getConstructors Constructors ();
for (the constructor constructor: Constructors) {
System.out.println (constructor);
}
// Get the specified constructor
the constructor c.getConstructor = CON (null);
System.out.println (CON );
the Constructor CON2 = c.getConstructor (int.class, String.class, String.class);
the System.out.println(con2);


}

}

 

 

 

Dynamic operational attributes, methods, constructing correlation Code Example:

package ReflectProject;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import ReflectEntity.User;

/**
* 动态的操作属性、方法、构造方法
* @author Administrator
*
*/
public class Test3 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchFieldException {
//得到class对象
Class c = Class.forName("ReflectEntity.User");
//得到无参构造方法
Constructor cons = c.getConstructor(null);
//通过无参构造方法的对象,创建User类的对象
User user = (User) cons.newInstance();
//动态操作属性
Field field = c.getDeclaredField("ID");
field.setAccessible(true);//这个属性不需要安全检查了,可以直接访问
//通过反射直接赋值
field.set(user, 1001);
System.out.println("取出ID这个属性的值:"+field.get(user));

//动态操作方法
Method m = c.getDeclaredMethod("setUserName", String.class);
//执行这个方法
m.invoke(user, "张三");
Method m2 = c.getDeclaredMethod("getUserName", null);
System.out.println(m2.invoke(user));
}
}

Guess you like

Origin www.cnblogs.com/LuJunlong/p/12155844.html