Skip to content

Commit

Permalink
Merge pull request #350 from Ladicek/primitive-boxing-unboxing
Browse files Browse the repository at this point in the history
add support for boxing/unboxing of primitive types
  • Loading branch information
Ladicek authored Jan 4, 2024
2 parents 0cf691c + 9354958 commit 894d41e
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 1 deletion.
10 changes: 10 additions & 0 deletions core/src/main/java/org/jboss/jandex/ClassType.java
Original file line number Diff line number Diff line change
Expand Up @@ -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}.
* <p>
Expand Down
54 changes: 53 additions & 1 deletion core/src/main/java/org/jboss/jandex/PrimitiveType.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, PrimitiveType> reverseMap = new HashMap<String, PrimitiveType>();
private static final Map<String, PrimitiveType> reverseMap = new HashMap<>();

private static final Map<Primitive, ClassType> boxingMap = new HashMap<>();
private static final Map<DotName, PrimitiveType> unboxingMap = new HashMap<>();

static {
reverseMap.put("byte", BYTE);
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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}.
* <p>
* 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}.
* <p>
* 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;
Expand Down
42 changes: 42 additions & 0 deletions core/src/test/java/org/jboss/jandex/test/PrimitiveTypeTest.java
Original file line number Diff line number Diff line change
@@ -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));
}
}

0 comments on commit 894d41e

Please sign in to comment.