Accessing constants in JSF

Posted by Daz Thu, 27 Aug 2009 23:13:57 GMT


marker interface:

package uk.co.tarbard.wfd;

public interface BackingPage {

}

custom property resolver:

package uk.co.tarbard.wfd;

import java.lang.reflect.Field;

import javax.faces.el.EvaluationException;
import javax.faces.el.PropertyNotFoundException;
import javax.faces.el.PropertyResolver;

public class ConstantPropertyResolver extends PropertyResolver {

	private PropertyResolver original;

	public ConstantPropertyResolver(PropertyResolver original) {
		this.original = original;
	}

	@Override
	public Class getType(Object arg0, int arg1) throws EvaluationException,
			PropertyNotFoundException {

		return original.getType(arg0, arg1);
	}

	@Override
	public Class getType(Object arg0, Object arg1) throws EvaluationException,
			PropertyNotFoundException {

		return original.getType(arg0, arg1);
	}

	@Override
	public Object getValue(Object arg0, int arg1) throws EvaluationException,
			PropertyNotFoundException {
		return original.getValue(arg0, arg1);
	}

	@Override
	public Object getValue(Object base, Object property)
			throws EvaluationException, PropertyNotFoundException {
		Object value = null;
		try {
			value = original.getValue(base, property);

		} catch (PropertyNotFoundException e) {

		}

		if (null != value) {
			return value;
		}
		if (null == base) {
			return null;
		}
		if (base instanceof BackingPage) {
			Field[] declaredFields = base.getClass().getDeclaredFields();
			for (int i = 0; i < declaredFields.length; i++) {
				Field x = declaredFields[i];
				Object constantValue = null;
				if (property.equals(x.getName())){
					try {
						constantValue =  x.get(x);
					} catch (IllegalArgumentException e) {
						throw new PropertyNotFoundException("unable to access property " + property) ;
					} catch (IllegalAccessException e) {
						throw new PropertyNotFoundException("unable to access property " + property + " check that it is public") ;
					}
					return constantValue;
				}
				
			}
			throw new PropertyNotFoundException("unable to locate constant with name " + property + " perhaps you made a typo.");
		}

		return null;

	}

	@Override
	public boolean isReadOnly(Object arg0, int arg1)
			throws EvaluationException, PropertyNotFoundException {
		return original.isReadOnly(arg0, arg1);
	}

	@Override
	public boolean isReadOnly(Object arg0, Object arg1)
			throws EvaluationException, PropertyNotFoundException {
		return original.isReadOnly(arg0, arg1);
	}

	@Override
	public void setValue(Object arg0, int arg1, Object arg2)
			throws EvaluationException, PropertyNotFoundException {
		original.setValue(arg0, arg1, arg2);
	}

	@Override
	public void setValue(Object arg0, Object arg1, Object arg2)
			throws EvaluationException, PropertyNotFoundException {
		original.setValue(arg0, arg1, arg2);
	}

}

Posted in ,  | 1 comment

Feeling constrained.

Posted by Daz Fri, 07 Aug 2009 20:02:02 GMT

Despite Android being much more open than iPhone there is still a lot of "Sorry, you can't do this. " and "it is not possible" on the android developer group.

Posted in ,  | 3 comments