反射使用(一)
import org.junit.Test;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.annotation.ElementType;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Properties;
public class ReflectionTest {
@Test
public void test1() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchFieldException {
Class<Per> perClass = Per.class;
Constructor<Per> perConstructor = perClass.getConstructor(String.class, int.class);
Per tom = perConstructor.newInstance("tom", 12);
System.out.println(tom.toString());
Field age = perClass.getDeclaredField("age");
age.set(tom, 20);
System.out.println(tom.toString());
Method show = perClass.getDeclaredMethod("show");
show.invoke(tom);
Constructor<Per> dConstructor = perClass.getDeclaredConstructor(String.class);
dConstructor.setAccessible(true);
Per jerry = dConstructor.newInstance("jerry");
System.out.println(jerry);
Field name = perClass.getDeclaredField("name");
name.setAccessible(true);
name.set(jerry, "tom");
System.out.println(jerry.toString());
Method call = perClass.getDeclaredMethod("call", String.class);
call.setAccessible(true);
Object invoke = call.invoke(jerry, "你好!");
System.out.println(invoke);
}
@Test
public void test2() throws ClassNotFoundException {
Class perClazz1 = Per.class;
System.out.println(perClazz1);
Per per = new Per();
Class perClazz2 = per.getClass();
System.out.println(perClazz2);
Class perClazz3 = Class.forName("Per");
System.out.println(perClazz3);
ClassLoader classLoader = ReflectionTest.class.getClassLoader();
Class perClazz4 = classLoader.loadClass("Per");
System.out.println(perClazz4);
System.out.println(perClazz1 == perClazz2);
System.out.println(perClazz1 == perClazz3);
System.out.println(perClazz1 == perClazz4);
System.out.println(perClazz2 == perClazz3);
System.out.println(perClazz2 == perClazz4);
System.out.println(perClazz3 == perClazz4);
}
@Test
public void test3() {
Class<Object> objectClass = Object.class;
Class<Comparable> comparableClass = Comparable.class;
Class<String> stringClass = String.class;
Class<int[]> aClass = int[].class;
Class<ElementType> elementTypeClass = ElementType.class;
Class<Override> overrideClass = Override.class;
Class<Integer> integerClass = int.class;
Class<Void> voidClass = void.class;
Class<Class> classClass = Class.class;
int[] ints = new int[10];
int[] ints1 = new int[100];
Class<? extends int[]> aClass1 = ints.getClass();
Class<? extends int[]> aClass2 = ints1.getClass();
System.out.println(aClass1 == aClass2);
}
@Test
public void test4() {
ClassLoader classLoader = ReflectionTest.class.getClassLoader();
System.out.println(classLoader);
ClassLoader parent = classLoader.getParent();
System.out.println(parent);
ClassLoader parent1 = parent.getParent();
System.out.println(parent1);
ClassLoader classLoader1 = String.class.getClassLoader();
System.out.println(classLoader1);
}
@Test
public void test5() throws IOException {
Properties properties = new Properties();
FileInputStream fis = new FileInputStream("F:\\Lean\\Text\\src\\main\\resources\\jdbc.properties");
properties.load(fis);
String name1 = properties.getProperty("name");
String password1 = properties.getProperty("password");
System.out.println(name1 + ":" + password1);
ClassLoader loader = ReflectionTest.class.getClassLoader();
InputStream resource = loader.getResourceAsStream("jdbc.properties");
properties.load(resource);
String name2 = properties.getProperty("name");
String password2 = properties.getProperty("password");
System.out.println(name2 + ":" + password2);
}
@Test
public void test6() throws IllegalAccessException, InstantiationException {
Class<Per> perClass = Per.class;
Per o = perClass.newInstance();
System.out.println(o);
}
}