Skip to content

Commit

Permalink
Fixed FORGE-1877: Default implementation of .equals fails with Proxy …
Browse files Browse the repository at this point in the history
…objects
  • Loading branch information
lincolnthree committed Jul 16, 2014
1 parent 17efe83 commit cf65025
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public Object invoke(final Object obj, final Method thisMethod, final Method pro
throw new ContainerException("Thread.interrupt() requested.");
}

return ClassLoaders.executeIn(delegateLoader, new Callable<Object>()
Object result = ClassLoaders.executeIn(delegateLoader, new Callable<Object>()
{
@Override
public Object call() throws Exception
Expand Down Expand Up @@ -178,6 +178,13 @@ private Method getDelegateMethod(final Method proxy) throws ClassNotFoundExcepti
return delegateMethod;
}
});

if (Thread.currentThread().isInterrupted())
{
throw new ContainerException("Thread.interrupt() requested.");
}

return result;
}

private Object enhanceResult(final Method method, Object result) throws Exception
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,20 @@
*/
public class ClassLoaderInterceptor implements ForgeProxy
{
private static final Method EQUALS_METHOD;

static
{
try
{
EQUALS_METHOD = Object.class.getMethod("equals", Object.class);
}
catch (NoSuchMethodException | SecurityException e)
{
throw new RuntimeException("Could not reflect Object.equals()", e);
}
}

private static final ThreadLocal<ClassLoader> currentLoader = new ThreadLocal<>();

private final ClassLoader loader;
Expand Down Expand Up @@ -62,6 +76,14 @@ public Object call() throws Exception
try
{
previousLoader = setCurrentLoader(loader);

if (thisMethod.equals(EQUALS_METHOD))
{
Object object = args[0];
Object unwrapped = Proxies.unwrap(object);
args[0] = unwrapped;
}

result = thisMethod.invoke(delegate, args);
}
catch (InvocationTargetException e)
Expand All @@ -78,7 +100,14 @@ public Object call() throws Exception
}
};

return ClassLoaders.executeIn(loader, task);
Object result = ClassLoaders.executeIn(loader, task);

if (Thread.currentThread().isInterrupted())
{
throw new ContainerException("Thread.interrupt() requested.");
}

return result;
}

public static ClassLoader getCurrentloader()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.Map;
import java.util.Set;

import org.jboss.forge.furnace.proxy.ClassLoaderInterceptor;
import org.jboss.forge.furnace.proxy.ForgeProxy;
import org.jboss.forge.furnace.proxy.Proxies;
import org.jboss.forge.furnace.proxy.javassist.util.proxy.MethodHandler;
Expand Down Expand Up @@ -151,6 +152,21 @@ public Object getHandler() throws Exception
Assert.assertTrue(Proxies.areEquivalent(enhancedObj, bean2));
}

@Test
public void testEqualsAndHashCode()
{
Bean bean1 = new Bean();
String attributeValue = "String";
bean1.setAtt(attributeValue);
Bean enhancedObj = Proxies.enhance(Bean.class, new ClassLoaderInterceptor(Bean.class.getClassLoader(), bean1));
enhancedObj.setAtt(attributeValue);

Bean bean2 = new Bean();
bean2.setAtt(attributeValue);

Assert.assertTrue(enhancedObj.equals(bean2));
}

@Test
public void testIsInstance()
{
Expand Down

0 comments on commit cf65025

Please sign in to comment.