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

Fix bugs when working with JRebel #51

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
34 changes: 29 additions & 5 deletions src/main/java/net/jodah/typetools/TypeResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,8 @@
import java.lang.reflect.WildcardType;
import java.security.AccessController;
import java.security.PrivilegedExceptionAction;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.WeakHashMap;
import java.util.*;
import java.util.function.Predicate;

import sun.misc.Unsafe;

Expand All @@ -57,6 +54,7 @@ public final class TypeResolver {
private static final Map<String, Method> OBJECT_METHODS = new HashMap<String, Method>();
private static final Map<Class<?>, Class<?>> PRIMITIVE_WRAPPERS;
private static final Double JAVA_VERSION;
private static final List<Predicate<Member>> lambdaMemberFilters = new ArrayList<>();

static {
JAVA_VERSION = Double.parseDouble(System.getProperty("java.specification.version", "0"));
Expand Down Expand Up @@ -373,6 +371,17 @@ public static Class<?> resolveRawClass(Type genericType, Class<?> subType) {
return resolveRawClass(genericType, subType, null);
}

/**
* Using some low-level instruction technology may add some members into the constant pool. The generated members
* could make {@link TypeResolver} to return a wrong type. To avoid this, use {@code addLambdaMemberFilter} to
* add a custom lambda member filter to skip wrong members.
*
* @param lambdaMemberFilter to add
*/
public static synchronized void addLambdaMemberFilter(Predicate<Member> lambdaMemberFilter) {
lambdaMemberFilters.add(lambdaMemberFilter);
}

private static Class<?> resolveRawClass(Type genericType, Class<?> subType, Class<?> functionalInterface) {
if (genericType instanceof Class) {
return (Class<?>) genericType;
Expand Down Expand Up @@ -702,6 +711,9 @@ private static Member getMemberRef(Class<?> type) {
&& member.getDeclaringClass().getName().equals("java.lang.invoke.SerializedLambda"))
|| member.getDeclaringClass().isAssignableFrom(type))
continue;
if (testAllLambdaMemberFilters(member)) {
continue;
}

result = member;

Expand All @@ -713,6 +725,18 @@ private static Member getMemberRef(Class<?> type) {
return result;
}

/**
* test member by custom lambda member filters, using OR operator.
*/
private static boolean testAllLambdaMemberFilters(Member member) {
for (Predicate<Member> lambdaMemberFilter : lambdaMemberFilters) {
if (lambdaMemberFilter.test(member)) {
return true;
}
}
return false;
}

private static boolean isAutoBoxingMethod(Method method) {
Class<?>[] parameters = method.getParameterTypes();
return method.getName().equals("valueOf") && parameters.length == 1 && parameters[0].isPrimitive()
Expand Down