net.sf.freecol.common.util
Class Introspector

java.lang.Object
  extended by net.sf.freecol.common.util.Introspector

public class Introspector
extends java.lang.Object

A class to allow access to the methods "fooType getFoo()" and "void setFoo(fooType)" conventionally seen in objects. Useful when Foo arrives as a run-time String, such as is the case in serialization to/from XML representations.


Field Summary
private  java.lang.String field
          The field whose get/set methods we wish to invoke.
private  java.lang.Class<?> theClass
          The class whose field we are to operate on.
 
Constructor Summary
Introspector(java.lang.Class<?> theClass, java.lang.String field)
          Build a new Introspector for the specified class and field name.
 
Method Summary
private  java.lang.reflect.Method getFromStringConverter(java.lang.Class<?> argType)
          Get a function that converts from String to a given class.
private  java.lang.reflect.Method getGetMethod()
          Get a get-method for this Introspector.
private  java.lang.Class<?> getMethodReturnType(java.lang.reflect.Method method)
          Get the return type from a Method.
private  java.lang.reflect.Method getSetMethod(java.lang.Class argType)
          Get a set-method for this Introspector.
 java.lang.String getter(java.lang.Object obj)
          Invoke the get-method for this Introspector.
private  java.lang.reflect.Method getToStringConverter(java.lang.Class<?> argType)
          Get a function that converts to String from a given class.
 void setter(java.lang.Object obj, java.lang.String value)
          Invoke the set-method provided by this Introspector.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

theClass

private java.lang.Class<?> theClass
The class whose field we are to operate on.


field

private java.lang.String field
The field whose get/set methods we wish to invoke.

Constructor Detail

Introspector

public Introspector(java.lang.Class<?> theClass,
                    java.lang.String field)
             throws java.lang.IllegalArgumentException
Build a new Introspector for the specified class and field name.

Parameters:
theClass - The Class of interest.
field - The field name within the class of interest.
Throws:
java.lang.IllegalArgumentException
Method Detail

getGetMethod

private java.lang.reflect.Method getGetMethod()
                                       throws java.lang.IllegalArgumentException
Get a get-method for this Introspector.

Returns:
A Method representing getField().
Throws:
java.lang.IllegalArgumentException

getSetMethod

private java.lang.reflect.Method getSetMethod(java.lang.Class argType)
                                       throws java.lang.IllegalArgumentException
Get a set-method for this Introspector.

Parameters:
argType - A Class that is the argument to the set-method
Returns:
A Method representing setField().
Throws:
java.lang.IllegalArgumentException

getMethodReturnType

private java.lang.Class<?> getMethodReturnType(java.lang.reflect.Method method)
                                        throws java.lang.IllegalArgumentException
Get the return type from a Method.

Parameters:
method - The Method to examine.
Returns:
The method return type, or null on error.
Throws:
java.lang.IllegalArgumentException

getToStringConverter

private java.lang.reflect.Method getToStringConverter(java.lang.Class<?> argType)
                                               throws java.lang.IllegalArgumentException
Get a function that converts to String from a given class. We use Enum.name() for enums, and String.valueOf(argType) for the rest.

Parameters:
argType - A Class to find a converter for.
Returns:
A conversion function, or null on error.
Throws:
java.lang.IllegalArgumentException

getFromStringConverter

private java.lang.reflect.Method getFromStringConverter(java.lang.Class<?> argType)
                                                 throws java.lang.IllegalArgumentException
Get a function that converts from String to a given class. We use Enum.valueOf(Class, String) for enums, and argType.valueOf(String) for the rest, having first dodged the primitive types.

Parameters:
argType - A Class to find a converter for.
Returns:
A conversion function, or null on error.
Throws:
IllegalArgumentExcpetion
java.lang.IllegalArgumentException

getter

public java.lang.String getter(java.lang.Object obj)
                        throws java.lang.IllegalArgumentException
Invoke the get-method for this Introspector.

Parameters:
obj - An Object (really of type theClass) whose get-method is to be invoked.
Returns:
A String containing the result of invoking the get-method.
Throws:
java.lang.IllegalArgumentException

setter

public void setter(java.lang.Object obj,
                   java.lang.String value)
            throws java.lang.IllegalArgumentException
Invoke the set-method provided by this Introspector.

Parameters:
obj - An Object (really of type theClass) whose set-method is to be invoked.
value - A String containing the value to be set.
Throws:
java.lang.IllegalArgumentException