From 93581304688ad64cbff4664e39ce76ef924ee464 Mon Sep 17 00:00:00 2001 From: Daniel Holle Date: Thu, 18 Apr 2024 11:51:08 +0200 Subject: [PATCH] Fix package weirdness? See #322 --- .../de/dhbwstuttgart/core/JavaTXCompiler.java | 28 +++++++++++-------- .../environment/CompilationEnvironment.java | 7 +++-- .../dhbwstuttgart/syntaxtree/SourceFile.java | 14 ++++++++++ 3 files changed, 35 insertions(+), 14 deletions(-) diff --git a/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java b/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java index d096935c..a77ff228 100644 --- a/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java +++ b/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java @@ -70,7 +70,7 @@ public class JavaTXCompiler { public volatile UnifyTaskModel usedTasks = new UnifyTaskModel(); public final DirectoryClassLoader classLoader; - private final List classPath; + public final List classPath; private final File outputPath; public DirectoryClassLoader getClassLoader() { @@ -670,9 +670,10 @@ public class JavaTXCompiler { environment.addClassesToRegistry(classRegistry, tree, sourceFile, this); SyntaxTreeGenerator generator = new SyntaxTreeGenerator(this, classRegistry, new GenericsRegistry(null), sourceFile.getName()); var classes = new ArrayList(); - var sf = new SourceFile(generator.pkgName, classes, generator.imports); + var sf = new SourceFile("", classes, generator.imports); addSourceFile(sourceFile, sf); generator.convert(classes, tree, environment.packageCrawler); + sf.setPackageName(generator.pkgName); sf.imports.addAll(generator.imports); return sf; } @@ -692,20 +693,17 @@ public class JavaTXCompiler { environment.addClassesToRegistry(classRegistry, tree, file, this); SyntaxTreeGenerator generator = new SyntaxTreeGenerator(this, classRegistry, new GenericsRegistry(null), file.getName()); var classes = new ArrayList(); - var sf = new SourceFile(generator.pkgName, classes, generator.imports); + var sf = new SourceFile("", classes, generator.imports); addSourceFile(file, sf); generator.convert(classes, tree, environment.packageCrawler); + sf.setPackageName(generator.pkgName); var classFiles = generateBytecode(file); - File path = outputPath; - if (outputPath == null) { - path = file.getParentFile(); // Set path to path of the parsed .jav file - } - - writeClassFile(classFiles, path, outputPath == null); + sf.setGenerated(); + writeClassFile(classFiles, outputPath != null ? outputPath : new File("."), false); var clazz = classLoader.loadClass(name.toString()); var classOrInterface = ASTFactory.createClass(clazz); - loadedClasses.put(name, new ClassEntry(new File(path, clazz.getName() + ".class"), classOrInterface)); + loadedClasses.put(name, new ClassEntry(new File(outputPath, clazz.getName() + ".class"), classOrInterface)); return classOrInterface; } catch (Exception e) { throw new RuntimeException(e); @@ -726,8 +724,11 @@ public class JavaTXCompiler { public void generateBytecode() throws ClassNotFoundException, IOException { for (var file : sourceFiles.keySet()) { + var sf = sourceFiles.get(file); + if (sf.isGenerated()) continue; var classes = generateBytecode(file); - writeClassFile(classes, outputPath, outputPath == null); + sf.setGenerated(); + writeClassFile(classes, outputPath == null ? file.getParentFile() : outputPath, outputPath == null); } } @@ -736,8 +737,10 @@ public class JavaTXCompiler { * @return */ public Map generateBytecode(File sourceFile) throws ClassNotFoundException, IOException { + var sf = sourceFiles.get(sourceFile); + if (sf.isGenerated()) return null; List typeinferenceResult = this.typeInference(sourceFile); - return generateBytecode(sourceFiles.get(sourceFile), typeinferenceResult); + return generateBytecode(sf, typeinferenceResult); } private Map> generatedGenerics = new HashMap<>(); @@ -788,6 +791,7 @@ public class JavaTXCompiler { var subPath = preserveHierarchy ? path : Path.of(path.toString(), name.getPackageName().split("\\.")).toFile(); File outputFile = new File(subPath, name.getClassName() + ".class"); outputFile.getAbsoluteFile().getParentFile().mkdirs(); + System.out.println(outputFile); output = new FileOutputStream(outputFile); output.write(bytecode); output.close(); diff --git a/src/main/java/de/dhbwstuttgart/environment/CompilationEnvironment.java b/src/main/java/de/dhbwstuttgart/environment/CompilationEnvironment.java index 0e2a4534..4b802fe5 100644 --- a/src/main/java/de/dhbwstuttgart/environment/CompilationEnvironment.java +++ b/src/main/java/de/dhbwstuttgart/environment/CompilationEnvironment.java @@ -77,7 +77,7 @@ public class CompilationEnvironment { // Set classLoader to include default package for this specific source file File dir = sourceFile.getAbsoluteFile().getParentFile(); String dirPath = dir.toString() + "/"; - if (packageName.length() > 0) + if (!packageName.isEmpty()) dirPath = dirPath.substring(0, dirPath.length() - packageName.length() - 1); String path = dirPath; ArrayList defaultPath = Lists.newArrayList(new File(path)); @@ -89,7 +89,10 @@ public class CompilationEnvironment { String className = classFile.getName().substring(0, classFile.getName().length() - 6); if (className.matches("Fun\\d+\\$\\$.*")) continue; - ret.add(classLoader.loadClass(packageName + className)); + var name = packageName; + if (!packageName.isEmpty()) name += "."; + name += className; + ret.add(classLoader.loadClass(name)); } return ret; } diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/SourceFile.java b/src/main/java/de/dhbwstuttgart/syntaxtree/SourceFile.java index a0a7e42e..12b77616 100644 --- a/src/main/java/de/dhbwstuttgart/syntaxtree/SourceFile.java +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/SourceFile.java @@ -15,6 +15,8 @@ public class SourceFile extends SyntaxTreeNode { public final List KlassenVektor; public final Set imports; + private boolean isGenerated; + public List availableClasses = new ArrayList<>(); /** @@ -38,6 +40,18 @@ public class SourceFile extends SyntaxTreeNode { this.imports = new HashSet<>(sf.imports); } + public void setPackageName(String packageName) { + this.pkgName = packageName; + } + + public void setGenerated() { + this.isGenerated = true; + } + + public boolean isGenerated() { + return this.isGenerated; + } + public String getPkgName() { return this.pkgName; }