diff --git a/core/src/main/java/org/jboss/jandex/ClassType.java b/core/src/main/java/org/jboss/jandex/ClassType.java index fed37958..715ffc34 100644 --- a/core/src/main/java/org/jboss/jandex/ClassType.java +++ b/core/src/main/java/org/jboss/jandex/ClassType.java @@ -30,6 +30,16 @@ public final class ClassType extends Type { public static final ClassType OBJECT_TYPE = new ClassType(DotName.OBJECT_NAME); + public static final ClassType BYTE_CLASS = new ClassType(DotName.createComponentized(DotName.JAVA_LANG_NAME, "Byte")); + public static final ClassType CHARACTER_CLASS = new ClassType( + DotName.createComponentized(DotName.JAVA_LANG_NAME, "Character")); + public static final ClassType DOUBLE_CLASS = new ClassType(DotName.createComponentized(DotName.JAVA_LANG_NAME, "Double")); + public static final ClassType FLOAT_CLASS = new ClassType(DotName.createComponentized(DotName.JAVA_LANG_NAME, "Float")); + public static final ClassType INTEGER_CLASS = new ClassType(DotName.createComponentized(DotName.JAVA_LANG_NAME, "Integer")); + public static final ClassType LONG_CLASS = new ClassType(DotName.createComponentized(DotName.JAVA_LANG_NAME, "Long")); + public static final ClassType SHORT_CLASS = new ClassType(DotName.createComponentized(DotName.JAVA_LANG_NAME, "Short")); + public static final ClassType BOOLEAN_CLASS = new ClassType(DotName.createComponentized(DotName.JAVA_LANG_NAME, "Boolean")); + /** * Create an instance of a class type with given {@code name}. *

diff --git a/core/src/main/java/org/jboss/jandex/PrimitiveType.java b/core/src/main/java/org/jboss/jandex/PrimitiveType.java index 2a9d2222..095e90e6 100644 --- a/core/src/main/java/org/jboss/jandex/PrimitiveType.java +++ b/core/src/main/java/org/jboss/jandex/PrimitiveType.java @@ -43,7 +43,10 @@ public final class PrimitiveType extends Type { public static final PrimitiveType SHORT = new PrimitiveType(Primitive.SHORT); public static final PrimitiveType BOOLEAN = new PrimitiveType(Primitive.BOOLEAN); - private static final Map reverseMap = new HashMap(); + private static final Map reverseMap = new HashMap<>(); + + private static final Map boxingMap = new HashMap<>(); + private static final Map unboxingMap = new HashMap<>(); static { reverseMap.put("byte", BYTE); @@ -54,6 +57,24 @@ public final class PrimitiveType extends Type { reverseMap.put("long", LONG); reverseMap.put("short", SHORT); reverseMap.put("boolean", BOOLEAN); + + boxingMap.put(Primitive.BYTE, ClassType.BYTE_CLASS); + boxingMap.put(Primitive.CHAR, ClassType.CHARACTER_CLASS); + boxingMap.put(Primitive.DOUBLE, ClassType.DOUBLE_CLASS); + boxingMap.put(Primitive.FLOAT, ClassType.FLOAT_CLASS); + boxingMap.put(Primitive.INT, ClassType.INTEGER_CLASS); + boxingMap.put(Primitive.LONG, ClassType.LONG_CLASS); + boxingMap.put(Primitive.SHORT, ClassType.SHORT_CLASS); + boxingMap.put(Primitive.BOOLEAN, ClassType.BOOLEAN_CLASS); + + unboxingMap.put(ClassType.BYTE_CLASS.name(), BYTE); + unboxingMap.put(ClassType.CHARACTER_CLASS.name(), CHAR); + unboxingMap.put(ClassType.DOUBLE_CLASS.name(), DOUBLE); + unboxingMap.put(ClassType.FLOAT_CLASS.name(), FLOAT); + unboxingMap.put(ClassType.INTEGER_CLASS.name(), INT); + unboxingMap.put(ClassType.LONG_CLASS.name(), LONG); + unboxingMap.put(ClassType.SHORT_CLASS.name(), SHORT); + unboxingMap.put(ClassType.BOOLEAN_CLASS.name(), BOOLEAN); } /** @@ -103,6 +124,37 @@ public Primitive primitive() { return primitive; } + /** + * Returns a class type that is the result of a boxing conversion of the given {@code primitiveType}. + *

+ * Returns {@code null} if {@code primitiveType} is {@code null}. + * + * @param primitiveType a primitive type, may be {@code null} + * @return the corresponding class type, or {@code null} if {@code primitiveType} is {@code null} + */ + public static ClassType box(PrimitiveType primitiveType) { + if (primitiveType == null) { + return null; + } + return boxingMap.get(primitiveType.primitive); + } + + /** + * Returns a primitive type that is the result of an unboxing conversion of the given {@code classType}. + *

+ * Returns {@code null} if no unboxing conversion exists for given class type + * or if {@code classType} is {@code null}. + * + * @param classType a class type, may be {@code null} + * @return the corresponding primitive type, or {@code null} if there's none + */ + public static PrimitiveType unbox(ClassType classType) { + if (classType == null) { + return null; + } + return unboxingMap.get(classType.name()); + } + @Override public PrimitiveType asPrimitiveType() { return this; diff --git a/core/src/test/java/org/jboss/jandex/test/PrimitiveTypeTest.java b/core/src/test/java/org/jboss/jandex/test/PrimitiveTypeTest.java new file mode 100644 index 00000000..581faae1 --- /dev/null +++ b/core/src/test/java/org/jboss/jandex/test/PrimitiveTypeTest.java @@ -0,0 +1,42 @@ +package org.jboss.jandex.test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; + +import org.jboss.jandex.ClassType; +import org.jboss.jandex.DotName; +import org.jboss.jandex.PrimitiveType; +import org.junit.jupiter.api.Test; + +public class PrimitiveTypeTest { + @Test + public void boxing() { + assertEquals(ClassType.create(Boolean.class), PrimitiveType.box(PrimitiveType.BOOLEAN)); + assertEquals(ClassType.create(Byte.class), PrimitiveType.box(PrimitiveType.BYTE)); + assertEquals(ClassType.create(Short.class), PrimitiveType.box(PrimitiveType.SHORT)); + assertEquals(ClassType.create(Integer.class), PrimitiveType.box(PrimitiveType.INT)); + assertEquals(ClassType.create(Long.class), PrimitiveType.box(PrimitiveType.LONG)); + assertEquals(ClassType.create(Float.class), PrimitiveType.box(PrimitiveType.FLOAT)); + assertEquals(ClassType.create(Double.class), PrimitiveType.box(PrimitiveType.DOUBLE)); + assertEquals(ClassType.create(Character.class), PrimitiveType.box(PrimitiveType.CHAR)); + + assertNull(PrimitiveType.box(null)); + } + + @Test + public void unboxing() { + assertEquals(PrimitiveType.BOOLEAN, PrimitiveType.unbox(ClassType.create(Boolean.class))); + assertEquals(PrimitiveType.BYTE, PrimitiveType.unbox(ClassType.create(Byte.class))); + assertEquals(PrimitiveType.SHORT, PrimitiveType.unbox(ClassType.create(Short.class))); + assertEquals(PrimitiveType.INT, PrimitiveType.unbox(ClassType.create(Integer.class))); + assertEquals(PrimitiveType.LONG, PrimitiveType.unbox(ClassType.create(Long.class))); + assertEquals(PrimitiveType.FLOAT, PrimitiveType.unbox(ClassType.create(Float.class))); + assertEquals(PrimitiveType.DOUBLE, PrimitiveType.unbox(ClassType.create(Double.class))); + assertEquals(PrimitiveType.CHAR, PrimitiveType.unbox(ClassType.create(Character.class))); + + assertNull(PrimitiveType.unbox(ClassType.OBJECT_TYPE)); + assertNull(PrimitiveType.unbox(ClassType.create(DotName.STRING_NAME))); + + assertNull(PrimitiveType.unbox(null)); + } +}