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

Lambda: Handle context final variables that are passed as argument #17

Merged
merged 1 commit into from
Jan 14, 2016
Merged
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
22 changes: 15 additions & 7 deletions src/main/java/net/jodah/typetools/TypeResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ private static void populateLambdaArgs(Class<?> functionalInterface, final Class
// Get functional interface's type params
Type returnTypeVar = m.getGenericReturnType();
Type[] paramTypeVars = m.getGenericParameterTypes();

// Get lambda's type arguments
ConstantPool constantPool = (ConstantPool) GET_CONSTANT_POOL.invoke(lambdaType);
String[] methodRefInfo = constantPool
Expand All @@ -330,19 +330,27 @@ private static void populateLambdaArgs(Class<?> functionalInterface, final Class
}

TypeDescriptor[] arguments = TypeDescriptor.getArgumentTypes(methodRefInfo[2]);

// Handle arbitrary object instance method references
int offset = 0;
int paramOffset = 0;
if (paramTypeVars[0] instanceof TypeVariable && paramTypeVars.length == arguments.length + 1) {
Class<?> instanceType = TypeDescriptor.getObjectType(methodRefInfo[0])
.getType(lambdaType.getClassLoader());
map.put((TypeVariable<?>) paramTypeVars[0], instanceType);
offset = 1;
paramOffset = 1;
}

for (int i = 0; i < arguments.length; i++)
if (paramTypeVars[i + offset] instanceof TypeVariable)
map.put((TypeVariable<?>) paramTypeVars[i + offset], arguments[i].getType(lambdaType.getClassLoader()));
// Handle local final variables from context that are passed as arguments.
int argOffset = 0;
if(paramTypeVars.length < arguments.length ) {
argOffset = arguments.length - paramTypeVars.length;
}

for(int i = 0; i + argOffset < arguments.length; i++) {
if (paramTypeVars[i] instanceof TypeVariable) {
map.put((TypeVariable<?>) paramTypeVars[i + paramOffset], arguments[i + argOffset].getType(lambdaType.getClassLoader()));
}
}
break;
}
}
Expand Down
13 changes: 13 additions & 0 deletions src/test/java/net/jodah/typetools/functional/LambdaTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import java.util.Comparator;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.BiPredicate;
Expand Down Expand Up @@ -103,6 +104,18 @@ public void shouldResolveArguments() {
assertEquals(TypeResolver.resolveRawArgument(Consumer.class, consumer.getClass()), String.class);
}

public void shouldResolveArgumentsCorrectly() {

final AtomicLong a = new AtomicLong(0);
Function<String, Long> func = (s) -> {
a.incrementAndGet();
return (long)s.hashCode();
};

assertEquals(new Class<?>[] {String.class, Long.class}, TypeResolver.resolveRawArguments(Function.class, func.getClass()));

}

/**
* Asserts that arguments can be resolved from method references for simple functional interfaces.
*/
Expand Down