Reflection 反射

How to use Reflection in Java
[url]http://www.java-tips.org/java-se-tips/java.lang.reflect/how-to-use-reflection-in-java.html[/url][quote]
Reflection is a powerful approach to analyze the class at runtime. If new classes are added into your application dynamically then Reflection is used to get the structure of the class.

Reflection uses special kind of java class: Class. The object of the Class type can hold all the information of the class and have getter methods to extract this information.

This example code extracts the structure of the String class. It will display the name of the constructors, declared fields and methods to the console.

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class ReflectionExample {

public static void main(String[] args) {
try {
// Creates an object of type Class which contains the information of
// the class String
Class cl = Class.forName("java.lang.String");

// getDeclaredFields() returns all the constructors of the class.
Constructor cnst[] = cl.getConstructors();

// getFields() returns all the declared fields of the class.
Field fld[] = cl.getDeclaredFields();

// getMethods() returns all the declared methods of the class.
Method mtd[] = cl.getMethods();
System.out.println("Name of the Constructors of the String class");

for (int i = 0; i < cnst.length; i++) {
System.out.println(cnst[i].getName());
}

System.out.println("Name of the Declared fields");

for (int i = 0; i < fld.length; i++) {
System.out.println(fld[i].getName());
}

System.out.println("Name of the Methods");

for (int i = 0; i < mtd.length; i++) {
System.out.println(mtd[i].getName());
}

} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}

}

[/quote]


Invoke method using Reflection
[url]http://www.java-tips.org/java-se-tips/java.lang.reflect/invoke-method-using-reflection.html[/url][quote]
Reflection is used to invoke a method when name of the method is supplied at run time. This tip will show a sample code to do that.

import java.lang.reflect.Method;

public class RunMthdRef {
public int add(int a, int b) {
return a+b;
}

public int sub(int a, int b) {
return a-b;
}

public int mul(int a, int b) {
return a*b;
}

public int div(int a, int b) {
return a/b;
}

public static void main(String[] args) {
try {
Integer[] input={new Integer(2),new Integer(6)};
Class cl=Class.forName("RunMthdRef");
Class[] par=new Class[2];
par[0]=Integer.TYPE;
par[1]=Integer.TYPE;
Method mthd=cl.getMethod("add",par);
Integer output=(Integer)mthd.invoke(new RunMthdRef(),input);
System.out.println(output.intValue());
} catch (Exception e) {
e.printStackTrace();
}
}
}

[/quote]


Explore the Dynamic Proxy API
http://www.javaworld.com/javaworld/jw-11-2000/jw-1110-proxy.html

Java Reflection: Dynamic Proxies
http://tutorials.jenkov.com/java-reflection/dynamic-proxies.html
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值