[CJ-dev] commonjava-projects/commonjava-reflection/src/java/org/commonjava/reflection Reflector.java
Brought to you by:
johnqueso
From: John C. <joh...@co...> - 2004-06-24 01:06:10
|
Update of /cvsroot/commonjava/commonjava-projects/commonjava-reflection/src/java/org/commonjava/reflection In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv18618/src/java/org/commonjava/reflection Modified Files: Reflector.java Log Message: added getField() and getStaticField(). Index: Reflector.java =================================================================== RCS file: /cvsroot/commonjava/commonjava-projects/commonjava-reflection/src/java/org/commonjava/reflection/Reflector.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -d -r1.3 -r1.4 --- Reflector.java 20 Apr 2004 22:52:05 -0000 1.3 +++ Reflector.java 24 Jun 2004 01:06:01 -0000 1.4 @@ -16,6 +16,7 @@ package org.commonjava.reflection; import java.lang.reflect.Constructor; +import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.HashMap; @@ -161,6 +162,41 @@ } } + public static Object getStaticField(Class targetClass, String fieldName) + throws ReflectorException + { + try { + Field field = targetClass.getField(fieldName); + return field.get(null); + } catch (SecurityException e) { + throw new ReflectorException(e); + } catch (NoSuchFieldException e) { + throw new ReflectorException(e); + } catch (IllegalArgumentException e) { + throw new ReflectorException(e); + } catch (IllegalAccessException e) { + throw new ReflectorException(e); + } + } + + public static Object getField(Object target, String fieldName) + throws ReflectorException + { + try { + Class targetClass = target.getClass(); + Field field = targetClass.getField(fieldName); + return field.get(target); + } catch (SecurityException e) { + throw new ReflectorException(e); + } catch (NoSuchFieldException e) { + throw new ReflectorException(e); + } catch (IllegalArgumentException e) { + throw new ReflectorException(e); + } catch (IllegalAccessException e) { + throw new ReflectorException(e); + } + } + /** Invoke the specified static method with the specified params... * * @param targetClass The target class of the invocation |