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

推荐订阅源

V
Visual Studio Blog
P
Privacy International News Feed
C
Cyber Attacks, Cyber Crime and Cyber Security
腾讯CDC
T
Threatpost
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
CERT Recently Published Vulnerability Notes
大猫的无限游戏
大猫的无限游戏
Apple Machine Learning Research
Apple Machine Learning Research
美团技术团队
Cisco Talos Blog
Cisco Talos Blog
C
Cisco Blogs
A
Arctic Wolf
人人都是产品经理
人人都是产品经理
NISL@THU
NISL@THU
L
LINUX DO - 热门话题
爱范儿
爱范儿
GbyAI
GbyAI
The Register - Security
The Register - Security
AWS News Blog
AWS News Blog
MyScale Blog
MyScale Blog
T
Tenable Blog
Hugging Face - Blog
Hugging Face - Blog
A
About on SuperTechFans
Cyberwarzone
Cyberwarzone
量子位
Microsoft Azure Blog
Microsoft Azure Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园_首页
C
Cybersecurity and Infrastructure Security Agency CISA
The Cloudflare Blog
B
Blog RSS Feed
小众软件
小众软件
D
Docker
Know Your Adversary
Know Your Adversary
Y
Y Combinator Blog
P
Privacy & Cybersecurity Law Blog
Engineering at Meta
Engineering at Meta
Latest news
Latest news
AI
AI
SecWiki News
SecWiki News
酷 壳 – CoolShell
酷 壳 – CoolShell
S
Secure Thoughts
N
News | PayPal Newsroom
The Hacker News
The Hacker News
MongoDB | Blog
MongoDB | Blog
Martin Fowler
Martin Fowler
博客园 - 司徒正美
L
Lohrmann on Cybersecurity
Cloudbric
Cloudbric

博客园 - 星小梦

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];
    }
}