The reflection of the Java Foundation

Source: Internet
Author: User

1. Three ways to reflect the generation of class objects

1) Use the GetClass method of the object type to get the class object

InputStream Is=this.getclass (). getClassLoader (). getResourceAsStream (path);

2) The class loader is forname by the static method of the class object to obtain

String Driver = ps.getproperty ("Driver");

Class.forName (driver);

3) by specifying the type of class to obtain

public class Jdbctools {

InputStream is = Jdbctools.class.getClassLoader (). getResourceAsStream ("jdbc.properties");

}

2. Class common methods

1) getannotation ( Class<A> annotationClass):获得指定类型的注释

2)Getannotations():获得所有的注释

3)getClassLoader()类加载器

4)GetConstructors():返回所有公有的构造方法

Class clazz = Class.forName ("reflect.student");

Clazz.getdeclaredconstructors ();

5) GetConstructor ( Class<?>... parameterTypes):返回指定公有的构造方法,没有会显示没有这种方法异常

constructor<?> Constructor = Clazz.getconstructor (String.class,int.class);

6) Getdeclaredconstructor (CLASS<?> parametertypes): Gets the private constructor method

7) getdeclaredconstructors (): Get all construction methods

8) Getdeclaredfield (String name): Gets a private property with

Clazz.getdeclaredfield ("name");

9)getDeclaredFields():获取所有属性

10)getDeclaredMethod(String name, Class<?>... parameterTypes):获取包含私有方法

    Clazz.getdeclaredmethod ("name", String.class);

11)getDeclaredMethods():获取所有方法

12)getField(String name):获取公有属性

13)getFields():获取所有私有属性

14)getGenericInterfaces():获取类或接口的直接实现

15)getGenericSuperclass():直接超类

Type Gentype = Clazz.getgenericsuperclass ()

16)getModifiers()获取修饰符

17)getSuperclass():表示实体的超类

  for(Class clazz=obj.getClass();clazz != Object.class; clazz.getSuperclass()){}

18)newInstance() :无参构造产生对象

3.field常用方法

getGenericType():获取声明类型

getModifiers():获取修饰权限

Field.getmodifiers ()

set(Object obj, Object value):设置值

Field.set (obj, param);

setAccessible(boolean flag):设置是否去除权限

Field.setaccessible (TRUE);

Get (Object obj): Get value

Field.get (object);

4.method

invoke(Object obj, Object... args):调用方法

Method.invoke (obj, parame);

setAccessible(boolean flag):设置是否去除权限

Method.setaccessible (TRUE);

The reflection of the Java Foundation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.