1 package org.apache.velocity.app; 2 3 18 19 import java.lang.Class ; 20 import java.lang.reflect.Field ; 21 import java.lang.reflect.Modifier ; 22 import java.util.HashMap ; 23 24 59 public class FieldMethodizer 60 { 61 62 private HashMap fieldHash = new HashMap (); 63 64 65 private HashMap classHash = new HashMap (); 66 67 71 public FieldMethodizer() 72 { 73 } 74 75 81 public FieldMethodizer( String s ) 82 { 83 try 84 { 85 addObject(s); 86 } 87 catch( Exception e ) 88 { 89 System.out.println( e ); 90 } 91 } 92 93 101 public FieldMethodizer( Object o ) 102 { 103 try 104 { 105 addObject(o); 106 } 107 catch( Exception e ) 108 { 109 System.out.println( e ); 110 } 111 } 112 113 116 public void addObject ( String s ) 117 throws Exception 118 { 119 inspect(Class.forName(s)); 120 } 121 122 125 public void addObject ( Object o ) 126 throws Exception 127 { 128 inspect(o.getClass()); 129 } 130 131 138 public Object get( String fieldName ) 139 { 140 try 141 { 142 Field f = (Field ) fieldHash.get( fieldName ); 143 if (f != null) 144 return f.get( (Class ) classHash.get(fieldName) ); 145 } 146 catch( Exception e ) 147 { 148 } 149 return null; 150 } 151 152 156 private void inspect(Class clas) 157 { 158 Field [] fields = clas.getFields(); 159 for( int i = 0; i < fields.length; i++) 160 { 161 164 int mod = fields[i].getModifiers(); 165 if ( Modifier.isStatic(mod) && Modifier.isPublic(mod) ) 166 { 167 fieldHash.put(fields[i].getName(), fields[i]); 168 classHash.put(fields[i].getName(), clas); 169 } 170 } 171 } 172 } 173 | Popular Tags |