Browse Source

Fix nullable types.

Stan Hebben 5 years ago
parent
commit
d151b5ef99

+ 25
- 2
JavaIntegration/src/main/java/org/openzen/zencode/java/JavaNativeModule.java View File

240
 		if ((cls.getModifiers() & Modifier.PUBLIC) == 0)
240
 		if ((cls.getModifiers() & Modifier.PUBLIC) == 0)
241
 			throw new IllegalArgumentException("Class must be public");
241
 			throw new IllegalArgumentException("Class must be public");
242
 		
242
 		
243
-		ZenCodeType.Name name = cls.getDeclaredAnnotation(ZenCodeType.Name.class);
244
-		String className = name == null ? cls.getName() : name.value();
243
+		String className = cls.getName();
245
 		boolean isStruct = cls.getAnnotation(ZenCodeType.Struct.class) != null;
244
 		boolean isStruct = cls.getAnnotation(ZenCodeType.Struct.class) != null;
246
 		
245
 		
247
 		ZSPackage classPkg = getPackage(className);
246
 		ZSPackage classPkg = getPackage(className);
247
+		ZenCodeType.Name name = cls.getDeclaredAnnotation(ZenCodeType.Name.class);
248
 		className = className.contains(".") ? className.substring(className.lastIndexOf('.') + 1) : className;
248
 		className = className.contains(".") ? className.substring(className.lastIndexOf('.') + 1) : className;
249
+		if (name != null) {
250
+			String specifiedName = name.value();
251
+			if (specifiedName.startsWith(".")) {
252
+				classPkg = getPackage(specifiedName);
253
+				className = className.substring(className.lastIndexOf('.') + 1);
254
+			} else if (specifiedName.indexOf('.') >= 0) {
255
+				if (!specifiedName.startsWith(pkg.fullName))
256
+					throw new IllegalArgumentException("Specified @Name as " + specifiedName + " but it's not in the module root package");
257
+				
258
+				classPkg = getPackage(basePackage + specifiedName.substring(pkg.fullName.length()));
259
+				className = className.substring(className.lastIndexOf('.') + 1);
260
+			} else {
261
+				className = name.value();
262
+			}
263
+		}
249
 		
264
 		
250
 		TypeVariableContext context = new TypeVariableContext();
265
 		TypeVariableContext context = new TypeVariableContext();
251
 		TypeVariable<Class<T>>[] javaTypeParameters = cls.getTypeParameters();
266
 		TypeVariable<Class<T>>[] javaTypeParameters = cls.getTypeParameters();
546
 	}
561
 	}
547
 	
562
 	
548
 	private StoredType loadType(TypeVariableContext context, Type type, boolean nullable, boolean unsigned) {
563
 	private StoredType loadType(TypeVariableContext context, Type type, boolean nullable, boolean unsigned) {
564
+		StoredType result = loadType(context, type, unsigned);
565
+		if (nullable)
566
+			result = new StoredType(registry.getOptional(result.type), result.getSpecifiedStorage());
567
+		
568
+		return result;
569
+	}
570
+	
571
+	private StoredType loadType(TypeVariableContext context, Type type, boolean unsigned) {
549
 		if (type instanceof Class) {
572
 		if (type instanceof Class) {
550
 			Class<?> classType = (Class<?>) type;
573
 			Class<?> classType = (Class<?>) type;
551
 			if (unsigned) {
574
 			if (unsigned) {

+ 12
- 0
ScriptingExample/src/main/java/org/openzen/zenscript/scriptingexample/Globals.java View File

6
 package org.openzen.zenscript.scriptingexample;
6
 package org.openzen.zenscript.scriptingexample;
7
 
7
 
8
 import org.openzen.zencode.java.ZenCodeGlobals;
8
 import org.openzen.zencode.java.ZenCodeGlobals;
9
+import static org.openzen.zencode.java.ZenCodeType.*;
9
 
10
 
10
 /**
11
 /**
11
  *
12
  *
57
 		System.out.println("FunctionalInt: " + calculator.doSomething(7, 13));
58
 		System.out.println("FunctionalInt: " + calculator.doSomething(7, 13));
58
 	}
59
 	}
59
 	
60
 	
61
+	@Global
62
+	public static void testOptional(MyOptionalInterface function) {
63
+		System.out.println("Null: " + function.doSomething(null));
64
+		System.out.println("Hello: " + function.doSomething("hello"));
65
+	}
66
+	
60
 	public static TestClass bracket(String value) {
67
 	public static TestClass bracket(String value) {
61
 		return new TestClass(value);
68
 		return new TestClass(value);
62
 	}
69
 	}
70
 	public static interface MyFunctionalInterfaceInt {
77
 	public static interface MyFunctionalInterfaceInt {
71
 		int doSomething(int valueA, int valueB);
78
 		int doSomething(int valueA, int valueB);
72
 	}
79
 	}
80
+	
81
+	@FunctionalInterface
82
+	public static interface MyOptionalInterface {
83
+		int doSomething(@Nullable String value);
84
+	}
73
 }
85
 }

+ 1
- 0
Validator/src/main/java/org/openzen/zenscript/validator/visitors/ExpressionValidator.java View File

286
 
286
 
287
 	@Override
287
 	@Override
288
 	public Void visitFunction(FunctionExpression expression) {
288
 	public Void visitFunction(FunctionExpression expression) {
289
+		
289
 		// TODO
290
 		// TODO
290
 		return null;
291
 		return null;
291
 	}
292
 	}

Loading…
Cancel
Save