FasterXML / FasterXML/java-classmate

Correct approach to compare `ResolvedType` with a raw `Class<?>`?

未关闭
#72 3 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
主要语言
Java
星标
266
派生
47
平均合并
1 小时 42 分钟
30 天内合并 PR
1

描述

What's the appropriate way to see if a `ResolvedType` represents a simple class that I know about?

Let's say I pass a `new GenericType{}` to a method, and I just want to see if the generic parameter is `String`. I think (I haven't tried it yet) I would do this (code not optimized, for illustration):

```java
void foo(GenericType genericType) {
TypeResolver typeResolver = new TypeResolver();
ResolvedType resolvedType = typeResolver.resolve(genericType);
```

How would I then see if `resolvedType` indicates a simple `String`? I'm guessing I could do this:

```java
if(resolvedType.getTypeParameters.isEmpty() && resolvedType.getErasedType().equals(String.class)) {

```

But that seems sort of awkward. Is there a better way?

(Note that the `resolvedType.getTypeParameters.isEmpty()` check is arguably not necessary here, as `String` has no generic parameters, but I'm looking for a general approach.)

Or should I be going in the other direction, resolving `String.class` and then comparing the resolved types?

```java
if(resolvedType.equals(typeResolver.resolve(String.class))) {

```

I don't know how much overhead the extra resolution of `String.class` adds, though.

This may be related to #31.

贡献指南

这个仓库没有索引到贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。