FileDocCategorySizeDatePackage
MethodUsageDemo.javaAPI DocExample2582Sun Dec 14 22:47:38 GMT 2003oreilly.hcj.reflection

MethodUsageDemo

public class MethodUsageDemo extends Object
Demonstrates dynamic usage of methods.
author
Robert Simmons jr. (kraythe)
version
$Revision: 1.3 $

Fields Summary
static final DateFormat
DATE_FORMATTER
Date formatter to be used.
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] args)
Main method.

param
args Command line arguments.
throws
RuntimeException If an unforseen error occurs.

		final Person person = new Person();

		try {
			person.setFirstName("Joe");
			person.setLastName("Smith");
			person.setGender(Gender.MALE);
			person.setTaxID("0121FG12");

			person.setBirthDate(DATE_FORMATTER.parse("01/12/1971"));

			// Output the object. 
			outputValues(person);
			outputValues(Gender.FEMALE);
		} catch (final Exception ex) {
			throw new RuntimeException(ex);
		}
	
public static final voidoutputValues(java.lang.Object obj)
Ouput the values of the "get" methods.

param
obj The object to output.
throws
InvocationTargetException If an error occurs in calling the get methods.
throws
IllegalAccessException If the security manager rejects access.


	                                 	 
	      
	       
		final String PREFIX = "get";  //$NON-NLS-1$
		System.out.println("--- Accessing Get Methods ---");
		Method[] meths = obj.getClass()
			                .getMethods();
		for (int idx = 0; idx < meths.length; idx++) {
			if (meths[idx].getName()
			              .startsWith(PREFIX)) {
				if (meths[idx].getParameterTypes().length == 0) {
					System.out.print(meths[idx].getName() + " = ");  //$NON-NLS-1$
					System.out.println(meths[idx].invoke(obj, null));
				}
			}
		}