Java The Complete Reference, Seventh Edition

(Greg DeLong) #1

Chapter 27: NIO, Regular Expressions, and Other Packages 835


Here is the output from this program. (The precise order may differ slightly from that shown.)


Constructors:
public java.awt.Dimension(int,int)
public java.awt.Dimension()
public java.awt.Dimension(java.awt.Dimension)
Fields:
public int java.awt.Dimension.width
public int java.awt.Dimension.height
Methods:
public int java.awt.Dimension.hashCode()
public boolean java.awt.Dimension.equals(java.lang.Object)
public java.lang.String java.awt.Dimension.toString()
public java.awt.Dimension java.awt.Dimension.getSize()
public void java.awt.Dimension.setSize(double,double)
public void java.awt.Dimension.setSize(java.awt.Dimension)
public void java.awt.Dimension.setSize(int,int)
public double java.awt.Dimension.getHeight()
public double java.awt.Dimension.getWidth()
public java.lang.Object java.awt.geom.Dimension2D.clone()
public void java.awt.geom.
Dimension2D.setSize(java.awt.geom.Dimension2D)
public final native java.lang.Class java.lang.Object.getClass()
public final native void java.lang.Object.wait(long)
throws java.lang.InterruptedException
public final void java.lang.Object.wait()
throws java.lang.InterruptedException
public final void java.lang.Object.wait(long,int)
throws java.lang.InterruptedException
public final native void java.lang.Object.notify()
public final native void java.lang.Object.notifyAll()

The next example uses Java’s reflection capabilities to obtain the public methods of a
class. The program begins by instantiating classA. ThegetClass( )method is applied to
this object reference, and it returns theClassobject for classA. ThegetDeclaredMethods( )
method returns an array ofMethodobjects that describe only the methods declared by
thisclass. Methods inherited from superclasses such asObjectare not included.
Each element of themethodsarray is then processed. ThegetModifiers( )method returns
anintcontaining flags that describe which modifiers apply for this element. TheModifier
class provides a set of methods, shown in Table 27-5, that can be used to examine this value.
For example, the static methodisPublic( )returnstrueif its argument includes thepublic
modifier. Otherwise, it returnsfalse. In the following program, if the method supports
public access, its name is obtained by thegetName( )method and is then printed.


// Show public methods.
import java.lang.reflect.*;
public class ReflectionDemo2 {
public static void main(String args[]) {

Free download pdf