Search in sources :

Example 1 with CClassType

use of com.laytonsmith.core.constructs.CClassType in project CommandHelper by EngineHub.

the class Static method AssertType.

/**
 * This verifies that the type required is actually present, and returns the value, cast to the appropriate type,
 * or, if not the correct type, a CRE.
 * <p>
 * Note that this does not do type coersion, and therefore does not work on primitives, and is only meant for
 * arrays, closures, and other complex types.
 *
 * @param <T> The type desired to be cast to
 * @param type The type desired to be cast to
 * @param args The array of arguments.
 * @param argNumber The argument number, used both for grabbing the correct argument from args, and building the
 * error message if the cast cannot occur.
 * @param func The function, in case this errors out, to build the error message.
 * @param t The code target
 * @return The value, cast to the desired type.
 */
public static <T extends Construct> T AssertType(Class<T> type, Construct[] args, int argNumber, Function func, Target t) {
    Construct value = args[argNumber];
    if (!type.isAssignableFrom(value.getClass())) {
        typeof todesired = type.getAnnotation(typeof.class);
        CClassType toactual = value.typeof();
        if (todesired != null) {
            throw new CRECastException("Argument " + (argNumber + 1) + " of " + func.getName() + " was expected to be a " + todesired.value() + ", but " + toactual + " \"" + value.val() + "\" was found.", t);
        } else {
            // If the typeof annotation isn't present, this is a programming error.
            throw new IllegalArgumentException("");
        }
    } else {
        return (T) value;
    }
}
Also used : CRECastException(com.laytonsmith.core.exceptions.CRE.CRECastException) Construct(com.laytonsmith.core.constructs.Construct) CClassType(com.laytonsmith.core.constructs.CClassType) com.laytonsmith.annotations.typeof(com.laytonsmith.annotations.typeof) CREIllegalArgumentException(com.laytonsmith.core.exceptions.CRE.CREIllegalArgumentException)

Example 2 with CClassType

use of com.laytonsmith.core.constructs.CClassType in project CommandHelper by EngineHub.

the class ClosureKeyword method process.

@Override
public int process(List<ParseTree> list, int keywordPosition) throws ConfigCompileException {
    try {
        if (list.get(keywordPosition).getData() instanceof CFunction) {
            // It's a function, so do the old processing
            SimpleBlockKeywordFunction.doProcess(this.getKeywordName(), null, true, list, keywordPosition);
            // easiest if we do the conversion here.
            try {
                if (list.get(keywordPosition - 1).getData() instanceof CClassType) {
                    ParseTree type = list.remove(keywordPosition - 1);
                    List<ParseTree> children = list.get(keywordPosition - 1).getChildren();
                    children.add(0, type);
                    list.get(keywordPosition - 1).setChildren(children);
                    return keywordPosition - 1;
                }
            } catch (IndexOutOfBoundsException ex) {
            // Ignore, it's not a typed closure
            }
            return keywordPosition;
        } else {
            // Else it's standalone, so this should be treated as the closure ClassType
            list.set(keywordPosition, new ParseTree(CClosure.TYPE, list.get(keywordPosition).getFileOptions()));
            return keywordPosition;
        }
    } catch (IndexOutOfBoundsException ex) {
        throw new ConfigCompileException("Unexpected \"closure\" reference", list.get(keywordPosition).getTarget());
    }
}
Also used : CFunction(com.laytonsmith.core.constructs.CFunction) CClassType(com.laytonsmith.core.constructs.CClassType) ConfigCompileException(com.laytonsmith.core.exceptions.ConfigCompileException) ParseTree(com.laytonsmith.core.ParseTree)

Example 3 with CClassType

use of com.laytonsmith.core.constructs.CClassType in project CommandHelper by EngineHub.

the class AnnotationChecks method checkForTypeInTypeofClasses.

public static void checkForTypeInTypeofClasses() throws Exception {
    Set<ClassMirror<?>> classes = ClassDiscovery.getDefaultInstance().getClassesWithAnnotation(typeof.class);
    Set<String> errors = new HashSet<>();
    for (ClassMirror<?> clazz : classes) {
        try {
            // Make sure that TYPE has the same type as the typeof annotation
            CClassType TYPE = (CClassType) ReflectionUtils.get(clazz.loadClass(), "TYPE");
            if (TYPE == null) {
                errors.add("TYPE is null? " + clazz.getClassName());
                continue;
            }
            if (!TYPE.val().equals(clazz.getAnnotation(typeof.class).getValue("value"))) {
                errors.add(clazz.getClassName() + "'s TYPE value is different than the typeof annotation on it");
            }
        } catch (ReflectionUtils.ReflectionException ex) {
            errors.add(clazz.getClassName() + " needs to add the following:\n\t@SuppressWarnings(\"FieldNameHidesFieldInSuperclass\")\n" + "\tpublic static final CClassType TYPE = CClassType.get(\"" + clazz.getAnnotation(typeof.class).getValue("value") + "\");");
        }
    }
    if (!errors.isEmpty()) {
        throw new Exception("\n" + StringUtils.Join(errors, "\n"));
    }
}
Also used : CClassType(com.laytonsmith.core.constructs.CClassType) ReflectionUtils(com.laytonsmith.PureUtilities.Common.ReflectionUtils) ClassMirror(com.laytonsmith.PureUtilities.ClassLoading.ClassMirror.ClassMirror) com.laytonsmith.annotations.typeof(com.laytonsmith.annotations.typeof) HashSet(java.util.HashSet)

Example 4 with CClassType

use of com.laytonsmith.core.constructs.CClassType in project CommandHelper by EngineHub.

the class IClosureKeyword method process.

@Override
public int process(List<ParseTree> list, int keywordPosition) throws ConfigCompileException {
    try {
        if (list.get(keywordPosition).getData() instanceof CFunction) {
            // It's a function, so do the old processing
            SimpleBlockKeywordFunction.doProcess(this.getKeywordName(), null, true, list, keywordPosition);
            // easiest if we do the conversion here.
            try {
                if (list.get(keywordPosition - 1).getData() instanceof CClassType) {
                    ParseTree type = list.remove(keywordPosition - 1);
                    List<ParseTree> children = list.get(keywordPosition - 1).getChildren();
                    children.add(0, type);
                    list.get(keywordPosition - 1).setChildren(children);
                    return keywordPosition - 1;
                }
            } catch (IndexOutOfBoundsException ex) {
            // Ignore, it's not a typed closure
            }
            return keywordPosition;
        } else {
            // Else it's standalone, so this should be treated as the closure ClassType
            list.set(keywordPosition, new ParseTree(CIClosure.TYPE, list.get(keywordPosition).getFileOptions()));
            return keywordPosition;
        }
    } catch (IndexOutOfBoundsException ex) {
        throw new ConfigCompileException("Unexpected \"iclosure\" reference", list.get(keywordPosition).getTarget());
    }
}
Also used : CFunction(com.laytonsmith.core.constructs.CFunction) CClassType(com.laytonsmith.core.constructs.CClassType) ConfigCompileException(com.laytonsmith.core.exceptions.ConfigCompileException) ParseTree(com.laytonsmith.core.ParseTree)

Aggregations

CClassType (com.laytonsmith.core.constructs.CClassType)4 com.laytonsmith.annotations.typeof (com.laytonsmith.annotations.typeof)2 ParseTree (com.laytonsmith.core.ParseTree)2 CFunction (com.laytonsmith.core.constructs.CFunction)2 ConfigCompileException (com.laytonsmith.core.exceptions.ConfigCompileException)2 ClassMirror (com.laytonsmith.PureUtilities.ClassLoading.ClassMirror.ClassMirror)1 ReflectionUtils (com.laytonsmith.PureUtilities.Common.ReflectionUtils)1 Construct (com.laytonsmith.core.constructs.Construct)1 CRECastException (com.laytonsmith.core.exceptions.CRE.CRECastException)1 CREIllegalArgumentException (com.laytonsmith.core.exceptions.CRE.CREIllegalArgumentException)1 HashSet (java.util.HashSet)1