|
@@ -5,7 +5,15 @@
|
5
|
5
|
*/
|
6
|
6
|
package org.openzen.zencode.java;
|
7
|
7
|
|
8
|
|
-import java.lang.reflect.*;
|
|
8
|
+import java.lang.reflect.AnnotatedType;
|
|
9
|
+import java.lang.reflect.Field;
|
|
10
|
+import java.lang.reflect.Method;
|
|
11
|
+import java.lang.reflect.Member;
|
|
12
|
+import java.lang.reflect.Modifier;
|
|
13
|
+import java.lang.reflect.Parameter;
|
|
14
|
+import java.lang.reflect.ParameterizedType;
|
|
15
|
+import java.lang.reflect.Type;
|
|
16
|
+import java.lang.reflect.TypeVariable;
|
9
|
17
|
import java.util.Collections;
|
10
|
18
|
import java.util.HashMap;
|
11
|
19
|
import java.util.Map;
|
|
@@ -25,8 +33,21 @@ import org.openzen.zenscript.codemodel.definition.EnumDefinition;
|
25
|
33
|
import org.openzen.zenscript.codemodel.definition.InterfaceDefinition;
|
26
|
34
|
import org.openzen.zenscript.codemodel.definition.StructDefinition;
|
27
|
35
|
import org.openzen.zenscript.codemodel.definition.ZSPackage;
|
|
36
|
+import org.openzen.zenscript.codemodel.expression.ConstantByteExpression;
|
|
37
|
+import org.openzen.zenscript.codemodel.expression.ConstantDoubleExpression;
|
|
38
|
+import org.openzen.zenscript.codemodel.expression.ConstantFloatExpression;
|
|
39
|
+import org.openzen.zenscript.codemodel.expression.ConstantIntExpression;
|
|
40
|
+import org.openzen.zenscript.codemodel.expression.ConstantLongExpression;
|
|
41
|
+import org.openzen.zenscript.codemodel.expression.ConstantSByteExpression;
|
|
42
|
+import org.openzen.zenscript.codemodel.expression.ConstantShortExpression;
|
|
43
|
+import org.openzen.zenscript.codemodel.expression.ConstantStringExpression;
|
|
44
|
+import org.openzen.zenscript.codemodel.expression.ConstantUIntExpression;
|
|
45
|
+import org.openzen.zenscript.codemodel.expression.ConstantULongExpression;
|
|
46
|
+import org.openzen.zenscript.codemodel.expression.ConstantUShortExpression;
|
|
47
|
+import org.openzen.zenscript.codemodel.expression.Expression;
|
28
|
48
|
import org.openzen.zenscript.codemodel.expression.ExpressionSymbol;
|
29
|
49
|
import org.openzen.zenscript.codemodel.expression.StaticGetterExpression;
|
|
50
|
+import org.openzen.zenscript.codemodel.expression.StorageCastExpression;
|
30
|
51
|
import org.openzen.zenscript.codemodel.generic.ParameterTypeBound;
|
31
|
52
|
import org.openzen.zenscript.codemodel.generic.TypeParameter;
|
32
|
53
|
import org.openzen.zenscript.codemodel.member.CasterMember;
|
|
@@ -48,8 +69,8 @@ import org.openzen.zenscript.codemodel.type.TypeID;
|
48
|
69
|
import org.openzen.zenscript.codemodel.type.member.BuiltinID;
|
49
|
70
|
import org.openzen.zenscript.codemodel.type.member.TypeMembers;
|
50
|
71
|
import org.openzen.zenscript.codemodel.type.storage.AutoStorageTag;
|
|
72
|
+import org.openzen.zenscript.codemodel.type.storage.StaticStorageTag;
|
51
|
73
|
import org.openzen.zenscript.codemodel.type.storage.StorageTag;
|
52
|
|
-import org.openzen.zenscript.javabytecode.compiler.CompilerUtils;
|
53
|
74
|
import org.openzen.zenscript.javashared.JavaClass;
|
54
|
75
|
import org.openzen.zenscript.javashared.JavaCompiledModule;
|
55
|
76
|
import org.openzen.zenscript.javashared.JavaField;
|
|
@@ -319,7 +340,6 @@ public class JavaNativeModule {
|
319
|
340
|
final String fieldName = annotation.value().isEmpty() ? field.getName() : annotation.value();
|
320
|
341
|
|
321
|
342
|
StoredType fieldType = loadStoredType(context, field.getAnnotatedType());
|
322
|
|
-
|
323
|
343
|
FieldMember member = new FieldMember(CodePosition.NATIVE, definition, getMethodModifiers(field), fieldName, thisType, fieldType, registry, 0, 0, null);
|
324
|
344
|
definition.addMember(member);
|
325
|
345
|
compiled.setFieldInfo(member, new JavaField(javaClass, field.getName(), getDescriptor(field.getType())));
|
|
@@ -498,7 +518,7 @@ public class JavaNativeModule {
|
498
|
518
|
return getHeader(
|
499
|
519
|
context,
|
500
|
520
|
null,
|
501
|
|
- constructor.getAnnotatedParameterTypes(),
|
|
521
|
+ constructor.getParameters(),
|
502
|
522
|
constructor.getTypeParameters(),
|
503
|
523
|
constructor.getAnnotatedExceptionTypes());
|
504
|
524
|
}
|
|
@@ -507,22 +527,84 @@ public class JavaNativeModule {
|
507
|
527
|
return getHeader(
|
508
|
528
|
context,
|
509
|
529
|
method.getAnnotatedReturnType(),
|
510
|
|
- method.getAnnotatedParameterTypes(),
|
|
530
|
+ method.getParameters(),
|
511
|
531
|
method.getTypeParameters(),
|
512
|
532
|
method.getAnnotatedExceptionTypes());
|
513
|
533
|
}
|
514
|
534
|
|
|
535
|
+ private Expression getDefaultValue(Parameter parameter, StoredType type) {
|
|
536
|
+ if (parameter.isAnnotationPresent(ZenCodeType.Optional.class)) {
|
|
537
|
+ Expression defaultValue = type.type.getDefaultValue();
|
|
538
|
+ if (defaultValue == null)
|
|
539
|
+ throw new IllegalArgumentException(type.toString() + " doesn't have a default value");
|
|
540
|
+ return defaultValue;
|
|
541
|
+ } else if (parameter.isAnnotationPresent(ZenCodeType.OptionalInt.class)) {
|
|
542
|
+ ZenCodeType.OptionalInt annotation = parameter.getAnnotation(ZenCodeType.OptionalInt.class);
|
|
543
|
+ if (type.type == BasicTypeID.BYTE)
|
|
544
|
+ return new ConstantByteExpression(CodePosition.NATIVE, annotation.value());
|
|
545
|
+ else if (type.type == BasicTypeID.SBYTE)
|
|
546
|
+ return new ConstantSByteExpression(CodePosition.NATIVE, (byte)annotation.value());
|
|
547
|
+ else if (type.type == BasicTypeID.SHORT)
|
|
548
|
+ return new ConstantShortExpression(CodePosition.NATIVE, (short)annotation.value());
|
|
549
|
+ else if (type.type == BasicTypeID.USHORT)
|
|
550
|
+ return new ConstantUShortExpression(CodePosition.NATIVE, annotation.value());
|
|
551
|
+ else if (type.type == BasicTypeID.INT)
|
|
552
|
+ return new ConstantIntExpression(CodePosition.NATIVE, annotation.value());
|
|
553
|
+ else if (type.type == BasicTypeID.UINT)
|
|
554
|
+ return new ConstantUIntExpression(CodePosition.NATIVE, annotation.value());
|
|
555
|
+ else
|
|
556
|
+ throw new IllegalArgumentException("Cannot use int default values for " + type.toString());
|
|
557
|
+ } else if (parameter.isAnnotationPresent(ZenCodeType.OptionalLong.class)) {
|
|
558
|
+ ZenCodeType.OptionalLong annotation = parameter.getAnnotation(ZenCodeType.OptionalLong.class);
|
|
559
|
+ if (type.type == BasicTypeID.LONG)
|
|
560
|
+ return new ConstantLongExpression(CodePosition.NATIVE, annotation.value());
|
|
561
|
+ else if (type.type == BasicTypeID.ULONG)
|
|
562
|
+ return new ConstantULongExpression(CodePosition.NATIVE, annotation.value());
|
|
563
|
+ else
|
|
564
|
+ throw new IllegalArgumentException("Cannot use long default values for " + type.toString());
|
|
565
|
+ } else if (parameter.isAnnotationPresent(ZenCodeType.OptionalFloat.class)) {
|
|
566
|
+ ZenCodeType.OptionalFloat annotation = parameter.getAnnotation(ZenCodeType.OptionalFloat.class);
|
|
567
|
+ if (type.type == BasicTypeID.FLOAT)
|
|
568
|
+ return new ConstantFloatExpression(CodePosition.NATIVE, annotation.value());
|
|
569
|
+ else
|
|
570
|
+ throw new IllegalArgumentException("Cannot use float default values for " + type.toString());
|
|
571
|
+ } else if (parameter.isAnnotationPresent(ZenCodeType.OptionalDouble.class)) {
|
|
572
|
+ ZenCodeType.OptionalDouble annotation = parameter.getAnnotation(ZenCodeType.OptionalDouble.class);
|
|
573
|
+ if (type.type == BasicTypeID.DOUBLE)
|
|
574
|
+ return new ConstantDoubleExpression(CodePosition.NATIVE, annotation.value());
|
|
575
|
+ else
|
|
576
|
+ throw new IllegalArgumentException("Cannot use double default values for " + type.toString());
|
|
577
|
+ } else if (parameter.isAnnotationPresent(ZenCodeType.OptionalString.class)) {
|
|
578
|
+ ZenCodeType.OptionalString annotation = parameter.getAnnotation(ZenCodeType.OptionalString.class);
|
|
579
|
+ if (type.type == StringTypeID.INSTANCE) {
|
|
580
|
+ Expression result = new ConstantStringExpression(CodePosition.NATIVE, annotation.value());
|
|
581
|
+ if (type.getActualStorage() != StaticStorageTag.INSTANCE)
|
|
582
|
+ result = new StorageCastExpression(CodePosition.NATIVE, result, type);
|
|
583
|
+ return result;
|
|
584
|
+ } else {
|
|
585
|
+ throw new IllegalArgumentException("Cannot use string default values for " + type.toString());
|
|
586
|
+ }
|
|
587
|
+ } else {
|
|
588
|
+ return null;
|
|
589
|
+ }
|
|
590
|
+ }
|
|
591
|
+
|
515
|
592
|
private FunctionHeader getHeader(
|
516
|
593
|
TypeVariableContext context,
|
517
|
594
|
AnnotatedType javaReturnType,
|
518
|
|
- AnnotatedType[] parameterTypes,
|
|
595
|
+ Parameter[] javaParameters,
|
519
|
596
|
TypeVariable<Method>[] javaTypeParameters,
|
520
|
597
|
AnnotatedType[] exceptionTypes) {
|
521
|
598
|
StoredType returnType = javaReturnType == null ? BasicTypeID.VOID.stored : loadStoredType(context, javaReturnType);
|
522
|
599
|
|
523
|
|
- FunctionParameter[] parameters = new FunctionParameter[parameterTypes.length];
|
|
600
|
+ FunctionParameter[] parameters = new FunctionParameter[javaParameters.length];
|
524
|
601
|
for (int i = 0; i < parameters.length; i++) {
|
525
|
|
- parameters[i] = new FunctionParameter(loadStoredType(context, parameterTypes[i]));
|
|
602
|
+ Parameter parameter = javaParameters[i];
|
|
603
|
+
|
|
604
|
+ AnnotatedType parameterType = parameter.getAnnotatedType();
|
|
605
|
+ StoredType type = loadStoredType(context, parameter.getAnnotatedType());
|
|
606
|
+ Expression defaultValue = getDefaultValue(parameter, type);
|
|
607
|
+ parameters[i] = new FunctionParameter(type, parameter.getName(), defaultValue, parameter.isVarArgs());
|
526
|
608
|
}
|
527
|
609
|
|
528
|
610
|
TypeParameter[] typeParameters = new TypeParameter[javaTypeParameters.length];
|