BriteSnow / snow

A Lightweight, Google Guice, Simple, and Powerful Web Application Framework that makes building modern Web Application a breeze! Fully open source, Apache V2 licensed.
http://britesnow.com/snow
29 stars 10 forks source link

Infinite Loop when having any Enum type in a WebHandler #1

Closed rbanikaz closed 12 years ago

rbanikaz commented 12 years ago

We ran into a problem where snow 1.9.x will go into an infinite loop when we have a Enum type in the WebHandler method. This was working in the old snow.

The infinite loop occurs in WebParamResolverRegistry.getWebParamResolverRef:

       // if still null, then, check the parent classes
       if (ref == null){
            Class parentClass = paramType.getSuperclass();
            while (ref == null && parentClass != Object.class){
                ref = refByReturnType.get(parentClass);
            }
        }

Enum types do not extend from Object.class so this was being repeated and system would hang.

Resolved the issue by creating a WebParamResolver for java.lang.Enum:

 @WebParamResolver
    public Enum resolveEnum(AnnotationMap annotationMap, Class paramType, RequestContext rc) {
        WebParam webParam = annotationMap.get(WebParam.class);
        String val = rc.getParam(webParam.value());
        return Enum.valueOf(paramType, val);
    }
jeremychone commented 12 years ago

Very cool, you are correct, this slip through the crack. We will put this fix in 1.9.9.

Note: In the Snow implementation, we will not use Enum.valueOf as it throws exception when no match, but the default Snow WebParamResolver in these type of circumstance is to return null.

rbanikaz commented 12 years ago

In xadcms we use the following utility function to return null in that case:

 public static <T extends Enum<T>> T enumValueOf(Class<T> enumClass, String name) {
        try {
            return Enum.valueOf(enumClass, name);
        } catch(Exception e) {
            return null;
        }

    }
jeremychone commented 12 years ago

ObjectUtil.getValue(value,EnumClass) does the same. But good to have your own.

jeremychone commented 12 years ago

Fix in the main trunk. Will be available in 1.9.9.

@rbanikaz note that your fix will still run, since it will override the default system parser.