|
| 1 | +package org.machinemc.foundry.model; |
| 2 | + |
| 3 | +import org.objectweb.asm.Type; |
| 4 | + |
| 5 | +import java.util.Arrays; |
| 6 | + |
| 7 | +/** |
| 8 | + * Groups data for byte code generation of {@link ObjectFactory} together. |
| 9 | + */ |
| 10 | +enum ContainerTypeMapping { |
| 11 | + |
| 12 | + /** |
| 13 | + * Primitive boolean mapping. |
| 14 | + */ |
| 15 | + BOOLEAN(boolean.class, Type.BOOLEAN_TYPE, "readBool", "writeBool", |
| 16 | + AttributeAccess.CustomGetter.Bool.class, AttributeAccess.CustomSetter.Bool.class), |
| 17 | + |
| 18 | + /** |
| 19 | + * Primitive char mapping. |
| 20 | + */ |
| 21 | + CHAR(char.class, Type.CHAR_TYPE, "readChar", "writeChar", |
| 22 | + AttributeAccess.CustomGetter.Char.class, AttributeAccess.CustomSetter.Char.class), |
| 23 | + |
| 24 | + /** |
| 25 | + * Primitive byte mapping. |
| 26 | + */ |
| 27 | + BYTE(byte.class, Type.BYTE_TYPE, "readByte", "writeByte", |
| 28 | + AttributeAccess.CustomGetter.Byte.class, AttributeAccess.CustomSetter.Byte.class), |
| 29 | + |
| 30 | + /** |
| 31 | + * Primitive short mapping. |
| 32 | + */ |
| 33 | + SHORT(short.class, Type.SHORT_TYPE, "readShort", "writeShort", |
| 34 | + AttributeAccess.CustomGetter.Short.class, AttributeAccess.CustomSetter.Short.class), |
| 35 | + |
| 36 | + /** |
| 37 | + * Primitive int mapping. |
| 38 | + */ |
| 39 | + INT(int.class, Type.INT_TYPE, "readInt", "writeInt", |
| 40 | + AttributeAccess.CustomGetter.Int.class, AttributeAccess.CustomSetter.Int.class), |
| 41 | + |
| 42 | + /** |
| 43 | + * Primitive long mapping. |
| 44 | + */ |
| 45 | + LONG(long.class, Type.LONG_TYPE, "readLong", "writeLong", |
| 46 | + AttributeAccess.CustomGetter.Long.class, AttributeAccess.CustomSetter.Long.class), |
| 47 | + |
| 48 | + /** |
| 49 | + * Primitive float mapping. |
| 50 | + */ |
| 51 | + FLOAT(float.class, Type.FLOAT_TYPE, "readFloat", "writeFloat", |
| 52 | + AttributeAccess.CustomGetter.Float.class, AttributeAccess.CustomSetter.Float.class), |
| 53 | + |
| 54 | + /** |
| 55 | + * Primitive double mapping. |
| 56 | + */ |
| 57 | + DOUBLE(double.class, Type.DOUBLE_TYPE, "readDouble", "writeDouble", |
| 58 | + AttributeAccess.CustomGetter.Double.class, AttributeAccess.CustomSetter.Double.class), |
| 59 | + |
| 60 | + /** |
| 61 | + * Object mapping. |
| 62 | + */ |
| 63 | + OBJECT(Object.class, Type.getType(Object.class), "readObject", "writeObject", |
| 64 | + AttributeAccess.CustomGetter.Object.class, AttributeAccess.CustomSetter.Object.class); |
| 65 | + |
| 66 | + final Class<?> javaType; |
| 67 | + final Type asmType; |
| 68 | + final String readMethod; |
| 69 | + final String writeMethod; |
| 70 | + final Class<?> customGetter; |
| 71 | + final Class<?> customSetter; |
| 72 | + |
| 73 | + ContainerTypeMapping(Class<?> javaType, Type asmType, String readMethod, String writeMethod, |
| 74 | + Class<?> customGetter, Class<?> customSetter) { |
| 75 | + this.javaType = javaType; |
| 76 | + this.asmType = asmType; |
| 77 | + this.readMethod = readMethod; |
| 78 | + this.writeMethod = writeMethod; |
| 79 | + this.customGetter = customGetter; |
| 80 | + this.customSetter = customSetter; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Returns the mapping for provided type. |
| 85 | + * |
| 86 | + * @param type type |
| 87 | + * @return mapping for given type |
| 88 | + */ |
| 89 | + static ContainerTypeMapping of(Class<?> type) { |
| 90 | + if (type.isPrimitive()) { |
| 91 | + return Arrays.stream(values()) |
| 92 | + .filter(m -> m.javaType == type) |
| 93 | + .findFirst() |
| 94 | + .orElseThrow(() -> new IllegalArgumentException("Unknown primitive type: " + type)); |
| 95 | + } |
| 96 | + return OBJECT; |
| 97 | + } |
| 98 | + |
| 99 | +} |
0 commit comments