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;
}
}
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());
}
}
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"));
}
}
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());
}
}
Aggregations