Browse Source

Test-Only: Changed JavaNativeModule dependency management

It now delegates all calls it doesn't know to its dependencies
kindlich 5 years ago
parent
commit
dcf0cb55ff
No known key found for this signature in database

+ 39
- 9
JavaIntegration/src/main/java/org/openzen/zencode/java/JavaNativeModule.java View File

76
 	private final JavaCompiledModule compiled;
76
 	private final JavaCompiledModule compiled;
77
 	
77
 	
78
 	private final Map<Class<?>, HighLevelDefinition> definitionByClass = new HashMap<>();
78
 	private final Map<Class<?>, HighLevelDefinition> definitionByClass = new HashMap<>();
79
+	private final JavaNativeModule[] dependencies;
79
 	private final Map<Class<?>, TypeID> typeByClass = new HashMap<>();
80
 	private final Map<Class<?>, TypeID> typeByClass = new HashMap<>();
80
 	private final Map<Class<?>, TypeID> unsignedByClass = new HashMap<>();
81
 	private final Map<Class<?>, TypeID> unsignedByClass = new HashMap<>();
81
 	
82
 	
91
 		this.basePackage = basePackage;
92
 		this.basePackage = basePackage;
92
 		module = new Module(name);
93
 		module = new Module(name);
93
 		this.registry = registry;
94
 		this.registry = registry;
94
-		
95
-		for (JavaNativeModule dependency : dependencies) {
96
-			definitionByClass.putAll(dependency.definitionByClass);
97
-		}
95
+		this.dependencies = dependencies;
98
 		
96
 		
99
 		compiled = new JavaCompiledModule(module, FunctionParameter.NONE);
97
 		compiled = new JavaCompiledModule(module, FunctionParameter.NONE);
100
 		
98
 		
145
 		return compiled;
143
 		return compiled;
146
 	}
144
 	}
147
 	
145
 	
146
+	private HighLevelDefinition getDefinitionForClass(Class<?> cls) {
147
+		HighLevelDefinition definition = definitionByClass.get(cls);
148
+		
149
+		if(definition != null)
150
+			return definition;
151
+		for (JavaNativeModule dependency : this.dependencies) {
152
+			definition = dependency.getDefinitionForClass(cls);
153
+			if(definition != null)
154
+				return definition;
155
+		}
156
+		return null;
157
+	}
158
+	
148
 	public HighLevelDefinition addClass(Class<?> cls) {
159
 	public HighLevelDefinition addClass(Class<?> cls) {
149
-		if (definitionByClass.containsKey(cls))
150
-			return definitionByClass.get(cls);
160
+		HighLevelDefinition result = getDefinitionForClass(cls);
161
+		if(result != null)
162
+			return result;
151
 		
163
 		
152
-		HighLevelDefinition result = convertClass(cls);
164
+		result = convertClass(cls);
153
 		return result;
165
 		return result;
154
 	}
166
 	}
155
 	
167
 	
216
 		return className.startsWith(basePackage + ".");
228
 		return className.startsWith(basePackage + ".");
217
 	}
229
 	}
218
 	
230
 	
231
+	private ZSPackage getPackageOrNull(String className) {
232
+		ZSPackage zsPackage;
233
+		for (JavaNativeModule dependency : this.dependencies) {
234
+			zsPackage = dependency.getPackageOrNull(className);
235
+			if(zsPackage != null)
236
+				return zsPackage;
237
+		}
238
+		
239
+		if(className.startsWith(this.basePackage))
240
+			return getPackage(className);
241
+		return null;
242
+	}
243
+	
244
+	
219
 	private ZSPackage getPackage(String className) {
245
 	private ZSPackage getPackage(String className) {
220
 		//TODO make a lang package?
246
 		//TODO make a lang package?
221
 		if (!className.contains(".") || className.startsWith("java.lang"))
247
 		if (!className.contains(".") || className.startsWith("java.lang"))
225
 			className = className.substring(1);
251
 			className = className.substring(1);
226
 		else if (className.startsWith(basePackage + "."))
252
 		else if (className.startsWith(basePackage + "."))
227
 			className = className.substring(basePackage.length() + 1);
253
 			className = className.substring(basePackage.length() + 1);
228
-		else
229
-			throw new IllegalArgumentException("Invalid class name: not in the given base package");
254
+		else {
255
+			ZSPackage zsPackage = getPackageOrNull(className);
256
+			if(zsPackage != null)
257
+				return zsPackage;
258
+			throw new IllegalArgumentException(String.format("Invalid class name: %s not in the given base package[%s]", className, this.basePackage));
259
+		}
230
 		
260
 		
231
 		String[] classNameParts = Strings.split(className, '.');
261
 		String[] classNameParts = Strings.split(className, '.');
232
 		ZSPackage classPkg = pkg;
262
 		ZSPackage classPkg = pkg;

Loading…
Cancel
Save