本文介绍了如何获得泛型定义实体的.class?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

那么如何在Java中执行以下操作?

So how can I do the following in Java?

List<String> strings = new ArrayList<String>();
JAXBElement<List<String>> jax = new JAXBElement<List<String>>(new QName("strings"), List<String>.class, strings);

问题特别发生在List.class,返回的错误是:

The issue specifically occurs at List.class and the error returned is:

Multiple markers at this line
- List cannot be resolved to a variable
- String cannot be resolved to a variable
- Syntax error on token ">", void expected after this token


推荐答案

你不能。

最接近你想要的是使用(Class< List< String>>) List.class

The closest approximation to what you want is to use (Class<List<String>>) List.class.

这是因为没有 List< String> .class ,因为在运行时, List< String> List< Banana> List< FruitSalad> List 在Java中都是相同的。这是故意为一大堆原因而做的,它被称为类型擦除。

This is because there is no List<String>.class, because at runtime, List<String>, List<Banana>, List<FruitSalad>, and List are all the same in Java. This is deliberately done for a whole bunch o' reasons, and it's called type erasure.

这篇关于如何获得泛型定义实体的.class?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 06:16