java 反射

Properties的使用注意事项

package atguigu.day28.review;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class PropertiesTest {

	public static void main(String[] args) throws IOException {
	
		//1创建 一个 Properties 实例
		Properties prop = new Properties();
		
		//2. 通过load 方法 与 jdbc.properties 关联
		//prop.load(new FileInputStream("D:\\workspace\\AtGuiGu\\src\\atguigu\\day28\\review\\jdbc.properties") );
		prop.load(new FileInputStream("jdbc.properties")); //必须放在当前项目路径下
		
//		ClassLoader cl = PropertiesTest.class.getClassLoader();
//		//以流的形式获取资源,参数列表为类加载路径(包路径)
//		InputStream in = cl.getResourceAsStream("atguigu/day28/review/jdbc.properties");
//		prop.load(in);
		
		//3. 根据 String 类型的 key 获取 String 类型的 value
		String username = prop.getProperty("username");
		String password = prop.getProperty("password");
		
		System.out.println(username);
		System.out.println(password);
		
		
		
	}
}

反射前的准备,一些类等

写在一个里面了

package atguigu.day28.review;

public class Creature <T>{

	String color;
	public int legs;

	public Creature() {
	}

	public Creature(String color, int legs) {
		this.color = color;
		this.legs = legs;
	}

	public String getColor() {
		return color;
	}

	public void setColor(String color) {
		this.color = color;
	}

	public int getLegs() {
		return legs;
	}

	public void setLegs(int legs) {
		this.legs = legs;
	}
}

package atguigu.day28.review;


import static java.lang.annotation.ElementType.CONSTRUCTOR;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.LOCAL_VARIABLE;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.ElementType.TYPE;


import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
	String value() default "atguigu";
}

package atguigu.day28.review;

public interface MyInterface {

}


package atguigu.day28.review;

public class Person  extends Creature<String> implements Comparable<Integer>, MyInterface{
	private String name;
	public int age;

	public Person() {
	}

	private Person(String name, int age) {
		this.name = name;
		this.age = age;
	}

	public String getName() {
		return name;
	}

	public void setName(String name, int age, double height) {
		System.out.println("setName 方法运行");
		System.out.println(name);
		System.out.println(age);
		System.out.println(height);
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	@Override
	public String toString() {
		return "Person [name=" + name + ", age=" + age + "]";
	}
	
	@MyAnnotation
	public void eat() throws RuntimeException{
		System.out.println("吃饭");
	}
	
	private String sleep(){
		System.out.println("睡觉");
		return "哈哈哈,方法返回值";
	}
	
	public class Computer{
		
	}
	
	private class Head{
		
	}

	@Override
	public int compareTo(Integer o) {
		// TODO Auto-generated method stub
		return 0;
	}
}

访问和修改构造器的方法

package atguigu.day28.review;

import java.lang.reflect.Constructor;

import org.junit.Test;

public class ConstructorTest {

	//在运行时获取运行时类的构造器
	@Test
	public void test1() throws Exception{
		Class<?> clazz = Class.forName("atguigu.day28.review.Person");
		//访问构造器 public 的
		Constructor<?>[] constructors = clazz.getConstructors();
		
		for (Constructor<?> constructor : constructors) {
			System.out.println(constructor.getName());
		}
		
		System.out.println("--------------------------");
		//访问 本类所有的 构造器 包括 private
		Constructor<?>[] constructors2 = clazz.getDeclaredConstructors();
		for (Constructor<?> constructor : constructors2) {
			System.out.println(constructor);
			
		}
	}
	
	@Test
	public void test2() throws Exception{
		Class<Person> clazz = Person.class;
		
		//不能获取 带参构造,因为带参构造是 private 
		//Constructor<Person> constructor = clazz.getConstructor(String.class, int.class);
		//访问本类 私有带参构造
		Constructor<Person> constructor = clazz.getDeclaredConstructor(String.class , int.class);
		
		//访问私有属性时,让这个属性可见。暴力访问 
		constructor.setAccessible(true);
		Person person = constructor.newInstance("hello",20);
		
		System.out.println(person);
	}
}

访问和修改字段的方法

package atguigu.day28.review;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;

import org.junit.Test;

public class FieldTest {

	//1. 在运行时获取任意类的属性
	@Test
	public void test1() throws Exception{
		String className = "atguigu.day28.review.Person";
		Class<?> clazz = Class.forName(className);
		
		//getFields():获取所有 public 修饰的属性,包括父类的
		Field[] fields = clazz.getFields();
		for (Field field : fields) {
			System.out.println(field.getName());
		}
		System.out.println("-----------------------");
		//getDeclaredFields():获取本类中所有的属性,包括私有的,不包括父类的
		Field[] fields2 = clazz.getDeclaredFields();
		for (Field field : fields2) {
			System.out.println(field.getName());
		}
		
	}
	
	
	//2. 在运行时获取运行时类属性的完整结构. 修饰符 数据类型 属性名;
	@Test
	public void test2(){
		Class clazz = Person.class;
		Field[] fields = clazz.getDeclaredFields();
		for (Field field : fields) {
			//1.修饰符
			int mod = field.getModifiers();
			String string = Modifier.toString(mod);
			System.out.println(string);
			
			//2. 数据类型
			Class<?> type = field.getType();
			System.out.println(type);
			
			//3, 属性名
			System.out.println(field.getName());
			
		}
	}
	
	//3. 【重要】在运行时获取并操作运行时类对象的属性
	@Test
	public void test3() throws Exception{
		Class<Person> clazz = Person.class;
		Person p = clazz.newInstance();
		Field age = clazz.getField("age");
		//1.设置属性值
		age.set(p, 201);
		//2.获取属性值
		Object value = age.get(p);
		System.out.println(value);
		
		System.out.println("-------------------");
		
		//name 字段是 private ,这个方法不可访问
		//Field field = clazz.getField("name");
		Field name = clazz.getDeclaredField("name");
		//忽略访问权限
		name.setAccessible(true);
		name.set(p, "李四");
		Object nameValue = name.get(p);
		System.out.println(nameValue);
		
		
		
		
	}
}

调用方法的方法

package atguigu.day28.review;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

import org.junit.Test;

public class MethodTest {

	//1. 在运行时获取运行时类的方法
	@Test
	public void test1() throws Exception{
		String str = "atguigu.day28.review.Person";
		Class<?> clazz = Class.forName(str);
		
		//获取 本类和父类的所有 public 方法
		Method[] methods = clazz.getMethods();
		for (Method method : methods) {
			System.out.println(method.getName());
		}
		
		//getDeclaredMethods():获取本类中所有的方法,包括私有的,不包括父类
		System.out.println("---------------------");
		Method[] methods2 = clazz.getDeclaredMethods();
		for (Method method : methods2) {
			System.out.println(method.getName());
		}
	}
	
	//2. 在运行时获取运行时类方法的完整结构: 注解 修饰符 返回值类型 方法名(参数类型1 参数名1, 参数类型2 参数名2 ……) 异常
	@Test 
	public void test2(){
		Class clazz = Person.class;
		
		Method[] methods = clazz.getDeclaredMethods();
		for (Method method : methods) {
			//注解
			Annotation[] annotations = method.getAnnotations();
			for (Annotation annotation : annotations) {
				System.out.println(annotation);
			}
			
			//修饰符 返回的是int 类型
			int modifiers = method.getModifiers();
			//转成 String类型   注意Modifier.toString是静态方法
			String string = Modifier.toString(modifiers);
			System.out.println("修饰符:" + string);
			
			
			//返回值类型
			Class<?> type = method.getReturnType();
			System.out.println( "返回值类型:" + type.getName());
			//3. 方法名
			System.out.print(method.getName() + "(");
			
			//4. 参数列表
			Class[] parameterTypes = method.getParameterTypes();
			for (Class class1 : parameterTypes) {
				System.out.print(" 参数列表" + class1.getName());
			}
			
			System.out.println(")");
			
			//5. 异常
			Class[] exceptionTypes = method.getExceptionTypes();
			
			for (Class class1 : exceptionTypes) {
				System.out.println("异常:" + class1.getName());
			}
		}
		
	}
	
	//3. 在运行时获取并调用运行时类对象的方法
	@Test
	public void test3() throws Exception {
		Class<Person> clazz = Person.class;
		//创建此 Class 对象所表示的类的一个新实例。
		Person p =  clazz.newInstance();
		
		//无参无返回值
		Method method = clazz.getMethod("eat");
	
		//对带有指定参数的指定对象调用由此 Method 对象表示的底层方法。	
		method.invoke(p);
		System.out.println("------------------");
		
		
		
		//有参无返回值
		Method method2 = clazz.getMethod("setName", String.class,int.class,double.class);
		Object invoke = method2.invoke(p, "李四",12,23.1);
		System.out.println(invoke);
		
		System.out.println("---------------------");
		
		//私有,无参有返回值   私有不能直接 invoke 
		Method method3 = clazz.getDeclaredMethod("sleep");
		//忽略访问权限
		method3.setAccessible(true);
		Object invoke2 = method3.invoke(p);
		System.out.println(invoke2);
		
	}
	
}

访问其他的方法(注解,内部类,接口,泛型,父类。。。)

package atguigu.day28.review;

import java.lang.annotation.Annotation;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

import org.junit.Test;

public class OtherTes {

	//7. 获取运行时类的包
	@Test
	public void test1(){
		Class<Person> clazz = Person.class;
		Package package1 = clazz.getPackage();
		System.out.println(package1);
		
	}
	
	//6. 获取运行时类的注解
	@Test
	public void test2(){
		Class clazz= Person.class;
		
		Annotation[] annotations = clazz.getAnnotations();
		for (Annotation annotation : annotations) {
			MyAnnotation ma = (MyAnnotation) annotation;
			System.out.println(ma.value());
		}
	}
	
	//5. 获取运行时类的内部类
	@Test
	public void test3(){
		Class clazz = Person.class;
		
		Class[] classes = clazz.getDeclaredClasses();
		for (Class class1 : classes) {
			
			System.out.println(class1.getName());
		}
	}
	
	//4. 获取运行时类的接口
	@Test
	public void test4(){
		Class clazz = Person.class;
		
		Class[] interfaces = clazz.getInterfaces();
		for (Class class1 : interfaces) {
			System.out.println(class1.getName());
		}
	}
	
	//3. 【重要】获取运行时类带泛型父类的泛型类型
	@Test
	public void test5(){
		Class clazz = Person.class;
		//1. 获取带泛型父类的类型
		Type type = clazz.getGenericSuperclass();
		
		//2. 参数化类型
		ParameterizedType pt = (ParameterizedType) type;
		
		//3. 获取真实参数类型
		Type[] types = pt.getActualTypeArguments();
		for (Type type2 : types) {
			System.out.println(type2.getTypeName());
			System.out.println("------------------");
			Class cl = (Class) type2;
			System.out.println(cl.getName());
			
		}
		
	}
	
	//2. 获取运行时类带泛型父类的类型 com.atguigu.java.Creature<java.lang.String> c = new Creature<>()
	@Test
	public void test6(){
		Class clazz = Person.class;
		Type type = clazz.getGenericSuperclass();
		System.out.println(type);
	}
	
	//1. 获取运行时类的父类
	@Test
	public void test7(){
		Class clazz = Person.class;
		Class class1 = clazz.getSuperclass();
		System.out.println(class1);
	}
	
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值