Просмотр исходного кода

Expansions: Check if the 1st parameter is the expanded type

kindlich 5 лет назад
Родитель
Сommit
c7c34cbc2a
Не найден GPG ключ соответствующий данной подписи

+ 28
- 6
JavaIntegration/src/main/java/org/openzen/zencode/java/JavaNativeModule.java Просмотреть файл

@@ -58,12 +58,7 @@ import org.openzen.zenscript.codemodel.member.ref.FunctionalMemberRef;
58 58
 import org.openzen.zenscript.codemodel.partial.PartialStaticMemberGroupExpression;
59 59
 import org.openzen.zenscript.codemodel.scope.ExpressionScope;
60 60
 import org.openzen.zenscript.codemodel.scope.FileScope;
61
-import org.openzen.zenscript.codemodel.type.BasicTypeID;
62
-import org.openzen.zenscript.codemodel.type.GlobalTypeRegistry;
63
-import org.openzen.zenscript.codemodel.type.ISymbol;
64
-import org.openzen.zenscript.codemodel.type.StoredType;
65
-import org.openzen.zenscript.codemodel.type.StringTypeID;
66
-import org.openzen.zenscript.codemodel.type.TypeID;
61
+import org.openzen.zenscript.codemodel.type.*;
67 62
 import org.openzen.zenscript.codemodel.type.member.BuiltinID;
68 63
 import org.openzen.zenscript.codemodel.type.member.TypeMembers;
69 64
 import org.openzen.zenscript.codemodel.type.storage.AutoStorageTag;
@@ -453,6 +448,10 @@ public class JavaNativeModule {
453 448
 			if(!method.isAnnotationPresent(ZenCodeType.Method.class))
454 449
 				continue;
455 450
 
451
+			if(!method.getParameterTypes()[0].isAssignableFrom(getClassFromType(expandedType)))
452
+				throw new IllegalArgumentException("Cannot add extension method " + method + " as its first parameter does not match the extended type.");
453
+
454
+
456 455
 			final ZenCodeType.Method annotation = method.getAnnotation(ZenCodeType.Method.class);
457 456
 			String name = !annotation.value().isEmpty() ? annotation.value() : method.getName();
458 457
 
@@ -476,6 +475,29 @@ public class JavaNativeModule {
476 475
 		return expansion;
477 476
 	}
478 477
 
478
+	private Class<?> getClassFromType(TypeID type) {
479
+		if(type instanceof DefinitionTypeID) {
480
+			DefinitionTypeID definitionType = ((DefinitionTypeID) type);
481
+
482
+			for (Map.Entry<Class<?>, HighLevelDefinition> ent : definitionByClass.entrySet()) {
483
+				if(ent.getValue().equals(definitionType.definition))
484
+					return ent.getKey();
485
+			}
486
+		}
487
+
488
+		for (Map.Entry<Class<?>, TypeID> ent : typeByClass.entrySet()) {
489
+			if(ent.getValue().equals(type))
490
+				return ent.getKey();
491
+		}
492
+
493
+		for (Map.Entry<Class<?>, TypeID> ent : unsignedByClass.entrySet()) {
494
+			if(ent.getValue().equals(type))
495
+				return ent.getKey();
496
+		}
497
+
498
+		throw new IllegalArgumentException("Could not find class for type " + type);
499
+	}
500
+
479 501
 
480 502
 	private TypeID getTypeFromName(String className) {
481 503
 		for (TypeID value : this.typeByClass.values()) {

Загрузка…
Отмена
Сохранить