ソースを参照

Allow the use of default expressions in Optional annotation

kindlich 5年前
コミット
5d6ed900ad
この署名に対応する既知のキーがデータベースに存在しません

+ 3
- 1
JavaAnnotations/src/main/java/org/openzen/zencode/java/ZenCodeType.java ファイルの表示

@@ -97,7 +97,9 @@ public interface ZenCodeType {
97 97
 	
98 98
 	@Retention(RetentionPolicy.RUNTIME)
99 99
 	@Target(ElementType.PARAMETER)
100
-	@interface Optional {}
100
+	@interface Optional {
101
+		String value() default "null";
102
+	}
101 103
 	
102 104
 	@Retention(RetentionPolicy.RUNTIME)
103 105
 	@Target(ElementType.PARAMETER)

+ 113
- 23
JavaIntegration/src/main/java/org/openzen/zencode/java/JavaNativeModule.java ファイルの表示

@@ -5,22 +5,94 @@
5 5
  */
6 6
 package org.openzen.zencode.java;
7 7
 
8
-import org.openzen.zencode.shared.*;
9
-import org.openzen.zenscript.codemodel.*;
10
-import org.openzen.zenscript.codemodel.definition.*;
11
-import org.openzen.zenscript.codemodel.expression.*;
12
-import org.openzen.zenscript.codemodel.generic.*;
13
-import org.openzen.zenscript.codemodel.member.*;
14
-import org.openzen.zenscript.codemodel.member.ref.*;
15
-import org.openzen.zenscript.codemodel.partial.*;
16
-import org.openzen.zenscript.codemodel.type.*;
17
-import org.openzen.zenscript.codemodel.type.member.*;
18
-import org.openzen.zenscript.codemodel.type.storage.*;
19
-import org.openzen.zenscript.javashared.*;
20
-import stdlib.*;
21
-
22
-import java.lang.reflect.*;
23
-import java.util.*;
8
+import java.io.IOException;
9
+import java.lang.reflect.AnnotatedType;
10
+import java.lang.reflect.Field;
11
+import java.lang.reflect.Method;
12
+import java.lang.reflect.Member;
13
+import java.lang.reflect.Modifier;
14
+import java.lang.reflect.Parameter;
15
+import java.lang.reflect.ParameterizedType;
16
+import java.lang.reflect.Type;
17
+import java.lang.reflect.TypeVariable;
18
+import java.util.Collections;
19
+import java.util.HashMap;
20
+import java.util.Map;
21
+import org.openzen.zencode.shared.CodePosition;
22
+import org.openzen.zencode.shared.CompileException;
23
+import org.openzen.zencode.shared.LiteralSourceFile;
24
+import org.openzen.zenscript.codemodel.FunctionHeader;
25
+import org.openzen.zenscript.codemodel.FunctionParameter;
26
+import org.openzen.zenscript.codemodel.GenericMapper;
27
+import org.openzen.zenscript.codemodel.HighLevelDefinition;
28
+import org.openzen.zenscript.codemodel.Modifiers;
29
+import org.openzen.zenscript.codemodel.Module;
30
+import org.openzen.zenscript.codemodel.ModuleSpace;
31
+import org.openzen.zenscript.codemodel.OperatorType;
32
+import org.openzen.zenscript.codemodel.PackageDefinitions;
33
+import org.openzen.zenscript.codemodel.SemanticModule;
34
+import org.openzen.zenscript.codemodel.annotations.AnnotationDefinition;
35
+import org.openzen.zenscript.codemodel.context.CompilingPackage;
36
+import org.openzen.zenscript.codemodel.context.FileResolutionContext;
37
+import org.openzen.zenscript.codemodel.context.ModuleTypeResolutionContext;
38
+import org.openzen.zenscript.codemodel.definition.ClassDefinition;
39
+import org.openzen.zenscript.codemodel.definition.EnumDefinition;
40
+import org.openzen.zenscript.codemodel.definition.InterfaceDefinition;
41
+import org.openzen.zenscript.codemodel.definition.StructDefinition;
42
+import org.openzen.zenscript.codemodel.definition.ZSPackage;
43
+import org.openzen.zenscript.codemodel.expression.ConstantByteExpression;
44
+import org.openzen.zenscript.codemodel.expression.ConstantDoubleExpression;
45
+import org.openzen.zenscript.codemodel.expression.ConstantFloatExpression;
46
+import org.openzen.zenscript.codemodel.expression.ConstantIntExpression;
47
+import org.openzen.zenscript.codemodel.expression.ConstantLongExpression;
48
+import org.openzen.zenscript.codemodel.expression.ConstantSByteExpression;
49
+import org.openzen.zenscript.codemodel.expression.ConstantShortExpression;
50
+import org.openzen.zenscript.codemodel.expression.ConstantStringExpression;
51
+import org.openzen.zenscript.codemodel.expression.ConstantUIntExpression;
52
+import org.openzen.zenscript.codemodel.expression.ConstantULongExpression;
53
+import org.openzen.zenscript.codemodel.expression.ConstantUShortExpression;
54
+import org.openzen.zenscript.codemodel.expression.Expression;
55
+import org.openzen.zenscript.codemodel.expression.ExpressionSymbol;
56
+import org.openzen.zenscript.codemodel.expression.StaticGetterExpression;
57
+import org.openzen.zenscript.codemodel.expression.StorageCastExpression;
58
+import org.openzen.zenscript.codemodel.generic.ParameterTypeBound;
59
+import org.openzen.zenscript.codemodel.generic.TypeParameter;
60
+import org.openzen.zenscript.codemodel.member.CasterMember;
61
+import org.openzen.zenscript.codemodel.member.ConstructorMember;
62
+import org.openzen.zenscript.codemodel.member.FieldMember;
63
+import org.openzen.zenscript.codemodel.member.GetterMember;
64
+import org.openzen.zenscript.codemodel.member.ImplementationMember;
65
+import org.openzen.zenscript.codemodel.member.MethodMember;
66
+import org.openzen.zenscript.codemodel.member.OperatorMember;
67
+import org.openzen.zenscript.codemodel.member.SetterMember;
68
+import org.openzen.zenscript.codemodel.member.ref.FunctionalMemberRef;
69
+import org.openzen.zenscript.codemodel.partial.PartialStaticMemberGroupExpression;
70
+import org.openzen.zenscript.codemodel.scope.ExpressionScope;
71
+import org.openzen.zenscript.codemodel.scope.FileScope;
72
+import org.openzen.zenscript.codemodel.type.BasicTypeID;
73
+import org.openzen.zenscript.codemodel.type.GlobalTypeRegistry;
74
+import org.openzen.zenscript.codemodel.type.ISymbol;
75
+import org.openzen.zenscript.codemodel.type.StoredType;
76
+import org.openzen.zenscript.codemodel.type.StringTypeID;
77
+import org.openzen.zenscript.codemodel.type.TypeID;
78
+import org.openzen.zenscript.codemodel.type.member.BuiltinID;
79
+import org.openzen.zenscript.codemodel.type.member.TypeMembers;
80
+import org.openzen.zenscript.codemodel.type.storage.AutoStorageTag;
81
+import org.openzen.zenscript.codemodel.type.storage.StaticStorageTag;
82
+import org.openzen.zenscript.codemodel.type.storage.StorageTag;
83
+import org.openzen.zenscript.codemodel.type.storage.StorageType;
84
+import org.openzen.zenscript.javashared.JavaClass;
85
+import org.openzen.zenscript.javashared.JavaCompiledModule;
86
+import org.openzen.zenscript.javashared.JavaField;
87
+import org.openzen.zenscript.javashared.JavaFunctionalInterfaceStorageTag;
88
+import org.openzen.zenscript.javashared.JavaImplementation;
89
+import org.openzen.zenscript.javashared.JavaMethod;
90
+import org.openzen.zenscript.javashared.JavaModifiers;
91
+import org.openzen.zenscript.lexer.ParseException;
92
+import org.openzen.zenscript.lexer.ZSTokenParser;
93
+import org.openzen.zenscript.parser.BracketExpressionParser;
94
+import org.openzen.zenscript.parser.expression.ParsedExpression;
95
+import stdlib.Strings;
24 96
 
25 97
 /**
26 98
  * @author Stan Hebben
@@ -38,7 +110,8 @@ public class JavaNativeModule {
38 110
 	private final Map<Class<?>, TypeID> unsignedByClass = new HashMap<>();
39 111
 	
40 112
 	public final Map<String, ISymbol> globals = new HashMap<>();
41
-	
113
+	private BracketExpressionParser bep;
114
+
42 115
 	public JavaNativeModule(
43 116
 			ZSPackage pkg,
44 117
 			String name,
@@ -480,12 +553,25 @@ public class JavaNativeModule {
480 553
 				method.getAnnotatedExceptionTypes());
481 554
 	}
482 555
 	
483
-	private Expression getDefaultValue(Parameter parameter, StoredType type) {
556
+	protected Expression getDefaultValue(Parameter parameter, StoredType type) {
484 557
 		if (parameter.isAnnotationPresent(ZenCodeType.Optional.class)) {
485
-			Expression defaultValue = type.type.getDefaultValue();
486
-			if (defaultValue == null)
487
-			    defaultValue = new NullExpression(CodePosition.NATIVE);
488
-			return defaultValue;
558
+				final String s = parameter.getAnnotation(ZenCodeType.Optional.class).value();
559
+				try {
560
+					final String filename = "internal: " + parameter.getDeclaringExecutable().getName();
561
+
562
+					final CompilingPackage rootCompiling = new CompilingPackage(pkg, module);
563
+					final ModuleTypeResolutionContext context = new ModuleTypeResolutionContext(registry, new AnnotationDefinition[0], StorageType.getStandard(), pkg, rootCompiling, globals);
564
+					final FileResolutionContext fContext = new FileResolutionContext(context, pkg, rootCompiling);
565
+					final FileScope fileScope = new FileScope(fContext, Collections.emptyList(), globals, member -> {});
566
+					final ZSTokenParser tokens = ZSTokenParser.create(new LiteralSourceFile(filename, s), bep, 4);
567
+
568
+					return ParsedExpression.parse(tokens).compile(new ExpressionScope(fileScope)).eval().castExplicit(CodePosition.GENERATED, fileScope, type, type.isOptional());
569
+				} catch (IOException | ParseException | CompileException ex) {
570
+					//TODO REMOVE
571
+					ex.printStackTrace();
572
+					return null;
573
+				}
574
+			//}
489 575
 		} else if (parameter.isAnnotationPresent(ZenCodeType.OptionalInt.class)) {
490 576
 			ZenCodeType.OptionalInt annotation = parameter.getAnnotation(ZenCodeType.OptionalInt.class);
491 577
 			if (type.type == BasicTypeID.BYTE)
@@ -747,7 +833,11 @@ public class JavaNativeModule {
747 833
 		
748 834
 		return new JavaMethod(cls, kind, method.getName(), false, getMethodDescriptor(method), method.getModifiers(), result.isGeneric());
749 835
 	}
750
-	
836
+
837
+	public void registerBEP(BracketExpressionParser bep) {
838
+		this.bep = bep;
839
+	}
840
+
751 841
 	private static class TypeVariableContext {
752 842
 		private final Map<TypeVariable, TypeParameter> typeVariables = new HashMap<>();
753 843
 		

読み込み中…
キャンセル
保存