Skip to content

Commit b8e3d07

Browse files
committed
Small changes in various places
1 parent c0f97dd commit b8e3d07

6 files changed

Lines changed: 40 additions & 18 deletions

File tree

src/main/java/com/laytonsmith/core/ArgumentValidation.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.laytonsmith.core.constructs.CNumber;
2121
import com.laytonsmith.core.constructs.CString;
2222
import com.laytonsmith.core.constructs.CVoid;
23+
import com.laytonsmith.core.constructs.InstanceofUtil;
2324
import com.laytonsmith.core.constructs.Target;
2425
import com.laytonsmith.core.exceptions.ConfigRuntimeException;
2526
import com.laytonsmith.core.natives.interfaces.Booleanish;
@@ -143,9 +144,9 @@ public static double getNumber(Mixed c, Target t, Environment env) {
143144
if(c == null || c instanceof CNull) {
144145
return 0.0;
145146
}
146-
if(c instanceof CNumber) {
147+
if(InstanceofUtil.isInstanceof(c, CNumber.class, env)) {
147148
d = ((CNumber) c).getNumber();
148-
} else if(c instanceof CString) {
149+
} else if(InstanceofUtil.isInstanceof(c, CString.class, env)) {
149150
try {
150151
d = Double.parseDouble(c.val());
151152
} catch (NumberFormatException e) {
@@ -157,7 +158,7 @@ public static double getNumber(Mixed c, Target t, Environment env) {
157158
} else {
158159
d = 0;
159160
}
160-
} else if(c instanceof CDecimal) {
161+
} else if(InstanceofUtil.isInstanceof(c, CDecimal.class, env)) {
161162
throw new CRECastException("Expecting a number, but received a decimal value instead. This cannot be"
162163
+ " automatically cast, please use double(@decimal) to manually cast down to a double.", t);
163164
} else {
@@ -200,7 +201,7 @@ public static boolean isNumber(Mixed c) {
200201
* @return boolean
201202
*/
202203
public static boolean isNumber(Mixed c, Environment env) {
203-
return c instanceof CNumber || VALID_DOUBLE.matcher(c.val()).matches();
204+
return InstanceofUtil.isInstanceof(c, CNumber.class, env) || VALID_DOUBLE.matcher(c.val()).matches();
204205
}
205206

206207
@Deprecated
@@ -448,7 +449,7 @@ public static boolean getBooleanish(Mixed c, Target t, Environment env) {
448449
if(c == null) {
449450
return false;
450451
}
451-
if(c instanceof Booleanish) {
452+
if(InstanceofUtil.isInstanceof(c, Booleanish.class, env)) {
452453
return ((Booleanish) c).getBooleanValue(t);
453454
}
454455
throw new CRECastException("Could not convert value of type " + c.typeof() + " to a " + Booleanish.TYPE, t);
@@ -532,7 +533,7 @@ public static boolean anyDoubles(Mixed... c) {
532533
*/
533534
public static boolean anyDoubles(Environment env, Mixed... c) {
534535
for(Mixed c1 : c) {
535-
if(c1 instanceof CDouble || c1 instanceof CString && c1.val().indexOf(".", 1) > -1) {
536+
if(InstanceofUtil.isInstanceof(c1, CDouble.class, env) || InstanceofUtil.isInstanceof(c1, CString.class, env) && c1.val().indexOf(".", 1) > -1) {
536537
return true;
537538
}
538539
}
@@ -553,7 +554,7 @@ public static boolean anyStrings(Mixed... c) {
553554
*/
554555
public static boolean anyStrings(Environment env, Mixed... c) {
555556
for(Mixed c1 : c) {
556-
if(c1 instanceof CString) {
557+
if(InstanceofUtil.isInstanceof(c1, CString.class, env)) {
557558
return true;
558559
}
559560
}

src/main/java/com/laytonsmith/core/constructs/CByteArray.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
*
3030
*/
3131
@typeof("ms.lang.byte_array")
32-
public class CByteArray extends CArray implements Sizeable, ArrayAccess {
32+
public final class CByteArray extends CArray implements Sizeable, ArrayAccess {
3333

3434
@SuppressWarnings("FieldNameHidesFieldInSuperclass")
3535
public static final CClassType TYPE = CClassType.get(CByteArray.class);
@@ -759,7 +759,7 @@ public CClassType[] getSuperclasses() {
759759

760760
@Override
761761
public Set<ObjectModifier> getObjectModifiers() {
762-
return EnumSet.of(ObjectModifier.STATIC);
762+
return EnumSet.of(ObjectModifier.STATIC, ObjectModifier.FINAL);
763763
}
764764

765765
@Override

src/main/java/com/laytonsmith/core/constructs/CMutablePrimitive.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,18 @@
99
import com.laytonsmith.core.natives.interfaces.Mixed;
1010
import com.laytonsmith.core.natives.interfaces.Sizeable;
1111
import com.laytonsmith.core.natives.interfaces.ValueType;
12+
import com.laytonsmith.core.objects.ObjectModifier;
1213
import java.util.ArrayList;
14+
import java.util.EnumSet;
1315
import java.util.List;
16+
import java.util.Set;
1417
import java.util.Stack;
1518

1619
/**
1720
*
1821
*/
1922
@typeof("ms.lang.mutable_primitive")
20-
public class CMutablePrimitive extends CArray implements Sizeable {
23+
public final class CMutablePrimitive extends CArray implements Sizeable {
2124

2225
@SuppressWarnings("FieldNameHidesFieldInSuperclass")
2326
public static final CClassType TYPE = CClassType.get(CMutablePrimitive.class);
@@ -180,4 +183,9 @@ public CClassType[] getInterfaces() {
180183
return new CClassType[]{Sizeable.TYPE};
181184
}
182185

186+
@Override
187+
public Set<ObjectModifier> getObjectModifiers() {
188+
return EnumSet.of(ObjectModifier.FINAL);
189+
}
190+
183191
}

src/main/java/com/laytonsmith/core/constructs/CResource.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@
66
import com.laytonsmith.core.MSVersion;
77
import com.laytonsmith.core.functions.ResourceManager.res_free_resource;
88
import com.laytonsmith.core.natives.interfaces.Mixed;
9+
import com.laytonsmith.core.objects.ObjectModifier;
910

1011
import java.io.File;
12+
import java.util.EnumSet;
13+
import java.util.Set;
1114
import java.util.concurrent.atomic.AtomicLong;
1215

1316
/**
@@ -17,7 +20,7 @@
1720
* created.
1821
*/
1922
@typeof("ms.lang.Resource")
20-
public class CResource<T> extends Construct implements Finalizable {
23+
public final class CResource<T> extends Construct implements Finalizable {
2124

2225
@SuppressWarnings("FieldNameHidesFieldInSuperclass")
2326
public static final CClassType TYPE = CClassType.get(CResource.class);
@@ -130,4 +133,8 @@ public CClassType[] getInterfaces() {
130133
return CClassType.EMPTY_CLASS_ARRAY;
131134
}
132135

136+
@Override
137+
public Set<ObjectModifier> getObjectModifiers() {
138+
return EnumSet.of(ObjectModifier.FINAL);
139+
}
133140
}

src/main/java/com/laytonsmith/core/constructs/Construct.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,9 @@ private static Object json_encode0(Mixed c, Target t) throws MarshalException {
195195
return c.val();
196196
} else if(c instanceof CVoid) {
197197
return "";
198-
} else if(c instanceof CInt) {
198+
} else if(InstanceofUtil.isInstanceof(c, CInt.class, null)) {
199199
return ((CInt) c).getInt();
200-
} else if(c instanceof CDouble) {
200+
} else if(InstanceofUtil.isInstanceof(c, CDouble.class, null)) {
201201
return ((CDouble) c).getDouble();
202202
} else if(c instanceof CBoolean) {
203203
return ((CBoolean) c).getBoolean();
@@ -414,13 +414,13 @@ public static Construct GetConstruct(Object o, boolean allowResources) throws Cl
414414
public static Object GetPOJO(Mixed c) throws ClassCastException {
415415
if(c instanceof CNull) {
416416
return null;
417-
} else if(c instanceof CString) {
417+
} else if(InstanceofUtil.isInstanceof(c, CString.class, null)) {
418418
return c.val();
419419
} else if(c instanceof CBoolean) {
420420
return Boolean.valueOf(((CBoolean) c).getBoolean());
421-
} else if(c instanceof CInt) {
421+
} else if(InstanceofUtil.isInstanceof(c, CInt.class, null)) {
422422
return Long.valueOf(((CInt) c).getInt());
423-
} else if(c instanceof CDouble) {
423+
} else if(InstanceofUtil.isInstanceof(c, CDouble.class, null)) {
424424
return Double.valueOf(((CDouble) c).getDouble());
425425
} else if(c.isInstanceOf(CByteArray.TYPE)) {
426426
return ((CByteArray) c).asByteArrayCopy();

src/main/java/com/laytonsmith/core/constructs/InstanceofUtil.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public static Set<CClassType> getAllCastableClasses(CClassType c, Environment en
3939
*/
4040
private static Set<CClassType> getAllCastableClassesWithBlacklist(CClassType c, Set<CClassType> blacklist,
4141
Environment env) {
42-
if(blacklist.contains(c)) {
42+
if(blacklist.contains(c) || CNull.TYPE.equals(c)) {
4343
return blacklist;
4444
}
4545
blacklist.add(c);
@@ -71,6 +71,9 @@ public static boolean isInstanceof(Mixed value, FullyQualifiedClassName instance
7171
if(instanceofThis.getFQCN().equals("auto")) {
7272
return true;
7373
}
74+
if(value instanceof CNull) {
75+
return true;
76+
}
7477
if(value instanceof CFunction) {
7578
// TODO: Need to put the return type here, so we can work with this, but for now, just always return false
7679
return false;
@@ -88,7 +91,7 @@ public static boolean isInstanceof(Mixed value, FullyQualifiedClassName instance
8891
*/
8992
public static boolean isInstanceof(CClassType type, FullyQualifiedClassName instanceofThis, Environment env) {
9093
Static.AssertNonNull(instanceofThis, "instanceofThis may not be null");
91-
if(instanceofThis.getFQCN().equals("auto")) {
94+
if(instanceofThis.getFQCN().equals("auto") || type == CNull.TYPE) {
9295
return true;
9396
}
9497
for(CClassType c : getAllCastableClasses(type, env)) {
@@ -108,6 +111,9 @@ public static boolean isInstanceof(CClassType type, FullyQualifiedClassName inst
108111
* @return
109112
*/
110113
public static boolean isInstanceof(Mixed value, Class<? extends Mixed> instanceofThis, Environment env) {
114+
if(instanceofThis.isAssignableFrom(value.getClass())) {
115+
return true;
116+
}
111117
FullyQualifiedClassName typeof = typeof(instanceofThis);
112118
return typeof == null ? false : isInstanceof(value, typeof, env);
113119
}

0 commit comments

Comments
 (0)