Inspecting a Class for Its Methods

The previous examples have focused on constructors, but Reflection also allows you to discover the regular methods a class declares. This functionality is again provided by java.lang.Class.

Obtaining a List of Methods

Going back to the Requestor/Provider example used in Listing 28.1, a few simple changes allow the methods of a class to be queried and displayed along with its constructors. Listing 28.6 shows the necessary modifications.

Code Listing 28.6. Reflection Reveals Methods as Well as Constructors
 /* * Requestor */ import java.lang.reflect.*; public class Requestor { public void requestConstuctors() { try { // load the Provider class and use Reflection // to get an array of constructor information Class ...

Get Special Edition Using Java 2 Standard Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.