簡體   English   中英

如何為Java類字段生成准確的泛型表達式?

[英]How do I generate an accurate generics expression for a Java class field?

我試圖在運行時推理泛型。 有幾個很棒的庫(例如gentyrefClassMateGuava )。 但是,它們的用法有點過頭了。

具體來說,我想提取一個與子類上下文中的特定字段匹配的表達式。

以下是使用gentyref的示例:

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");
  }

}

這是輸出:

thing class = class java.lang.Object
thing type = T

exact thing type = class java.lang.Object
exact number type = class java.lang.Object
exact integer type = class java.lang.Integer

better thing type = capture of ?
better number type = capture of ?
better integer type = class java.lang.Integer

desired thing type = T
desired number thing type = N extends Number
desired integer thing type = Integer

我知道betterThingType Type對象( gentyref特定的實現 )比toString()在這里顯示的更復雜。 但我猜我需要再次使用非通配符Type調用getExactFieldType來獲取我正在尋找的內容。

我的主要要求是我需要一個表達式,該表達式可以成為代碼生成的源文件的一部分,可以成功編譯 - 或者至少可以通過最少的修改進行編譯。 我願意使用任何最適合工作的庫。

要獲得此類信息,必須確定是否已向泛型類型參數提供實際類型(例如, Integer )。 如果沒有,您將需要獲取類型參數名稱,因為它在您需要的類中已知,以及任何邊界。

事實證明這很復雜。 但首先,讓我們回顧一下我們將在解決方案中使用的一些反射技術和方法。

首先, FieldgetGenericType()方法返回所需的Type信息。 這里,如果提供一個實際的類作為類型, Type可以是一個簡單的Class ,例如Integer thing; ,或者它可以是TypeVariable ,表示你在Thing定義的泛型類型參數,例如T thing;

如果它是泛型類型,那么我們需要知道以下內容:

  • 在最初宣布這種類型的類中。 這是使用FieldgetDeclaringClass方法檢索的。
  • 在每個子類中,從聲明Field的原始類中,在extends子句中提供了哪些類型的參數。 這些類型參數本身可能是像Integer這樣的實際類型,或者它們可能是它們自己的類的泛型類型參數。 更復雜的是,這些類型參數可能有不同的名稱,並且它們可以以與超類中不同的順序聲明。 可以通過調用ClassgetGenericSuperclass()方法來檢索extends子句數據,該方法返回一個可以是簡單ClassType ,例如Object ,或者它可以是ParameterizedType ,例如Thing<N>NumberThing<Integer>
  • 可以使用ClassgetTypeParameters()方法檢索類自己的類型參數,該方法返回TypeVariable的數組。
  • TypeVariable您可以提取名稱,例如T和邊界,作為Type對象的數組,例如Number for N extends Number

對於泛型類型參數,我們需要跟蹤哪些子類型參數與原始泛型類型參數匹配,直到類層次結構,直到我們到達原始Class ,在Class中我們報告具有任何邊界的泛型類型參數,或者我們到達一個實際的Class對象,我們在其中報告該類。

這是一個基於您的課程的程序,用於報告您所需的信息。

它必須創建一個Stack of Class es,從原始類到聲明該字段的類。 然后它會彈出類,沿着類層次結構向下移動。 它在當前類中找到與前一個類中的type參數匹配的type參數,記下當前類提供的任何類型參數名稱更改和新類型參數的新位置。 例如,當從Thing轉到NumberThing時, T變為N extends Number NumberThing 當類型參數是實際類(例如Integer ,或者如果我們已經到達原始類,循環迭代停止,在這種情況下,我們報告類型參數名稱和任何邊界,例如N extends Number

我還包括了一些額外的類, SuperclassSubclass ,其中Subclass顛倒了在Superclass中聲明的泛型類型參數的順序,以提供額外的測試。 我還包括了SpecificIntegerThing (非泛型),作為測試用例,以便迭代在IntegerThing停止,報告Integer ,然后到達堆棧中的SpecificIntegerThing

// 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() + "!");
         }
      }
   }

getFieldTypeInformation方法可以處理堆棧。

   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();
   }
}

這個輸出:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM