package org.osgl.util;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * Create {@link PropertyGetter} and {@link PropertySetter} based on Java reflection
 */
public class ReflectionPropertyHandlerFactory implements PropertyHandlerFactory {
    @Override
    public PropertySetter createPropertySetter(Class c, String propName) {
        PropertySetter propertySetter;
        String p = S.capFirst(propName);
        String setter = "set" + p;
        String isser = "is" + p;
        Method[] ma = c.getMethods();
        for (Method m : ma) {
            String mn = m.getName();
            if (S.neq(setter, mn) && S.neq(isser, mn) && S.neq(p, mn)) {
                continue;
            }
            Class[] ca = m.getParameterTypes();
            if (ca != null && ca.length == 1) {
                return new ReflectionPropertySetter(c, m, null);
            }
        }
        try {
            Field f = c.getDeclaredField(propName);
            f.setAccessible(true);
            return new ReflectionPropertySetter(c, null, f);
        } catch (NoSuchFieldException e) {
            throw E.unexpected("Cannot find access method to field %s on class %s", propName, c);
        }
    }

    @Override
    public PropertyGetter createPropertyGetter(Class c, String propName) {
        PropertyGetter propertyGetter;
        String p = S.capFirst(propName);
        String getter = "get" + p;
        try {
            Method m = c.getMethod(getter);
            propertyGetter = new ReflectionPropertyGetter(c, m, null);
        } catch (NoSuchMethodException e) {
            String isser = "is" + p;
            try {
                Method m = c.getMethod(isser);
                propertyGetter = new ReflectionPropertyGetter(c, m, null);
            } catch (NoSuchMethodException e1) {
                try {
                    // try jquery style getter
                    Method m = c.getMethod(propName);
                    propertyGetter = new ReflectionPropertyGetter(c, m, null);
                } catch (NoSuchMethodException e2) {
                    try {
                        Field f = c.getDeclaredField(propName);
                        f.setAccessible(true);
                        propertyGetter = new ReflectionPropertyGetter(c, null, f);
                    } catch (NoSuchFieldException e3) {
                        throw E.unexpected("Cannot find access method to field %s on class %s", propName, c);
                    }
                }
            }
        }
        return propertyGetter;
    }
}

@@ -835,35 +835,16 @@
 turn new
- ReflectionProperty
 Setter(c
@@ -847,32 +847,32 @@
 er(c, m, null);%0A
+
             %7D%0A  
@@ -1002,35 +1002,16 @@
 turn new
- ReflectionProperty
 Setter(c
@@ -1474,35 +1474,16 @@
 er = new
- ReflectionProperty
 Getter(c
@@ -1676,35 +1676,16 @@
 er = new
- ReflectionProperty
 Getter(c
@@ -1908,35 +1908,16 @@
 er = new
- ReflectionProperty
 Getter(c
@@ -2162,35 +2162,16 @@
 er = new
- ReflectionProperty
 Getter(c
@@ -2404,32 +2404,32 @@
     %7D%0A        %7D%0A
-
         return p
@@ -2448,11 +2448,271 @@
 r;%0A    %7D
+%0A%0A    protected PropertyGetter newGetter(Class c, Method m, Field f) %7B%0A        return new ReflectionPropertyGetter(c, m, f);%0A    %7D%0A%0A    protected PropertySetter newSetter(Class c, Method m, Field f) %7B%0A        return new ReflectionPropertySetter(c, m, f);%0A    %7D
 %0A%7D%0A

make ReflectionPropertyHandlerFactory be extensible