Skip to content

Commit

Permalink
Don't need inspected proxy creation. Should always know the type to u…
Browse files Browse the repository at this point in the history
…se this API.
  • Loading branch information
lincolnthree committed Sep 13, 2013
1 parent d68df81 commit e73f082
Showing 1 changed file with 0 additions and 87 deletions.
87 changes: 0 additions & 87 deletions proxy/src/main/java/org/jboss/forge/furnace/proxy/Proxies.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,93 +18,6 @@
*/
public class Proxies
{
/**
* Create a proxy for the given {@link Class} type.
*/
@SuppressWarnings("unchecked")
public static <T> T enhance(final ClassLoader loader, Object instance, ForgeProxy handler)
{
MethodFilter filter = new MethodFilter()
{
@Override
public boolean isHandled(Method method)
{
String name = method.getName();
Class<?>[] parameterTypes = method.getParameterTypes();
if (!method.getDeclaringClass().getName().contains("java.lang")
|| ("clone".equals(name) && parameterTypes.length == 0)
|| ("equals".equals(name) && parameterTypes.length == 1)
|| ("hashCode".equals(name) && parameterTypes.length == 0)
|| ("toString".equals(name) && parameterTypes.length == 0))
return true;
return false;
}
};

Object enhancedResult = null;

ProxyFactory f = new ProxyFactory()
{
@Override
protected ClassLoader getClassLoader()
{
return loader;
}
};

f.setUseCache(true);

Class<?>[] hierarchy = null;
Class<?> unwrappedInstanceType = Proxies.unwrapProxyTypes(instance.getClass(), loader);
hierarchy = ProxyTypeInspector.getCompatibleClassHierarchy(loader, unwrappedInstanceType);
if (hierarchy == null || hierarchy.length == 0)
throw new IllegalArgumentException("Must specify at least one non-final type to enhance for Object: "
+ instance + " of type " + instance.getClass());

Class<?> first = hierarchy[0];
if (!first.isInterface())
{
f.setSuperclass(Proxies.unwrapProxyTypes(first, loader));
hierarchy = Arrays.shiftLeft(hierarchy, new Class<?>[hierarchy.length - 1]);
}

int index = Arrays.indexOf(hierarchy, ProxyObject.class);
if (index >= 0)
{
hierarchy = Arrays.removeElementAtIndex(hierarchy, index);
}

if (!Proxies.isProxyType(first) && !Arrays.contains(hierarchy, ForgeProxy.class))
hierarchy = Arrays.append(hierarchy, ForgeProxy.class);

if (hierarchy.length > 0)
f.setInterfaces(hierarchy);

f.setFilter(filter);

Class<?> c = f.createClass();

try
{
enhancedResult = c.newInstance();
}
catch (InstantiationException e)
{
throw new IllegalStateException(
"Could not instantiate proxy for object [" + instance + "] of type [" + unwrappedInstanceType
+ "]. For optimal proxy compatability, ensure " +
"that this type is an interface, or a class with a default constructor.", e);
}
catch (IllegalAccessException e)
{
throw new IllegalStateException(e);
}

((ProxyObject) enhancedResult).setHandler(handler);

return (T) enhancedResult;
}

/**
* Create a proxy for the given {@link Class} type.
*/
Expand Down

0 comments on commit e73f082

Please sign in to comment.