/
CNull.java
115 lines (95 loc) · 3.07 KB
/
CNull.java
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package com.laytonsmith.core.constructs;
import com.laytonsmith.PureUtilities.Version;
import com.laytonsmith.annotations.typeof;
import com.laytonsmith.core.MSVersion;
import com.laytonsmith.core.natives.interfaces.Booleanish;
/**
* Represents a MethodScript null value.
*/
@typeof("null")
public final class CNull extends Construct implements Cloneable, Booleanish {
@SuppressWarnings("FieldNameHidesFieldInSuperclass")
public static final CClassType TYPE = CClassType.get("null");
public static final long serialVersionUID = 1L;
/**
* Null values do not normally need to be duplicated, since they are immutable, and for values that have an unknown
* code target, are always equal. In cases where a null is generated from inside Java, this value should be
* returned, instead of generating a new one.
*/
public static final CNull NULL = new CNull(Target.UNKNOWN);
/**
* Since NULL != UNDEFINED, but NULL.equals(UNDEFINED), UNDEFINED works like NULL in all cases, except ones where
* special handling is defined. This is meant for variables that are declared with a type, but otherwise not
* defined, for instance string @s; When the variable is used, if the type is UNDEFINED, a warning should be issued.
*/
public static final CNull UNDEFINED = new CNull(Target.UNKNOWN);
/**
* Constructs a new CNull object. Generally speaking, this should only be used when creating null values that are
* literally created by user code, all internal code should simply use {@link #NULL}. This method DOES check the
* target however, and if the target is {@link Target#UNKNOWN}, {@link CNull#NULL} is returned anyways.
*
* @param t
* @return
*/
public static CNull GenerateCNull(Target t) {
if(t == Target.UNKNOWN) {
return NULL;
} else {
return new CNull(t);
}
}
/**
* Private constructor to force usage of {@link #GenerateCNull(com.laytonsmith.core.constructs.Target)}, which can
* return existing objects.
*
* @param t
*/
private CNull(Target t) {
super("null", ConstructType.NULL, t);
}
@Override
@SuppressWarnings("CloneDoesntCallSuperClone")
public CNull clone() throws CloneNotSupportedException {
return this;
}
@Override
public String val() {
return "null";
}
@Override
public boolean equals(Object obj) {
return obj instanceof CNull;
}
@Override
public boolean isDynamic() {
return false;
}
@Override
public int hashCode() {
int hash = 7;
return hash;
}
@Override
public String docs() {
return "null is a default value. All values can be null.";
}
@Override
public Version since() {
return MSVersion.V3_0_1;
}
// null is so special, that if some code tries to call these methods on it, we
// want to immediately fail, because they need to implement special handling
// for this value anyways.
@Override
public CClassType[] getSuperclasses() {
throw new RuntimeException("Cannot call getSuperclasses on null");
}
@Override
public CClassType[] getInterfaces() {
throw new RuntimeException("Cannot call getInterfaces on null");
}
@Override
public boolean getBooleanValue(Target t) {
return false;
}
}