-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTypeReference.ts
More file actions
57 lines (47 loc) · 1.41 KB
/
Copy pathTypeReference.ts
File metadata and controls
57 lines (47 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { simpleName } from "./common";
import { PrimitiveType } from "./PrimitiveType";
const BOX_TYPES = {
"java.lang.Boolean": "boolean",
"java.lang.Byte": "byte",
"java.lang.Character": "char",
"java.lang.Double": "double",
"java.lang.Float": "float",
"java.lang.Integer": "int",
"java.lang.Long": "long",
"java.lang.Short": "short",
"java.lang.Void": "void",
};
type BoxName = keyof typeof BOX_TYPES;
const NUMBER_TYPES = [
"java.lang.Byte",
"java.lang.Double",
"java.lang.Float",
"java.lang.Integer",
"java.lang.Long",
"java.lang.Short",
];
/** Fully qualified type reference */
export class TypeReference {
qualifiedName: string;
constructor(qualifiedName: string) {
this.qualifiedName = qualifiedName;
}
get name() {
return simpleName(this.qualifiedName);
}
canonicalName = () => this.qualifiedName;
isVoid = () => this.qualifiedName === "java.lang.Void";
isBoolean = () => this.qualifiedName === "java.lang.Boolean";
isNumber = () => NUMBER_TYPES.includes(this.qualifiedName);
isString = () => this.qualifiedName === "java.lang.String";
boxed(): this is TypeReference & { qualifiedName: BoxName } {
return BOX_TYPES.hasOwnProperty(this.qualifiedName);
}
unbox(): PrimitiveType {
if (this.boxed()) {
return new PrimitiveType(BOX_TYPES[this.qualifiedName]);
} else {
throw new Error(`not a box type: ${this.qualifiedName}`);
}
}
}