TYPE_USE 通用类型的注释未被编译到 class 文件中

TYPE_USE annotations on generic type not being compiled into class file

我想通过我的 spring 启动应用程序为具有多个 @Valid 元素的负载定义一个通用的 class @Validated。 class 正在进入图书馆。

如果我定义一个通用的 class 看起来像:

class GroupPayload<T> {
    @Size(min = 1) List<@Valid T> data;
}

编译后,class 文件如下所示:

class GroupPayload<T> {
    @Size(
        min = 1
    )
    private List<T> data; // T is not @Valid !?
}

即使 @Size 进入了 class 文件,@Valid 注释(两个注释都有 @Retention(RUNTIME)),T 没有输入@Valid 在编译后 class.

如果我使用 MyGroupPayload extends GroupPayload<MyType>,验证 不会 触发元素,但如果我使用硬编码(非通用)class List<@Valid MyType> data 验证有效。

如何将通用类型的 TYPE_USE 注释添加到已编译的 class 中,以便我可以将其用作直接项目之外的库 class。

注释编译到class文件中。 我怀疑您用来检查 class 文件的任何工具都有问题。

这是一个 MWE:

import java.util.List;
import java.lang.annotation.ElementType;
import java.lang.annotation.Target;

@Target(ElementType.TYPE_USE)
@interface Valid{}

public class GroupPayload<T> {
    List<@Valid T> data;
}

的输出
javac GroupPayload.java
javap -v GroupPayload.class

出现在下方。您可以从这些片段中看到:

...
   #9 = Utf8               LValid;
...
  java.util.List<T> data;
...
    RuntimeInvisibleTypeAnnotations:
      0: #9(): FIELD, location=[TYPE_ARGUMENT(0)]

@Valid 注释在 .class 文件中。

这里是 javap -v GroupPayload.class 的完整输出:

Classfile /home/mernst/GroupPayload.class
  Last modified Jan 16, 2020; size 515 bytes
  MD5 checksum 3db07417a8da20b35032650b64e9ffce
  Compiled from "GroupPayload.java"
public class GroupPayload<T extends java.lang.Object> extends java.lang.Object
  minor version: 0
  major version: 52
  flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
   #1 = Methodref          #3.#22         // java/lang/Object."<init>":()V
   #2 = Class              #23            // GroupPayload
   #3 = Class              #24            // java/lang/Object
   #4 = Utf8               data
   #5 = Utf8               Ljava/util/List;
   #6 = Utf8               Signature
   #7 = Utf8               Ljava/util/List<TT;>;
   #8 = Utf8               RuntimeInvisibleTypeAnnotations
   #9 = Utf8               LValid;
  #10 = Utf8               <init>
  #11 = Utf8               ()V
  #12 = Utf8               Code
  #13 = Utf8               LineNumberTable
  #14 = Utf8               LocalVariableTable
  #15 = Utf8               this
  #16 = Utf8               LGroupPayload;
  #17 = Utf8               LocalVariableTypeTable
  #18 = Utf8               LGroupPayload<TT;>;
  #19 = Utf8               <T:Ljava/lang/Object;>Ljava/lang/Object;
  #20 = Utf8               SourceFile
  #21 = Utf8               GroupPayload.java
  #22 = NameAndType        #10:#11        // "<init>":()V
  #23 = Utf8               GroupPayload
  #24 = Utf8               java/lang/Object
{
  java.util.List<T> data;
    descriptor: Ljava/util/List;
    flags:
    Signature: #7                           // Ljava/util/List<TT;>;
    RuntimeInvisibleTypeAnnotations:
      0: #9(): FIELD, location=[TYPE_ARGUMENT(0)]

  public GroupPayload();
    descriptor: ()V
    flags: ACC_PUBLIC
    Code:
      stack=1, locals=1, args_size=1
         0: aload_0
         1: invokespecial #1                  // Method java/lang/Object."<init>":()V
         4: return
      LineNumberTable:
        line 8: 0
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0       5     0  this   LGroupPayload;
      LocalVariableTypeTable:
        Start  Length  Slot  Name   Signature
            0       5     0  this   LGroupPayload<TT;>;
}
Signature: #19                          // <T:Ljava/lang/Object;>Ljava/lang/Object;
SourceFile: "GroupPayload.java"