Java反射(Reflection)是一种新的操作类中成员变量、构造方法和普通方法的机制,为了实现对成员变量、构造方法和普通方法的操作,我们需要借助Java自身提供的java.lang包下的Class类和java.lang.reflect包下的反射API 。
package demo;
public class demo {
public static void main(String[] args) throws ClassNotFoundException {
Class class1=Class.forName("java.lang.Object");
System.out.println(class1.getName());
}
}
package demo;
public class demo {
public static void main(String[] args) throws ClassNotFoundException {
Class class1=Object.class;
System.out.println(class1.getName());
}
}
package demo;
public class demo {
public static void main(String[] args) throws ClassNotFoundException {
Class class1=int.class;
System.out.println(class1.getName());
Class class2=Integer.TYPE;
System.out.println(class2.getName());
}
}
package demo;
public class demo {
public static void main(String[] args) throws ClassNotFoundException {
Class class1=new Student().getClass();
System.out.println(class1.getName());
}
}
package demo;
public class demo {
public static void main(String[] args) throws ClassNotFoundException {
Class class1=int[].class;
System.out.println(class1.getName());
}
}
package demo;
public class demo {
public static void main(String[] args) throws ClassNotFoundException {
Class class1=new Student().getClass().getSuperclass();
System.out.println(class1.getName());
}
}
package demo;
import java.lang.reflect.Constructor;
public class demo {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, SecurityException {
Class class1 =Dog.class;
Constructor[] constructors=class1.getConstructors();
for (Constructor constructor : constructors) {
System.out.println(constructor);
}
Constructor constructor=class1.getConstructor();
System.out.println(constructor);
Constructor constructor1=class1.getDeclaredConstructor(String.class);
System.out.println(constructor1);
Constructor[] constructor2=class1.getDeclaredConstructors();
for (Constructor constructor3 : constructor2) {
System.out.println(constructor3);
}
}
}
class Dog{
private Dog(int age) {
}
protected Dog(String name) {
}
public Dog() {
}
public Dog(int age,String name) {
}
}
package demo;
import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;
public class demo {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, SecurityException {
Class class1 =Dog.class;
Constructor constructor=class1.getDeclaredConstructor(Integer.TYPE,String.class);
Class class2=constructor.getDeclaringClass();
System.out.println(class2.getName());
String constructorName=constructor.getName();
System.out.println(constructorName);
int modifier=constructor.getModifiers();
System.out.println(Modifier.toString(modifier));
Class[] classes=constructor.getParameterTypes();
for (Class class3 : classes) {
System.out.println(class3.getName());
}
}
}
class Dog{
private Dog(int age) {
}
protected Dog(String name) {
}
public Dog(int age,String name) {
}
}
package demo;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier;
public class demo {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Class class1 =Dog.class;
Constructor constructor=class1.getDeclaredConstructor(Integer.TYPE);
constructor.setAccessible(true);
Dog dog=(Dog) constructor.newInstance(1);
}
}
class Dog{
private Dog(int age) {
System.out.println(age);
}
protected Dog(String name) {
}
public Dog(int age,String name) {
}
public Dog() {
System.out.println("无参构造方法");
}
}
package demo;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
public class demo {
public static void main(String[] args) throws NoSuchFieldException, SecurityException {
Class class1 =Dog.class;
Field[] field1=class1.getFields();
System.out.println(field1.length);
Field[] field2=class1.getDeclaredFields();
System.out.println(field2.length);
Field field3=class1.getField("age");
System.out.println(field3);
Field field4=class1.getDeclaredField("name");
System.out.println(field4);
}
}
class Dog{
String name;
public int age;
private Dog(int age) {
System.out.println(age);
}
protected Dog(String name) {
}
public Dog(int age,String name) {
}
public Dog() {
System.out.println("无参构造方法");
}
}
package demo;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
public class demo {
public static void main(String[] args) throws NoSuchFieldException, SecurityException {
Class class1 =Dog.class;
Field field=class1.getDeclaredField("age");
System.out.println(field.getDeclaringClass().getName());
int mod=field.getModifiers();
System.out.println(Modifier.toString(mod));
Class class2=field.getType();
System.out.println(class2.getName());
Type object=field.getGenericType();
System.out.println(object);
String name=field.getName();
System.out.println(name);
}
}
class Dog{
String name;
public String age;
private Dog(int age) {
System.out.println(age);
}
protected Dog(String name) {
}
public Dog(int age,String name) {
}
public Dog() {
System.out.println("无参构造方法");
}
}
package demo;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
public class demo {
public static void main(String[] args) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
Class class1 =Dog.class;
Field field=class1.getDeclaredField("name");
Dog dog=new Dog();
field.setAccessible(true);
field.set(dog, "xxx");
Object obj=field.get(dog);
System.out.println(obj);
}
}
class Dog{
private String name;
public String age;
private Dog(int age) {
System.out.println(age);
}
protected Dog(String name) {
}
public Dog(int age,String name) {
}
public Dog() {
}
}
package demo;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
public class demo {
public static void main(String[] args) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, NoSuchMethodException {
Class class1 =Dog.class;
Method[] method1=class1.getMethods();
System.out.println(method1.length);
Method[] method2=class1.getDeclaredMethods();
System.out.println(method2.length);
Method method3=class1.getMethod("eat", String.class);
System.out.println(method3);
Method method4=class1.getDeclaredMethod("eat", String.class,Integer.TYPE);
System.out.println(method4);
}
}
class Dog{
public void eat(String a) {
}
public void eat(int b) {
}
public void eat() {
}
void eat(String a,int b) {
}
}
package demo;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
public class demo {
public static void main(String[] args) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, NoSuchMethodException {
Class class1 =Dog.class;
Method method=class1.getMethod("eat", String.class);
Class class2=method.getDeclaringClass();
System.out.println(class2.getName());
int mod=method.getModifiers();
System.out.println(Modifier.toString(mod));
Class class3=method.getReturnType();
System.out.println(class3.getName());
String name=method.getName();
System.out.println(name);
Class[] class4=method.getParameterTypes();
System.out.println(class4.length);
Class[] class5=method.getExceptionTypes();
System.out.println(class5.length);
}
}
class Dog{
public String eat(String a) throws NullPointerException,ClassNotFoundException{
return null;
}
public void eat(int b) {
}
public void eat() {
}
void eat(String a,int b) {
}
}
package demo;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
public class demo {
public static void main(String[] args) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
Class class1 =Dog.class;
Method method=class1.getDeclaredMethod("eat", String.class);
method.setAccessible(true);
Object object=method.invoke(new Dog(), "xx");
System.out.println(object);
}
}
class Dog{
private String eat(String a) {
return "sasa";
}
public void eat(int b) {
}
public void eat() {
}
void eat(String a,int b) {
}
}