如何为Java类字段生成准确的泛型表达式?
我试图在运行时对泛型进行推理。有几个很好的库可以做到这一点(例如,和)。然而,它们的用法有点让我摸不着头脑 具体来说,我想提取一个表达式,该表达式与子类上下文中的特定字段相匹配 下面是一个使用gentyref的示例:如何为Java类字段生成准确的泛型表达式?,java,generics,reflection,Java,Generics,Reflection,我试图在运行时对泛型进行推理。有几个很好的库可以做到这一点(例如,和)。然而,它们的用法有点让我摸不着头脑 具体来说,我想提取一个表达式,该表达式与子类上下文中的特定字段相匹配 下面是一个使用gentyref的示例: import com.googlecode.gentyref.GenericTypeReflector; import java.lang.reflect.Field; import java.lang.reflect.Type; public class ExtractArg
import com.googlecode.gentyref.GenericTypeReflector;
import java.lang.reflect.Field;
import java.lang.reflect.Type;
public class ExtractArguments {
public static class Thing<T> {
public T thing;
}
public static class NumberThing<N extends Number> extends Thing<N> { }
public static class IntegerThing extends NumberThing<Integer> { }
public static void main(final String... args) throws Exception {
final Field thing = Thing.class.getField("thing");
// naive type without context
Class<?> thingClass = thing.getType(); // Object
System.out.println("thing class = " + thingClass);
Type thingType = thing.getGenericType(); // T
System.out.println("thing type = " + thingType);
System.out.println();
// exact types without adding wildcard
Type exactThingType = GenericTypeReflector.getExactFieldType(thing, Thing.class);
System.out.println("exact thing type = " + exactThingType);
Type exactNumberType = GenericTypeReflector.getExactFieldType(thing, NumberThing.class);
System.out.println("exact number type = " + exactNumberType);
Type exactIntegerType = GenericTypeReflector.getExactFieldType(thing, IntegerThing.class);
System.out.println("exact integer type = " + exactIntegerType);
System.out.println();
// exact type with wildcard
final Type wildThingType = GenericTypeReflector.addWildcardParameters(Thing.class);
final Type betterThingType = GenericTypeReflector.getExactFieldType(thing, wildThingType);
System.out.println("better thing type = " + betterThingType);
final Type wildNumberType = GenericTypeReflector.addWildcardParameters(NumberThing.class);
final Type betterNumberType = GenericTypeReflector.getExactFieldType(thing, wildNumberType);
System.out.println("better number type = " + betterNumberType);
final Type wildIntegerType = GenericTypeReflector.addWildcardParameters(IntegerThing.class);
final Type betterIntegerType = GenericTypeReflector.getExactFieldType(thing, wildIntegerType);
System.out.println("better integer type = " + betterIntegerType);
System.out.println();
System.out.println("desired thing type = T");
System.out.println("desired number thing type = N extends Number");
System.out.println("desired integer thing type = Integer");
}
}
我知道更好的thingtype
类型
对象(a)比这里的toString()
显示的更复杂。但我猜我需要再次使用非通配符Type
调用getExactFieldType
,以获取我要查找的内容
我的主要要求是,我需要一个表达式,它可以成为代码生成的源文件的一部分,该源文件可以成功编译,或者至少只需很少的修改就可以编译。我愿意使用最适合此工作的任何库。要获取此类信息,必须确定是否已向泛型类型参数提供了实际类型(例如
Integer
)。如果没有,您将需要获取类型参数名,正如您所需的类中已知的那样,以及任何边界
// Helper method to print a generic type parameter and its bounds.
private static String getTypeVariableString(TypeVariable<?> typeVariable)
{
StringBuilder buf = new StringBuilder();
buf.append(typeVariable.getName());
Type[] bounds = typeVariable.getBounds();
boolean first = true;
// Don't report explicit "extends Object"
if (bounds.length == 1 && bounds[0].equals(Object.class))
{
return buf.toString();
}
for (Type bound : bounds)
{
if (first)
{
buf.append(" extends ");
first = false;
}
else
{
buf.append(" & ");
}
if (bound instanceof Class)
{
Class<?> boundClass = (Class) bound;
buf.append(boundClass.getName());
}
else if (bound instanceof TypeVariable)
{
TypeVariable<?> typeVariableBound = (TypeVariable<?>) bound;
buf.append(typeVariableBound.getName());
}
}
return buf.toString();
}
}
事实证明,这相当复杂。但首先,让我们回顾一下我们将在解决方案中使用的一些反射技术和方法
首先,返回所需的类型
信息。这里,类型
可以是简单的类
,如果实际类作为类型提供,例如整数事物
,或者它可以是类型变量
,表示您在Thing
中定义的通用类型参数,例如T Thing代码>
如果是泛型类型,则我们需要了解以下内容:
- 此类型最初在哪个类中声明。这是检索与
- 在每个子类中,从声明
字段的原始类开始,扩展
子句中提供了什么类型的参数。这些类型参数本身可能是实际类型,如Integer
,也可能是它们自己的类的泛型类型参数。更复杂的是,这些类型参数的名称可能不同,并且它们的声明顺序可能与在超类中的顺序不同。扩展
子句数据可以通过调用来检索,它返回一个类型
,该类型可以是一个简单的类
,例如对象
,也可以是参数化类型
,例如事物
或数字字符串
- 可以使用检索类自己的类型参数,这将返回
TypeVariable
s的数组
- 从
TypeVariable
中,您可以提取名称(例如T
)和边界,作为类型
对象的数组,例如N扩展数字
对于泛型类型参数,我们需要跟踪哪些子类类型参数与原始泛型类型参数匹配,直到我们到达原始的类
,在该类中我们报告具有任何边界的泛型类型参数,或者到达实际的类
对象,在报告中,我们报告了课堂情况
这是一个基于您的类的程序,它报告您所需的信息
它必须创建类
的堆栈
,从原始类一直到声明字段的类。然后它弹出类,沿着类层次结构走。它在当前类中查找与上一个类中的类型参数匹配的类型参数,并记录所有类型参数名称的更改以及当前类提供的新类型参数的新位置。例如,T
从Thing
到NumberThing
时变为N扩展数字。当类型参数是实际类时,循环迭代停止,例如Integer
,或者如果我们已经到达原始类,在这种情况下,我们报告类型参数名称和任何边界,例如N extends Number
我还包括了两个附加类,超类
和子类
,其中子类
反转了在超类
中声明的泛型类型参数的顺序,以提供附加测试。我还包括了SpecificIntegerThing
(非泛型)作为测试用例,以便迭代在IntegerThing
停止,在到达堆栈中的SpecificIntegerThing
之前报告Integer>
// Just to have some bounds to report.
import java.io.Serializable;
import java.util.RandomAccess;
// Needed for the implementation.
import java.lang.reflect.*;
import java.util.Arrays;
import java.util.Stack;
public class ExtractArguments {
public static class Thing<T> {
public T thing;
}
public static class NumberThing<N extends Number> extends Thing<N> {}
public static class IntegerThing extends NumberThing<Integer> {}
public static class SpecificIntegerThing extends IntegerThing {}
public static class Superclass<A extends Serializable, B> {
public A thing;
}
// A and B are reversed in the extends clause!
public static class Subclass<A, B extends RandomAccess & Serializable>
extends Superclass<B, A> {}
public static void main(String[] args)
{
for (Class<?> clazz : Arrays.asList(
Thing.class, NumberThing.class,
IntegerThing.class, SpecificIntegerThing.class,
Superclass.class, Subclass.class))
{
try
{
Field field = clazz.getField("thing");
System.out.println("Field " + field.getName() + " of class " + clazz.getName() + " is: " +
getFieldTypeInformation(clazz, field));
}
catch (NoSuchFieldException e)
{
System.out.println("Field \"thing\" is not found in class " + clazz.getName() + "!");
}
}
}
private static String getFieldTypeInformation(Class<?> clazz, Field field)
{
Type genericType = field.getGenericType();
// Declared as actual type name...
if (genericType instanceof Class)
{
Class<?> genericTypeClass = (Class<?>) genericType;
return genericTypeClass.getName();
}
// .. or as a generic type?
else if (genericType instanceof TypeVariable)
{
TypeVariable<?> typeVariable = (TypeVariable<?>) genericType;
Class<?> declaringClass = field.getDeclaringClass();
//System.out.println(declaringClass.getName() + "." + typeVariable.getName());
// Create a Stack of classes going from clazz up to, but not including, the declaring class.
Stack<Class<?>> stack = new Stack<Class<?>>();
Class<?> currClass = clazz;
while (!currClass.equals(declaringClass))
{
stack.push(currClass);
currClass = currClass.getSuperclass();
}
// Get the original type parameter from the declaring class.
int typeVariableIndex = -1;
String typeVariableName = typeVariable.getName();
TypeVariable<?>[] currTypeParameters = currClass.getTypeParameters();
for (int i = 0; i < currTypeParameters.length; i++)
{
TypeVariable<?> currTypeVariable = currTypeParameters[i];
if (currTypeVariable.getName().equals(typeVariableName))
{
typeVariableIndex = i;
break;
}
}
if (typeVariableIndex == -1)
{
throw new RuntimeException("Expected Type variable \"" + typeVariable.getName() +
"\" in class " + clazz + "; but it was not found.");
}
// If the type parameter is from the same class, don't bother walking down
// a non-existent hierarchy.
if (declaringClass.equals(clazz))
{
return getTypeVariableString(typeVariable);
}
// Pop them in order, keeping track of which index is the type variable.
while (!stack.isEmpty())
{
currClass = stack.pop();
// Must be ParameterizedType, not Class, because type arguments must be
// supplied to the generic superclass.
ParameterizedType superclassParameterizedType = (ParameterizedType) currClass.getGenericSuperclass();
Type currType = superclassParameterizedType.getActualTypeArguments()[typeVariableIndex];
if (currType instanceof Class)
{
// Type argument is an actual Class, e.g. "extends ArrayList<Integer>".
currClass = (Class) currType;
return currClass.getName();
}
else if (currType instanceof TypeVariable)
{
TypeVariable<?> currTypeVariable = (TypeVariable<?>) currType;
typeVariableName = currTypeVariable.getName();
// Reached passed-in class (bottom of hierarchy)? Report it.
if (currClass.equals(clazz))
{
return getTypeVariableString(currTypeVariable);
}
// Not at bottom? Find the type parameter to set up for next loop.
else
{
typeVariableIndex = -1;
currTypeParameters = currClass.getTypeParameters();
for (int i = 0; i < currTypeParameters.length; i++)
{
currTypeVariable = currTypeParameters[i];
if (currTypeVariable.getName().equals(typeVariableName))
{
typeVariableIndex = i;
break;
}
}
if (typeVariableIndex == -1)
{
// Shouldn't get here.
throw new RuntimeException("Expected Type variable \"" + typeVariable.getName() +
"\" in class " + currClass.getName() + "; but it was not found.");
}
}
}
}
}
// Shouldn't get here.
throw new RuntimeException("Missed the original class somehow!");
}
getTypeVariableString
方法有助于生成类型参数名称和任何边界
// Helper method to print a generic type parameter and its bounds.
private static String getTypeVariableString(TypeVariable<?> typeVariable)
{
StringBuilder buf = new StringBuilder();
buf.append(typeVariable.getName());
Type[] bounds = typeVariable.getBounds();
boolean first = true;
// Don't report explicit "extends Object"
if (bounds.length == 1 && bounds[0].equals(Object.class))
{
return buf.toString();
}
for (Type bound : bounds)
{
if (first)
{
buf.append(" extends ");
first = false;
}
else
{
buf.append(" & ");
}
if (bound instanceof Class)
{
Class<?> boundClass = (Class) bound;
buf.append(boundClass.getName());
}
else if (bound instanceof TypeVariable)
{
TypeVariable<?> typeVariableBound = (TypeVariable<?>) bound;
buf.append(typeVariableBound.getName());
}
}
return buf.toString();
}
}
我没有一个完整的答案,但我确实使用反射获得了给定示例所需的输出,请参见(runnable示例)。代码可能会提供一些正确答案方向的见解。谢谢,我非常感谢您花时间深入了解泛型API的一些细节。在接下来的几天里,我将详细查看您的答案,稍后在GitHub上发布最终解决方案的位置,以方便其他感兴趣的人。干杯再次感谢您的实施。我开始针对它编写测试,发现有一种情况它无法处理,那就是嵌套泛型。我开始尝试扩展代码以支持它们,但它感觉像是车轮的改造,所以我有一个问题,那就是库是否已经支持这种东西了。我将在这里继续了解更多信息。
Field thing of class ExtractArguments$Thing is: T
Field thing of class ExtractArguments$NumberThing is: N extends java.lang.Number
Field thing of class ExtractArguments$IntegerThing is: java.lang.Integer
Field thing of class ExtractArguments$SpecificIntegerThing is: java.lang.Integer
Field thing of class ExtractArguments$Superclass is: A extends java.io.Serializable
Field thing of class ExtractArguments$Subclass is: B extends java.util.RandomAccess & java.io.Serializable