jon         01/01/21 16:03:12

  Modified:    src/java/org/apache/velocity/util FieldMethodizer.java
  Log:
  untested, but it *should* work. added functionality to this class to allow it to be 
used for multiple classes...
  
  Revision  Changes    Path
  1.2       +39 -22    
jakarta-velocity/src/java/org/apache/velocity/util/FieldMethodizer.java
  
  Index: FieldMethodizer.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-velocity/src/java/org/apache/velocity/util/FieldMethodizer.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- FieldMethodizer.java      2001/01/21 20:52:48     1.1
  +++ FieldMethodizer.java      2001/01/22 00:03:11     1.2
  @@ -92,17 +92,25 @@
    *  to handle them by explicitly placing them into the context.
    *
    * @author <a href="mailto:[EMAIL PROTECTED]">Geir Magnusson Jr.</a>
  - * @version $Id: FieldMethodizer.java,v 1.1 2001/01/21 20:52:48 geirm Exp $ 
  + * @version $Id: FieldMethodizer.java,v 1.2 2001/01/22 00:03:11 jon Exp $ 
    */
   public class FieldMethodizer
   {
       /** Hold the field objects by field name */
  -    private HashMap fieldhash = new HashMap();
  -    
  -    /** The class we are 'methodizing' */
  -    Class clas = null;
  -    
  +    private HashMap fieldHash = new HashMap();
  +
  +    /** Hold the class objects by field name */
  +    private HashMap classHash = new HashMap();
  +
       /**
  +     * Allow object to be initialized without any data. You would use
  +     * addObject() to add data later.
  +     */
  +    public FieldMethodizer()
  +    {
  +    }
  +
  +    /**
        *  Constructor that takes as it's arg the name of the class
        *  to methodize.
        *
  @@ -112,8 +120,7 @@
       {
           try
           {
  -            clas = Class.forName( s );
  -            inspect();
  +            addObject(s);
           }
           catch( Exception e )
           {
  @@ -133,14 +140,31 @@
       {
           try
           {
  -            clas =  o.getClass();
  -            inspect();
  +            addObject(o);
           }
           catch( Exception e )
           {
               System.out.println( e );
           }
       }
  +    
  +    /**
  +     * Add the Name of the class to methodize
  +     */
  +    public void addObject ( String s )
  +        throws Exception
  +    {
  +        inspect(Class.forName(s));
  +    }
  +    
  +    /**
  +     * Add an Object to methodize
  +     */
  +    public void addObject ( Object o )
  +        throws Exception
  +    {
  +        inspect(o.getClass());
  +    }
   
       /**
        *  Accessor method to get the fields by name.
  @@ -153,15 +177,13 @@
       {
           try 
           {
  -            Field f = (Field) fieldhash.get( fieldName );
  -         
  +            Field f = (Field) fieldHash.get( fieldName );
               if (f != null)
  -                return f.get( clas );
  +                return f.get( (Class) classHash.get(fieldName) );
           }
           catch( Exception e )
           {
           }
  -      
           return null;
       }
   
  @@ -169,25 +191,20 @@
        *  Method that retrieves all public static fields
        *  in the class we are methodizing.
        */
  -    private void inspect()
  +    private void inspect(Class clas)
       {
           Field[] fields = clas.getFields();
  -
           for( int i = 0; i < fields.length; i++)
           {
               /*
                *  only if public and static
                */
  -
               int mod = fields[i].getModifiers();
  -
               if ( Modifier.isStatic(mod) && Modifier.isPublic(mod) )
               {
  -                fieldhash.put(fields[i].getName(), fields[i]);
  +                fieldHash.put(fields[i].getName(), fields[i]);
  +                classHash.put(fields[i].getName(), clas);
               }
           }
       }
   }
  -
  -
  -
  
  
  

Reply via email to