import java.lang.reflect.Constructor;
public class ConstructorTest {
/**
* 根据参数类型 区别选择构造方法
* @param args
* @throws NoSuchMethodException
* @throws SecurityException
*/
public static void main(String[] args) throws Exception {
//new String(new StringBuffer("abc"));
Constructor constructor1 = String.class.getConstructor(StringBuffer.class);
//class java.lang.String
System.out.println(constructor1.getDeclaringClass());
String str1 = (String) constructor1.newInstance(new StringBuffer("abc"));
System.out.println(str1.charAt(2));
}
}
public class ReflectPoint {
private int x;
public int y;
public String str1 = "a";
public String str2 = "b";
public String str3 = "c";
public ReflectPoint(int x, int y) {
super();
this.x = x;
this.y = y;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + x;
result = prime * result + y;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ReflectPoint other = (ReflectPoint) obj;
if (x != other.x)
return false;
if (y != other.y)
return false;
return true;
}
@Override
public String toString() {
return "ReflectPoint [str1=" + str1 + ", str2=" + str2 + ", str3="
+ str3 + "]";
}
}
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class FieldTest {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
ReflectPoint pt1 = new ReflectPoint(1, 3);
Field fieldY = pt1.getClass().getField("y");
//在pt1对象上的y值
//fieldY不是对象身上的变量,而是类上。要用它去取每个对象的fieldY值
System.out.println(fieldY.get(pt1));
//getField("x")报错,私有不可见
Field fieldX = pt1.getClass().getDeclaredField("x");
//设置可以访问
fieldX.setAccessible(true);
System.out.println(fieldX.get(pt1));
//改变x变量的值
fieldX.set(pt1, 2);
System.out.println(fieldX.get(pt1));
//ReflectPoint [str1=new, str2=new, str3=new]
changeStringValue(pt1);
System.out.println(pt1);
Method method1 =String.class.getMethod("charAt", int.class);
String str1 = "abc";
char ch = (Character) method1.invoke(str1, 1);
//char ch = (Character) method1.invoke(str1, new Object[]{2});
//b
System.out.println(ch);
//ReflectPoint [str1=new, str2=new, str3=new]
Method toStr = ReflectPoint.class.getMethod("toString");
System.out.println(toStr.invoke(pt1));
}
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(oldValue, "new");
field.set(obj, newValue);
}
}
}
}
import java.lang.reflect.Method;
public class MethodTest {
/**
* @param args
* @throws ClassNotFoundException
* @throws NoSuchMethodException
* @throws SecurityException
*/
public static void main(String[] args) throws Exception {
String startingClassName = args[0];
//根据类名拿到字节码,再拿到方法
Method mainMethod = Class.forName(startingClassName).getMethod("main", String[].class);
//main方法接收一个参数,数组对象。所以要把字符串数组包装成Object,数组也是对象
//它自己会拆开,就是三个参数,三个对象,就会报wrong number of arguments 异常
//或者new Object[]{new String[]{"1","2","3"}},拆开后是一个对象
mainMethod.invoke(null, (Object)new String[]{"1","2","3"});
}
}
class TestArguments {
//main方法接收一个参数,数组对象。所以要把字符串数组包装成Object
public static void main(String[] args) {
for(String arg:args) {
System.out.println(arg);
}
}
}
public class ReflectTest {
/**
* @param args
* @throws ClassNotFoundException
*/
public static void main(String[] args) throws ClassNotFoundException {
String str1 = "abc";
Class cls1 = str1.getClass();
Class cls2 = String.class;
Class cls3 = Class.forName("java.lang.String");
//true,同一份字节码
System.out.println(cls1==cls2);
System.out.println(cls1==cls3);
//非基本类型字节码
System.out.println(cls1.isPrimitive());
System.out.println(int.class.isPrimitive());
//false
System.out.println(int.class==Integer.class);
//true,常量代表基本类型
System.out.println(int.class==Integer.TYPE);
//false
System.out.println(int[].class.isPrimitive());
System.out.println(int[].class.isArray());
}
}