java -w_java反射介绍

反射是java中的非常重要的一项机制,也称做reflection。它让java在运行中对自身进行检查,并能直接操作程序的内部属性或方法。

反射机制中常用的类

Reflection api中的内部信息有很多包括:package、 type parameters、 superclass、 implemented interfaces、 inner classes、 outer classes、 fields、 constructors、 methods、 modifiers等

常用的类主要包括:

Class:标示某个具体的类或接口

Constructor:封装了类的构造方法

Method:提供关于类或接口的方法的信息

Field:提供有关类或接口的属性信息,以及对他的动态访问权限

Class类:

Class本身就是一个类,Class是该类的名称,也是反射的起点;

获取Class的方法有:

getClass():调用通过指定类的对象调用;如:Class cl=str.getClass() 假设str是String对象,类属性为String

getSuperClass():通过Class类调用;如:Class clsup=cl.getSuperClass() 因为String的父类为Object,此去得到类属性为Object

Class.forName():Class中的静态方法,通过将类名当参数调用:如Class cl2=Class.forName("java.lang.String");注意:此去加载类

.Class:通过类名进行调用:如:Class cl3=String.Class;注意:此去不加载类

基本类型TYPE语法:包装类的调用方法:Class cl4=Integer.TYPE

主要的方法有:

1,public static Class> forName(String className) :natice 方法,动态加载类。非常重要。

如在sql中动态加载驱动程序:class.forName(sqlDriver);

2,public T newInstance() :根据对象的class新建一个对象,用于反射。非常重要。

可用在反射中构建对象,调用对象方法:

class doubleClass= class.forName("java.lang.Double");

Object objDouble = doubleClass.newInstance();

如在javaBean中就应用了这个方法,因为java默认要有一个无参构造函数。

3, public ClassLoader getClassLoader() :获得类的类加载器Bootstrap ,Extension ,System or user custom ClassLoader(一般为system classloader)。重要。

4,public String getName() :获取类或接口的名字。记住enum为类,annotation为接口。重要

5,public native Class getSuperclass():获取类的父类,继承了父类则返回父类,否则返回java.lang.Object。返回Object的父类为空-null。一般

6,public java.net.URL getResource(String name) :根据字符串获得资源。

7,其他类

public boolean isEnum() :判断是否为枚举类型。

public native boolean isArray() :判断是否为数组类型。

public native boolean isPrimitive() :判断是否为基本类型。

public boolean isAnnotation() :判断是否为注解类型。

public Package getPackage() :反射中获得package,如java.lang.Object 的package为java.lang。

public native int getModifiers() : 反射中获得修饰符,如public static void等 。

public Field getField(String name):反射中获得域成员。

public Field[] getFields() :获得域数组成员。

public Method[] getMethods() :获得方法。

public Method getDeclaredMethod(String name, Class>... parameterTypes):加个Declared代表本类,继承,父类均不包括。

public Constructor>[] getConstructors() :获得所有的构造函数。

Constructor类

获取构造器的方法:在Class类中提供的方法;

Constructor getConstructor(Class[] params) 根据构造函数的参数,返回一个具体的具有public属性的构造函数

Constructor getConstructors() 返回所有具有public属性的构造函数数组

Constructor getDeclaredConstructor(Class[] params) 根据构造函数的参数,返回一个具体的构造函数(不分public和非public属性)

Constructor getDeclaredConstructors() 返回该类中所有的构造函数数组(不分public和非public属性)

主要的方法有:

public String getName() :获取构造器的名字。

public native int getModifiers() : 反射中获得修饰符,如public static void等 。

getParameterTypes():获得参数的属性

newInstance(Object... initargs):使用构造器创建实例

Method类:

和获取构造器方法相同也有四种方法:

Method getMethod(String name, Class[] params) 根据方法名和参数,返回一个具体的具有public属性的方法

Method[] getMethods() 返回所有具有public属性的方法数组.包括从父类继承的public方法和实现接口的public方法

Method getDeclaredMethod(String name, Class[] params) 根据方法名和参数,返回一个具体的方法(不分public和非public属性)

Method[] getDeclaredMethods() 返回该类中的所有的方法数组(不分public和非public属性)不包括从父类继承的方法。

主要的方法有:

public String getName() :获取构造器的名字。

public native int getModifiers() : 反射中获得修饰符,如public static void等 。

getParameterTypes():获得参数的属性

getReturnType():获得返回值的属性

invoke(Object obj, Object... args):第一个参数为该方法的对象,第二个为参数;最重要的方法;

Field类:

获取Field的方法:在Class类中提供的方法;

Field getField(String name) 根据变量名,返回一个具体的具有public属性的成员变量

Field[] getFields() 返回具有public属性的成员变量的数组

Field getDeclaredField(String name) 根据变量名,返回一个成员变量(不分public和非public属性)

Field[] getDelcaredField() 返回所有成员变量组成的数组(不分public和非public属性)

主要方法有:Field类方法比较多,就不翻译了,不过完全可以通过名字知道用法

Object get(Object obj)

Returns the value of the field represented by this Field, on the specified object.

boolean getBoolean(Object obj)

Gets the value of a static or instance boolean field.

byte getByte(Object obj)

Gets the value of a static or instance byte field.

char getChar(Object obj)

Gets the value of a static or instance field of type char or of another primitive type convertible to type char via a widening conversion.

Class> getDeclaringClass()

Returns the Class object representing the class or interface that declares the field represented by this Fieldobject.

double getDouble(Object obj)

Gets the value of a static or instance field of type double or of another primitive type convertible to type doublevia a widening conversion.

float getFloat(Object obj)

Gets the value of a static or instance field of type float or of another primitive type convertible to type floatvia a widening conversion.

Type getGenericType()

Returns a Type object that represents the declared type for the field represented by this Field object.

int getInt(Object obj)

Gets the value of a static or instance field of type int or of another primitive type convertible to type int via a widening conversion.

long getLong(Object obj)

Gets the value of a static or instance field of type long or of another primitive type convertible to type long via a widening conversion.

int getModifiers()

Returns the Java language modifiers for the field represented by this Field object, as an integer.

String getName()

Returns the name of the field represented by this Field object.

short getShort(Object obj)

Gets the value of a static or instance field of type short or of another primitive type convertible to type shortvia a widening conversion.

Class> getType()

Returns a Class object that identifies the declared type for the field represented by this Field object.

boolean isEnumConstant()

Returns true if this field represents an element of an enumerated type; returns false otherwise.

boolean isSynthetic()

Returns true if this field is a synthetic field; returns false otherwise.

void set(Object obj, Object value)

Sets the field represented by this Field object on the specified object argument to the specified new value.

void setBoolean(Object obj, boolean z)

Sets the value of a field as a boolean on the specified object.

void setByte(Object obj, byte b)

Sets the value of a field as a byte on the specified object.

void setChar(Object obj, char c)

Sets the value of a field as a char on the specified object.

void setDouble(Object obj, double d)

Sets the value of a field as a double on the specified object.

void setFloat(Object obj, float f)

Sets the value of a field as a float on the specified object.

void setInt(Object obj, int i)

Sets the value of a field as an int on the specified object.

void setLong(Object obj, long l)

Sets the value of a field as a long on the specified object.

void setShort(Object obj, short s)

Sets the value of a field as a short on the specified object.

String toGenericString()

Returns a string describing this Field, including its generic type.

简要演示如下,详情看注释:

package net.peace.ref;

import java.lang.reflect.Constructor;

import java.lang.reflect.Field;

import java.lang.reflect.InvocationTargetException;

import java.lang.reflect.Method;

import java.util.ArrayList;

import java.util.Collection;

import java.util.Stack;

public class TestRef {

public static void main(String[] args) {

// TODO Auto-generated method stub

//Stack

try {

//装载指定的类

Class c=(Class) Class.forName("java.util.Stack");

//获得指定类名的方法;

Method ms[]=c.getDeclaredMethods();

//判断是否属于特定的类

System.out.println(c.isInstance(new Stack<>()));

for(Method m:ms){

//获得方法的一些信息;

System.out.println(m.getModifiers()+" "+m.getReturnType()+" "+m.getName());

//获得参数属性类

Class[] cc=m.getParameterTypes();

for(Class p:cc){

//获得类名

System.out.println(p.getName());

}

//获得异常类

Class[] ce=m.getExceptionTypes();

for(Class p:ce){

System.out.println(p.getName());

}

}

System.out.println("******************************");

//获得构造器方法

Constructor[] con=(Constructor[]) c.getDeclaredConstructors();

for(Constructor p:con){

System.out.println(p);

}

System.out.println("******************************");

//获取域

Field[] field=c.getDeclaredFields();

for(Field f:field){

System.out.println(f);

}

} catch (ClassNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

System.out.println("******************************");

try {

//通过反射创建不是默认对象的构造器

Class list=Class.forName("java.util.ArrayList");

//参数类型设置

Class ps[]=new Class[1];

ps[0]=Integer.TYPE;

//获得对应参数的构造器

Constructor cons=list.getConstructor(ps);

//通过调用构造器构建对象

Integer[] os={3};

ArrayList t=( ArrayList)cons.newInstance(os);

t.add(1);

System.out.println(t.size());

//利用反射调用方法

//创建参数

Class ps3[]= new Class[1];

ps3[0]=Object.class;

//获得想要的方法 add

Method method1=list.getDeclaredMethod("add", ps3);

//进行调用 添加一个元素2;

method1.invoke(t, 2);//第一个参数是对象,第二个参数是方法参数;

///调用另外一个构造方法

Class ps2[]=new Class[1];

ps2[0]=Collection.class;

Constructor cons2=list.getConstructor(ps2);

ArrayList t2=( ArrayList)cons2.newInstance(t);

System.out.println(t2);

} catch (ClassNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (NoSuchMethodException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (SecurityException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (InstantiationException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IllegalAccessException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IllegalArgumentException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (InvocationTargetException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值