您的位置:首页 > 其它

反射

2015-11-17 22:28 316 查看
Field[] getFields()

Field[] getDeclaredFields()

getFields 返回一个包含Field对象的数组,这些对象记录了这个类或其他超类的公有域。

getDeclaredFields 返回包含Field对象的数组,记录了这个类的全部域。

如果类没有域,或者Class对象描述的是基本类型或数组类型,这将返回一个长度为0的数组。

Method[] getMethods()

Method[] getDeclareMethods() 返回包含Mehod对象的数组,getMethods将返回所有的公有方法,包括从超类继承的公有方法;

Constructor[] getConstructors() 返回这个类或接口的全部方法,但不包括有超类继承了的方法。

Constructor[] getDeclaredConstructors() 返回包含Constructor对象的数组,其中包含了Class对象所描述的类的所有公有构造器(getConstructors)或所有构造器(getDeclaredConstructors).

---------------------------------

java.lang.reflect Constructor

Class getDeclaringClass()  返回一个用于描述类中定义的构造器,方法或域的Class对象

Class[] getExceptionTypes()  (在Constructor和Method类中)返回一个用于描述方法抛出的异常类型的Class对象数组

int getModifiers()  返回一个用于描述构造器,方法或域的修饰符的整形数值。使用Modifier类中的这个方法可以分析这个返回值

String getName()  返回一个用于描述构造器,方法或域名的字符串

Class[] getParameterType()  返回一个用于描述参数类型的Class对象数组

Class getReturnType()  返回一个用于描述返回类型的Class对象

---------------------------------

java.lang.reflect.Modifier

static String toString(int modifiers)

返回对应modifiers中位设置的修饰符的字符串表示

static boolean isAbstract(int modifiers)

static boolean isFinal(int modifiers)

static boolean isInterface(int modifiers)

static boolean isNative(int modifiers)

static boolean isPrivate(int modifiers)

static boolean isProtected(int modifiers)

static boolean isPublic(int modifiers)

static boolean isStatic(int modifiers)

static boolean isStrict(int modifiers)

static boolean isSynchronized(int modifiers)

static boolean isVolatile(int modifiers)

这些方法将检测方法名中对应的修饰符在modifiers值中的位。

---------------------------------

package reflection;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Scanner;

public class ReflectionTest {

public static void main(String[] args) {
//read class  name from command line args or user input
String name;
if(args.length > 0) {
name = args[0];
}
else {
Scanner in = new Scanner(System.in);
System.out.println("Enter class name (e.g. java.util.Date):");
name = in.next();
}

try{
//print class name and superclass name (if != Object)
Class c1 = Class.forName(name);
Class superc1 = c1.getSuperclass();
String modifiers = Modifier.toString(c1.getModifiers());
if(modifiers.length() > 0 ){
System.out.println(modifiers + " ");
}
System.out.println("class " + name);
if(superc1 != null && superc1 != Object.class)
System.out.println(" extens " + superc1.getName());
System.out.print("\n{\n");
printConstructors(c1);
System.out.println();
printMethods(c1);
System.out.println();
printFields(c1);
System.out.println("}");
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
System.exit(0);
}

/**
* Prints all constructors of a class
* @param c1 a class
*/
public static void printConstructors(Class c1) {
Constructor[] constructors = c1.getDeclaredConstructors();
for(Constructor c : constructors) {
String name = c.getName();
System.out.print(" ");
String modifiers = Modifier.toString(c.getModifiers());
if(modifiers.length()>0) System.out.print(modifiers +" ");
System.out.print(name + "(");

Class[] paramTypes = c.getParameterTypes();
for(int j = 0; j < paramTypes.length; j++) {
if(j > 0){
System.out.print(", ");
System.out.print(paramTypes[j].getName());
}
}
System.out.println(");");
}
}

/**
* Prints all methods of a class
* @param c1 a class
*/
public static void printMethods(Class c1) {
Method[] methods = c1.getDeclaredMethods();
for(Method m : methods) {
Class retType = m.getReturnType();
String name = m.getName();

System.out.print("  ");
// print modifiers, return type and method name
String modifiers = Modifier.toString(m.getModifiers());
if(modifiers.length()>0) {
System.out.print(modifiers + " ");
}
System.out.print(retType.getName() + " " + name + "(");

// print parameter types
Class[] paramTypes = m.getParameterTypes();
for(int j = 0; j<paramTypes.length; j++) {
if(j > 0) System.out.print(", ");
System.out.print(paramTypes[j].getName());
}
System.out.println(");");
}
}

/**
*  Prints all fields of a class
*  @param c1 a class
*/
public static void printFields(Class c1) {
Field[] field = c1.getDeclaredFields();

for (Field f : field) {
Class type = f.getType();
String name = f.getName();
System.out.print("  ");
String modifies  = Modifier.toString(f.getModifiers());
if(modifies.length() > 0) {
System.out.print(modifies + " ");
}
System.out.println(type.getName() + " " + name + ";");
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: