Permalink
Browse files

Add a way for objects to provide a static constructor

Previously, in NativeTypeList, there was no way to customize how an invalid
instance was constructed. Now, if a subclass of Mixed provides a method named
ConstructInvalidInstance, this will be called instead. For now, this is not
necessary in any native classes, but since this picks up extension objects
as well, it might be useful already.
  • Loading branch information...
LadyCailin committed Nov 28, 2018
1 parent b76d94c commit c31dc908e7fe582e20549acf86aa33e1f2788a07
Showing with 10 additions and 0 deletions.
  1. +10 −0 src/main/java/com/laytonsmith/core/constructs/NativeTypeList.java
@@ -34,6 +34,13 @@
private static volatile Set<String> nativeTypes;
private static Set<FullyQualifiedClassName> fqNativeTypes;
/**
* The name of the static method in all Mixed classes, which if present, is called when
* getInvalidInstanceForUse is called on that class. The method should return a value of
* that type, and should accept no parameters. The method name is "ConstructInvalidInstance".
*/
public static String INVALID_INSTANCE_METHOD_NAME = "ConstructInvalidInstance";
/**
* Given a simple name of a class, attempts to resolve
* within the native types (not user defined types). If the class can't be found, null is returned,
@@ -283,6 +290,9 @@ public static MEnumType getNativeEnumType(FullyQualifiedClassName fqcn) throws C
*/
public static Mixed getInvalidInstanceForUse(FullyQualifiedClassName fqcn) throws ClassNotFoundException {
Class<? extends Mixed> c = getNativeClassOrInterfaceRunner(fqcn);
if(ReflectionUtils.hasMethod(c, INVALID_INSTANCE_METHOD_NAME, Mixed.class)) {
return (Mixed) ReflectionUtils.invokeMethod(c, null, INVALID_INSTANCE_METHOD_NAME);
}
if(MEnumType.class.isAssignableFrom(c)) {
return getNativeEnumType(fqcn);
} else {

0 comments on commit c31dc90

Please sign in to comment.