소스 검색

More work on expansions, casters are done

Jared 5 년 전
부모
커밋
6f6b96ddb5
No account linked to committer's email address
1개의 변경된 파일87개의 추가작업 그리고 35개의 파일을 삭제
  1. 87
    35
      JavaIntegration/src/main/java/org/openzen/zencode/java/JavaNativeModule.java

+ 87
- 35
JavaIntegration/src/main/java/org/openzen/zencode/java/JavaNativeModule.java 파일 보기

@@ -5,19 +5,6 @@
5 5
  */
6 6
 package org.openzen.zencode.java;
7 7
 
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 8
 import org.openzen.zencode.shared.CodePosition;
22 9
 import org.openzen.zencode.shared.CompileException;
23 10
 import org.openzen.zencode.shared.LiteralSourceFile;
@@ -35,7 +22,12 @@ import org.openzen.zenscript.codemodel.annotations.AnnotationDefinition;
35 22
 import org.openzen.zenscript.codemodel.context.CompilingPackage;
36 23
 import org.openzen.zenscript.codemodel.context.FileResolutionContext;
37 24
 import org.openzen.zenscript.codemodel.context.ModuleTypeResolutionContext;
38
-import org.openzen.zenscript.codemodel.definition.*;
25
+import org.openzen.zenscript.codemodel.definition.ClassDefinition;
26
+import org.openzen.zenscript.codemodel.definition.EnumDefinition;
27
+import org.openzen.zenscript.codemodel.definition.ExpansionDefinition;
28
+import org.openzen.zenscript.codemodel.definition.InterfaceDefinition;
29
+import org.openzen.zenscript.codemodel.definition.StructDefinition;
30
+import org.openzen.zenscript.codemodel.definition.ZSPackage;
39 31
 import org.openzen.zenscript.codemodel.expression.ConstantByteExpression;
40 32
 import org.openzen.zenscript.codemodel.expression.ConstantDoubleExpression;
41 33
 import org.openzen.zenscript.codemodel.expression.ConstantFloatExpression;
@@ -53,7 +45,14 @@ import org.openzen.zenscript.codemodel.expression.StaticGetterExpression;
53 45
 import org.openzen.zenscript.codemodel.expression.StorageCastExpression;
54 46
 import org.openzen.zenscript.codemodel.generic.ParameterTypeBound;
55 47
 import org.openzen.zenscript.codemodel.generic.TypeParameter;
56
-import org.openzen.zenscript.codemodel.member.*;
48
+import org.openzen.zenscript.codemodel.member.CasterMember;
49
+import org.openzen.zenscript.codemodel.member.ConstructorMember;
50
+import org.openzen.zenscript.codemodel.member.FieldMember;
51
+import org.openzen.zenscript.codemodel.member.GetterMember;
52
+import org.openzen.zenscript.codemodel.member.ImplementationMember;
53
+import org.openzen.zenscript.codemodel.member.MethodMember;
54
+import org.openzen.zenscript.codemodel.member.OperatorMember;
55
+import org.openzen.zenscript.codemodel.member.SetterMember;
57 56
 import org.openzen.zenscript.codemodel.member.ref.FunctionalMemberRef;
58 57
 import org.openzen.zenscript.codemodel.partial.PartialStaticMemberGroupExpression;
59 58
 import org.openzen.zenscript.codemodel.scope.ExpressionScope;
@@ -83,6 +82,20 @@ import org.openzen.zenscript.parser.BracketExpressionParser;
83 82
 import org.openzen.zenscript.parser.expression.ParsedExpression;
84 83
 import stdlib.Strings;
85 84
 
85
+import java.io.IOException;
86
+import java.lang.reflect.AnnotatedType;
87
+import java.lang.reflect.Field;
88
+import java.lang.reflect.Member;
89
+import java.lang.reflect.Method;
90
+import java.lang.reflect.Modifier;
91
+import java.lang.reflect.Parameter;
92
+import java.lang.reflect.ParameterizedType;
93
+import java.lang.reflect.Type;
94
+import java.lang.reflect.TypeVariable;
95
+import java.util.Collections;
96
+import java.util.HashMap;
97
+import java.util.Map;
98
+
86 99
 /**
87 100
  * @author Stan Hebben
88 101
  */
@@ -443,32 +456,66 @@ public class JavaNativeModule {
443 456
 		expansion.target = expandedType.stored();
444 457
 		definitionByClass.put(cls, expansion);
445 458
 
446
-
459
+        boolean addExpansion = false;
447 460
 		for (Method method : cls.getDeclaredMethods()) {
448 461
 			if(!Modifier.isStatic(method.getModifiers()) || method.getParameterCount() < 1) {
449 462
 				//Log?
450 463
 				continue;
451 464
 			}
452 465
 
453
-			if(!method.isAnnotationPresent(ZenCodeType.Method.class))
454
-				continue;
455
-
456
-			final ZenCodeType.Method annotation = method.getAnnotation(ZenCodeType.Method.class);
457
-			String name = !annotation.value().isEmpty() ? annotation.value() : method.getName();
458
-
459
-			TypeVariableContext context = new TypeVariableContext();
460
-
461
-			final Parameter[] parameters = new Parameter[method.getParameterCount() - 1];
462
-			System.arraycopy(method.getParameters(), 1, parameters, 0, method.getParameterCount() - 1);
463
-
464
-			FunctionHeader header = getHeader(context, method.getAnnotatedReturnType(), parameters, method.getTypeParameters(), method.getAnnotatedExceptionTypes());
465
-			final MethodMember member = new MethodMember(CodePosition.NATIVE, expansion, getMethodModifiers(method) ^ Modifiers.STATIC, name, header, null);
466
-
467
-			expansion.addMember(member);
468
-			compiled.setMethodInfo(member, JavaMethod.getStatic(javaClass, name, getMethodDescriptor(method), getMethodModifiers(method)));
469
-		}
470
-
471
-		if(!expansion.members.isEmpty()) {
466
+//			if(!method.isAnnotationPresent(ZenCodeType.Method.class))
467
+//				continue;
468
+            
469
+            final ZenCodeType.Method methodAnnotation = method.getAnnotation(ZenCodeType.Method.class);
470
+            if(methodAnnotation != null) {
471
+                String name = !methodAnnotation.value().isEmpty() ? methodAnnotation.value() : method.getName();
472
+                
473
+                TypeVariableContext context = new TypeVariableContext();
474
+                
475
+                final Parameter[] parameters = getExpansionParameters(method);
476
+                
477
+                FunctionHeader header = getHeader(context, method.getAnnotatedReturnType(), parameters, method.getTypeParameters(), method.getAnnotatedExceptionTypes());
478
+                final MethodMember member = new MethodMember(CodePosition.NATIVE, expansion, getMethodModifiers(method) ^ Modifiers.STATIC, name, header, null);
479
+                
480
+                expansion.addMember(member);
481
+                compiled.setMethodInfo(member, JavaMethod.getStatic(javaClass, name, getMethodDescriptor(method), getMethodModifiers(method)));
482
+                addExpansion = true;
483
+            }
484
+            
485
+            final ZenCodeType.Caster casterAnnotation = method.getAnnotation(ZenCodeType.Caster.class);
486
+            if(casterAnnotation != null) {
487
+                boolean implicit = casterAnnotation.implicit();
488
+                int modifiers = getMethodModifiers(method) ^ Modifiers.STATIC;
489
+                if (implicit) {
490
+                    modifiers |= Modifiers.IMPLICIT;
491
+                }
492
+                TypeVariableContext context = new TypeVariableContext();
493
+                StoredType toType = loadStoredType(context, method.getAnnotatedReturnType());
494
+                final CasterMember member = new CasterMember(CodePosition.NATIVE, expansion, modifiers, toType,  null);
495
+                
496
+                expansion.addMember(member);
497
+                compiled.setMethodInfo(member, getMethod(javaClass, method, member.toType));
498
+                addExpansion = true;
499
+            }
500
+            
501
+            //TODO not working, not sure if it *should* work
502
+//            final ZenCodeType.Operator operatorAnnotation = method.getAnnotation(ZenCodeType.Operator.class);
503
+//            if(operatorAnnotation != null) {
504
+//
505
+//                TypeVariableContext context = new TypeVariableContext();
506
+//
507
+//                final Parameter[] parameters = getExpansionParameters(method);
508
+//
509
+//                FunctionHeader header = getHeader(context, method.getAnnotatedReturnType(), parameters, method.getTypeParameters(), method.getAnnotatedExceptionTypes());
510
+//                final OperatorMember member = new OperatorMember(CodePosition.NATIVE, expansion, getMethodModifiers(method) ^ Modifiers.STATIC, OperatorType.valueOf(operatorAnnotation.value().toString()), header, null);
511
+//
512
+//                expansion.addMember(member);
513
+//                compiled.setMethodInfo(member, getMethod(javaClass, method, member.header.getReturnType()));
514
+//                addExpansion = true;
515
+//            }
516
+        }
517
+
518
+		if(addExpansion) {
472 519
 			compiled.setExpansionClassInfo(expansion, javaClass);
473 520
 			definitions.add(expansion);
474 521
 		}
@@ -476,6 +523,11 @@ public class JavaNativeModule {
476 523
 		return expansion;
477 524
 	}
478 525
 
526
+	private Parameter[] getExpansionParameters(Method method){
527
+        final Parameter[] parameters = new Parameter[method.getParameterCount() - 1];
528
+        System.arraycopy(method.getParameters(), 1, parameters, 0, method.getParameterCount() - 1);
529
+        return parameters;
530
+    }
479 531
 
480 532
 	private TypeID getTypeFromName(String className) {
481 533
 		for (TypeID value : this.typeByClass.values()) {

Loading…
취소
저장