Skip to content

Commit

Permalink
Add a way for objects to provide a static constructor
Browse files Browse the repository at this point in the history
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 c31dc90
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/main/java/com/laytonsmith/core/constructs/NativeTypeList.java
Expand Up @@ -34,6 +34,13 @@ public class NativeTypeList {
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,
Expand Down Expand Up @@ -283,6 +290,9 @@ public static Class<? extends MixedInterfaceRunner> getInterfaceRunnerFor(FullyQ
*/
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 {
Expand Down

0 comments on commit c31dc90

Please sign in to comment.