草庐IT

java - 如何为 Java 类字段生成准确的泛型表达式?

coder 2024-03-20 原文

我正在尝试在运行时推理泛型。有几个很棒的库可以做到这一点(例如 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-specific implementation )比这里的 toString() 显示的更复杂。但我猜我需要使用非通配符 Type 再次调用 getExactFieldType 以获得我正在寻找的内容。

我的主要要求是我需要一个表达式,它可以成为代码生成的源文件的一部分,该文件可以成功编译——或者至少在进行最少修改的情况下编译。我愿意使用最适合这项工作的任何库。

最佳答案

要获得此类信息,您必须确定是否已将实际类型(例如 Integer)提供给泛型类型参数。如果不是,您将需要获取类型参数名称,因为它在您需要的类中已知,以及任何边界。

事实证明这很复杂。但首先,让我们回顾一下我们将在解决方案中使用的一些反射技术和方法。

首先, Field 's getGenericType() method返回 Type需要的信息。在这里,Type可以是一个简单的 Class如果提供了一个实际的类作为类型,例如Integer thing; , 或者它可以是 TypeVariable , 表示您在 Thing 中定义的通用类型参数,例如T thing; .

如果它是泛型,那么我们需要知道以下内容:

  • 这个类型最初是在哪个类中声明的。这是用 Field 's getDeclaringClass method 检索的.
  • 在每个子类中,来自声明 Field 的原始类, extends 中提供了哪些类型的参数条款。这些类型参数本身可能是实际类型,如 Integer ,或者它们可能是它们自己类的泛型类型参数。使事情复杂化的是,这些类型参数的名称可能不同,并且它们的声明顺序可能与父类(super class)中的顺序不同。 extends可以通过调用 Class 's getGenericSuperclass() method 来检索子句数据,它返回一个 Type这可以是一个简单的 Class ,例如 Object , 或者它可以是 ParameterizedType ,例如Thing<N>NumberThing<Integer> .
  • 可以使用 Class 's getTypeParameters() method 检索一个类自己的类型参数, 它返回 TypeVariable 的数组s.
  • 来自TypeVariable您可以提取名称,例如T和边界,作为 Type 的数组对象,例如Number对于 N extends Number .

对于泛型类型参数,我们需要跟踪哪些子类类型参数与原始泛型类型参数匹配,通过类层次结构向下,直到我们到达原始 Class ,其中我们报告具有任何边界的泛型类型参数,或者我们达到实际的 Class对象,我们在其中报告类。

这是一个基于您的类(class)的程序,它可以报告您想要的信息。

它必须创建一个 StackClass es,从原始类到声明该字段的类。然后弹出类,沿着类层次结构向下走。它在当前类中找到与前一类的类型参数匹配的类型参数,记下任何类型参数名称更改和当前类提供的新类型参数的新位置。例如。 T变成 N extends NumberThing出发时至 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

关于java - 如何为 Java 类字段生成准确的泛型表达式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28143029/

有关java - 如何为 Java 类字段生成准确的泛型表达式?的更多相关文章

  1. ruby - 使用 RubyZip 生成 ZIP 文件时设置压缩级别 - 2

    我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看ruby​​zip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d

  2. ruby - 如何为 emacs 安装 ruby​​-mode - 2

    我刚刚为fedora安装了emacs。我想用emacs编写ruby。为ruby​​提供代码提示、代码完成类型功能所需的工具、扩展是什么? 最佳答案 ruby-mode已经包含在Emacs23之后的版本中。不过,它也可以通过ELPA获得。您可能感兴趣的其他一些事情是集成RVM、feature-mode(Cucumber)、rspec-mode、ruby-electric、inf-ruby、rinari(用于Rails)等。这是我当前用于Ruby开发的Emacs配置:https://github.com/citizen428/emacs

  3. ruby - 在 jRuby 中使用 'fork' 生成进程的替代方案? - 2

    在MRIRuby中我可以这样做:deftransferinternal_server=self.init_serverpid=forkdointernal_server.runend#Maketheserverprocessrunindependently.Process.detach(pid)internal_client=self.init_client#Dootherstuffwithconnectingtointernal_server...internal_client.post('somedata')ensure#KillserverProcess.kill('KILL',

  4. ruby 正则表达式 - 如何替换字符串中匹配项的第 n 个实例 - 2

    在我的应用程序中,我需要能够找到所有数字子字符串,然后扫描每个子字符串,找到第一个匹配范围(例如5到15之间)的子字符串,并将该实例替换为另一个字符串“X”。我的测试字符串s="1foo100bar10gee1"我的初始模式是1个或多个数字的任何字符串,例如,re=Regexp.new(/\d+/)matches=s.scan(re)给出["1","100","10","1"]如果我想用“X”替换第N个匹配项,并且只替换第N个匹配项,我该怎么做?例如,如果我想替换第三个匹配项“10”(匹配项[2]),我不能只说s[matches[2]]="X"因为它做了两次替换“1fooX0barXg

  5. ruby-on-rails - 如何验证非模型(甚至非对象)字段 - 2

    我有一个表单,其中有很多字段取自数组(而不是模型或对象)。我如何验证这些字段的存在?solve_problem_pathdo|f|%>... 最佳答案 创建一个简单的类来包装请求参数并使用ActiveModel::Validations。#definedsomewhere,atthesimplest:require'ostruct'classSolvetrue#youcouldevencheckthesolutionwithavalidatorvalidatedoerrors.add(:base,"WRONG!!!")unlesss

  6. ruby-on-rails - form_for 中不在模型中的自定义字段 - 2

    我想向我的Controller传递一个参数,它是一个简单的复选框,但我不知道如何在模型的form_for中引入它,这是我的观点:{:id=>'go_finance'}do|f|%>Transferirde:para:Entrada:"input",:placeholder=>"Quantofoiganho?"%>Saída:"output",:placeholder=>"Quantofoigasto?"%>Nota:我想做一个额外的复选框,但我该怎么做,模型中没有一个对象,而是一个要检查的对象,以便在Controller中创建一个ifelse,如果没有检查,请帮助我,非常感谢,谢谢

  7. ruby - 如何使用 Ruby aws/s3 Gem 生成安全 URL 以从 s3 下载文件 - 2

    我正在编写一个小脚本来定位aws存储桶中的特定文件,并创建一个临时验证的url以发送给同事。(理想情况下,这将创建类似于在控制台上右键单击存储桶中的文件并复制链接地址的结果)。我研究过回形针,它似乎不符合这个标准,但我可能只是不知道它的全部功能。我尝试了以下方法:defauthenticated_url(file_name,bucket)AWS::S3::S3Object.url_for(file_name,bucket,:secure=>true,:expires=>20*60)end产生这种类型的结果:...-1.amazonaws.com/file_path/file.zip.A

  8. java - 等价于 Java 中的 Ruby Hash - 2

    我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/

  9. ruby-on-rails - 在 Rails 和 ActiveRecord 中查询时忽略某些字段 - 2

    我知道我可以指定某些字段来使用pluck查询数据库。ids=Item.where('due_at但是我想知道,是否有一种方法可以指定我想避免从数据库查询的某些字段。某种反拔?posts=Post.where(published:true).do_not_lookup(:enormous_field) 最佳答案 Model#attribute_names应该返回列/属性数组。您可以排除其中一些并传递给pluck或select方法。像这样:posts=Post.where(published:true).select(Post.attr

  10. ruby-on-rails - Ruby on Rails - 为文本区域和图片生成列 - 2

    我是Rails的新手,所以请原谅简单的问题。我正在为一家公司创建一个网站。那家公司想在网站上展示它的客户。我想让客户自己管理这个。我正在为“客户”生成一个表格,我想要的三列是:公司名称、公司描述和Logo。对于名称,我使用的是name:string但不确定如何在脚本/生成脚手架终端命令中最好地创建描述列(因为我打算将其设置为文本区域)和图片。我怀疑描述(我想成为一个文本区域)应该仍然是描述:字符串,然后以实际形式进行调整。不确定如何处理图片字段。那么……说来话长:我在脚手架命令中输入什么来生成描述和图片列? 最佳答案 对于“文本”数

随机推荐