Java反射示例
- 创建数据类型ReflectPoint.java
package com.reflection;
import java.util.Date;
public class ReflectPoint {
private Date birthday = new Date();
private int x;
public int y;
public String str1 = "ball";
public String str2 = "basketball";
public String str3 = "itany";
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getStr1() {
return str1;
}
public void setStr1(String str1) {
this.str1 = str1;
}
public String getStr2() {
return str2;
}
public void setStr2(String str2) {
this.str2 = str2;
}
public String getStr3() {
return str3;
}
public void setStr3(String str3) {
this.str3 = str3;
}
public ReflectPoint(int x, int y) {
super();
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
@Override
public String toString() {
return str1+"\t"+str2+"\t"+str3+"\t"+birthday;
}
}
- 创建测试类
package com.reflection;
import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;
public class ReflectTest {
public static void main(String[] args) throws Exception {
String str1="abc";
Class cls1=str1.getClass();
Class cls2=String.class;
Class cls3=Class.forName("java.lang.String");
System.out.println(cls1==cls2);
System.out.println(cls1==cls3);
System.out.println(cls1.isPrimitive());
System.out.println(int.class.isPrimitive());
System.out.println(int.class==Integer.class);
System.out.println(int.class==Integer.TYPE);
System.out.println(int[].class.isPrimitive());
System.out.println(int[].class.isArray());
System.out.println(void.class.isPrimitive());
String s1="123";
int s11=Integer.parseInt(s1);
Constructor constructor1=String.class.getConstructor(String.class);
String str2=(String)constructor1.newInstance("abc");
System.out.println(str2.charAt(2));
ReflectPoint pt1=new ReflectPoint(3,5);
Field fieldY=pt1.getClass().getField("y");
System.out.println(fieldY.get(pt1));
Field fieldX=pt1.getClass().getDeclaredField("x");
fieldX.setAccessible(true);
System.out.println(fieldX.get(pt1));
changeStringValue(pt1);
System.out.println(pt1);
Method methodcharAt=String.class.getMethod("charAt", int.class);
System.out.println(methodcharAt.invoke(str2, 2));
String startingClass=args[0];
Method methodMain=Class.forName(startingClass).getMethod("main", String[].class);
methodMain.invoke(null, (Object)new String[]{"111","222","333"});
int[] a1=new int[]{1,2,3};
int[] a2=new int[4];
int[][]a3=new int[2][3];
String[]a4=new String[]{"a","b","c","d"};
System.out.println(a1.getClass()==a2.getClass());
System.out.println(a1.getClass().getName());
System.out.println(a3.getClass().getName());
System.out.println(a4.getClass().getName());
System.out.println(a1.getClass().getSuperclass().getName());
System.out.println(a4.getClass().getSuperclass().getName());
Object aObj1=a1;
Object aObj2=a4;
Object[] aObj4=a3;
Object[] aObj5=a4;
System.out.println(a1);
System.out.println(a4);
System.out.println(Arrays.asList(a1));
System.out.println(Arrays.asList(a4));
printObject(a1);
printObject(a4);
printObject("xyz");
InputStream in = ReflectionTest.class.getClassLoader().getResourceAsStream("config.properties");
Properties props = new Properties();
props.load(in);
in.close();
String className = props.getProperty("className");
Collection c=(Collection)Class.forName(className).newInstance();
ReflectPoint pt1=new ReflectPoint(3,3);
ReflectPoint pt2=new ReflectPoint(5,5);
ReflectPoint pt3=new ReflectPoint(3,3);
c.add(pt1);
c.add(pt2);
c.add(pt3);
c.add(pt1);
System.out.println(c.size());
}
private static void printObject(Object obj) {
Class clazz=obj.getClass();
if(clazz.isArray()){
int len=Array.getLength(obj);
for(int i=0;i<len;i++){
System.out.println(Array.get(obj, i));
}
}else{
System.out.println(obj);
}
}
private static void changeStringValue(Object obj) throws Exception {
Field[] fields=obj.getClass().getFields();
for(Field field:fields){
if(field.getType()==String.class){
String oldValue=(String)field.get(obj);
String newValue=oldValue.replace('b', 'a');
field.set(obj, newValue);
}
}
}
}
class TestArguments{
public static void main(String[]args){
for(String arg:args){
System.out.println(arg);
}
}
}