package org.osgl.util;

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

/**
 * Create {@link PropertyGetter} and {@link PropertySetter} based on Java reflection
 */
public class ReflectionPropertyHandlerFactory implements PropertyHandlerFactory {
    @Override
    public PropertySetter createPropertySetter(Class c, String propName) {
        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 newSetter(c, m, null);
            }
        }
        try {
            Field f = c.getDeclaredField(propName);
            f.setAccessible(true);
            return newSetter(c, null, f);
        } catch (NoSuchFieldException e) {
            throw E.unexpected(e, "Cannot find access method to field %s on class %s", propName, c);
        }
    }

    @Override
    public PropertyGetter createPropertyGetter(Class c, String propName, boolean requireField) {
        PropertyGetter propertyGetter;
        if (requireField) {
            try {
                return getterViaField(c, propName);
            } catch (Exception e) {
                // ignore: try the java bean getter later on
            }
        }
        String p = S.capFirst(propName);
        String getter = "get" + p;
        try {
            Method m = c.getMethod(getter);
            propertyGetter = newGetter(c, m, null);
        } catch (NoSuchMethodException e) {
            String isser = "is" + p;
            try {
                Method m = c.getMethod(isser);
                propertyGetter = newGetter(c, m, null);
            } catch (NoSuchMethodException e1) {
                try {
                    // try jquery style getter
                    Method m = c.getMethod(propName);
                    propertyGetter = newGetter(c, m, null);
                } catch (NoSuchMethodException e2) {
                    propertyGetter = getterViaField(c, propName);
                }
            }
        }
        return propertyGetter;
    }

    @Override
    public MapPropertyGetter createMapPropertyGetter(Class keyType, Class valType) {
        return new MapPropertyGetter(keyType, valType);
    }

    @Override
    public MapPropertySetter createMapPropertySetter(Class keyType, Class valType) {
        return new MapPropertySetter(keyType, valType);
    }

    @Override
    public ListPropertyGetter createListPropertyGetter(Class itemType) {
        return new ListPropertyGetter(itemType);
    }

    @Override
    public ListPropertySetter createListPropertySetter(Class itemType) {
        return new ListPropertySetter(itemType);
    }

    private PropertyGetter getterViaField(Class entityClass, String propName) {
        while (!Object.class.equals(entityClass)) {
            try {
                Field f = entityClass.getDeclaredField(propName);
                f.setAccessible(true);
                return newGetter(entityClass, null, f);
            } catch (NoSuchFieldException e3) {
                entityClass = entityClass.getSuperclass();
                throw E.unexpected(e3, "Cannot find access method to field %s on class %s", propName, entityClass);
            }
        }
        throw E.unexpected("entity class is Object.class");
    }

    protected PropertyGetter newGetter(Class c, Method m, Field f) {
        return new ReflectionPropertyGetter(c, m, f, this);
    }

    protected PropertySetter newSetter(Class c, Method m, Field f) {
        return new ReflectionPropertySetter(c, m, f);
    }


}

@@ -866,113 +866,8 @@
   %7D%0A
-        try %7B%0A            Field f = c.getDeclaredField(propName);%0A            f.setAccessible(true);%0A    
     
@@ -881,184 +881,36 @@
 urn 
-newS
+s
 etter
-(c, null, f);%0A        %7D catch (NoSuchFieldException e) %7B%0A            throw E.unexpected(e, %22Cannot find access method to field %25s on class %25s%22, propName, c);%0A        %7D
+ViaField(c, propName);
 %0A   
@@ -2707,24 +2707,571 @@
 pe);%0A    %7D%0A%0A
+    private PropertySetter setterViaField(Class entityClass, String propName) %7B%0A        while (!Object.class.equals(entityClass)) %7B%0A            try %7B%0A                Field f = entityClass.getDeclaredField(propName);%0A                f.setAccessible(true);%0A                return newSetter(entityClass, null, f);%0A            %7D catch (NoSuchFieldException e3) %7B%0A                entityClass = entityClass.getSuperclass();%0A            %7D%0A        %7D%0A        throw E.unexpected(%22Cannot find access method to field %25s on %25s%22, propName, entityClass);%0A    %7D%0A%0A
     private 
@@ -3686,22 +3686,38 @@
         
+  %7D%0A        %7D%0A
       
+  
 throw E.
@@ -3731,12 +3731,8 @@
 ted(
-e3, 
 %22Can
@@ -3769,22 +3769,16 @@
 d %25s on 
-class 
 %25s%22, pro
@@ -3802,92 +3802,8 @@
 s);%0A
-            %7D%0A        %7D%0A        throw E.unexpected(%22entity class is Object.class%22);%0A
     

fix an issue in property get