You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Christer Sandberg <si...@init.se> on 2003/12/31 13:27:39 UTC
Introspection of fields in Velocity.
Hi, maybe this collides with the philosophy of Velocity (after reading
the developer guide) but I made this class (see below) since we already
have a number of classes which acts as DAO, with all fields as public
with no setters or getters. Since a change in these classes would have
led to other changes as well etc, this is how I did for Velocity
v1.4-rc1 if someone is interested.
package se.baanii.velocity;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import org.apache.velocity.util.introspection.Info;
import org.apache.velocity.util.introspection.UberspectImpl;
import org.apache.velocity.util.introspection.VelPropertyGet;
import org.apache.velocity.util.introspection.VelPropertySet;
/**
* Overrides UberspectImpl to allow public fields to be used
* as properties.
*
* @author <a href="mailto:sillen@init.se">Christer Sandberg</a>
* @version $Id$
*/
public class FieldUberspect extends UberspectImpl
{
/**
* Property getter.
*/
public VelPropertyGet getPropertyGet(Object obj, String identifier,
Info i)
throws Exception
{
Class clazz = obj.getClass();
Field field = getField(clazz, identifier);
if(field != null)
{
return new VelFieldGetter(field);
}
else
{
return super.getPropertyGet(obj, identifier, i);
}
}
/**
* Property setter.
*/
public VelPropertySet getPropertySet(Object obj, String identifier,
Object arg, Info i)
throws Exception
{
Class clazz = obj.getClass();
Field field = getField(clazz, identifier);
if(field != null && !Modifier.isFinal(field.getModifiers()))
{
return new VelFieldSetter(field);
}
else
{
return super.getPropertySet(obj, identifier, arg, i);
}
}
public class VelFieldGetter implements VelPropertyGet
{
Field field;
public VelFieldGetter(Field field)
{
this.field = field;
}
public Object invoke(Object o) throws Exception
{
return field.get(o);
}
public boolean isCacheable()
{
return true;
}
public String getMethodName()
{
return field.getName();
}
}
public class VelFieldSetter implements VelPropertySet
{
Field field;
public VelFieldSetter(Field field)
{
this.field = field;
}
public Object invoke(Object o, Object value)
throws Exception
{
field.set(o, value);
return null;
}
public boolean isCacheable()
{
return true;
}
public String getMethodName()
{
return field.getName();
}
}
/**
* Get field for class.
*
* @param clazz Class to be used.
* @param name Name of field to get.
* @return Field or null if class dosen't contain a field
* with the specified name or if the field is not public.
*/
protected Field getField(Class clazz, String name)
{
try
{
Field field = clazz.getDeclaredField(name);
int mod = field.getModifiers();
if(Modifier.isPublic(mod))
{
return field;
}
}
catch(NoSuchFieldException e)
{
}
return null;
}
}
/Christer
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org
Re: Introspection of fields in Velocity.
Posted by Christer Sandberg <si...@init.se>.
Was a bit fast on the keyboard ...
Usage:
Velocity.setProperty(Velocity.UBERSPECT_CLASSNAME,
se.baanii.velocity.FieldUberspect");
Velocity.init();
etc.
This will check for the property as a public field first and if not
found it will delegate to the default UberspectImpl to check for methods
etc. Also works for the #set directive
(i.e. #set($foo.name = 'foobar')).
Christer Sandberg wrote:
> Hi, maybe this collides with the philosophy of Velocity (after reading
> the developer guide) but I made this class (see below) since we already
> have a number of classes which acts as DAO, with all fields as public
> with no setters or getters. Since a change in these classes would have
> led to other changes as well etc, this is how I did for Velocity
> v1.4-rc1 if someone is interested.
>
> package se.baanii.velocity;
>
> import java.lang.reflect.Field;
> import java.lang.reflect.Modifier;
>
> import org.apache.velocity.util.introspection.Info;
> import org.apache.velocity.util.introspection.UberspectImpl;
> import org.apache.velocity.util.introspection.VelPropertyGet;
> import org.apache.velocity.util.introspection.VelPropertySet;
>
> /**
> * Overrides UberspectImpl to allow public fields to be used
> * as properties.
> *
> * @author <a href="mailto:sillen@init.se">Christer Sandberg</a>
> * @version $Id$
> */
> public class FieldUberspect extends UberspectImpl
> {
> /**
> * Property getter.
> */
> public VelPropertyGet getPropertyGet(Object obj, String identifier,
> Info i)
> throws Exception
> {
> Class clazz = obj.getClass();
> Field field = getField(clazz, identifier);
>
> if(field != null)
> {
> return new VelFieldGetter(field);
> }
> else
> {
> return super.getPropertyGet(obj, identifier, i);
> }
> }
>
> /**
> * Property setter.
> */
> public VelPropertySet getPropertySet(Object obj, String identifier,
> Object arg, Info i)
> throws Exception
> {
> Class clazz = obj.getClass();
> Field field = getField(clazz, identifier);
>
> if(field != null && !Modifier.isFinal(field.getModifiers()))
> {
> return new VelFieldSetter(field);
> }
> else
> {
> return super.getPropertySet(obj, identifier, arg, i);
> }
> }
>
> public class VelFieldGetter implements VelPropertyGet
> {
> Field field;
>
> public VelFieldGetter(Field field)
> {
> this.field = field;
> }
>
> public Object invoke(Object o) throws Exception
> {
> return field.get(o);
> }
>
> public boolean isCacheable()
> {
> return true;
> }
>
> public String getMethodName()
> {
> return field.getName();
> }
> }
>
> public class VelFieldSetter implements VelPropertySet
> {
> Field field;
>
> public VelFieldSetter(Field field)
> {
> this.field = field;
> }
>
> public Object invoke(Object o, Object value)
> throws Exception
> {
> field.set(o, value);
>
> return null;
> }
>
> public boolean isCacheable()
> {
> return true;
> }
>
> public String getMethodName()
> {
> return field.getName();
> }
> }
>
> /**
> * Get field for class.
> *
> * @param clazz Class to be used.
> * @param name Name of field to get.
> * @return Field or null if class dosen't contain a field
> * with the specified name or if the field is not public.
> */
> protected Field getField(Class clazz, String name)
> {
> try
> {
> Field field = clazz.getDeclaredField(name);
> int mod = field.getModifiers();
>
> if(Modifier.isPublic(mod))
> {
> return field;
> }
> }
> catch(NoSuchFieldException e)
> {
> }
>
> return null;
> }
> }
>
> /Christer
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
>
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org