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
 
36
 
37
 		if(name.startsWith("java")) {
37
 		if(name.startsWith("java")) {
38
 			logger.debug("Warning: Invalid name " + name);
38
 			logger.debug("Warning: Invalid name " + name);
39
+		} else if(classes.containsKey(name)) {
40
+			logger.error("Trying to register " + name + " a 2nd time");
39
 		} else {
41
 		} else {
40
 			classes.put(name, bytecode);
42
 			classes.put(name, bytecode);
41
 		}
43
 		}

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

5
  */
5
  */
6
 package org.openzen.zenscript.javabytecode;
6
 package org.openzen.zenscript.javabytecode;
7
 
7
 
8
-import java.util.LinkedHashMap;
9
-import java.util.Map;
8
+import java.util.*;
9
+
10
 import org.objectweb.asm.ClassWriter;
10
 import org.objectweb.asm.ClassWriter;
11
 import org.objectweb.asm.Opcodes;
11
 import org.objectweb.asm.Opcodes;
12
 import org.openzen.zencode.shared.CodePosition;
12
 import org.openzen.zencode.shared.CodePosition;
39
     
39
     
40
     private int generatedScriptBlockCounter = 0;
40
     private int generatedScriptBlockCounter = 0;
41
 	private int expansionCounter = 0;
41
 	private int expansionCounter = 0;
42
-	private IZSLogger logger;
42
+	private final IZSLogger logger;
43
     
43
     
44
     public JavaCompiler(IZSLogger logger) {
44
     public JavaCompiler(IZSLogger logger) {
45
         this.logger = logger;
45
         this.logger = logger;
47
 	
47
 	
48
 	public JavaBytecodeModule compile(String packageName, SemanticModule module, JavaCompileSpace space) {
48
 	public JavaBytecodeModule compile(String packageName, SemanticModule module, JavaCompileSpace space) {
49
 		Map<String, JavaScriptFile> scriptBlocks = new LinkedHashMap<>();
49
 		Map<String, JavaScriptFile> scriptBlocks = new LinkedHashMap<>();
50
+		Set<JavaScriptFile> scriptFilesThatAreActuallyUsedInScripts = new HashSet<>();
50
 		
51
 		
51
 		JavaBytecodeModule target = new JavaBytecodeModule(module.module, module.parameters, logger);
52
 		JavaBytecodeModule target = new JavaBytecodeModule(module.module, module.parameters, logger);
52
 		JavaBytecodeContext context = new JavaBytecodeContext(target, space, module.modulePackage, packageName, logger);
53
 		JavaBytecodeContext context = new JavaBytecodeContext(target, space, module.modulePackage, packageName, logger);
55
 		for (HighLevelDefinition definition : module.definitions.getAll()) {
56
 		for (HighLevelDefinition definition : module.definitions.getAll()) {
56
 			final String className = getClassName(getFilename(definition));
57
 			final String className = getClassName(getFilename(definition));
57
 			String filename = className + "_" + (definition.name == null ? "generated" : definition.name) + "_" + expansionCounter++;
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
 			definition.accept(definitionPreparer);
60
 			definition.accept(definitionPreparer);
60
 		}
61
 		}
61
 		
62
 		
65
 		}
66
 		}
66
 		
67
 		
67
 		for (HighLevelDefinition definition : module.definitions.getAll()) {
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
 			target.addClass(cls.internalName, definition.accept(new JavaDefinitionVisitor(context, scriptFile.classWriter)));
72
 			target.addClass(cls.internalName, definition.accept(new JavaDefinitionVisitor(context, scriptFile.classWriter)));
74
 		}
73
 		}
75
 		
74
 		
87
 			final SourceFile sourceFile = script.file;
86
 			final SourceFile sourceFile = script.file;
88
 			final String className = getClassName(sourceFile == null ? null : sourceFile.getFilename());
87
 			final String className = getClassName(sourceFile == null ? null : sourceFile.getFilename());
89
 			JavaScriptFile scriptFile = getScriptFile(scriptBlocks, script.pkg.fullName + "/" + className);
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
 			String methodName = scriptFile.scriptMethods.isEmpty() ? "run" : "run" + scriptFile.scriptMethods.size();
94
 			String methodName = scriptFile.scriptMethods.isEmpty() ? "run" : "run" + scriptFile.scriptMethods.size();
93
 
95
 
110
 				target.addScript(method);
112
 				target.addScript(method);
111
 
113
 
112
 			entry.getValue().classWriter.visitEnd();
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
 				target.addClass(entry.getKey(), entry.getValue().classWriter.toByteArray());
116
 				target.addClass(entry.getKey(), entry.getValue().classWriter.toByteArray());
119
 			}
117
 			}
120
 		}
118
 		}

Loading…
Cancel
Save