|
@@ -10,40 +10,16 @@ import org.openzen.zencode.shared.CompileException;
|
10
|
10
|
import org.openzen.zencode.shared.LiteralSourceFile;
|
11
|
11
|
import org.openzen.zenscript.codemodel.*;
|
12
|
12
|
import org.openzen.zenscript.codemodel.annotations.AnnotationDefinition;
|
|
13
|
+import org.openzen.zenscript.codemodel.annotations.DefinitionAnnotation;
|
|
14
|
+import org.openzen.zenscript.codemodel.annotations.NativeDefinitionAnnotation;
|
13
|
15
|
import org.openzen.zenscript.codemodel.context.CompilingPackage;
|
14
|
16
|
import org.openzen.zenscript.codemodel.context.FileResolutionContext;
|
15
|
17
|
import org.openzen.zenscript.codemodel.context.ModuleTypeResolutionContext;
|
16
|
|
-import org.openzen.zenscript.codemodel.definition.ClassDefinition;
|
17
|
|
-import org.openzen.zenscript.codemodel.definition.EnumDefinition;
|
18
|
|
-import org.openzen.zenscript.codemodel.definition.ExpansionDefinition;
|
19
|
|
-import org.openzen.zenscript.codemodel.definition.InterfaceDefinition;
|
20
|
|
-import org.openzen.zenscript.codemodel.definition.StructDefinition;
|
21
|
|
-import org.openzen.zenscript.codemodel.definition.ZSPackage;
|
22
|
|
-import org.openzen.zenscript.codemodel.expression.ConstantByteExpression;
|
23
|
|
-import org.openzen.zenscript.codemodel.expression.ConstantDoubleExpression;
|
24
|
|
-import org.openzen.zenscript.codemodel.expression.ConstantFloatExpression;
|
25
|
|
-import org.openzen.zenscript.codemodel.expression.ConstantIntExpression;
|
26
|
|
-import org.openzen.zenscript.codemodel.expression.ConstantLongExpression;
|
27
|
|
-import org.openzen.zenscript.codemodel.expression.ConstantSByteExpression;
|
28
|
|
-import org.openzen.zenscript.codemodel.expression.ConstantShortExpression;
|
29
|
|
-import org.openzen.zenscript.codemodel.expression.ConstantStringExpression;
|
30
|
|
-import org.openzen.zenscript.codemodel.expression.ConstantUIntExpression;
|
31
|
|
-import org.openzen.zenscript.codemodel.expression.ConstantULongExpression;
|
32
|
|
-import org.openzen.zenscript.codemodel.expression.ConstantUShortExpression;
|
33
|
|
-import org.openzen.zenscript.codemodel.expression.Expression;
|
34
|
|
-import org.openzen.zenscript.codemodel.expression.ExpressionSymbol;
|
35
|
|
-import org.openzen.zenscript.codemodel.expression.StaticGetterExpression;
|
36
|
|
-import org.openzen.zenscript.codemodel.expression.StorageCastExpression;
|
|
18
|
+import org.openzen.zenscript.codemodel.definition.*;
|
|
19
|
+import org.openzen.zenscript.codemodel.expression.*;
|
37
|
20
|
import org.openzen.zenscript.codemodel.generic.ParameterTypeBound;
|
38
|
21
|
import org.openzen.zenscript.codemodel.generic.TypeParameter;
|
39
|
|
-import org.openzen.zenscript.codemodel.member.CasterMember;
|
40
|
|
-import org.openzen.zenscript.codemodel.member.ConstructorMember;
|
41
|
|
-import org.openzen.zenscript.codemodel.member.FieldMember;
|
42
|
|
-import org.openzen.zenscript.codemodel.member.GetterMember;
|
43
|
|
-import org.openzen.zenscript.codemodel.member.ImplementationMember;
|
44
|
|
-import org.openzen.zenscript.codemodel.member.MethodMember;
|
45
|
|
-import org.openzen.zenscript.codemodel.member.OperatorMember;
|
46
|
|
-import org.openzen.zenscript.codemodel.member.SetterMember;
|
|
22
|
+import org.openzen.zenscript.codemodel.member.*;
|
47
|
23
|
import org.openzen.zenscript.codemodel.member.ref.FunctionalMemberRef;
|
48
|
24
|
import org.openzen.zenscript.codemodel.partial.PartialStaticMemberGroupExpression;
|
49
|
25
|
import org.openzen.zenscript.codemodel.scope.ExpressionScope;
|
|
@@ -56,7 +32,8 @@ import org.openzen.zenscript.codemodel.type.storage.StaticStorageTag;
|
56
|
32
|
import org.openzen.zenscript.codemodel.type.storage.StorageTag;
|
57
|
33
|
import org.openzen.zenscript.codemodel.type.storage.StorageType;
|
58
|
34
|
import org.openzen.zenscript.javashared.*;
|
59
|
|
-import org.openzen.zenscript.lexer.*;
|
|
35
|
+import org.openzen.zenscript.lexer.ParseException;
|
|
36
|
+import org.openzen.zenscript.lexer.ZSTokenParser;
|
60
|
37
|
import org.openzen.zenscript.parser.BracketExpressionParser;
|
61
|
38
|
import org.openzen.zenscript.parser.expression.ParsedExpression;
|
62
|
39
|
import org.openzen.zenscript.parser.type.IParsedType;
|
|
@@ -77,11 +54,12 @@ public class JavaNativeModule {
|
77
|
54
|
private final GlobalTypeRegistry registry;
|
78
|
55
|
private final PackageDefinitions definitions = new PackageDefinitions();
|
79
|
56
|
private final JavaCompiledModule compiled;
|
80
|
|
-
|
|
57
|
+
|
81
|
58
|
private final Map<Class<?>, HighLevelDefinition> definitionByClass = new HashMap<>();
|
82
|
59
|
private final Map<Class<?>, TypeID> typeByClass = new HashMap<>();
|
83
|
60
|
private final Map<Class<?>, TypeID> unsignedByClass = new HashMap<>();
|
84
|
|
-
|
|
61
|
+ private final TypeVariableContext context = new TypeVariableContext();
|
|
62
|
+
|
85
|
63
|
public final Map<String, ISymbol> globals = new HashMap<>();
|
86
|
64
|
private BracketExpressionParser bep;
|
87
|
65
|
|
|
@@ -95,13 +73,13 @@ public class JavaNativeModule {
|
95
|
73
|
this.basePackage = basePackage;
|
96
|
74
|
module = new Module(name);
|
97
|
75
|
this.registry = registry;
|
98
|
|
-
|
|
76
|
+
|
99
|
77
|
for (JavaNativeModule dependency : dependencies) {
|
100
|
78
|
definitionByClass.putAll(dependency.definitionByClass);
|
101
|
79
|
}
|
102
|
|
-
|
|
80
|
+
|
103
|
81
|
compiled = new JavaCompiledModule(module, FunctionParameter.NONE);
|
104
|
|
-
|
|
82
|
+
|
105
|
83
|
typeByClass.put(void.class, BasicTypeID.VOID);
|
106
|
84
|
typeByClass.put(boolean.class, BasicTypeID.BOOL);
|
107
|
85
|
typeByClass.put(byte.class, BasicTypeID.SBYTE);
|
|
@@ -118,7 +96,7 @@ public class JavaNativeModule {
|
118
|
96
|
typeByClass.put(Long.class, registry.getOptional(BasicTypeID.LONG));
|
119
|
97
|
typeByClass.put(Float.class, registry.getOptional(BasicTypeID.FLOAT));
|
120
|
98
|
typeByClass.put(Double.class, registry.getOptional(BasicTypeID.DOUBLE));
|
121
|
|
-
|
|
99
|
+
|
122
|
100
|
unsignedByClass.put(byte.class, BasicTypeID.BYTE);
|
123
|
101
|
unsignedByClass.put(short.class, BasicTypeID.USHORT);
|
124
|
102
|
unsignedByClass.put(int.class, BasicTypeID.UINT);
|
|
@@ -128,7 +106,7 @@ public class JavaNativeModule {
|
128
|
106
|
unsignedByClass.put(Integer.class, registry.getOptional(BasicTypeID.INT));
|
129
|
107
|
unsignedByClass.put(Long.class, registry.getOptional(BasicTypeID.LONG));
|
130
|
108
|
}
|
131
|
|
-
|
|
109
|
+
|
132
|
110
|
public SemanticModule toSemantic(ModuleSpace space) {
|
133
|
111
|
return new SemanticModule(
|
134
|
112
|
module,
|
|
@@ -144,31 +122,31 @@ public class JavaNativeModule {
|
144
|
122
|
space.getAnnotations(),
|
145
|
123
|
space.getStorageTypes());
|
146
|
124
|
}
|
147
|
|
-
|
|
125
|
+
|
148
|
126
|
public JavaCompiledModule getCompiled() {
|
149
|
127
|
return compiled;
|
150
|
128
|
}
|
151
|
|
-
|
|
129
|
+
|
152
|
130
|
public HighLevelDefinition addClass(Class<?> cls) {
|
153
|
|
- if (definitionByClass.containsKey(cls))
|
|
131
|
+ if (definitionByClass.containsKey(cls)) {
|
154
|
132
|
return definitionByClass.get(cls);
|
155
|
|
-
|
|
133
|
+ }
|
156
|
134
|
return convertClass(cls);
|
157
|
135
|
}
|
158
|
|
-
|
|
136
|
+
|
159
|
137
|
public void addGlobals(Class<?> cls) {
|
160
|
138
|
HighLevelDefinition definition = new ClassDefinition(CodePosition.NATIVE, module, pkg, "__globals__", Modifiers.PUBLIC);
|
161
|
139
|
JavaClass jcls = JavaClass.fromInternalName(getInternalName(cls), JavaClass.Kind.CLASS);
|
162
|
140
|
compiled.setClassInfo(definition, jcls);
|
163
|
141
|
StoredType thisType = registry.getForMyDefinition(definition).stored();
|
164
|
|
- TypeVariableContext context = new TypeVariableContext();
|
165
|
|
-
|
|
142
|
+ //TypeVariableContext context = new TypeVariableContext();
|
|
143
|
+
|
166
|
144
|
for (Field field : cls.getDeclaredFields()) {
|
167
|
145
|
if (!field.isAnnotationPresent(ZenCodeGlobals.Global.class))
|
168
|
146
|
continue;
|
169
|
147
|
if (!isStatic(field.getModifiers()))
|
170
|
148
|
continue;
|
171
|
|
-
|
|
149
|
+
|
172
|
150
|
ZenCodeGlobals.Global global = field.getAnnotation(ZenCodeGlobals.Global.class);
|
173
|
151
|
StoredType type = loadStoredType(context, field.getAnnotatedType());
|
174
|
152
|
String name = global.value().isEmpty() ? field.getName() : global.value();
|
|
@@ -179,18 +157,18 @@ public class JavaNativeModule {
|
179
|
157
|
compiled.setFieldInfo(fieldMember.autoGetter, javaField);
|
180
|
158
|
globals.put(name, new ExpressionSymbol((position, scope) -> new StaticGetterExpression(CodePosition.BUILTIN, fieldMember.autoGetter.ref(thisType, GenericMapper.EMPTY))));
|
181
|
159
|
}
|
182
|
|
-
|
|
160
|
+
|
183
|
161
|
for (Method method : cls.getDeclaredMethods()) {
|
184
|
162
|
if (!method.isAnnotationPresent(ZenCodeGlobals.Global.class))
|
185
|
163
|
continue;
|
186
|
164
|
if (!isStatic(method.getModifiers()))
|
187
|
165
|
continue;
|
188
|
|
-
|
|
166
|
+
|
189
|
167
|
ZenCodeGlobals.Global global = method.getAnnotation(ZenCodeGlobals.Global.class);
|
190
|
168
|
String name = global.value().isEmpty() ? method.getName() : global.value();
|
191
|
169
|
MethodMember methodMember = new MethodMember(CodePosition.NATIVE, definition, Modifiers.PUBLIC | Modifiers.STATIC, name, getHeader(context, method), null);
|
192
|
170
|
definition.addMember(methodMember);
|
193
|
|
-
|
|
171
|
+
|
194
|
172
|
boolean isGenericResult = methodMember.header.getReturnType().isGeneric();
|
195
|
173
|
compiled.setMethodInfo(methodMember, new JavaMethod(jcls, JavaMethod.Kind.STATIC, method.getName(), false, getMethodDescriptor(method), method.getModifiers(), isGenericResult));
|
196
|
174
|
globals.put(name, new ExpressionSymbol((position, scope) -> {
|
|
@@ -199,15 +177,15 @@ public class JavaNativeModule {
|
199
|
177
|
}));
|
200
|
178
|
}
|
201
|
179
|
}
|
202
|
|
-
|
|
180
|
+
|
203
|
181
|
public FunctionalMemberRef loadStaticMethod(Method method) {
|
204
|
182
|
if (!isStatic(method.getModifiers()))
|
205
|
183
|
throw new IllegalArgumentException("Method \"" + method.toString() + "\" is not static");
|
206
|
|
-
|
|
184
|
+
|
207
|
185
|
HighLevelDefinition definition = addClass(method.getDeclaringClass());
|
208
|
186
|
JavaClass jcls = JavaClass.fromInternalName(getInternalName(method.getDeclaringClass()), JavaClass.Kind.CLASS);
|
209
|
|
-
|
210
|
|
- TypeVariableContext context = new TypeVariableContext();
|
|
187
|
+
|
|
188
|
+ //TypeVariableContext context = new TypeVariableContext();
|
211
|
189
|
MethodMember methodMember = new MethodMember(CodePosition.NATIVE, definition, Modifiers.PUBLIC | Modifiers.STATIC, method.getName(), getHeader(context, method), null);
|
212
|
190
|
definition.addMember(methodMember);
|
213
|
191
|
boolean isGenericResult = methodMember.header.getReturnType().isGeneric();
|
|
@@ -218,7 +196,7 @@ public class JavaNativeModule {
|
218
|
196
|
private boolean isInBasePackage(String className) {
|
219
|
197
|
return className.startsWith(basePackage + ".");
|
220
|
198
|
}
|
221
|
|
-
|
|
199
|
+
|
222
|
200
|
private ZSPackage getPackage(String className) {
|
223
|
201
|
//TODO validate
|
224
|
202
|
if(this.basePackage == null || this.basePackage.isEmpty())
|
|
@@ -226,22 +204,44 @@ public class JavaNativeModule {
|
226
|
204
|
//TODO make a lang package?
|
227
|
205
|
if (!className.contains(".") || className.startsWith("java.lang"))
|
228
|
206
|
return pkg;
|
229
|
|
-
|
|
207
|
+
|
230
|
208
|
if (className.startsWith("."))
|
231
|
209
|
className = className.substring(1);
|
232
|
210
|
else if (className.startsWith(basePackage + "."))
|
233
|
211
|
className = className.substring(basePackage.length() + 1);
|
234
|
212
|
else
|
235
|
213
|
throw new IllegalArgumentException("Invalid class name: \"" + className + "\" not in the given base package: \"" + basePackage + "\"");
|
236
|
|
-
|
|
214
|
+
|
237
|
215
|
String[] classNameParts = Strings.split(className, '.');
|
238
|
216
|
ZSPackage classPkg = pkg;
|
239
|
217
|
for (int i = 0; i < classNameParts.length - 1; i++)
|
240
|
218
|
classPkg = classPkg.getOrCreatePackage(classNameParts[i]);
|
241
|
|
-
|
|
219
|
+
|
242
|
220
|
return classPkg;
|
243
|
221
|
}
|
244
|
|
-
|
|
222
|
+
|
|
223
|
+ private <T> HighLevelDefinition checkRegistry(Class<T> cls) {
|
|
224
|
+ String name = cls.getCanonicalName();
|
|
225
|
+ if (!name.startsWith("java.lang.") && !name.startsWith("java.util.")) {
|
|
226
|
+ return null;
|
|
227
|
+ }
|
|
228
|
+
|
|
229
|
+ name = name.substring("java.lang.".length());
|
|
230
|
+ for (DefinitionTypeID definition : registry.getDefinitions()) {
|
|
231
|
+ final HighLevelDefinition highLevelDefinition = definition.definition;
|
|
232
|
+ for (DefinitionAnnotation annotation : highLevelDefinition.annotations) {
|
|
233
|
+ if (annotation instanceof NativeDefinitionAnnotation) {
|
|
234
|
+ final String identifier = ((NativeDefinitionAnnotation) annotation).getIdentifier();
|
|
235
|
+ if (identifier.equals(name) || identifier.equals("stdlib::" + name)) {
|
|
236
|
+ return highLevelDefinition;
|
|
237
|
+ }
|
|
238
|
+ }
|
|
239
|
+ }
|
|
240
|
+ }
|
|
241
|
+
|
|
242
|
+ return null;
|
|
243
|
+ }
|
|
244
|
+
|
245
|
245
|
private <T> HighLevelDefinition convertClass(Class<T> cls) {
|
246
|
246
|
if ((cls.getModifiers() & Modifier.PUBLIC) == 0)
|
247
|
247
|
throw new IllegalArgumentException("Class \" " + cls.getName() + "\" must be public");
|
|
@@ -252,71 +252,86 @@ public class JavaNativeModule {
|
252
|
252
|
|
253
|
253
|
String className = cls.getName();
|
254
|
254
|
boolean isStruct = cls.isAnnotationPresent(ZenCodeType.Struct.class);
|
255
|
|
-
|
256
|
|
- ZSPackage classPkg;
|
257
|
|
- ZenCodeType.Name nameAnnotation = cls.getDeclaredAnnotation(ZenCodeType.Name.class);
|
258
|
|
- className = className.contains(".") ? className.substring(className.lastIndexOf('.') + 1) : className;
|
259
|
|
- if (nameAnnotation == null) {
|
260
|
|
- classPkg = getPackage(className);
|
261
|
|
- } else {
|
262
|
|
- String specifiedName = nameAnnotation.value();
|
263
|
|
- if (specifiedName.startsWith(".")) {
|
264
|
|
- classPkg = getPackage(specifiedName);
|
265
|
|
- className = specifiedName.substring(specifiedName.lastIndexOf('.') + 1);
|
266
|
|
- } else if (specifiedName.indexOf('.') >= 0) {
|
267
|
|
- if (!specifiedName.startsWith(pkg.fullName))
|
268
|
|
- throw new IllegalArgumentException("Specified @Name as \"" + specifiedName + "\" for class: \"" + cls.toString() + "\" but it's not in the module root package: \"" + pkg.fullName + "\"");
|
269
|
|
-
|
270
|
|
- classPkg = getPackage(basePackage + specifiedName.substring(pkg.fullName.length()));
|
271
|
|
- className = specifiedName.substring(specifiedName.lastIndexOf('.') + 1);
|
272
|
|
- } else {
|
273
|
|
- classPkg = getPackage(specifiedName);
|
274
|
|
- className = nameAnnotation.value();
|
275
|
|
- }
|
276
|
|
- }
|
277
|
255
|
|
278
|
|
-
|
279
|
|
-
|
280
|
|
- HighLevelDefinition definition;
|
|
256
|
+ HighLevelDefinition definition = checkRegistry(cls);
|
|
257
|
+ final boolean foundRegistry = definition != null;
|
281
|
258
|
String internalName = getInternalName(cls);
|
282
|
259
|
JavaClass javaClass;
|
283
|
|
- if (cls.isInterface()) {
|
284
|
|
- definition = new InterfaceDefinition(CodePosition.NATIVE, module, classPkg, className, Modifiers.PUBLIC, null);
|
285
|
|
- javaClass = JavaClass.fromInternalName(internalName, JavaClass.Kind.INTERFACE);
|
286
|
|
- } else if (cls.isEnum()) {
|
287
|
|
- definition = new EnumDefinition(CodePosition.NATIVE, module, classPkg, className, Modifiers.PUBLIC, null);
|
288
|
|
- javaClass = JavaClass.fromInternalName(internalName, JavaClass.Kind.ENUM);
|
289
|
|
- } else if (isStruct) {
|
290
|
|
- definition = new StructDefinition(CodePosition.NATIVE, module, classPkg, className, Modifiers.PUBLIC, null);
|
291
|
|
- javaClass = JavaClass.fromInternalName(internalName, JavaClass.Kind.CLASS);
|
|
260
|
+
|
|
261
|
+ if (foundRegistry) {
|
|
262
|
+ javaClass = JavaClass.fromInternalName(internalName, definition.isInterface() ? JavaClass.Kind.INTERFACE : JavaClass.Kind.CLASS);
|
292
|
263
|
} else {
|
293
|
|
- definition = new ClassDefinition(CodePosition.NATIVE, module, classPkg, className, Modifiers.PUBLIC);
|
294
|
|
- javaClass = JavaClass.fromInternalName(internalName, JavaClass.Kind.CLASS);
|
|
264
|
+ ZSPackage classPkg;
|
|
265
|
+ ZenCodeType.Name nameAnnotation = cls.getDeclaredAnnotation(ZenCodeType.Name.class);
|
|
266
|
+ className = className.contains(".") ? className.substring(className.lastIndexOf('.') + 1) : className;
|
|
267
|
+ if (nameAnnotation == null) {
|
|
268
|
+ classPkg = getPackage(className);
|
|
269
|
+ } else {
|
|
270
|
+ String specifiedName = nameAnnotation.value();
|
|
271
|
+ if (specifiedName.startsWith(".")) {
|
|
272
|
+ classPkg = getPackage(specifiedName);
|
|
273
|
+ className = specifiedName.substring(specifiedName.lastIndexOf('.') + 1);
|
|
274
|
+ } else if (specifiedName.indexOf('.') >= 0) {
|
|
275
|
+ if (!specifiedName.startsWith(pkg.fullName))
|
|
276
|
+ throw new IllegalArgumentException("Specified @Name as \"" + specifiedName + "\" for class: \"" + cls
|
|
277
|
+ .toString() + "\" but it's not in the module root package");
|
|
278
|
+
|
|
279
|
+ classPkg = getPackage(basePackage + specifiedName.substring(pkg.fullName.length()));
|
|
280
|
+ className = specifiedName.substring(specifiedName.lastIndexOf('.') + 1);
|
|
281
|
+ } else {
|
|
282
|
+ classPkg = getPackage(specifiedName);
|
|
283
|
+ className = nameAnnotation.value();
|
|
284
|
+ }
|
|
285
|
+ }
|
295
|
286
|
|
|
287
|
+
|
|
288
|
+ if (cls.isInterface()) {
|
|
289
|
+ definition = new InterfaceDefinition(CodePosition.NATIVE, module, classPkg, className, Modifiers.PUBLIC, null);
|
|
290
|
+ javaClass = JavaClass.fromInternalName(internalName, JavaClass.Kind.INTERFACE);
|
|
291
|
+ } else if (cls.isEnum()) {
|
|
292
|
+ definition = new EnumDefinition(CodePosition.NATIVE, module, classPkg, className, Modifiers.PUBLIC, null);
|
|
293
|
+ javaClass = JavaClass.fromInternalName(internalName, JavaClass.Kind.ENUM);
|
|
294
|
+ } else if (isStruct) {
|
|
295
|
+ definition = new StructDefinition(CodePosition.NATIVE, module, classPkg, className, Modifiers.PUBLIC, null);
|
|
296
|
+ javaClass = JavaClass.fromInternalName(internalName, JavaClass.Kind.CLASS);
|
|
297
|
+ } else {
|
|
298
|
+ definition = new ClassDefinition(CodePosition.NATIVE, module, classPkg, className, Modifiers.PUBLIC);
|
|
299
|
+ javaClass = JavaClass.fromInternalName(internalName, JavaClass.Kind.CLASS);
|
|
300
|
+
|
|
301
|
+ }
|
296
|
302
|
}
|
297
|
303
|
|
298
|
304
|
//Moved up here so that circular dependencies are caught (hopefully)
|
299
|
305
|
definitionByClass.put(cls, definition);
|
300
|
306
|
|
301
|
|
- TypeVariableContext context = new TypeVariableContext();
|
|
307
|
+ //TypeVariableContext context = new TypeVariableContext();
|
302
|
308
|
TypeVariable<Class<T>>[] javaTypeParameters = cls.getTypeParameters();
|
303
|
|
- definition.typeParameters = new TypeParameter[cls.getTypeParameters().length];
|
|
309
|
+ TypeParameter[] typeParameters = new TypeParameter[cls.getTypeParameters().length];
|
|
310
|
+ definition.typeParameters = typeParameters;
|
|
311
|
+
|
304
|
312
|
for (int i = 0; i < javaTypeParameters.length; i++) {
|
|
313
|
+ //Put up here for nested parameters?
|
305
|
314
|
TypeVariable<Class<T>> typeVariable = javaTypeParameters[i];
|
306
|
315
|
TypeParameter parameter = new TypeParameter(CodePosition.NATIVE, typeVariable.getName());
|
|
316
|
+ typeParameters[i] = parameter;
|
|
317
|
+ context.put(typeVariable, parameter);
|
|
318
|
+ }
|
|
319
|
+
|
|
320
|
+ for (int i = 0; i < javaTypeParameters.length; i++) {
|
|
321
|
+ TypeVariable<Class<T>> typeVariable = javaTypeParameters[i];
|
|
322
|
+ TypeParameter parameter = typeParameters[i];
|
307
|
323
|
for (AnnotatedType bound : typeVariable.getAnnotatedBounds()) {
|
308
|
324
|
TypeID type = loadType(context, bound).type;
|
309
|
325
|
parameter.addBound(new ParameterTypeBound(CodePosition.NATIVE, type));
|
310
|
326
|
}
|
311
|
|
- definition.typeParameters[i] = parameter;
|
312
|
|
- context.put(typeVariable, parameter);
|
|
327
|
+ typeParameters[i] = parameter;
|
313
|
328
|
}
|
314
|
|
-
|
315
|
|
- if (definition instanceof ClassDefinition && cls.getAnnotatedSuperclass() != null && shouldLoadType(cls.getAnnotatedSuperclass().getType())) {
|
|
329
|
+
|
|
330
|
+ if (!foundRegistry && definition instanceof ClassDefinition && cls.getAnnotatedSuperclass() != null && shouldLoadType(cls.getAnnotatedSuperclass().getType())) {
|
316
|
331
|
definition.setSuperType(loadType(context, cls.getAnnotatedSuperclass()).type);
|
317
|
332
|
}
|
318
|
333
|
|
319
|
|
- if (definition.getSuperType() == null && cls != Object.class) {
|
|
334
|
+ if (!foundRegistry && definition.getSuperType() == null && cls != Object.class) {
|
320
|
335
|
definition.setSuperType(loadType(context, Object.class, false, false).type);
|
321
|
336
|
}
|
322
|
337
|
|
|
@@ -328,10 +343,9 @@ public class JavaNativeModule {
|
328
|
343
|
compiled.setImplementationInfo(member, new JavaImplementation(true, javaClass));
|
329
|
344
|
}
|
330
|
345
|
}
|
331
|
|
-
|
332
|
|
-
|
|
346
|
+
|
333
|
347
|
compiled.setClassInfo(definition, javaClass);
|
334
|
|
-
|
|
348
|
+
|
335
|
349
|
StoredType thisType = new StoredType(registry.getForMyDefinition(definition), AutoStorageTag.INSTANCE);
|
336
|
350
|
for (Field field : cls.getDeclaredFields()) {
|
337
|
351
|
ZenCodeType.Field annotation = field.getAnnotation(ZenCodeType.Field.class);
|
|
@@ -339,17 +353,17 @@ public class JavaNativeModule {
|
339
|
353
|
continue;
|
340
|
354
|
if (!isPublic(field.getModifiers()))
|
341
|
355
|
continue;
|
342
|
|
-
|
|
356
|
+
|
343
|
357
|
final String fieldName = annotation.value().isEmpty() ? field.getName() : annotation.value();
|
344
|
|
-
|
|
358
|
+
|
345
|
359
|
StoredType fieldType = loadStoredType(context, field.getAnnotatedType());
|
346
|
360
|
FieldMember member = new FieldMember(CodePosition.NATIVE, definition, getMethodModifiers(field), fieldName, thisType, fieldType, registry, 0, 0, null);
|
347
|
361
|
definition.addMember(member);
|
348
|
362
|
compiled.setFieldInfo(member, new JavaField(javaClass, field.getName(), getDescriptor(field.getType())));
|
349
|
363
|
}
|
350
|
|
-
|
|
364
|
+
|
351
|
365
|
boolean hasConstructor = false;
|
352
|
|
- for (java.lang.reflect.Constructor constructor : cls.getConstructors()) {
|
|
366
|
+ for (java.lang.reflect.Constructor<?> constructor : cls.getConstructors()) {
|
353
|
367
|
ZenCodeType.Constructor constructorAnnotation = (ZenCodeType.Constructor) constructor.getAnnotation(ZenCodeType.Constructor.class);
|
354
|
368
|
if (constructorAnnotation != null) {
|
355
|
369
|
ConstructorMember member = asConstructor(context, definition, constructor);
|
|
@@ -358,13 +372,13 @@ public class JavaNativeModule {
|
358
|
372
|
hasConstructor = true;
|
359
|
373
|
}
|
360
|
374
|
}
|
361
|
|
-
|
362
|
|
- if (!hasConstructor) {
|
|
375
|
+
|
|
376
|
+ if (!hasConstructor && !foundRegistry) {
|
363
|
377
|
// no constructor! make a private constructor so the compiler doesn't add one
|
364
|
378
|
ConstructorMember member = new ConstructorMember(CodePosition.BUILTIN, definition, Modifiers.PRIVATE, new FunctionHeader(BasicTypeID.VOID), BuiltinID.CLASS_DEFAULT_CONSTRUCTOR);
|
365
|
379
|
definition.addMember(member);
|
366
|
380
|
}
|
367
|
|
-
|
|
381
|
+
|
368
|
382
|
for (Method method : cls.getDeclaredMethods()) {
|
369
|
383
|
ZenCodeType.Method methodAnnotation = method.getAnnotation(ZenCodeType.Method.class);
|
370
|
384
|
if (methodAnnotation != null) {
|
|
@@ -373,42 +387,42 @@ public class JavaNativeModule {
|
373
|
387
|
compiled.setMethodInfo(member, getMethod(javaClass, method, member.header.getReturnType()));
|
374
|
388
|
continue;
|
375
|
389
|
}
|
376
|
|
-
|
|
390
|
+
|
377
|
391
|
ZenCodeType.Getter getter = method.getAnnotation(ZenCodeType.Getter.class);
|
378
|
392
|
if (getter != null) {
|
379
|
393
|
GetterMember member = asGetter(context, definition, method, getter);
|
380
|
394
|
definition.addMember(member);
|
381
|
395
|
compiled.setMethodInfo(member, getMethod(javaClass, method, member.getType()));
|
382
|
396
|
}
|
383
|
|
-
|
|
397
|
+
|
384
|
398
|
ZenCodeType.Setter setter = method.getAnnotation(ZenCodeType.Setter.class);
|
385
|
399
|
if (setter != null) {
|
386
|
400
|
SetterMember member = asSetter(context, definition, method, setter);
|
387
|
401
|
definition.addMember(member);
|
388
|
402
|
compiled.setMethodInfo(member, getMethod(javaClass, method, BasicTypeID.VOID.stored));
|
389
|
403
|
}
|
390
|
|
-
|
|
404
|
+
|
391
|
405
|
ZenCodeType.Operator operator = method.getAnnotation(ZenCodeType.Operator.class);
|
392
|
406
|
if (operator != null) {
|
393
|
407
|
OperatorMember member = asOperator(context, definition, method, operator);
|
394
|
408
|
definition.addMember(member);
|
395
|
409
|
compiled.setMethodInfo(member, getMethod(javaClass, method, member.header.getReturnType()));
|
396
|
410
|
}
|
397
|
|
-
|
|
411
|
+
|
398
|
412
|
ZenCodeType.Caster caster = method.getAnnotation(ZenCodeType.Caster.class);
|
399
|
413
|
if (caster != null) {
|
400
|
414
|
CasterMember member = asCaster(context, definition, method, caster);
|
401
|
415
|
definition.addMember(member);
|
402
|
416
|
compiled.setMethodInfo(member, getMethod(javaClass, method, member.toType));
|
403
|
417
|
}
|
404
|
|
-
|
|
418
|
+
|
405
|
419
|
/*if (!annotated) {
|
406
|
420
|
MethodMember member = asMethod(definition, method, null);
|
407
|
421
|
definition.addMember(member);
|
408
|
422
|
compiled.setMethodInfo(member, getMethod(javaClass, method, member.header.getReturnType()));
|
409
|
423
|
}*/
|
410
|
424
|
}
|
411
|
|
-
|
|
425
|
+
|
412
|
426
|
return definition;
|
413
|
427
|
}
|
414
|
428
|
|
|
@@ -449,7 +463,7 @@ public class JavaNativeModule {
|
449
|
463
|
if(methodAnnotation != null) {
|
450
|
464
|
String name = !methodAnnotation.value().isEmpty() ? methodAnnotation.value() : method.getName();
|
451
|
465
|
|
452
|
|
- TypeVariableContext context = new TypeVariableContext();
|
|
466
|
+ //TypeVariableContext context = new TypeVariableContext();
|
453
|
467
|
|
454
|
468
|
final Parameter[] parameters = getExpansionParameters(method);
|
455
|
469
|
|
|
@@ -468,7 +482,7 @@ public class JavaNativeModule {
|
468
|
482
|
if (implicit) {
|
469
|
483
|
modifiers |= Modifiers.IMPLICIT;
|
470
|
484
|
}
|
471
|
|
- TypeVariableContext context = new TypeVariableContext();
|
|
485
|
+ //TypeVariableContext context = new TypeVariableContext();
|
472
|
486
|
StoredType toType = loadStoredType(context, method.getAnnotatedReturnType());
|
473
|
487
|
final CasterMember member = new CasterMember(CodePosition.NATIVE, expansion, modifiers, toType, null);
|
474
|
488
|
|
|
@@ -571,16 +585,16 @@ public class JavaNativeModule {
|
571
|
585
|
return null;
|
572
|
586
|
}
|
573
|
587
|
|
574
|
|
-
|
|
588
|
+
|
575
|
589
|
private boolean shouldLoadType(Type type) {
|
576
|
590
|
if (type instanceof Class)
|
577
|
591
|
return definitionByClass.containsKey(type) || shouldLoadClass((Class<?>)type);
|
578
|
592
|
if (type instanceof ParameterizedType)
|
579
|
593
|
return shouldLoadType(((ParameterizedType)type).getRawType());
|
580
|
|
-
|
|
594
|
+
|
581
|
595
|
return false;
|
582
|
596
|
}
|
583
|
|
-
|
|
597
|
+
|
584
|
598
|
private String getClassName(Class<?> cls) {
|
585
|
599
|
return cls.isAnnotationPresent(ZenCodeType.Name.class) ? cls.getAnnotation(ZenCodeType.Name.class).value() : cls.getName();
|
586
|
600
|
}
|
|
@@ -588,25 +602,25 @@ public class JavaNativeModule {
|
588
|
602
|
private boolean shouldLoadClass(Class<?> cls) {
|
589
|
603
|
return isInBasePackage(getClassName(cls));
|
590
|
604
|
}
|
591
|
|
-
|
|
605
|
+
|
592
|
606
|
private boolean isGetterName(String name) {
|
593
|
607
|
return name.startsWith("get") || name.startsWith("is") || name.startsWith("has");
|
594
|
608
|
}
|
595
|
|
-
|
|
609
|
+
|
596
|
610
|
private String translateGetterName(String name) {
|
597
|
611
|
if (name.startsWith("get"))
|
598
|
612
|
return name.substring(3, 4).toLowerCase() + name.substring(4);
|
599
|
|
-
|
|
613
|
+
|
600
|
614
|
return name;
|
601
|
615
|
}
|
602
|
|
-
|
|
616
|
+
|
603
|
617
|
private String translateSetterName(String name) {
|
604
|
618
|
if (name.startsWith("set"))
|
605
|
619
|
return name.substring(3, 4).toLowerCase() + name.substring(4);
|
606
|
|
-
|
|
620
|
+
|
607
|
621
|
return name;
|
608
|
622
|
}
|
609
|
|
-
|
|
623
|
+
|
610
|
624
|
private ConstructorMember asConstructor(TypeVariableContext context, HighLevelDefinition definition, java.lang.reflect.Constructor method) {
|
611
|
625
|
FunctionHeader header = getHeader(context, method);
|
612
|
626
|
return new ConstructorMember(
|
|
@@ -616,7 +630,7 @@ public class JavaNativeModule {
|
616
|
630
|
header,
|
617
|
631
|
null);
|
618
|
632
|
}
|
619
|
|
-
|
|
633
|
+
|
620
|
634
|
private MethodMember asMethod(TypeVariableContext context, HighLevelDefinition definition, Method method, ZenCodeType.Method annotation) {
|
621
|
635
|
String name = annotation != null && !annotation.value().isEmpty() ? annotation.value() : method.getName();
|
622
|
636
|
FunctionHeader header = getHeader(context, method);
|
|
@@ -628,15 +642,15 @@ public class JavaNativeModule {
|
628
|
642
|
header,
|
629
|
643
|
null);
|
630
|
644
|
}
|
631
|
|
-
|
|
645
|
+
|
632
|
646
|
private OperatorMember asOperator(TypeVariableContext context, HighLevelDefinition definition, Method method, ZenCodeType.Operator annotation) {
|
633
|
647
|
FunctionHeader header = getHeader(context, method);
|
634
|
648
|
if (isStatic(method.getModifiers()))
|
635
|
649
|
throw new IllegalArgumentException("operator method \"" + method.toString() + "\"cannot be static");
|
636
|
|
-
|
|
650
|
+
|
637
|
651
|
// TODO: check number of parameters
|
638
|
652
|
//if (header.parameters.length != annotation.value().parameters)
|
639
|
|
-
|
|
653
|
+
|
640
|
654
|
return new OperatorMember(
|
641
|
655
|
CodePosition.NATIVE,
|
642
|
656
|
definition,
|
|
@@ -645,7 +659,7 @@ public class JavaNativeModule {
|
645
|
659
|
header,
|
646
|
660
|
null);
|
647
|
661
|
}
|
648
|
|
-
|
|
662
|
+
|
649
|
663
|
private GetterMember asGetter(TypeVariableContext context, HighLevelDefinition definition, Method method, ZenCodeType.Getter annotation) {
|
650
|
664
|
StoredType type = loadStoredType(context, method.getAnnotatedReturnType());
|
651
|
665
|
String name = null;
|
|
@@ -653,34 +667,34 @@ public class JavaNativeModule {
|
653
|
667
|
name = annotation.value();
|
654
|
668
|
if (name == null)
|
655
|
669
|
name = translateGetterName(method.getName());
|
656
|
|
-
|
|
670
|
+
|
657
|
671
|
return new GetterMember(CodePosition.NATIVE, definition, getMethodModifiers(method), name, type, null);
|
658
|
672
|
}
|
659
|
|
-
|
|
673
|
+
|
660
|
674
|
private SetterMember asSetter(TypeVariableContext context, HighLevelDefinition definition, Method method, ZenCodeType.Setter annotation) {
|
661
|
675
|
if (method.getParameterCount() != 1)
|
662
|
676
|
throw new IllegalArgumentException("Illegal setter: \"" + method.toString() + "\"must have exactly 1 parameter");
|
663
|
|
-
|
|
677
|
+
|
664
|
678
|
StoredType type = loadStoredType(context, method.getAnnotatedParameterTypes()[0]);
|
665
|
679
|
String name = null;
|
666
|
680
|
if (annotation != null && !annotation.value().isEmpty())
|
667
|
681
|
name = annotation.value();
|
668
|
682
|
if (name == null)
|
669
|
683
|
name = translateSetterName(method.getName());
|
670
|
|
-
|
|
684
|
+
|
671
|
685
|
return new SetterMember(CodePosition.NATIVE, definition, getMethodModifiers(method), name, type, null);
|
672
|
686
|
}
|
673
|
|
-
|
|
687
|
+
|
674
|
688
|
private CasterMember asCaster(TypeVariableContext context, HighLevelDefinition definition, Method method, ZenCodeType.Caster annotation) {
|
675
|
689
|
boolean implicit = annotation != null && annotation.implicit();
|
676
|
690
|
int modifiers = Modifiers.PUBLIC;
|
677
|
691
|
if (implicit)
|
678
|
692
|
modifiers |= Modifiers.IMPLICIT;
|
679
|
|
-
|
|
693
|
+
|
680
|
694
|
StoredType toType = loadStoredType(context, method.getAnnotatedReturnType());
|
681
|
695
|
return new CasterMember(CodePosition.NATIVE, definition, modifiers, toType, null);
|
682
|
696
|
}
|
683
|
|
-
|
|
697
|
+
|
684
|
698
|
private FunctionHeader getHeader(TypeVariableContext context, java.lang.reflect.Constructor constructor) {
|
685
|
699
|
return getHeader(
|
686
|
700
|
context,
|
|
@@ -689,7 +703,7 @@ public class JavaNativeModule {
|
689
|
703
|
constructor.getTypeParameters(),
|
690
|
704
|
constructor.getAnnotatedExceptionTypes());
|
691
|
705
|
}
|
692
|
|
-
|
|
706
|
+
|
693
|
707
|
private FunctionHeader getHeader(TypeVariableContext context, Method method) {
|
694
|
708
|
return getHeader(
|
695
|
709
|
context,
|
|
@@ -698,7 +712,7 @@ public class JavaNativeModule {
|
698
|
712
|
method.getTypeParameters(),
|
699
|
713
|
method.getAnnotatedExceptionTypes());
|
700
|
714
|
}
|
701
|
|
-
|
|
715
|
+
|
702
|
716
|
protected Expression getDefaultValue(Parameter parameter, StoredType type) {
|
703
|
717
|
if (parameter.isAnnotationPresent(ZenCodeType.Optional.class)) {
|
704
|
718
|
final String s = parameter.getAnnotation(ZenCodeType.Optional.class).value();
|
|
@@ -776,34 +790,41 @@ public class JavaNativeModule {
|
776
|
790
|
TypeVariable<Method>[] javaTypeParameters,
|
777
|
791
|
AnnotatedType[] exceptionTypes) {
|
778
|
792
|
StoredType returnType = javaReturnType == null ? BasicTypeID.VOID.stored : loadStoredType(context, javaReturnType);
|
779
|
|
-
|
|
793
|
+
|
|
794
|
+
|
|
795
|
+ TypeParameter[] typeParameters = new TypeParameter[javaTypeParameters.length];
|
|
796
|
+ for (int i = 0; i < javaTypeParameters.length; i++) {
|
|
797
|
+ //Put up here for nested parameters?
|
|
798
|
+ TypeVariable<Method> typeVariable = javaTypeParameters[i];
|
|
799
|
+ TypeParameter parameter = new TypeParameter(CodePosition.NATIVE, typeVariable.getName());
|
|
800
|
+ typeParameters[i] = parameter;
|
|
801
|
+ context.put(typeVariable, parameter);
|
|
802
|
+ }
|
|
803
|
+
|
|
804
|
+ for (int i = 0; i < javaTypeParameters.length; i++) {
|
|
805
|
+ TypeVariable<Method> javaTypeParameter = javaTypeParameters[i];
|
|
806
|
+
|
|
807
|
+ for (AnnotatedType bound : javaTypeParameter.getAnnotatedBounds())
|
|
808
|
+ typeParameters[i].addBound(new ParameterTypeBound(CodePosition.NATIVE, loadType(context, bound).type));
|
|
809
|
+ }
|
|
810
|
+
|
780
|
811
|
FunctionParameter[] parameters = new FunctionParameter[javaParameters.length];
|
781
|
812
|
for (int i = 0; i < parameters.length; i++) {
|
782
|
813
|
Parameter parameter = javaParameters[i];
|
783
|
814
|
|
784
|
815
|
//AnnotatedType parameterType = parameter.getAnnotatedType();
|
785
|
|
- StoredType type = loadStoredType(context, parameter);
|
|
816
|
+ StoredType type = loadStoredType(context, parameter);
|
786
|
817
|
Expression defaultValue = getDefaultValue(parameter, type);
|
787
|
818
|
parameters[i] = new FunctionParameter(type, parameter.getName(), defaultValue, parameter.isVarArgs());
|
788
|
819
|
}
|
789
|
|
-
|
790
|
|
- TypeParameter[] typeParameters = new TypeParameter[javaTypeParameters.length];
|
791
|
|
- for (int i = 0; i < javaTypeParameters.length; i++) {
|
792
|
|
- TypeVariable<Method> javaTypeParameter = javaTypeParameters[i];
|
793
|
|
- typeParameters[i] = new TypeParameter(CodePosition.UNKNOWN, javaTypeParameter.getName());
|
794
|
|
- context.put(javaTypeParameter, typeParameters[i]);
|
795
|
|
-
|
796
|
|
- for (AnnotatedType bound : javaTypeParameter.getAnnotatedBounds())
|
797
|
|
- typeParameters[i].addBound(new ParameterTypeBound(CodePosition.NATIVE, loadType(context, bound).type));
|
798
|
|
- }
|
799
|
|
-
|
|
820
|
+
|
800
|
821
|
if (exceptionTypes.length > 1)
|
801
|
822
|
throw new IllegalArgumentException("A method can only throw a single exception type!");
|
802
|
|
-
|
|
823
|
+
|
803
|
824
|
StoredType thrownType = exceptionTypes.length == 0 ? null : loadStoredType(context, exceptionTypes[0]);
|
804
|
825
|
return new FunctionHeader(typeParameters, returnType, thrownType, AutoStorageTag.INSTANCE, parameters);
|
805
|
826
|
}
|
806
|
|
-
|
|
827
|
+
|
807
|
828
|
private StoredType loadStoredType(TypeVariableContext context, AnnotatedType annotatedType) {
|
808
|
829
|
return loadType(context, annotatedType);
|
809
|
830
|
}
|
|
@@ -815,27 +836,27 @@ public class JavaNativeModule {
|
815
|
836
|
return new StoredType(registry.getOptional(type.type), type.getSpecifiedStorage());
|
816
|
837
|
return type;
|
817
|
838
|
}
|
818
|
|
-
|
|
839
|
+
|
819
|
840
|
private StoredType loadType(TypeVariableContext context, AnnotatedType annotatedType) {
|
820
|
841
|
if (annotatedType.isAnnotationPresent(ZenCodeType.USize.class))
|
821
|
842
|
return BasicTypeID.USIZE.stored;
|
822
|
843
|
else if (annotatedType.isAnnotationPresent(ZenCodeType.NullableUSize.class))
|
823
|
844
|
return registry.getOptional(BasicTypeID.USIZE).stored();
|
824
|
|
-
|
|
845
|
+
|
825
|
846
|
boolean nullable = annotatedType.isAnnotationPresent(ZenCodeType.Nullable.class);
|
826
|
847
|
boolean unsigned = annotatedType.isAnnotationPresent(ZenCodeType.Unsigned.class);
|
827
|
|
-
|
|
848
|
+
|
828
|
849
|
return loadType(context, annotatedType, nullable, unsigned);
|
829
|
850
|
}
|
830
|
|
-
|
|
851
|
+
|
831
|
852
|
private StoredType loadType(TypeVariableContext context, AnnotatedElement type, boolean nullable, boolean unsigned) {
|
832
|
853
|
StoredType result = loadType(context, type, unsigned);
|
833
|
854
|
if (nullable)
|
834
|
855
|
result = new StoredType(registry.getOptional(result.type), result.getSpecifiedStorage());
|
835
|
|
-
|
|
856
|
+
|
836
|
857
|
return result;
|
837
|
858
|
}
|
838
|
|
-
|
|
859
|
+
|
839
|
860
|
@SuppressWarnings("ChainOfInstanceofChecks")
|
840
|
861
|
private StoredType loadType(TypeVariableContext context, AnnotatedElement type, boolean unsigned) {
|
841
|
862
|
if (type instanceof Class) {
|
|
@@ -851,53 +872,71 @@ public class JavaNativeModule {
|
851
|
872
|
} else if (classType.isAnnotationPresent(FunctionalInterface.class)) {
|
852
|
873
|
return loadFunctionalInterface(context, classType, new AnnotatedElement[0]);
|
853
|
874
|
}
|
854
|
|
-
|
|
875
|
+
|
855
|
876
|
if (typeByClass.containsKey(classType))
|
856
|
877
|
return typeByClass.get(classType).stored();
|
857
|
|
-
|
|
878
|
+
|
858
|
879
|
HighLevelDefinition definition = addClass(classType);
|
859
|
|
- return registry.getForDefinition(definition).stored();
|
|
880
|
+ final List<StoredType> s = new ArrayList<>();
|
|
881
|
+ for (TypeVariable<? extends Class<?>> typeParameter : classType.getTypeParameters()) {
|
|
882
|
+ s.add(registry.getGeneric(context.get(typeParameter)).stored());
|
|
883
|
+ }
|
|
884
|
+
|
|
885
|
+ return registry.getForDefinition(definition, s.toArray(StoredType.NONE)).stored();
|
860
|
886
|
} else if (type instanceof ParameterizedType) {
|
861
|
887
|
ParameterizedType parameterizedType = (ParameterizedType) type;
|
862
|
|
- Class<?> rawType = (Class) parameterizedType.getRawType();
|
|
888
|
+ Class<?> rawType = (Class<?>) parameterizedType.getRawType();
|
863
|
889
|
if (rawType.isAnnotationPresent(FunctionalInterface.class))
|
864
|
890
|
return loadFunctionalInterface(context, rawType, (AnnotatedElement[]) parameterizedType.getActualTypeArguments());
|
865
|
|
-
|
|
891
|
+
|
866
|
892
|
Type[] parameters = parameterizedType.getActualTypeArguments();
|
867
|
893
|
StoredType[] codeParameters = new StoredType[parameters.length];
|
868
|
894
|
for (int i = 0; i < parameters.length; i++)
|
869
|
895
|
codeParameters[i] = loadType(context, (AnnotatedElement) parameters[i], false, false);
|
870
|
|
-
|
|
896
|
+
|
871
|
897
|
if(rawType == Map.class) {
|
872
|
898
|
return registry.getAssociative(codeParameters[0], codeParameters[1]).stored();
|
873
|
899
|
}
|
874
|
|
-
|
|
900
|
+
|
875
|
901
|
HighLevelDefinition definition = addClass(rawType);
|
876
|
902
|
return registry.getForDefinition(definition, codeParameters).stored();
|
877
|
|
- } else if (type instanceof TypeVariable) {
|
878
|
|
- TypeVariable variable = (TypeVariable) type;
|
|
903
|
+ } else if (type instanceof TypeVariable<?>) {
|
|
904
|
+ TypeVariable<?> variable = (TypeVariable<?>) type;
|
879
|
905
|
return registry.getGeneric(context.get(variable)).stored();
|
880
|
906
|
}else if(type instanceof AnnotatedType){
|
881
|
|
- final StoredType storedType;
|
|
907
|
+ StoredType storedType;
|
882
|
908
|
if(type instanceof AnnotatedParameterizedType) {
|
883
|
909
|
AnnotatedParameterizedType parameterizedType = (AnnotatedParameterizedType) type;
|
|
910
|
+
|
884
|
911
|
final Type rawType = ((ParameterizedType) parameterizedType.getType()).getRawType();
|
885
|
912
|
final AnnotatedType[] actualTypeArguments = parameterizedType.getAnnotatedActualTypeArguments();
|
886
|
913
|
final StoredType[] codeParameters = new StoredType[actualTypeArguments.length];
|
887
|
914
|
for(int i = 0; i < actualTypeArguments.length; i++) {
|
888
|
915
|
codeParameters[i] = loadType(context, actualTypeArguments[i], false, false);
|
889
|
916
|
}
|
890
|
|
-
|
|
917
|
+
|
891
|
918
|
if(rawType == Map.class) {
|
892
|
919
|
storedType = registry.getAssociative(codeParameters[0], codeParameters[1]).stored();
|
893
|
|
- } else {
|
894
|
|
- HighLevelDefinition definition = addClass((Class<?>) rawType);
|
895
|
|
- storedType = registry.getForDefinition(definition, codeParameters).stored();
|
|
920
|
+ } else if(rawType instanceof Class<?>){
|
|
921
|
+ final Map<TypeParameter, StoredType> map = new HashMap<>();
|
|
922
|
+ final TypeVariable<? extends Class<?>>[] typeParameters = ((Class<?>) rawType).getTypeParameters();
|
|
923
|
+ final StoredType loadType = loadType(context, (AnnotatedElement) rawType, unsigned);
|
|
924
|
+ for (int i = 0; i < typeParameters.length; i++) {
|
|
925
|
+ final TypeParameter typeParameter = context.get(typeParameters[i]);
|
|
926
|
+ map.put(typeParameter, codeParameters[i]);
|
|
927
|
+ }
|
|
928
|
+ storedType = loadType.instance(new GenericMapper(CodePosition.NATIVE, registry, map));
|
|
929
|
+ } else {
|
|
930
|
+ storedType = loadType(context, (AnnotatedElement) rawType, unsigned);
|
896
|
931
|
}
|
897
|
932
|
} else {
|
898
|
|
- storedType = loadType(context, (AnnotatedElement) ((AnnotatedType) type).getType(), unsigned);
|
|
933
|
+ if (((AnnotatedType) type).getType() instanceof WildcardType) {
|
|
934
|
+ storedType = BasicTypeID.UNDETERMINED.stored();
|
|
935
|
+ } else {
|
|
936
|
+ storedType = loadType(context, (AnnotatedElement) ((AnnotatedType) type).getType(), unsigned);
|
|
937
|
+ }
|
899
|
938
|
}
|
900
|
|
-
|
|
939
|
+
|
901
|
940
|
if(type.isAnnotationPresent(ZenCodeStorageTag.class)) {
|
902
|
941
|
//Replace with switch if more StorageTagTypes are added
|
903
|
942
|
if(type.getAnnotation(ZenCodeStorageTag.class).value() == StorageTagType.STATIC) {
|
|
@@ -905,22 +944,22 @@ public class JavaNativeModule {
|
905
|
944
|
}
|
906
|
945
|
}
|
907
|
946
|
return storedType;
|
908
|
|
-
|
|
947
|
+
|
909
|
948
|
} else {
|
910
|
949
|
throw new IllegalArgumentException("Could not analyze type: " + type);
|
911
|
950
|
}
|
912
|
951
|
}
|
913
|
|
-
|
|
952
|
+
|
914
|
953
|
private StoredType loadFunctionalInterface(TypeVariableContext loadContext, Class<?> cls, AnnotatedElement[] parameters) {
|
915
|
954
|
Method functionalInterfaceMethod = getFunctionalInterfaceMethod(cls);
|
916
|
955
|
TypeVariableContext context = convertTypeParameters(cls);
|
917
|
956
|
FunctionHeader header = getHeader(context, functionalInterfaceMethod);
|
918
|
|
-
|
|
957
|
+
|
919
|
958
|
Map<TypeParameter, StoredType> mapping = new HashMap<>();
|
920
|
959
|
TypeVariable[] javaParameters = cls.getTypeParameters();
|
921
|
|
- for (int i = 0; i < javaParameters.length; i++)
|
|
960
|
+ for (int i = 0; i < parameters.length; i++)
|
922
|
961
|
mapping.put(context.get(javaParameters[i]), loadType(loadContext, parameters[i], false, false));
|
923
|
|
-
|
|
962
|
+
|
924
|
963
|
JavaMethod method = new JavaMethod(
|
925
|
964
|
JavaClass.fromInternalName(getInternalName(cls), JavaClass.Kind.INTERFACE),
|
926
|
965
|
JavaMethod.Kind.INTERFACE,
|
|
@@ -932,71 +971,77 @@ public class JavaNativeModule {
|
932
|
971
|
StorageTag tag = new JavaFunctionalInterfaceStorageTag(functionalInterfaceMethod, method);
|
933
|
972
|
return registry.getFunction(header).stored(tag);
|
934
|
973
|
}
|
935
|
|
-
|
|
974
|
+
|
936
|
975
|
private <T> TypeVariableContext convertTypeParameters(Class<T> cls) {
|
937
|
|
- TypeVariableContext context = new TypeVariableContext();
|
|
976
|
+ //TypeVariableContext context = new TypeVariableContext();
|
938
|
977
|
TypeVariable<Class<T>>[] javaTypeParameters = cls.getTypeParameters();
|
939
|
978
|
TypeParameter[] typeParameters = new TypeParameter[cls.getTypeParameters().length];
|
|
979
|
+
|
940
|
980
|
for (int i = 0; i < javaTypeParameters.length; i++) {
|
941
|
981
|
TypeVariable<Class<T>> typeVariable = javaTypeParameters[i];
|
942
|
982
|
TypeParameter parameter = new TypeParameter(CodePosition.NATIVE, typeVariable.getName());
|
943
|
|
- for (AnnotatedType bound : typeVariable.getAnnotatedBounds()) {
|
944
|
|
- TypeID type = loadType(context, bound).type;
|
945
|
|
- parameter.addBound(new ParameterTypeBound(CodePosition.NATIVE, type));
|
946
|
|
- }
|
947
|
983
|
typeParameters[i] = parameter;
|
|
984
|
+
|
|
985
|
+ //Put up here so that Nested Type parameters may work..?
|
948
|
986
|
context.put(typeVariable, parameter);
|
949
|
987
|
}
|
|
988
|
+
|
|
989
|
+ for (int i = 0; i < javaTypeParameters.length; i++) {
|
|
990
|
+ for (AnnotatedType bound : javaTypeParameters[i].getAnnotatedBounds()) {
|
|
991
|
+ TypeID type = loadType(context, bound).type;
|
|
992
|
+ typeParameters[i].addBound(new ParameterTypeBound(CodePosition.NATIVE, type));
|
|
993
|
+ }
|
|
994
|
+ }
|
950
|
995
|
return context;
|
951
|
996
|
}
|
952
|
|
-
|
|
997
|
+
|
953
|
998
|
private Method getFunctionalInterfaceMethod(Class<?> functionalInterface) {
|
954
|
999
|
for (Method method : functionalInterface.getDeclaredMethods()) {
|
955
|
1000
|
if (!method.isDefault())
|
956
|
1001
|
return method;
|
957
|
1002
|
}
|
958
|
|
-
|
|
1003
|
+
|
959
|
1004
|
return null;
|
960
|
1005
|
}
|
961
|
|
-
|
|
1006
|
+
|
962
|
1007
|
private int getMethodModifiers(Member method) {
|
963
|
1008
|
int result = Modifiers.PUBLIC;
|
964
|
1009
|
if (isStatic(method.getModifiers()))
|
965
|
1010
|
result |= Modifiers.STATIC;
|
966
|
1011
|
if (isFinal(method.getModifiers()))
|
967
|
1012
|
result |= Modifiers.FINAL;
|
968
|
|
-
|
|
1013
|
+
|
969
|
1014
|
return result;
|
970
|
1015
|
}
|
971
|
|
-
|
|
1016
|
+
|
972
|
1017
|
private static boolean isPublic(int modifiers) {
|
973
|
1018
|
return (modifiers & Modifier.PUBLIC) > 0;
|
974
|
1019
|
}
|
975
|
|
-
|
|
1020
|
+
|
976
|
1021
|
private static boolean isStatic(int modifiers) {
|
977
|
1022
|
return (modifiers & Modifier.STATIC) > 0;
|
978
|
1023
|
}
|
979
|
|
-
|
|
1024
|
+
|
980
|
1025
|
private static boolean isFinal(int modifiers) {
|
981
|
1026
|
return (modifiers & Modifier.FINAL) > 0;
|
982
|
1027
|
}
|
983
|
|
-
|
|
1028
|
+
|
984
|
1029
|
private static String getInternalName(Class<?> cls) {
|
985
|
1030
|
return org.objectweb.asm.Type.getInternalName(cls);
|
986
|
1031
|
}
|
987
|
|
-
|
|
1032
|
+
|
988
|
1033
|
private static String getDescriptor(Class<?> cls) {
|
989
|
1034
|
return org.objectweb.asm.Type.getDescriptor(cls);
|
990
|
1035
|
}
|
991
|
|
-
|
|
1036
|
+
|
992
|
1037
|
private static String getMethodDescriptor(java.lang.reflect.Constructor constructor) {
|
993
|
1038
|
return org.objectweb.asm.Type.getConstructorDescriptor(constructor);
|
994
|
1039
|
}
|
995
|
|
-
|
|
1040
|
+
|
996
|
1041
|
private static String getMethodDescriptor(Method method) {
|
997
|
1042
|
return org.objectweb.asm.Type.getMethodDescriptor(method);
|
998
|
1043
|
}
|
999
|
|
-
|
|
1044
|
+
|
1000
|
1045
|
private static JavaMethod getMethod(JavaClass cls, java.lang.reflect.Constructor constructor) {
|
1001
|
1046
|
return new JavaMethod(
|
1002
|
1047
|
cls,
|
|
@@ -1007,7 +1052,7 @@ public class JavaNativeModule {
|
1007
|
1052
|
constructor.getModifiers(),
|
1008
|
1053
|
false);
|
1009
|
1054
|
}
|
1010
|
|
-
|
|
1055
|
+
|
1011
|
1056
|
private static JavaMethod getMethod(JavaClass cls, Method method, StoredType result) {
|
1012
|
1057
|
JavaMethod.Kind kind;
|
1013
|
1058
|
if (method.getName().equals("<init>"))
|
|
@@ -1018,7 +1063,7 @@ public class JavaNativeModule {
|
1018
|
1063
|
kind = JavaMethod.Kind.STATIC;
|
1019
|
1064
|
else
|
1020
|
1065
|
kind = JavaMethod.Kind.INSTANCE;
|
1021
|
|
-
|
|
1066
|
+
|
1022
|
1067
|
return new JavaMethod(cls, kind, method.getName(), false, getMethodDescriptor(method), method.getModifiers(), result.isGeneric());
|
1023
|
1068
|
}
|
1024
|
1069
|
|
|
@@ -1028,15 +1073,15 @@ public class JavaNativeModule {
|
1028
|
1073
|
|
1029
|
1074
|
private static class TypeVariableContext {
|
1030
|
1075
|
private final Map<TypeVariable, TypeParameter> typeVariables = new HashMap<>();
|
1031
|
|
-
|
|
1076
|
+
|
1032
|
1077
|
public void put(TypeVariable variable, TypeParameter parameter) {
|
1033
|
1078
|
typeVariables.put(variable, parameter);
|
1034
|
1079
|
}
|
1035
|
|
-
|
|
1080
|
+
|
1036
|
1081
|
public TypeParameter get(TypeVariable variable) {
|
1037
|
1082
|
if (!typeVariables.containsKey(variable))
|
1038
|
1083
|
throw new IllegalStateException("Could not find type variable " + variable.getName());
|
1039
|
|
-
|
|
1084
|
+
|
1040
|
1085
|
return typeVariables.get(variable);
|
1041
|
1086
|
}
|
1042
|
1087
|
}
|