JavaBean.java :  » Database-ORM » jaxor-3.5 » net » sourceforge » jaxor » util » Java Open Source

Java Open Source » Database ORM » jaxor 3.5 
jaxor 3.5 » net » sourceforge » jaxor » util » JavaBean.java
package net.sourceforge.jaxor.util;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/*
 * User: Mike
 * Date: Sep 2, 2003
 * Time: 10:12:30 PM
 */

public class JavaBean {

    private final Object _bean;

    public JavaBean(Object bean) {
        _bean = bean;
    }

    public JavaBean(Class clzz) {
        try {
            _bean = clzz.newInstance();
        } catch (InstantiationException e) {
            throw new SystemException(e);
        } catch (IllegalAccessException e) {
            throw new SystemException(e);
        }
    }

    public void add(String propertyName, Object value) {
        Method found = findMethod("add" + propertyName);
        try {
            found.invoke(_bean, new Object[]{value});
        } catch (IllegalAccessException e) {
            throw new SystemException(e);
        } catch (IllegalArgumentException e) {
            throw new SystemException(e);
        } catch (InvocationTargetException e) {
            throw new SystemException(e);
        }
    }

    public void set(String property, String value) {
        Method found = findMethod("set" + property);
        Converter converter = this.findConverter(found.getParameterTypes()[0]);
        try {
            found.invoke(_bean, new Object[]{converter.getValue(value)});
        } catch (IllegalAccessException e) {
            throw new SystemException(e);
        } catch (IllegalArgumentException e) {
            throw new SystemException(e);
        } catch (InvocationTargetException e) {
            throw new SystemException(e);
        }
    }

    private Method findMethod(String methodName) {
        Method[] methods = _bean.getClass().getMethods();
        for (int i = 0; i < methods.length; i++) {
            Method method = methods[i];
            String name = method.getName();
            int length = method.getParameterTypes().length;
            if (name.equalsIgnoreCase(methodName) && length == 1) {
                method.setAccessible(true);
                return method;
            }
        }
        throw new MethodNotFound(methodName, _bean);
    }

    public Object get(String propertyName) {
        String name = "get" + propertyName;
        try {
            Method m = _bean.getClass().getMethod(name, new Class[0]);
            m.setAccessible(true);
            return m.invoke(_bean, new Object[0]);
        } catch (NoSuchMethodException e) {
            throw new MethodNotFound(name, _bean);
        } catch (SecurityException e) {
            throw new SystemException(e);
        } catch (IllegalAccessException e) {
            throw new SystemException(e);
        } catch (IllegalArgumentException e) {
            throw new SystemException(e);
        } catch (InvocationTargetException e) {
            throw new SystemException(e);
        }
    }

    private Converter findConverter(Class param) {
        if (param.equals(String.class))
            return new StringMethod();
        if (param.equals(boolean.class) || param.equals(Boolean.class))
            return new BooleanMethod();
        if (param.equals(int.class))
            return new IntMethod();
        throw new SystemException("Failed to find converter method for class: " + param);
    }

    public Object getWrappedObject() {
        return _bean;
    }

    private static class IntMethod implements Converter {
        public Object getValue(String value) {
            return new Integer(value);
        }
    }


    private static class BooleanMethod implements Converter {
        public Object getValue(String value) {
            return new Boolean(value);
        }
    }

    private static class StringMethod implements Converter {
        public Object getValue(String value) {
            return value;
        }
    }

    private interface Converter {
        Object getValue(String value);
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.