Browse Source

Fix duplicate Class Warnings

Expansions classes now properly get a number suffix so that expansion classes and scripts no longer share the same name. Also fixes that scripts and expansions cant be in one file
kindlich 4 years ago
parent
commit
6aae26601c
No known key found for this signature in database

+ 2
- 0
JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/JavaBytecodeModule.java View File

@@ -36,6 +36,8 @@ public class JavaBytecodeModule extends JavaCompiledModule {
36 36
 
37 37
 		if(name.startsWith("java")) {
38 38
 			logger.debug("Warning: Invalid name " + name);
39
+		} else if(classes.containsKey(name)) {
40
+			logger.error("Trying to register " + name + " a 2nd time");
39 41
 		} else {
40 42
 			classes.put(name, bytecode);
41 43
 		}

+ 13
- 15
JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/JavaCompiler.java View File

@@ -5,8 +5,8 @@
5 5
  */
6 6
 package org.openzen.zenscript.javabytecode;
7 7
 
8
-import java.util.LinkedHashMap;
9
-import java.util.Map;
8
+import java.util.*;
9
+
10 10
 import org.objectweb.asm.ClassWriter;
11 11
 import org.objectweb.asm.Opcodes;
12 12
 import org.openzen.zencode.shared.CodePosition;
@@ -39,7 +39,7 @@ public class JavaCompiler {
39 39
     
40 40
     private int generatedScriptBlockCounter = 0;
41 41
 	private int expansionCounter = 0;
42
-	private IZSLogger logger;
42
+	private final IZSLogger logger;
43 43
     
44 44
     public JavaCompiler(IZSLogger logger) {
45 45
         this.logger = logger;
@@ -47,6 +47,7 @@ public class JavaCompiler {
47 47
 	
48 48
 	public JavaBytecodeModule compile(String packageName, SemanticModule module, JavaCompileSpace space) {
49 49
 		Map<String, JavaScriptFile> scriptBlocks = new LinkedHashMap<>();
50
+		Set<JavaScriptFile> scriptFilesThatAreActuallyUsedInScripts = new HashSet<>();
50 51
 		
51 52
 		JavaBytecodeModule target = new JavaBytecodeModule(module.module, module.parameters, logger);
52 53
 		JavaBytecodeContext context = new JavaBytecodeContext(target, space, module.modulePackage, packageName, logger);
@@ -55,7 +56,7 @@ public class JavaCompiler {
55 56
 		for (HighLevelDefinition definition : module.definitions.getAll()) {
56 57
 			final String className = getClassName(getFilename(definition));
57 58
 			String filename = className + "_" + (definition.name == null ? "generated" : definition.name) + "_" + expansionCounter++;
58
-			JavaPrepareDefinitionVisitor definitionPreparer = new JavaPrepareDefinitionVisitor(context, target, filename, null, className);
59
+			JavaPrepareDefinitionVisitor definitionPreparer = new JavaPrepareDefinitionVisitor(context, target, filename, null, filename);
59 60
 			definition.accept(definitionPreparer);
60 61
 		}
61 62
 		
@@ -65,11 +66,9 @@ public class JavaCompiler {
65 66
 		}
66 67
 		
67 68
 		for (HighLevelDefinition definition : module.definitions.getAll()) {
68
-			String className = getClassName(definition.position.getFilename());
69
-			JavaScriptFile scriptFile = getScriptFile(scriptBlocks, definition.pkg.fullName + "/" + className);
70
-			scriptFile.classWriter.visitSource(definition.position.getFilename(), null);
71
-			
72
-			JavaClass cls = definition instanceof ExpansionDefinition ? context.getJavaExpansionClass(definition) : context.getJavaClass(definition);
69
+            JavaClass cls = definition instanceof ExpansionDefinition ? context.getJavaExpansionClass(definition) : context.getJavaClass(definition);
70
+            JavaScriptFile scriptFile = getScriptFile(scriptBlocks, cls.fullName);
71
+            scriptFile.classWriter.visitSource(definition.position.getFilename(), null);
73 72
 			target.addClass(cls.internalName, definition.accept(new JavaDefinitionVisitor(context, scriptFile.classWriter)));
74 73
 		}
75 74
 		
@@ -87,7 +86,10 @@ public class JavaCompiler {
87 86
 			final SourceFile sourceFile = script.file;
88 87
 			final String className = getClassName(sourceFile == null ? null : sourceFile.getFilename());
89 88
 			JavaScriptFile scriptFile = getScriptFile(scriptBlocks, script.pkg.fullName + "/" + className);
90
-			scriptFile.classWriter.visitSource(script.file.getFilename(), null);
89
+			scriptFilesThatAreActuallyUsedInScripts.add(scriptFile);
90
+			if(sourceFile != null) {
91
+                scriptFile.classWriter.visitSource(sourceFile.getFilename(), null);
92
+            }
91 93
 
92 94
 			String methodName = scriptFile.scriptMethods.isEmpty() ? "run" : "run" + scriptFile.scriptMethods.size();
93 95
 
@@ -110,11 +112,7 @@ public class JavaCompiler {
110 112
 				target.addScript(method);
111 113
 
112 114
 			entry.getValue().classWriter.visitEnd();
113
-
114
-			if (target.getClasses().containsKey(entry.getKey())) {
115
-				//TODO Scripts and definitions seem to create the same class. Bad!
116
-				logger.warning("Warning: Trying to register " + entry.getKey() + " a 2nd time");
117
-			}else{
115
+			if(scriptFilesThatAreActuallyUsedInScripts.contains(entry.getValue())) {
118 116
 				target.addClass(entry.getKey(), entry.getValue().classWriter.toByteArray());
119 117
 			}
120 118
 		}

Loading…
Cancel
Save