package JavaFour;
import org.junit.Test;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class ReflectionTest1 {
@Test
public void test() throws Exception {
Class<?> aClass = Class.forName("JavaFour.PersonReflection");
PersonReflection reflection = (PersonReflection) aClass.newInstance();
Field age = aClass.getDeclaredField("age");
age.setAccessible(true);
age.setInt(reflection, 20);
System.out.println(age.get(reflection));
}
@Test
public void test1() throws Exception {
Class<?> clazz = Class.forName("JavaFour.PersonReflection");
PersonReflection personReflection = (PersonReflection) clazz.newInstance();
Method show = clazz.getDeclaredMethod("show", String.class);
show.setAccessible(true);
show.invoke(personReflection, "gaoconghui");
System.out.println("************如何获取静态方法*******************");
Method showHobby = clazz.getDeclaredMethod("showHobby");
showHobby.setAccessible(true);
Object invoke = showHobby.invoke(PersonReflection.class);
}
@Test
public void test3() throws Exception {
Class clazz = PersonReflection.class;
Constructor declaredConstructor = clazz.getDeclaredConstructor(String.class);
declaredConstructor.setAccessible(true);
Object gaoconghui = declaredConstructor.newInstance("gaoconghui");
System.out.println(gaoconghui);
}
}
@Test
public void test2() throws Exception {
Properties pros = new Properties();
ClassLoader classLoader = ClassLoaderTest.class.getClassLoader();
InputStream is = classLoader.getResourceAsStream("jdbc1.properties");
pros.load(is);
String user = pros.getProperty("user");
String password = pros.getProperty("password");
System.out.println("user = " + user + ",password = " + password);
}
package com.atguigu.java;
import org.junit.Test;
import java.util.Random;
public class NewInstanceTest {
@Test
public void test1() throws IllegalAccessException, InstantiationException {
Class<Person> clazz = Person.class;
Person obj = clazz.newInstance();
System.out.println(obj);
}
@Test
public void test2(){
for(int i = 0;i < 100;i++){
int num = new Random().nextInt(3);
String classPath = "";
switch(num){
case 0:
classPath = "java.util.Date";
break;
case 1:
classPath = "java.lang.Object";
break;
case 2:
classPath = "com.atguigu.java.Person";
break;
}
try {
Object obj = getInstance(classPath);
System.out.println(obj);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public Object getInstance(String classPath) throws Exception {
Class clazz = Class.forName(classPath);
return clazz.newInstance();
}
}
@Test
public void test4(){
Class clazz = Person.class;
Type genericSuperclass = clazz.getGenericSuperclass();
ParameterizedType paramType = (ParameterizedType) genericSuperclass;
Type[] actualTypeArguments = paramType.getActualTypeArguments();
System.out.println(((Class)actualTypeArguments[0]).getName());
}
@Test
public void test7(){
Class clazz = Person.class;
Annotation[] annotations = clazz.getAnnotations();
for(Annotation annos : annotations){
System.out.println(annos);
}
}