package io.trygvis.container.compiler.model; import javax.lang.model.type.TypeKind; import javax.lang.model.type.TypeMirror; 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 just the name or the fully qualified name. */ public final String name; public final String className; public final String fqName; private final boolean primitive; public TypeRef(String name, String fqName) { this.name = name; this.primitive = fqName == null; this.fqName = fqName == null ? name : fqName; int i = this.fqName.lastIndexOf('.'); if (i == -1) { this.className = this.fqName; } else { this.className = this.fqName.substring(i + 1, this.fqName.length()); } } public TypeRef(TypeMirror type) { this(type.toString()); } public TypeRef(Class klass) { this(klass.getCanonicalName(), klass.getCanonicalName()); } public TypeRef(String fqName) { this(fqName, fqName); } public boolean isPrimitive() { return primitive; } public boolean inUnnamedPackage() { return fqName.indexOf('.') == -1; } public String packageName() { int i = fqName.lastIndexOf('.'); if (i == -1) { throw new RuntimeException("This type is in the unnamed package: " + name); } return fqName.substring(0, i); } @Override public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof TypeRef)) return false; TypeRef classRef = (TypeRef) o; return fqName.equals(classRef.fqName); } @Override public int hashCode() { return fqName.hashCode(); } @Override public int compareTo(TypeRef o) { return fqName.compareTo(o.fqName); } 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); } }