FasterXML / FasterXML/java-classmate

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

オープン
#72 コメント 3 件 リアクション 0 件 担当者 0 名 GitHub で見る
主要言語
Java
スター
266
フォーク
47
平均マージ
1時間 42分
マージ済み PR(30日)
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 を短くまとめたダイジェスト。