Java lang reflect package and element Is in Class. Reflection is an important capability, needed when using components coiled Java Beans, It allows you to analyze CI software component and describe its capabilities dynamically, at run time rather than at compile lime. For example by using reflection, you can determine what methods, constructors, and fields class sports.

The package java.lang.reflect has one interface, called Member, which defines methods that allow you to get information about 0 field, constructor, or method of a class, There IHC also seven classes in this package. These are listed in Table Zd-Z, The following application illustrates  simple use of the Java reflection capabilities, It prints the constructors,methods of the class java.awt.Dimension. The progl’ilm begins by  forname{ ) method of c;J:lSS lo Hct a class object for java.awt.Dlmenslons. Once this is obtained, gelCollstructors( ), getFields( ), and get Methods( ) are used to analyze Inls class object. They return arrays of Constructor, Field, and Method objects that provide the information about the object. The Constructor, Field, and Method iasses define several methods that can be to obtain information . You will want to these on your own.

Each element of the methods array is then processed. The getmodifiers ) method returns an int containing flags that describe which access modifiers apply for this element. The. Modifier class provides a set of methods, shown in Table 24-3, that can be Used to examine this value. For example, the static method i~I’ub1ic( ) returns true if Hz argument includes the public access modifier. Otherwise, it returns false. InIn The method supports public access, its name is obtained by the gcmc() method and is then printed.

[vfb id=1]

Share This