Browse Source

Allow generic types in Java Signatures

Provide them as class parameters
kindlich 4 years ago
parent
commit
8ff353bcd1
No known key found for this signature in database

+ 11
- 4
JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/compiler/JavaExpressionVisitor.java View File

405
 			JavaMethod methodInfo = context.getJavaMethod(expression.member);
405
 			JavaMethod methodInfo = context.getJavaMethod(expression.member);
406
 
406
 
407
 			if(methodInfo.compile) {
407
 			if(methodInfo.compile) {
408
-				for (TypeParameter typeParameter : typeParameters) {
409
-					javaWriter.aConstNull(); // TODO: Replace with actual class
410
-					javaWriter.checkCast("java/lang/Class");
411
-				}
408
+			    if(expression.member.getHeader().typeParameters.length == expression.arguments.typeArguments.length) {
409
+                    final JavaTypeExpressionVisitor javaTypeExpressionVisitor = new JavaTypeExpressionVisitor(context);
410
+                    for(StoredType typeArgument : expression.arguments.typeArguments) {
411
+                        typeArgument.type.accept(javaWriter, javaTypeExpressionVisitor);
412
+                    }
413
+                } else {
414
+                    for(TypeParameter typeParameter : typeParameters) {
415
+                        javaWriter.aConstNull(); // TODO: Replace with actual class
416
+                        javaWriter.checkCast("java/lang/Class");
417
+                    }
418
+                }
412
 			}
419
 			}
413
 
420
 
414
 			final Expression[] arguments = expression.arguments.arguments;
421
 			final Expression[] arguments = expression.arguments.arguments;

+ 1
- 1
JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/compiler/JavaWriter.java View File

196
 	}
196
 	}
197
 	
197
 	
198
 	public void constantClass(JavaClass cls) {
198
 	public void constantClass(JavaClass cls) {
199
-		visitor.visitLdcInsn(Type.getType(cls.internalName));
199
+		visitor.visitLdcInsn(Type.getObjectType(cls.internalName));
200
 	}
200
 	}
201
 	
201
 	
202
 	public void pop() {
202
 	public void pop() {

+ 32
- 31
JavaIntegration/src/main/java/org/openzen/zencode/java/JavaNativeModule.java View File

5
  */
5
  */
6
 package org.openzen.zencode.java;
6
 package org.openzen.zencode.java;
7
 
7
 
8
-import org.openzen.zencode.shared.CodePosition;
9
-import org.openzen.zencode.shared.CompileException;
10
-import org.openzen.zencode.shared.LiteralSourceFile;
8
+import org.openzen.zencode.shared.*;
11
 import org.openzen.zenscript.codemodel.*;
9
 import org.openzen.zenscript.codemodel.*;
12
-import org.openzen.zenscript.codemodel.annotations.AnnotationDefinition;
13
-import org.openzen.zenscript.codemodel.annotations.DefinitionAnnotation;
14
-import org.openzen.zenscript.codemodel.annotations.NativeDefinitionAnnotation;
15
-import org.openzen.zenscript.codemodel.context.CompilingPackage;
16
-import org.openzen.zenscript.codemodel.context.FileResolutionContext;
17
-import org.openzen.zenscript.codemodel.context.ModuleTypeResolutionContext;
10
+import org.openzen.zenscript.codemodel.annotations.*;
11
+import org.openzen.zenscript.codemodel.context.*;
18
 import org.openzen.zenscript.codemodel.definition.*;
12
 import org.openzen.zenscript.codemodel.definition.*;
19
 import org.openzen.zenscript.codemodel.expression.*;
13
 import org.openzen.zenscript.codemodel.expression.*;
20
-import org.openzen.zenscript.codemodel.generic.ParameterTypeBound;
21
-import org.openzen.zenscript.codemodel.generic.TypeParameter;
14
+import org.openzen.zenscript.codemodel.generic.*;
22
 import org.openzen.zenscript.codemodel.member.*;
15
 import org.openzen.zenscript.codemodel.member.*;
23
-import org.openzen.zenscript.codemodel.member.ref.FunctionalMemberRef;
24
-import org.openzen.zenscript.codemodel.partial.PartialStaticMemberGroupExpression;
25
-import org.openzen.zenscript.codemodel.scope.ExpressionScope;
26
-import org.openzen.zenscript.codemodel.scope.FileScope;
16
+import org.openzen.zenscript.codemodel.member.ref.*;
17
+import org.openzen.zenscript.codemodel.partial.*;
18
+import org.openzen.zenscript.codemodel.scope.*;
27
 import org.openzen.zenscript.codemodel.type.*;
19
 import org.openzen.zenscript.codemodel.type.*;
28
-import org.openzen.zenscript.codemodel.type.member.BuiltinID;
29
-import org.openzen.zenscript.codemodel.type.member.TypeMembers;
30
-import org.openzen.zenscript.codemodel.type.storage.AutoStorageTag;
31
-import org.openzen.zenscript.codemodel.type.storage.StaticStorageTag;
32
-import org.openzen.zenscript.codemodel.type.storage.StorageTag;
33
-import org.openzen.zenscript.codemodel.type.storage.StorageType;
20
+import org.openzen.zenscript.codemodel.type.member.*;
21
+import org.openzen.zenscript.codemodel.type.storage.*;
34
 import org.openzen.zenscript.javashared.*;
22
 import org.openzen.zenscript.javashared.*;
35
-import org.openzen.zenscript.lexer.ParseException;
36
-import org.openzen.zenscript.lexer.ZSTokenParser;
37
-import org.openzen.zenscript.parser.BracketExpressionParser;
38
-import org.openzen.zenscript.parser.expression.ParsedExpression;
39
-import org.openzen.zenscript.parser.type.IParsedType;
40
-import stdlib.Strings;
41
-
42
-import java.io.IOException;
23
+import org.openzen.zenscript.lexer.*;
24
+import org.openzen.zenscript.parser.*;
25
+import org.openzen.zenscript.parser.expression.*;
26
+import org.openzen.zenscript.parser.type.*;
27
+import stdlib.*;
28
+
29
+import java.io.*;
43
 import java.lang.reflect.*;
30
 import java.lang.reflect.*;
31
+import java.util.Arrays;
44
 import java.util.*;
32
 import java.util.*;
45
 
33
 
46
 
34
 
852
 		}
840
 		}
853
 
841
 
854
 		FunctionParameter[] parameters = new FunctionParameter[javaParameters.length];
842
 		FunctionParameter[] parameters = new FunctionParameter[javaParameters.length];
843
+		int classParameters = 0;
855
 		for (int i = 0; i < parameters.length; i++) {
844
 		for (int i = 0; i < parameters.length; i++) {
856
 			Parameter parameter = javaParameters[i];
845
 			Parameter parameter = javaParameters[i];
846
+			if(parameter.getType().getCanonicalName().contentEquals("java.lang.Class")) {
847
+			    classParameters++;
848
+            }
857
 
849
 
858
 			//AnnotatedType parameterType = parameter.getAnnotatedType();
850
 			//AnnotatedType parameterType = parameter.getAnnotatedType();
859
  			StoredType type = loadStoredType(context, parameter);
851
  			StoredType type = loadStoredType(context, parameter);
860
 			Expression defaultValue = getDefaultValue(parameter, type);
852
 			Expression defaultValue = getDefaultValue(parameter, type);
861
 			parameters[i] = new FunctionParameter(type, parameter.getName(), defaultValue, parameter.isVarArgs());
853
 			parameters[i] = new FunctionParameter(type, parameter.getName(), defaultValue, parameter.isVarArgs());
862
 		}
854
 		}
855
+		if(classParameters > 0 && classParameters == typeParameters.length) {
856
+		    parameters = Arrays.copyOfRange(parameters, classParameters, parameters.length);
857
+        }
863
 
858
 
864
 		if (exceptionTypes.length > 1)
859
 		if (exceptionTypes.length > 1)
865
 			throw new IllegalArgumentException("A method can only throw a single exception type!");
860
 			throw new IllegalArgumentException("A method can only throw a single exception type!");
1110
 			kind = JavaMethod.Kind.STATIC;
1105
 			kind = JavaMethod.Kind.STATIC;
1111
 		else
1106
 		else
1112
 			kind = JavaMethod.Kind.INSTANCE;
1107
 			kind = JavaMethod.Kind.INSTANCE;
1113
-
1114
-		return new JavaMethod(cls, kind, method.getName(), false, getMethodDescriptor(method), method.getModifiers(), result.isGeneric());
1108
+        
1109
+        final int length = method.getTypeParameters().length;
1110
+        boolean compile = length > 0 && length == Arrays.stream(method.getParameterTypes())
1111
+                .filter(s -> s.getCanonicalName().contentEquals("java.lang.Class"))
1112
+                .count();
1113
+        
1114
+        return new JavaMethod(cls, kind, method.getName(), compile, getMethodDescriptor(method), method
1115
+                .getModifiers(), result.isGeneric());
1115
 	}
1116
 	}
1116
 
1117
 
1117
 	public void registerBEP(BracketExpressionParser bep) {
1118
 	public void registerBEP(BracketExpressionParser bep) {

Loading…
Cancel
Save