- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
public class Foo<T> {
public List<Integer> aGenericList;
public T item;
public Foo() {
aGenericList = new ArrayList<>();
}
public static void main(String[] args) throws NoSuchFieldException {
Foo foo = new Foo<String>();
System.out.println(foo.aGenericList.getClass());
Field testField = Foo.class.getField("aGenericList");
Type genericType1 = testField.getGenericType();
System.out.println(genericType1.getTypeName());
}
}
结果是:
class java.util.ArrayList
java.util.List<java.lang.Integer>
这意味着使用反射方法,可以获得删除的类型信息。
foo
得到类似 Foo<java.lang.String>
的东西? 最佳答案
你的问题
1. 这种行为是在 JLS/JVMS 规范中正式定义的(如果是,在哪里?),还是由实现该语言的不同供应商决定?
Java 语言规范似乎专门用于 not describe reflection :
Consequently, this specification does not describe reflection in any detail.
4.7.9. The
Signature
AttributeThe
Signature
attribute is a fixed-length attribute in the attributes table of aClassFile
,field_info
, ormethod_info
structure (§4.1, §4.5, §4.6). ASignature
attribute records a signature (§4.7.9.1) for a class, interface, constructor, method, or field whose declaration in the Java programming language uses type variables or parameterized types. See The Java Language Specification, Java SE 15 Edition for details about these constructs.[...]
4.7.9.1. Signatures
Signatures encode declarations written in the Java programming language that use types outside the type system of the Java Virtual Machine. They support reflection and debugging, as well as compilation when only
class
files are available.A Java compiler must emit a signature for any class, interface, constructor, method, or field whose declaration uses type variables or parameterized types. Specifically, a Java compiler must emit:
A class signature for any class or interface declaration which is either generic, or has a parameterized type as a superclass or superinterface, or both.
A method signature for any method or constructor declaration which is either generic, or has a type variable or parameterized type as the return type or a formal parameter type, or has a type variable in a
throws
clause, or any combination thereof.If the
throws
clause of a method or constructor declaration does not involve type variables, then a compiler may treat the declaration as having nothrows
clause for the purpose of emitting a method signature.A field signature for any field, formal parameter, or local variable declaration whose type uses a type variable or a parameterized type.
[...]
foo
是否可以应用反射方式?得到类似
Foo<java.lang.String>
的东西?
Foo foo = new Foo<String>();
将反射(reflect)的是左侧。这是一个原始类型,所以你只知道
foo
的类型是
Foo
.您将无法判断右侧创建的实例是使用
String
参数化的。 .
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.WildcardType;
import java.util.ArrayList;
import java.util.List;
public class Main {
private static List<? extends Number> list = new ArrayList<Integer>();
public static void main(String[] args) throws Exception {
Field field = Main.class.getDeclaredField("list");
// Due to List being a generic type the returned Type is actually
// an instance of java.lang.reflect.ParameterizedType
Type genericType = field.getGenericType();
System.out.println("Generic Type = " + genericType);
// The raw type can be gotten from the ParameterizedType. Here the
// returned Type will actually be an instance of java.lang.Class
Type rawType = ((ParameterizedType) genericType).getRawType();
System.out.println("Raw Type = " + rawType);
// The ParameterizedType gives us access to the actual type
// arguments declared. Also, since a bounded wildcard was used
// the returned Type is actually an instance of
// java.lang.reflect.WildcardType
Type typeArgument = ((ParameterizedType) genericType).getActualTypeArguments()[0];
System.out.println("Type Argument = " + typeArgument);
// We know in this case that there is a single upper bound. Here
// the returned Type will actually be an instance of java.lang.Class
Type upperBound = ((WildcardType) typeArgument).getUpperBounds()[0];
System.out.println("Upper Bound = " + upperBound);
}
}
将输出:
Generic Type = java.util.List<? extends java.lang.Number>
Raw Type = interface java.util.List
Type Argument = ? extends java.lang.Number
Upper Bound = class java.lang.Number
所有这些信息都在源代码中。请注意,我们正在反射(reflection)地查看
list
领域 .我们是
不是 查看由所述字段引用的实例(即运行时对象)。知道字段的泛型类型与知道字段的名称是
list
真的没有什么不同。 .
ArrayList
参数化为
Integer
.将以上内容改为:
import java.lang.reflect.TypeVariable;
import java.util.ArrayList;
import java.util.List;
public class Main {
private static List<? extends Number> list = new ArrayList<Integer>();
public static void main(String[] args) {
Class<?> clazz = list.getClass();
System.out.println("Class = " + clazz);
TypeVariable<?> typeParameter = clazz.getTypeParameters()[0];
System.out.println("Type Parameter = " + typeParameter);
}
}
输出:
Class = class java.util.ArrayList
Type Parameter = E
我们可以看到我们知道
list
引用的实例是
java.util.ArrayList
的一个实例.但从那里我们可以确定的是
ArrayList
类是泛型的,只有一个类型参数
E
.我们无法确定
list
字段被分配了
ArrayList
类型参数为
Integer
.换句话说,
ArrayList
实例本身不知道它声明包含什么类型的元素——该信息已被删除。
list
字段的类型在运行时已知,但
ArrayList
实例(即在运行时创建的对象)只知道它是一个
ArrayList
.
关于java - 为什么在 Java 中没有删除成员字段的通用类型信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65651242/
我使用 AppFuse 创建项目已经有一段时间了。我已经知道有两种方法可以开发 DAO 和 Manager 类: GenericDao/GenericManager 方法 UniversalDao/U
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
在普通的单线程程序中,捕获异常只需要通过try ... catch ... finally ...代码块就可以了。那么,在并发情况下,比如在父线程中启动了子线程,如何在父线程中捕获来自子线程的异常,
假设我有一个这样的界面 interface Example { first_name: string, last_name: string, home_town: string
我已经成为 hg 用户几年了,对此我很高兴! 我必须开始一个我以前从未做过的项目。我们的想法是开发一个具有批处理模式和 GUI 的软件。 因此,批处理模式和 GUI 模式都有共同的源,但每种模式也都包
我可以在Silverlight中使用generic.xaml来设置应用程序中所有TextBlock的样式吗? 我原以为它会起作用,但它没
顶部 map 有 3 个子 map ,每个子 map 都有不同的对象。 像下面的代码,如何将通用添加到 map 顶部? Map top = new ConcurrentHashMap();
我想创建一个hashmap,其中键是接口(interface)A,值是接口(interface)B。然后我想用实现A和B的类来初始化它。是否可以使用java泛型来做到这一点? 也就是说,我想要类似的东
Enum 位于 java.lang.Enum 中,Object 位于 java.lang.Object 中>。那么,为什么 Enum 不是 Object 呢? (我收到一个java.lang.Clas
我有一种方法,check,它有两个 HashMap 作为参数。这些映射的键是 String,值是 String 或 Arraylist。 哪个是更好的解决方案: public static boole
我启动了针对iPhone的应用程序,现在我也想将其应用程序用于iPad。当我开始做iPhone项目时,即使我添加了iPad xib,它也无法正确显示,如何转换我的项目同时适用于iPhone和iPad(
这行代码(代码1)有什么区别 auto l1 = [](auto a) { static int l = 0; std::cout operator() for type const char*) 被
使用 Generic#to,我可以获得 case class 的 HList 表示: import shapeless._ case class F(x: Int, y: String) scala>
我有一个 BiDiMap 类。如何使其通用,不仅接受 String 而且接受 Object 类型的对象作为输入参数,同时保持所有原始函数正常工作。例如,我希望能够使用函数 put() 和 Object
我在编译 foreach 循环时遇到问题。我很确定这是我的泛型处理的问题,因为该错误是对象兼容性问题。我已搜索解决方案,但找不到任何可以解决该问题的内容。 这是定义 Iterable adjList
大约有 6 个 POJO 类(域实体、DTO、DMO)都具有几乎相同的字段。为了从一个对象转换为另一个对象,我传递一个对象并调用它的 getter 将其设置到另一个对象中。 private UserT
有没有什么方法可以创建一个通用的 for 循环,它可以正确地循环遍历数组或对象?我知道我可以编写以下 for 循环,但它也会遍历将添加到数组的其他属性。 for (item in x) { co
我已经有一段时间没有写js了,显然有点生疏了。试图理解以下问题。 getCurrentPosition successCallback 中的警报正确显示纬度,但最后一行警报未定义。为什么我的 clie
请帮助我,我从来没有用 xib 为 iPhone/iPad 制作过通用的 UIViewControllers。如何使用 .m 和 .h 文件以及 _iphone.xib 和 _ipad.xib 创建类
我正在尝试创建一个 createRequest 函数,我可以将其重新用于我的所有网络调用,有些需要发布 JSON 而其他则不需要,所以我正在考虑创建一个采用可选通用对象的函数;理论上是这样的: str
我是一名优秀的程序员,十分优秀!