Skip to content

Commit

Permalink
fix: path was not recorded when two different PathRecorders from diff…
Browse files Browse the repository at this point in the history
…erent threads were used
  • Loading branch information
davherrmann committed May 29, 2016
1 parent f7c7bbd commit 460676b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/main/java/de/davherrmann/immutable/PathRecorder.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ protected Map<Class<?>, PathRecorder<?>> initialValue()

private final I path;

private ThreadLocal<PathInfo> lastPathInfo = new ThreadLocal<>();
private static ThreadLocal<PathInfo> lastPathInfo = new ThreadLocal<>();

private PathRecorder(Class<I> type)
{
Expand All @@ -50,11 +50,11 @@ public Method methodFor(Supplier<?> method)

private PathInfo pathInfoFor(Supplier<?> method)
{
this.lastPathInfo.set(null);
lastPathInfo.set(null);

method.get();

final PathInfo pathInfo = this.lastPathInfo.get();
final PathInfo pathInfo = lastPathInfo.get();

if (pathInfo == null)
{
Expand All @@ -64,7 +64,7 @@ private PathInfo pathInfoFor(Supplier<?> method)
}

@SuppressWarnings("unchecked")
private <T> T pathFor(Class<T> type, List<String> nestedPath)
private static <T> T pathFor(Class<T> type, List<String> nestedPath)
{
return (T) Proxy.newProxyInstance( //
type.getClassLoader(), //
Expand All @@ -85,7 +85,7 @@ public static <T> PathRecorder<T> pathRecorderInstanceFor(Class<T> type)
return (PathRecorder<T>) pathRecorders.get().get(type);
}

private class PathInvocationHandler extends AbstractPathInvocationHandler
private static class PathInvocationHandler extends AbstractPathInvocationHandler
{
public PathInvocationHandler(final List<String> nestedPath)
{
Expand Down
13 changes: 13 additions & 0 deletions src/test/java/de/davherrmann/immutable/PathRecorderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,19 @@ public void methodFor_returnsMethod_forPassedSupplier() throws Exception
assertThat(pathRecorder.methodFor(path.pojo()::integer), is(method));
}

@Test
public void pathFor_returnsPath_whenPathWasCreatedWithPathRecorderFromDifferentThread() throws Exception
{
// given
final POJO[] path = new POJO[1];
final Thread thread = new Thread(() -> path[0] = pathRecorderInstanceFor(POJO.class).path());
thread.start();
thread.join();

// when /then
assertThat(pathRecorder.pathFor(path[0].pojo()::integer), is(newArrayList("pojo", "integer")));
}

private interface POJO
{
POJO pojo();
Expand Down

0 comments on commit 460676b

Please sign in to comment.