From 887e9c61f8522d15f46e0899a042a3b22c6e3b03 Mon Sep 17 00:00:00 2001 From: Knut Wannheden Date: Mon, 6 Nov 2023 21:21:24 +0100 Subject: [PATCH] Improve `isAssignableTo()` for primitive wrapper types --- .../org/openrewrite/java/tree/JavaType.java | 27 +++++++++++++++++++ .../org/openrewrite/java/tree/TypeUtils.java | 6 +++++ 2 files changed, 33 insertions(+) diff --git a/rewrite-java/src/main/java/org/openrewrite/java/tree/JavaType.java b/rewrite-java/src/main/java/org/openrewrite/java/tree/JavaType.java index aa29dfd44c0..2759aefba45 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/tree/JavaType.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/tree/JavaType.java @@ -921,6 +921,33 @@ public static Primitive fromKeyword(String keyword) { return null; } + @Nullable + public static Primitive fromClassName(String className) { + switch (className) { + case "java.lang.Boolean": + return Boolean; + case "java.lang.Byte": + return Byte; + case "java.lang.Character": + return Char; + case "java.lang.Double": + return Double; + case "java.lang.Float": + return Float; + case "java.lang.Integer": + return Int; + case "java.lang.Long": + return Long; + case "java.lang.Short": + return Short; + case "java.lang.Void": + return Void; + case "java.lang.String": + return String; + } + return null; + } + public String getKeyword() { switch (this) { case Boolean: diff --git a/rewrite-java/src/main/java/org/openrewrite/java/tree/TypeUtils.java b/rewrite-java/src/main/java/org/openrewrite/java/tree/TypeUtils.java index c4561301549..89034bd0985 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/tree/TypeUtils.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/tree/TypeUtils.java @@ -205,6 +205,12 @@ public static boolean isAssignableTo(@Nullable JavaType to, @Nullable JavaType f IntStream.range(0, parameterCount).allMatch(i -> isAssignableTo(toParameters.get(i), fromParameters.get(i))); } else if (to instanceof JavaType.FullyQualified) { JavaType.FullyQualified toFq = (JavaType.FullyQualified) to; + if (from instanceof JavaType.Primitive) { + JavaType.Primitive toPrimitive = JavaType.Primitive.fromClassName(toFq.getFullyQualifiedName()); + if (toPrimitive != null) { + return isAssignableTo(toPrimitive, from); + } + } return isAssignableTo(toFq.getFullyQualifiedName(), from); } else if (to instanceof JavaType.GenericTypeVariable) { JavaType.GenericTypeVariable toGeneric = (JavaType.GenericTypeVariable) to;