Skip to content

Commit

Permalink
move lambda related methods at the end of the TypeResolver class (cos…
Browse files Browse the repository at this point in the history
…metic)
  • Loading branch information
csoroiu committed Sep 29, 2016
1 parent 0defa4f commit b7a7fa6
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 100 deletions.
189 changes: 95 additions & 94 deletions src/main/java/net/jodah/typetools/TypeResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public final class TypeResolver {
private static volatile boolean CACHE_ENABLED = true;
private static boolean RESOLVES_LAMBDAS;
private static Method GET_CONSTANT_POOL;
private static Map<String, Method> OBJECT_METHODS = new HashMap<>();
private static Map<String, Method> OBJECT_METHODS = new HashMap<String, Method>();
private static final Double javaVersion;

static {
Expand Down Expand Up @@ -239,7 +239,7 @@ private static Map<TypeVariable<?>, Type> getTypeVariableMap(final Class<?> targ
Map<TypeVariable<?>, Type> map = ref != null ? ref.get() : null;

if (map == null) {
map = new HashMap<>();
map = new HashMap<TypeVariable<?>, Type>();

// Populate lambdas
if (functionalInterface != null)
Expand Down Expand Up @@ -271,103 +271,12 @@ private static Map<TypeVariable<?>, Type> getTypeVariableMap(final Class<?> targ
}

if (CACHE_ENABLED)
typeVariableCache.put(targetType, new WeakReference<>(map));
typeVariableCache.put(targetType, new WeakReference<Map<TypeVariable<?>, Type>>(map));
}

return map;
}

/**
* Populates the {@code map} with variable/argument pairs for the {@code functionalInterface}.
*/
private static void populateLambdaArgs(Class<?> functionalInterface, final Class<?> lambdaType,
Map<TypeVariable<?>, Type> map) {
if (GET_CONSTANT_POOL != null) {
// Find SAM
for (Method m : functionalInterface.getMethods()) {
if (!isDefaultMethod(m) && !Modifier.isStatic(m.getModifiers()) && !m.isBridge()) {
// Skip methods that override Object.class
Method objectMethod = OBJECT_METHODS.get(m.getName());
if (objectMethod != null && Arrays.equals(m.getTypeParameters(), objectMethod.getTypeParameters()))
continue;

// Get functional interface's type params
Type returnTypeVar = m.getGenericReturnType();
Type[] paramTypeVars = m.getGenericParameterTypes();

Method methodInfo;
try {
methodInfo = getMethodInfo((ConstantPool) GET_CONSTANT_POOL.invoke(lambdaType));
if (methodInfo == null) {
return;
}
} catch (Exception e) {
return;
}

// Populate return type argument
if (returnTypeVar instanceof TypeVariable) {
Class<?> returnType = methodInfo.getReturnType();
returnType = wrapPrimitives(returnType);
if (!returnType.equals(Void.class)) {
map.put((TypeVariable<?>) returnTypeVar, returnType);
}
}

Class<?>[] arguments = methodInfo.getParameterTypes();

// Populate object type from arbitrary object method reference
int paramOffset = 0;
if (paramTypeVars.length > 0 && paramTypeVars[0] instanceof TypeVariable
&& paramTypeVars.length == arguments.length + 1) {
Class<?> instanceType = methodInfo.getDeclaringClass();
map.put((TypeVariable<?>) paramTypeVars[0], instanceType);
paramOffset = 1;
}

// Handle additional arguments that are captured from the lambda's enclosing scope
int argOffset = 0;
if (paramTypeVars.length < arguments.length) {
argOffset = arguments.length - paramTypeVars.length;
}

// Populate type arguments
for (int i = 0; i + argOffset < arguments.length; i++) {
if (paramTypeVars[i] instanceof TypeVariable) {
map.put((TypeVariable<?>) paramTypeVars[i + paramOffset],
wrapPrimitives(arguments[i + argOffset]));
}
}

return;
}
}
}
}

private static final Map<Class<?>, Class<?>> primitives;
static {
HashMap<Class<?>, Class<?>> types = new HashMap<>();
types.put(boolean.class, Boolean.class);
types.put(byte.class, Byte.class);
types.put(char.class, Character.class);
types.put(double.class, Double.class);
types.put(float.class, Float.class);
types.put(int.class, Integer.class);
types.put(long.class, Long.class);
types.put(short.class, Short.class);
types.put(void.class, Void.class);
primitives = Collections.unmodifiableMap(types);
}

private static Class<?> wrapPrimitives(Class<?> clazz) {
return clazz.isPrimitive()? primitives.get(clazz): clazz;
}

private static boolean isDefaultMethod(Method m) {
return javaVersion >= 1.8 && m.isDefault();
}

/**
* Populates the {@code map} with with variable/argument pairs for the given {@code types}.
*/
Expand Down Expand Up @@ -447,12 +356,85 @@ public static Type resolveBound(TypeVariable<?> typeVariable) {
return bound == Object.class ? Unknown.class : bound;
}

/**
* Populates the {@code map} with variable/argument pairs for the {@code functionalInterface}.
*/
private static void populateLambdaArgs(Class<?> functionalInterface, final Class<?> lambdaType,
Map<TypeVariable<?>, Type> map) {
if (GET_CONSTANT_POOL != null) {
// Find SAM
for (Method m : functionalInterface.getMethods()) {
if (!isDefaultMethod(m) && !Modifier.isStatic(m.getModifiers()) && !m.isBridge()) {
// Skip methods that override Object.class
Method objectMethod = OBJECT_METHODS.get(m.getName());
if (objectMethod != null && Arrays.equals(m.getTypeParameters(), objectMethod.getTypeParameters()))
continue;

// Get functional interface's type params
Type returnTypeVar = m.getGenericReturnType();
Type[] paramTypeVars = m.getGenericParameterTypes();

Method methodInfo;
try {
methodInfo = getMethodInfo((ConstantPool) GET_CONSTANT_POOL.invoke(lambdaType));
if (methodInfo == null) {
return;
}
} catch (Exception ignore) {
return;
}

// Populate return type argument
if (returnTypeVar instanceof TypeVariable) {
Class<?> returnType = methodInfo.getReturnType();
returnType = wrapPrimitives(returnType);
if (!returnType.equals(Void.class)) {
map.put((TypeVariable<?>) returnTypeVar, returnType);
}
}

Class<?>[] arguments = methodInfo.getParameterTypes();

// Populate object type from arbitrary object method reference
int paramOffset = 0;
if (paramTypeVars.length > 0 && paramTypeVars[0] instanceof TypeVariable
&& paramTypeVars.length == arguments.length + 1) {
Class<?> instanceType = methodInfo.getDeclaringClass();
map.put((TypeVariable<?>) paramTypeVars[0], instanceType);
paramOffset = 1;
}

// Handle additional arguments that are captured from the lambda's enclosing scope
int argOffset = 0;
if (paramTypeVars.length < arguments.length) {
argOffset = arguments.length - paramTypeVars.length;
}

// Populate type arguments
for (int i = 0; i + argOffset < arguments.length; i++) {
if (paramTypeVars[i] instanceof TypeVariable) {
map.put((TypeVariable<?>) paramTypeVars[i + paramOffset],
wrapPrimitives(arguments[i + argOffset]));
}
}

return;
}
}
}
}

private static boolean isDefaultMethod(Method m) {
return javaVersion >= 1.8 && m.isDefault();
}

private static Method getMethodInfo(ConstantPool constantPool) {
Method returnValue = null;

for (int i = constantPool.getSize() - 1; i >= 0; i--) {
try {
Member member = constantPool.getMethodAt(i);
//skip constructors
if (!(member instanceof Method)) {
continue;
}
Expand All @@ -468,4 +450,23 @@ private static Method getMethodInfo(ConstantPool constantPool) {

return returnValue;
}

private static final Map<Class<?>, Class<?>> primitives;
static {
HashMap<Class<?>, Class<?>> types = new HashMap<Class<?>, Class<?>>();
types.put(boolean.class, Boolean.class);
types.put(byte.class, Byte.class);
types.put(char.class, Character.class);
types.put(double.class, Double.class);
types.put(float.class, Float.class);
types.put(int.class, Integer.class);
types.put(long.class, Long.class);
types.put(short.class, Short.class);
types.put(void.class, Void.class);
primitives = Collections.unmodifiableMap(types);
}

private static Class<?> wrapPrimitives(Class<?> clazz) {
return clazz.isPrimitive()? primitives.get(clazz): clazz;
}
}
12 changes: 6 additions & 6 deletions src/test/java/net/jodah/typetools/functional/LambdaTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

/**
* Tests the resolution of type arguments defined on lambda expressions.
*
*
* @author Jonathan Halterman
*/
@Test
Expand All @@ -44,15 +44,15 @@ interface ReverseFn<D, E> extends Function<E, D> {
}

interface SelectingFn<A, B, C> extends ReverseFn<A, C> {
};
}

interface StrToInt extends SelectingFn<Integer, Long, String> {
};
}

interface SerializableFn<T, V> extends Function<T, V>, Serializable {
};
}

public static interface Function3<T, U, V, R> {
public interface Function3<T, U, V, R> {
R apply(T t, U u, V v);
}

Expand Down Expand Up @@ -176,7 +176,7 @@ public void shouldResolveArgumentsFromNonSamMethodRef() throws Throwable {
/**
* Asserts that method references with primitive type arguments that are auto boxed to primitive wrappers are properly
* handled.
*
*
* Note: disabled since method signature exposed via constant pool contains convert(String, Object). Subsequent
* bytecode contains convert(String, String). May need ASM to read.
*/
Expand Down

0 comments on commit b7a7fa6

Please sign in to comment.