Chris Rathman / Chris.Rathman@tx.rr.com

/************************************************************************
 *                                                                      *
 * java.lang.reflect                                                    *
 *                                                                      *
 *    Interfaces:                                                       *
 *       Member                                                         *
 *                                                                      *
 *    Classes:                                                          *
 *       Array                           Method                         *
 *       Constructor                     Modifier                       *
 *       Field                                                          *
 *                                                                      *
 *    Exceptions:                                                       *
 *       InvocationTargetException                                      *
 *                                                                      *
 ************************************************************************/
package Test.Chris;
import java.lang.reflect.*;

public class Java_lang_reflect {

   public void exercise() {
      constructor();
      field();
      method();

      array();
      modifier();
      member();
   }

   /*********************************************************************
    *                                                                   *
    * Constructor:                                                      *
    *                                                                   *
    *    Desc:                                                          *
    *                                                                   *
    *    Methods:                                                       *
    *       equals               getModifiers         hashCode          *
    *       getDeclaringClass    getName              newInstance       *
    *       getExceptionTypes    getParameterTypes    toString          *
    *                                                                   *
    *********************************************************************/
   void constructor() {
      int i;
      String s;
      Integer n;
      Class c;
      Class d;
      Class[] cx;
      testCMR z;
      Object[] ox = new Object[4];
      Constructor mx;

      c = testCMR.class;
      try {
         mx = c.getConstructor(new Class[] {char.class, int.class, double.class, String.class});
         s = mx.getName();              // name of the constructor
         d = mx.getDeclaringClass();    // returns class that declares the constructor
         i = mx.getModifiers();         // returns the Java language modifiers for the constructor
         i = mx.hashCode();             // returns hashcode for this Constructor
         s = mx.toString();             // returns string describing this Constructor

         cx = mx.getParameterTypes();   // returns array of Class objects of constructor parameter types
         for (i = 0; i < cx.length; i++) {
            d = cx[i];
         }
         cx = mx.getExceptionTypes();   // returns array of Class objects of the checked exceptions thrown
         for (i = 0; i < cx.length; i++) {
            d = cx[i];
         }

         ox[0] = new Character('X');
         ox[1] = new Integer(321);
         ox[2] = new Double(1.23);
         ox[3] = new String("ABC");
         z = (testCMR)mx.newInstance(ox); // create a new instance of the class

      } catch(InstantiationException e) {
      } catch(IllegalAccessException e) {
      } catch(InvocationTargetException e) {
      } catch(NoSuchMethodException e) {
      }

   /* TO BE DETERMINED
      equals(Object)                    // compares this Constructor against the specified object
   */
   }

   /*********************************************************************
    *                                                                   *
    * Field:                                                            *
    *                                                                   *
    *    Desc:                                                          *
    *                                                                   *
    *    Methods:                                                       *
    *       equals               getLong              setChar           *
    *       get                  getModifiers         setDouble         *
    *       getBoolean           getName              setFloat          *
    *       getByte              getShort             setInt            *
    *       getChar              getType              setLong           *
    *       getDeclaringClass    hashCode             setShort          *
    *       getDouble            set                  toString          *
    *       getFloat             setBoolean                             *
    *       getInt               setByte                                *
    *                                                                   *
    *********************************************************************/
   void field() {
      String s;
      boolean f;
      char a;
      byte b;
      int i;
      short j;
      long k;
      float x;
      double y;
      Class c;
      Class d;
      testCMR z = new testCMR();
      Field fx;
      Field[] fa;

      c = z.getClass();
      fa = c.getDeclaredFields();
      for (int n = 0; n < fa.length; n++) {
         fx = fa[n];
         d = fx.getDeclaringClass();    // returns Class object representing the class declares the field
         d = fx.getType();              // returns Class object that identifies the declared type of field
         s = fx.getName();              // returns the name of the field represented by this Field object
         try {
            if (s.equals("f")) {
               fx.setBoolean(z, false); // set the value of a field as a boolean on specified object
               f = fx.getBoolean(z);    // get the value of a field as a boolean on specified object
            } else if (s.equals("a")) {
               fx.setChar(z, 'Z');      // set the value of a field as a char on specified object
               a = fx.getChar(z);       // get the value of a field as a char on specified object
            } else if (s.equals("b")) {
               fx.setByte(z, (byte)44); // set the value of a field as a byte on specified object
               b = fx.getByte(z);       // get the value of a field as a int on specified object
            } else if (s.equals("i")) {
               fx.setInt(z, 555);       // set the value of a field as a int on specified object
               i = fx.getInt(z);        // get the value of a field as a int on specified object
            } else if (s.equals("j")) {
               fx.setShort(z, (short)7);// set the value of a field as a short on specified object
               j = fx.getShort(z);      // get the value of a field as a short on specified object
            } else if (s.equals("k")) {
               fx.setLong(z, 888);      // set the value of a field as a long on specified object
               k = fx.getLong(z);       // get the value of a field as a long on specified object
            } else if (s.equals("x")) {
               fx.setFloat(z, 9.13F);   // set the value of a field as a float on specified object
               x = fx.getFloat(z);      // get the value of a field as a float on specified object
            } else if (s.equals("y")) {
               fx.setDouble(z, 3.56);   // set the value of a field as a double on specified object
               y = fx.getDouble(z);     // get the value of a field as a double on specified object
            } else if (s.equals("s")) {
               fx.set(z, "World");      // set the value of a field on specified object
               s = (String)fx.get(z);   // get the value of a field on specified object
            }
            i = fx.getModifiers();      // returns the Java language modifiers for the field as integer
            i = fx.hashCode();          // returns a hashcode for this Field
         } catch(IllegalAccessException e) {
         }
      }

   /* TO BE DETERMINED
      equals(Object)                    // compares this Field against the specified object
   */
   }

   /*********************************************************************
    *                                                                   *
    * Method:                                                           *
    *                                                                   *
    *    Desc:                                                          *
    *                                                                   *
    *    Methods:                                                       *
    *       equals               getName              invoke            *
    *       getDeclaringClass    getParameterTypes    toString          *
    *       getExceptionTypes    getReturnType                          *
    *       getModifiers         hashCode                               *
    *                                                                   *
    *********************************************************************/
   void method() {
      int i;
      String s;
      Integer n;
      Class c;
      Class d;
      Class[] cx;
      testCMR z = new testCMR();
      Object[] ox = new Object[4];
      Method mx;

      c = z.getClass();
      try {
         mx = c.getMethod("testMethod", new Class[] {char.class, int.class, double.class, String.class});

         s = mx.getName();              // name of the method
         d = mx.getReturnType();        // return type of the method
         d = mx.getDeclaringClass();    // returns class that declares the method
         i = mx.getModifiers();         // returns the Java language modifiers for the method
         i = mx.hashCode();             // returns hashcode for this Method
         s = mx.toString();             // returns string describing this Method

         cx = mx.getParameterTypes();   // returns array of Class objects of method parameter types
         for (i = 0; i < cx.length; i++) {
            d = cx[i];
         }
         cx = mx.getExceptionTypes();   // returns array of Class objects of the checked exceptions thrown
         for (i = 0; i < cx.length; i++) {
            d = cx[i];
         }

         ox[0] = new Character('X');
         ox[1] = new Integer(321);
         ox[2] = new Double(1.23);
         ox[3] = new String("ABC");
         n = (Integer)mx.invoke(z, ox); // invokes the underlying method

      } catch(IllegalAccessException e) {
      } catch(InvocationTargetException e) {
      } catch(NoSuchMethodException e) {
      }

   /* TO BE DETERMINED
      equals(Object)                    // compares this Method against the specified object
   */
   }

   /*********************************************************************
    *                                                                   *
    * Array:                                                            *
    *                                                                   *
    *    Desc:                                                          *
    *                                                                   *
    *    Methods:                                                       *
    *       get                  getLength            setByte           *
    *       getBoolean           getLong              setChar           *
    *       getByte              getShort             setDouble         *
    *       getChar              newInstance          setFloat          *
    *       getDouble            newInstance          setInt            *
    *       getFloat             set                  setLong           *
    *       getInt               setBoolean           setShort          *
    *                                                                   *
    *********************************************************************/
   void array() {
      int i;
      int j;
      Object x;
      Object z;
      int[] oz = {5, 10};

      x = Array.newInstance(boolean.class, 5);        // create new array of component type and length
      for (i = 0; i < Array.getLength(x); i++) {      // length of array object
         Array.setBoolean(x, i, true);                // sets value of indexed component in array
         boolean n = (boolean)Array.getBoolean(x, i); // gets value of indexed component in array
      }
      x = Array.newInstance(char.class, 5);
      for (i = 0; i < Array.getLength(x); i++) {
         Array.setChar(x, i, 'A');
         char n = (char)Array.getChar(x, i);
      }
      x = Array.newInstance(byte.class, 5);
      for (i = 0; i < Array.getLength(x); i++) {
         Array.setByte(x, i, (byte)(i*2));
         byte n = (byte)Array.getByte(x, i);
      }
      x = Array.newInstance(short.class, 5);
      for (i = 0; i < Array.getLength(x); i++) {
         Array.setShort(x, i, (short)(i*2));
         short n = (short)Array.getShort(x, i);
      }
      x = Array.newInstance(int.class, 5);
      for (i = 0; i < Array.getLength(x); i++) {
         Array.setInt(x, i, i*2);
         int n = (int)Array.getInt(x, i);
      }
      x = Array.newInstance(long.class, 5);
      for (i = 0; i < Array.getLength(x); i++) {
         Array.setLong(x, i, i*2);
         long n = (long)Array.getLong(x, i);
      }
      x = Array.newInstance(float.class, 5);
      for (i = 0; i < Array.getLength(x); i++) {
         Array.setFloat(x, i, i*2);
         float n = (float)Array.getFloat(x, i);
      }
      x = Array.newInstance(double.class, 5);
      for (i = 0; i < Array.getLength(x); i++) {
         Array.setDouble(x, i, i*2);
         double n = (double)Array.getDouble(x, i);
      }

      z = Array.newInstance(Integer.class, oz);       // create new multidimensional array
      for (i = 0; i < 5; i++) {
         x = Array.get(z, i);
         for (j = 0; j < 10; j++) {
            Array.set(x, j, new Integer(i*j));        // sets value of indexed component in array
            Integer n = (Integer)Array.get(x, j);     // gets value of indexed component in array
         }
      }
   }

   /*********************************************************************
    *                                                                   *
    * Modifier:                                                         *
    *                                                                   *
    *    Desc:                                                          *
    *                                                                   *
    *    Methods:                                                       *
    *       ABSTRACT             PRIVATE              SYNCHRONIZED      *
    *       FINAL                PROTECTED            TRANSIENT         *
    *       INTERFACE            PUBLIC               VOLATILE          *
    *       NATIVE               STATIC                                 *
    *                                                                   *
    *    Methods:                                                       *
    *       isAbstract           isPrivate            isSynchronized    *
    *       isFinal              isProtected          isTransient       *
    *       isInterface          isPublic             isVolatile        *
    *       isNative             isStatic             toString          *
    *                                                                   *
    *********************************************************************/
   void modifier() {
      int i;
      boolean f;
      String s;
      Class c;
      testCMR z = new testCMR();
      Field fx;
      Field[] fa;

      c = z.getClass();
      fa = c.getDeclaredFields();
      for (int n = 0; n < fa.length; n++) {
         fx = fa[n];
         i = fx.getModifiers();

         f = Modifier.isAbstract(i);       // test for abstract modifier
         f = Modifier.isFinal(i);          // test for final modifier
         f = Modifier.isInterface(i);      // test for interface modifier
         f = Modifier.isNative(i);         // test for native modifier
         f = Modifier.isPrivate(i);        // test for private modifier
         f = Modifier.isProtected(i);      // test for protected modifier
         f = Modifier.isPublic(i);         // test for public modifier
         f = Modifier.isStatic(i);         // test for static modifier
         f = Modifier.isSynchronized(i);   // test for synchronized modifier
         f = Modifier.isTransient(i);      // test for transient modifier
         f = Modifier.isVolatile(i);       // test for volatile modifier
         s = Modifier.toString(i);         // string describing the access modifier flags
      }

      i = Modifier.ABSTRACT;
      i = Modifier.FINAL;
      i = Modifier.INTERFACE;
      i = Modifier.NATIVE;
      i = Modifier.PRIVATE;
      i = Modifier.PROTECTED;
      i = Modifier.PUBLIC;
      i = Modifier.STATIC;
      i = Modifier.SYNCHRONIZED;
      i = Modifier.TRANSIENT;
      i = Modifier.VOLATILE;
   }

   /*********************************************************************
    *                                                                   *
    * Member:                                                           *
    *                                                                   *
    *    Desc:                                                          *
    *                                                                   *
    *    Var:                                                           *
    *       DECLARED             PUBLIC                                 *
    *                                                                   *
    *    Methods:                                                       *
    *       getDeclaringClass    getModifiers         getName           *
    *                                                                   *
    *********************************************************************/
   void member() {
   /* TO BE DETERMINED
      DECLARED                          // identifies the set of declared members of a class or interface
      PUBLIC                            // identifies the set of all public members of a class or interface, including inherited members

      getDeclaringClass()               // returns the Class object representing the class or interface that declares the member or constructor represented by this Member
      getModifiers()                    // returns the Java language modifiers for the member or constructor represented by this Member, as an integer
      getName()                         // returns the simple name of the underlying member or constructor represented by this Member
   */
   }
}

class CMRException extends Throwable {
   CMRException() {
   }
   CMRException(String s) {
      super(s);
   }
}

class testCMR {
   public boolean f = true;
   public char a = 'A';
   public byte b = 123;
   public int i = 1234;
   public short j = 4321;
   public long k = 2134;
   public float x = 1.23F;
   public double y = 3.21;
   public String s = "Hello";

   public testCMR() {
   }

   public testCMR(char a, int i, double y, String s) throws CMRException {
      if (a == ' ') {
         throw new CMRException("Test Exception");
      }
   }

   public int testMethod(char a, int i, double y, String s) throws CMRException {
      if (a == ' ') {
         throw new CMRException("Test Exception");
      } else {
         return(1234);
      }
   }
}

Chris Rathman / Chris.Rathman@tx.rr.com