|
|
@@ -7,9 +7,7 @@
|
|
|
|
|
|
package net.ranides.assira.junit;
|
|
|
|
|
|
-import java.lang.reflect.Array;
|
|
|
import java.lang.reflect.Field;
|
|
|
-import java.lang.reflect.Member;
|
|
|
import java.lang.reflect.Method;
|
|
|
import java.lang.reflect.ParameterizedType;
|
|
|
import java.lang.reflect.Type;
|
|
|
@@ -19,7 +17,6 @@ import java.util.function.Function;
|
|
|
import java.util.function.Supplier;
|
|
|
import java.util.regex.Pattern;
|
|
|
import javax.annotation.Resource;
|
|
|
-import sun.reflect.ConstantPool;
|
|
|
|
|
|
/**
|
|
|
*
|
|
|
@@ -55,58 +52,6 @@ final class TestUtils {
|
|
|
return cparam;
|
|
|
}
|
|
|
|
|
|
- public static Class<?> typeof(Function<?,?> function) {
|
|
|
- Class<?> clazz = function.getClass();
|
|
|
- if(!clazz.isSynthetic()) {
|
|
|
- return typeof_generic(clazz);
|
|
|
- } else {
|
|
|
- return typeof_lambda(clazz);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private static Class<?> typeof_generic(Class<?> clazz) {
|
|
|
- ParameterizedType it = getGenericInterface(clazz, Function.class);
|
|
|
- java.lang.reflect.Type pt = it.getActualTypeArguments()[1];
|
|
|
- if(pt instanceof Class<?>) {
|
|
|
- return (Class<?>)pt;
|
|
|
- }
|
|
|
- if(pt instanceof ParameterizedType) {
|
|
|
- return (Class<?>)(((ParameterizedType)pt).getRawType());
|
|
|
- }
|
|
|
- throw new AssertionError("Deduction of return type failed. Unsupported function: " + clazz);
|
|
|
- }
|
|
|
-
|
|
|
- private static ParameterizedType getGenericInterface(Class<?> type, Class<?> intface) {
|
|
|
- for(java.lang.reflect.Type t : type.getGenericInterfaces()) {
|
|
|
- ParameterizedType pt = (ParameterizedType)t;
|
|
|
- if( intface.equals(pt.getRawType()) ) {
|
|
|
- return pt;
|
|
|
- }
|
|
|
- }
|
|
|
- throw new AssertionError(type.getName() + " does not implement " + intface.getName());
|
|
|
- }
|
|
|
-
|
|
|
- private static Class<?> typeof_lambda(Class<?> clazz) {
|
|
|
- // dość generyczne filtrowanie, które chyba eliminuje różne cuda
|
|
|
- // związane z wewnętrzną implementacją lambdy. uwaga! to działa TYLKO
|
|
|
- // dla lambdy.
|
|
|
-
|
|
|
- ConstantPool cp = Li.get(clazz);
|
|
|
-
|
|
|
- for(int i=0, n=cp.getSize(); i<n; i++) {
|
|
|
- try {
|
|
|
- Member member = cp.getMethodAt(i);
|
|
|
- if(member instanceof Method) {
|
|
|
- Method method = (Method)member;
|
|
|
- return method.getReturnType();
|
|
|
- }
|
|
|
- } catch(IllegalArgumentException $0) {
|
|
|
- continue;
|
|
|
- }
|
|
|
- }
|
|
|
- throw new AssertionError("Deduction of return type failed. Unsupported lambda: " + clazz);
|
|
|
- }
|
|
|
-
|
|
|
private static Class<?> typeof_supplier(Method method) throws AssertionError {
|
|
|
ParameterizedType gparam = (ParameterizedType)method.getGenericParameterTypes()[0];
|
|
|
Type tparam = gparam.getActualTypeArguments()[0];
|
|
|
@@ -285,26 +230,4 @@ final class TestUtils {
|
|
|
|
|
|
}
|
|
|
|
|
|
- private static final class Li { // NOPMD - lazy init idiom
|
|
|
-
|
|
|
- public static final Method GET_CONSTANT_POOL;
|
|
|
-
|
|
|
- static {
|
|
|
- try {
|
|
|
- GET_CONSTANT_POOL = Class.class.getDeclaredMethod("getConstantPool");
|
|
|
- GET_CONSTANT_POOL.setAccessible(true);
|
|
|
- } catch(ReflectiveOperationException cause) {
|
|
|
- throw rethrow(cause);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public static ConstantPool get(Class<?> type) {
|
|
|
- try {
|
|
|
- return (ConstantPool) GET_CONSTANT_POOL.invoke(type);
|
|
|
- } catch (ReflectiveOperationException cause) {
|
|
|
- throw rethrow(cause);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
}
|