package io.trygvis.container.compiler.model; import javax.lang.model.type.TypeKind; public class TypeRef implements Comparable { public static final TypeRef VOID = new TypeRef("void", "void"); public static final TypeRef BOOLEAN = new TypeRef("boolean", null); public static final TypeRef BYTE = new TypeRef("byte", null); public static final TypeRef SHORT = new TypeRef("short", null); public static final TypeRef CHAR = new TypeRef("char", null); public static final TypeRef INT = new TypeRef("int", null); public static final TypeRef LONG = new TypeRef("long", null); public static final TypeRef FLOAT = new TypeRef("float", null); public static final TypeRef DOUBLE = new TypeRef("double", null); /** * The name of a class used within a class file. Is either the simple name or the canonical name. */ public final String name; private final String canonicalName; public String canonicalName() { if (canonicalName == null) { throw new RuntimeException("This type doesn't have a canonical name"); } return canonicalName; } public boolean isPrimitive() { return canonicalName == null; } public TypeRef(String name, String canonicalName) { this.name = name; this.canonicalName = canonicalName; } @Override public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof TypeRef)) return false; TypeRef classRef = (TypeRef) o; return canonicalName.equals(classRef.canonicalName); } @Override public int hashCode() { return canonicalName.hashCode(); } @Override public int compareTo(TypeRef o) { return canonicalName.compareTo(o.canonicalName); } public static TypeRef find(TypeKind kind) { switch (kind) { case BOOLEAN: return BOOLEAN; case BYTE: return BYTE; case SHORT: return SHORT; case CHAR: return CHAR; case INT: return INT; case LONG: return LONG; case FLOAT: return FLOAT; case DOUBLE: return DOUBLE; } throw new RuntimeException("Unknown kind: " + kind); } }