Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support of getting and setting properties customly in PropertyResolver #74

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
Expand Down Expand Up @@ -80,6 +81,8 @@ public final class PropertyResolver
private static final String GET = "get";
private static final String IS = "is";
private static final String SET = "set";

private final static ConcurrentHashMap<Object, ICustomPropertyResolver> applicationToCustomPropertyResolver = Generics.newConcurrentHashMap(2);

/**
* Looks up the value from the object with the given expression. If the expression, the object
Expand Down Expand Up @@ -410,6 +413,7 @@ else if (exp.endsWith("()"))
}
if (method == null)
{
Class<? extends IGetAndSet> getAndSetClass;
if (List.class.isAssignableFrom(clz))
{
try
Expand Down Expand Up @@ -445,9 +449,20 @@ else if (exp.endsWith("()"))
}
}
}
/* Can be uncommented for better performance
else if (Map.class.isAssignableFrom(clz))
{
getAndSetter = new MapGetSet(exp);
}*/
else if ((getAndSetClass=getCustomPropertyResolver().resolve(clz))!=null)
{
try
{
getAndSetter = getAndSetClass.getConstructor(String.class).newInstance(exp);
} catch (Exception e)
{
throw new WicketRuntimeException("Can't use custom IGetAndSet '"+getAndSetClass.getName()+"'.", e);
}
}
else if (clz.isArray())
{
Expand Down Expand Up @@ -739,7 +754,7 @@ public void setValue(final Object object, final Object value,
public Method getSetter();
}

private static abstract class AbstractGetAndSet implements IGetAndSet
public static abstract class AbstractGetAndSet implements IGetAndSet
{
/**
* {@inheritDoc}
Expand Down Expand Up @@ -778,11 +793,11 @@ public Class<?> getTargetClass()
}
}

private static final class MapGetSet extends AbstractGetAndSet
public static final class MapGetSet extends AbstractGetAndSet
{
private final String key;

MapGetSet(String key)
public MapGetSet(String key)
{
this.key = key;
}
Expand Down Expand Up @@ -819,7 +834,7 @@ public Object newValue(final Object object)
}
}

private static final class ListGetSet extends AbstractGetAndSet
public static final class ListGetSet extends AbstractGetAndSet
{
final private int index;

Expand Down Expand Up @@ -1468,7 +1483,35 @@ private static IClassCache getClassesToGetAndSetters()
}
return result;
}


public static ICustomPropertyResolver getCustomPropertyResolver()
{
Object key;
if (Application.exists())
{
key = Application.get();
}
else
{
key = PropertyResolver.class;
}
ICustomPropertyResolver result = applicationToCustomPropertyResolver.get(key);
if (result == null)
{
ICustomPropertyResolver tmpResult = applicationToCustomPropertyResolver.putIfAbsent(key, result = new DefaultCustomPropertyResolver());
if (tmpResult != null)
{
result = tmpResult;
}
}
return result;
}

public static void registerCustomPropertyGetAndSet(Class<?> clz, Class<? extends IGetAndSet> getAndSetClass)
{
getCustomPropertyResolver().register(clz, getAndSetClass);
}

/**
* Clean up cache for this app.
*
Expand All @@ -1477,6 +1520,7 @@ private static IClassCache getClassesToGetAndSetters()
public static void destroy(Application application)
{
applicationToClassesToGetAndSetters.remove(application);
applicationToCustomPropertyResolver.remove(application);
}

/**
Expand Down Expand Up @@ -1550,4 +1594,50 @@ public void put(Class<?> clz, Map<String, IGetAndSet> values)
map.put(clz, values);
}
}

public static interface ICustomPropertyResolver
{
public void register(Class<?> clz, Class<? extends IGetAndSet> getAndSetClass);
public Class<? extends IGetAndSet> resolve(Class<?> clz);
}

private static class DefaultCustomPropertyResolver implements ICustomPropertyResolver
{
private final ConcurrentHashMap<Class<?>, Class<? extends IGetAndSet>> map = Generics.newConcurrentHashMap(16);
private final ConcurrentHashMap<Class<?>, Class<? extends IGetAndSet>> localCacheMap = Generics.newConcurrentHashMap(16);

public DefaultCustomPropertyResolver()
{
register(Map.class, MapGetSet.class);
}

@Override
public void register(Class<?> clz, Class<? extends IGetAndSet> getAndSetClass) {
map.put(clz, getAndSetClass);
localCacheMap.clear();
}

@Override
public Class<? extends IGetAndSet> resolve(Class<?> clz) {
Class<? extends IGetAndSet> ret = map.get(clz);
if(ret==null)
{
ret = localCacheMap.get(clz);
if(ret==null)
{
for (Map.Entry<Class<?>, Class<? extends IGetAndSet>> entry : map.entrySet())
{
if(entry.getKey().isAssignableFrom(clz))
{
ret = entry.getValue();
localCacheMap.put(clz, ret);
break;
}
}
}
}
return ret;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.wicket.util.lang;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
Expand Down Expand Up @@ -50,6 +51,54 @@ public class PropertyResolverTest extends WicketTestCase
new ConverterLocator(), Locale.US);

private Person person;

public static class CustomDocument implements Serializable
{
private Map<String, Object> map = new HashMap<String, Object>();

public Object field(String field)
{
return map.get(field);
}

public void field(String field, Object value)
{
map.put(field, value);
}
}

public static class CustomGetAndSet extends PropertyResolver.AbstractGetAndSet
{
private final String field;
public CustomGetAndSet(String field)
{
this.field = field;
}

@Override
public Object getValue(Object object) {
return checkObject(object).field(field);
}

@Override
public Object newValue(Object object) {
return null;
}

@Override
public void setValue(Object object, Object value,
PropertyResolverConverter converter) {
checkObject(object).field(field, value);

}

private CustomDocument checkObject(Object object)
{
if(object instanceof CustomDocument) return (CustomDocument) object;
throw new WicketRuntimeException("This GetAndSet doesn't support "+object.getClass().getName());
}

}

/**
* @throws Exception
Expand Down Expand Up @@ -739,4 +788,14 @@ protected Class<Long> getTargetType()
Object actual = converter.convert(date, Long.class);
assertEquals(date.getTime(), actual);
}

@Test
public void customGetAndSetSupport()
{
PropertyResolver.registerCustomPropertyGetAndSet(CustomDocument.class, CustomGetAndSet.class);
CustomDocument doc = new CustomDocument();
PropertyResolver.setValue("field1", doc, "field1Value", CONVERTER);
assertEquals("field1Value", doc.field("field1"));
assertEquals("field1Value", PropertyResolver.getValue("field1", doc));
}
}