惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

P
Privacy & Cybersecurity Law Blog
V
V2EX
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
The Register - Security
The Register - Security
MongoDB | Blog
MongoDB | Blog
P
Privacy International News Feed
The Last Watchdog
The Last Watchdog
Security Archives - TechRepublic
Security Archives - TechRepublic
美团技术团队
Stack Overflow Blog
Stack Overflow Blog
博客园 - 司徒正美
博客园 - 三生石上(FineUI控件)
V
Visual Studio Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
K
Kaspersky official blog
S
Secure Thoughts
T
Tenable Blog
Security Latest
Security Latest
The Cloudflare Blog
S
Security @ Cisco Blogs
H
Heimdal Security Blog
aimingoo的专栏
aimingoo的专栏
TaoSecurity Blog
TaoSecurity Blog
Blog — PlanetScale
Blog — PlanetScale
Microsoft Security Blog
Microsoft Security Blog
Schneier on Security
Schneier on Security
Webroot Blog
Webroot Blog
G
Google Developers Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Scott Helme
Scott Helme
IT之家
IT之家
Latest news
Latest news
The Hacker News
The Hacker News
C
Check Point Blog
T
The Exploit Database - CXSecurity.com
H
Hackread – Cybersecurity News, Data Breaches, AI and More
腾讯CDC
C
CERT Recently Published Vulnerability Notes
NISL@THU
NISL@THU
N
News | PayPal Newsroom
Forbes - Security
Forbes - Security
P
Palo Alto Networks Blog
S
Security Affairs
S
Securelist
Google Online Security Blog
Google Online Security Blog
WordPress大学
WordPress大学
Last Week in AI
Last Week in AI
C
Cybersecurity and Infrastructure Security Agency CISA
A
About on SuperTechFans

博客园 - 星小梦

Linux系统调整java程序启动用户,导出xlsx时抛出Permission denied异常问题。 vue2的devtools开发工具卡死现象的解决方法 echarts图表在浏览器上打印出现裁剪的问题 apache echarts数据点重影或 Cannot read properties of undefined (reading 'type')错误问题 yarn install出现error Error: certificate has expired异常 git多代码仓库合并的方式 docker容器oshi如何获取宿主机的运行状态信息? su命令引起的nohup进程以root身份启动导致的问题 docker-compose启动服务,影响其他服务的原因 xxl-job provider netty_http server caught exception flutter升级导致的旧项目的运行环境问题排查记录 vue3插件库以及对JSX的支持。 JSX、TSX扩展语法学习材料 commitlint Lint 提交消息格式控制 Chromium历史版本下载方式 Window10 关闭Edge浏览器的多选项卡通过Alt+Tab组合键切换的方式 Postgres16数据库集成外部库dblink和postgres_fdw扩展的方式 Postgres16 常见问题 docker镜像安装字体支持,解决jdk服务验证码生成找不到字体问题 window和Linux命令行执行多条命令的方法
java 泛型类型如何保留类型的信息的方式
星小梦 · 2025-07-21 · via 博客园 - 星小梦

https://www.baeldung.com/gson-typetoken-dynamic-list-item-type
https://stackoverflow.com/questions/43117731/what-is-type-typetoken

大意就是通过各种方式保留泛型的信息。有通过变量的方式,有通过继承的方式。

变量的方式就不说了,只是在构造函数那里处理泛型的传入,然后解析相应的值放到类变量中。

继承的方式这里说下,TypeToken是gson特有的,不过意思是相同的。

TypeToken<List<Student>> typeTokenForListOfStudents = new TypeToken<List<Student>>() {};

与上面的作用是相同的,都是为了留存泛型的信息


// 泛型基础类
class A<T> {};

// 子类
class B extends A<B> {};

// 使用的时候,使用B类即可,这样继承A的方法等需要类型信息的时候,都能通过`this.getClass()`来获取到。

// 使用例子:
B b1 = new B();

// 又或者是
A<String> a1 = new A<String>() {};

但是如果你想获取B extends A<B>中的<>里的类型的话,就需要使用以下代码来获取泛型的类型

Type superclass = this.getClass().getGenericSuperclass();
if (superclass instanceof ParameterizedType parameterized) {
    if (parameterized.getRawType() == A.class) {
        return parameterized.getActualTypeArguments()[0];
    }
}