Преглед изворни кода

Added Storage tag annotation

kindlich пре 5 година
родитељ
комит
4a078a08ae
No known key found for this signature in database

+ 5
- 0
JavaAnnotations/src/main/java/org/openzen/zencode/java/StorageTagType.java Прегледај датотеку

@@ -0,0 +1,5 @@
1
+package org.openzen.zencode.java;
2
+
3
+public enum StorageTagType {
4
+    STATIC
5
+}

+ 11
- 0
JavaAnnotations/src/main/java/org/openzen/zencode/java/ZenCodeStorageTag.java Прегледај датотеку

@@ -0,0 +1,11 @@
1
+package org.openzen.zencode.java;
2
+
3
+import java.lang.annotation.*;
4
+
5
+@Retention(RetentionPolicy.RUNTIME)
6
+@Target(ElementType.TYPE_USE)
7
+public @interface ZenCodeStorageTag {
8
+    
9
+    StorageTagType value();
10
+    
11
+}

+ 56
- 31
JavaIntegration/src/main/java/org/openzen/zencode/java/JavaNativeModule.java Прегледај датотеку

@@ -56,25 +56,16 @@ import org.openzen.zenscript.codemodel.type.storage.StaticStorageTag;
56 56
 import org.openzen.zenscript.codemodel.type.storage.StorageTag;
57 57
 import org.openzen.zenscript.codemodel.type.storage.StorageType;
58 58
 import org.openzen.zenscript.javashared.*;
59
-import org.openzen.zenscript.lexer.ParseException;
60
-import org.openzen.zenscript.lexer.ZSTokenParser;
59
+import org.openzen.zenscript.lexer.*;
61 60
 import org.openzen.zenscript.parser.BracketExpressionParser;
62 61
 import org.openzen.zenscript.parser.expression.ParsedExpression;
63 62
 import org.openzen.zenscript.parser.type.IParsedType;
64 63
 import stdlib.Strings;
65 64
 
66 65
 import java.io.IOException;
67
-import java.lang.reflect.AnnotatedType;
68
-import java.lang.reflect.Field;
69
-import java.lang.reflect.Member;
70
-import java.lang.reflect.Method;
71
-import java.lang.reflect.Modifier;
72
-import java.lang.reflect.Parameter;
73
-import java.lang.reflect.ParameterizedType;
74
-import java.lang.reflect.Type;
75
-import java.lang.reflect.TypeVariable;
66
+import java.lang.reflect.*;
76 67
 import java.util.*;
77
-import java.util.stream.Collectors;
68
+
78 69
 
79 70
 /**
80 71
  * @author Stan Hebben
@@ -834,11 +825,10 @@ public class JavaNativeModule {
834 825
 		boolean nullable = annotatedType.isAnnotationPresent(ZenCodeType.Nullable.class);
835 826
 		boolean unsigned = annotatedType.isAnnotationPresent(ZenCodeType.Unsigned.class);
836 827
 		
837
-		Type type = annotatedType.getType();
838
-		return loadType(context, type, nullable, unsigned);
828
+		return loadType(context, annotatedType, nullable, unsigned);
839 829
 	}
840 830
 	
841
-	private StoredType loadType(TypeVariableContext context, Type type, boolean nullable, boolean unsigned) {
831
+	private StoredType loadType(TypeVariableContext context, AnnotatedElement type, boolean nullable, boolean unsigned) {
842 832
 		StoredType result = loadType(context, type, unsigned);
843 833
 		if (nullable)
844 834
 			result = new StoredType(registry.getOptional(result.type), result.getSpecifiedStorage());
@@ -846,18 +836,20 @@ public class JavaNativeModule {
846 836
 		return result;
847 837
 	}
848 838
 	
849
-	private StoredType loadType(TypeVariableContext context, Type type, boolean unsigned) {
839
+	@SuppressWarnings("ChainOfInstanceofChecks")
840
+    private StoredType loadType(TypeVariableContext context, AnnotatedElement type, boolean unsigned) {
850 841
 		if (type instanceof Class) {
851
-			Class<?> classType = (Class<?>) type;
842
+            Class<?> classType = (Class<?>) type;
852 843
 			if (unsigned) {
853
-				if (unsignedByClass.containsKey(classType))
854
-					return unsignedByClass.get(classType).stored();
855
-				else
856
-					throw new IllegalArgumentException("This class cannot be used as unsigned: " + classType);
844
+				if (unsignedByClass.containsKey(classType)) {
845
+                    return unsignedByClass.get(classType).stored();
846
+                } else {
847
+                    throw new IllegalArgumentException("This class cannot be used as unsigned: " + classType);
848
+                }
857 849
 			} else if (classType.isArray()) {
858 850
 				return registry.getArray(loadType(context, classType.getComponentType(), false, false), 1).stored();
859 851
 			} else if (classType.isAnnotationPresent(FunctionalInterface.class)) {
860
-				return loadFunctionalInterface(context, classType, new Type[0]);
852
+				return loadFunctionalInterface(context, classType, new AnnotatedElement[0]);
861 853
 			}
862 854
 			
863 855
 			if (typeByClass.containsKey(classType))
@@ -869,24 +861,57 @@ public class JavaNativeModule {
869 861
 			ParameterizedType parameterizedType = (ParameterizedType) type;
870 862
 			Class<?> rawType = (Class) parameterizedType.getRawType();
871 863
 			if (rawType.isAnnotationPresent(FunctionalInterface.class))
872
-				return loadFunctionalInterface(context, rawType, parameterizedType.getActualTypeArguments());
864
+				return loadFunctionalInterface(context, rawType, (AnnotatedElement[]) parameterizedType.getActualTypeArguments());
873 865
 			
874
-			HighLevelDefinition definition = addClass(rawType);
875 866
 			Type[] parameters = parameterizedType.getActualTypeArguments();
876 867
 			StoredType[] codeParameters = new StoredType[parameters.length];
877 868
 			for (int i = 0; i < parameters.length; i++)
878
-				codeParameters[i] = loadType(context, parameters[i], false, false);
879
-			
880
-			return registry.getForDefinition(definition, codeParameters).stored();
869
+			    codeParameters[i] = loadType(context, (AnnotatedElement) parameters[i], false, false);
870
+       
871
+			if(rawType == Map.class) {
872
+                return registry.getAssociative(codeParameters[0], codeParameters[1]).stored();
873
+            }
874
+            
875
+            HighLevelDefinition definition = addClass(rawType);
876
+            return registry.getForDefinition(definition, codeParameters).stored();
881 877
 		} else if (type instanceof TypeVariable) {
882
-			TypeVariable variable = (TypeVariable)type;
883
-			return registry.getGeneric(context.get(variable)).stored();
884
-		} else {
878
+            TypeVariable variable = (TypeVariable) type;
879
+            return registry.getGeneric(context.get(variable)).stored();
880
+        }else if(type instanceof AnnotatedType){
881
+		    final TypeID baseType;
882
+		    if(type instanceof AnnotatedParameterizedType) {
883
+                AnnotatedParameterizedType parameterizedType = (AnnotatedParameterizedType) type;
884
+                final Type rawType = ((ParameterizedType) parameterizedType.getType()).getRawType();
885
+                final AnnotatedType[] actualTypeArguments = parameterizedType.getAnnotatedActualTypeArguments();
886
+                final StoredType[] codeParameters = new StoredType[actualTypeArguments.length];
887
+                for(int i = 0; i < actualTypeArguments.length; i++) {
888
+                    codeParameters[i] = loadType(context, actualTypeArguments[i], false, false);
889
+                }
890
+            
891
+                if(rawType == Map.class) {
892
+                    baseType = registry.getAssociative(codeParameters[0], codeParameters[1]);
893
+                } else {
894
+                    HighLevelDefinition definition = addClass((Class<?>) rawType);
895
+                    baseType = registry.getForDefinition(definition, codeParameters);
896
+                }
897
+            } else {
898
+		        baseType = loadType(context, (AnnotatedElement) ((AnnotatedType) type).getType(), unsigned).type;
899
+            }
900
+            
901
+		    if(type.isAnnotationPresent(ZenCodeStorageTag.class)) {
902
+		        //Replace with switch if more StorageTagTypes are added
903
+                if(type.getAnnotation(ZenCodeStorageTag.class).value() == StorageTagType.STATIC) {
904
+                    return baseType.stored(StaticStorageTag.INSTANCE);
905
+                }
906
+            }
907
+		    return baseType.stored();
908
+		    
909
+        } else {
885 910
 			throw new IllegalArgumentException("Could not analyze type: " + type);
886 911
 		}
887 912
 	}
888 913
 	
889
-	private StoredType loadFunctionalInterface(TypeVariableContext loadContext, Class<?> cls, Type[] parameters) {
914
+	private StoredType loadFunctionalInterface(TypeVariableContext loadContext, Class<?> cls, AnnotatedElement[] parameters) {
890 915
 		Method functionalInterfaceMethod = getFunctionalInterfaceMethod(cls);
891 916
 		TypeVariableContext context = convertTypeParameters(cls);
892 917
 		FunctionHeader header = getHeader(context, functionalInterfaceMethod);

Loading…
Откажи
Сачувај