Skip to content

Commit

Permalink
Improve isAssignableTo() for primitive wrapper types
Browse files Browse the repository at this point in the history
  • Loading branch information
knutwannheden committed Nov 6, 2023
1 parent 2173a71 commit 887e9c6
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
27 changes: 27 additions & 0 deletions rewrite-java/src/main/java/org/openrewrite/java/tree/JavaType.java
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 887e9c6

Please sign in to comment.