Fix package weirdness? See #322
All checks were successful
Build and Test with Maven / Build-and-test-with-Maven (push) Successful in 3m3s

This commit is contained in:
Daniel Holle 2024-04-18 11:51:08 +02:00
parent 708aa64283
commit 9358130468
3 changed files with 35 additions and 14 deletions

View File

@ -70,7 +70,7 @@ public class JavaTXCompiler {
public volatile UnifyTaskModel usedTasks = new UnifyTaskModel(); public volatile UnifyTaskModel usedTasks = new UnifyTaskModel();
public final DirectoryClassLoader classLoader; public final DirectoryClassLoader classLoader;
private final List<File> classPath; public final List<File> classPath;
private final File outputPath; private final File outputPath;
public DirectoryClassLoader getClassLoader() { public DirectoryClassLoader getClassLoader() {
@ -670,9 +670,10 @@ public class JavaTXCompiler {
environment.addClassesToRegistry(classRegistry, tree, sourceFile, this); environment.addClassesToRegistry(classRegistry, tree, sourceFile, this);
SyntaxTreeGenerator generator = new SyntaxTreeGenerator(this, classRegistry, new GenericsRegistry(null), sourceFile.getName()); SyntaxTreeGenerator generator = new SyntaxTreeGenerator(this, classRegistry, new GenericsRegistry(null), sourceFile.getName());
var classes = new ArrayList<ClassOrInterface>(); var classes = new ArrayList<ClassOrInterface>();
var sf = new SourceFile(generator.pkgName, classes, generator.imports); var sf = new SourceFile("", classes, generator.imports);
addSourceFile(sourceFile, sf); addSourceFile(sourceFile, sf);
generator.convert(classes, tree, environment.packageCrawler); generator.convert(classes, tree, environment.packageCrawler);
sf.setPackageName(generator.pkgName);
sf.imports.addAll(generator.imports); sf.imports.addAll(generator.imports);
return sf; return sf;
} }
@ -692,20 +693,17 @@ public class JavaTXCompiler {
environment.addClassesToRegistry(classRegistry, tree, file, this); environment.addClassesToRegistry(classRegistry, tree, file, this);
SyntaxTreeGenerator generator = new SyntaxTreeGenerator(this, classRegistry, new GenericsRegistry(null), file.getName()); SyntaxTreeGenerator generator = new SyntaxTreeGenerator(this, classRegistry, new GenericsRegistry(null), file.getName());
var classes = new ArrayList<ClassOrInterface>(); var classes = new ArrayList<ClassOrInterface>();
var sf = new SourceFile(generator.pkgName, classes, generator.imports); var sf = new SourceFile("", classes, generator.imports);
addSourceFile(file, sf); addSourceFile(file, sf);
generator.convert(classes, tree, environment.packageCrawler); generator.convert(classes, tree, environment.packageCrawler);
sf.setPackageName(generator.pkgName);
var classFiles = generateBytecode(file); var classFiles = generateBytecode(file);
File path = outputPath; sf.setGenerated();
if (outputPath == null) { writeClassFile(classFiles, outputPath != null ? outputPath : new File("."), false);
path = file.getParentFile(); // Set path to path of the parsed .jav file
}
writeClassFile(classFiles, path, outputPath == null);
var clazz = classLoader.loadClass(name.toString()); var clazz = classLoader.loadClass(name.toString());
var classOrInterface = ASTFactory.createClass(clazz); 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; return classOrInterface;
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException(e); throw new RuntimeException(e);
@ -726,8 +724,11 @@ public class JavaTXCompiler {
public void generateBytecode() throws ClassNotFoundException, IOException { public void generateBytecode() throws ClassNotFoundException, IOException {
for (var file : sourceFiles.keySet()) { for (var file : sourceFiles.keySet()) {
var sf = sourceFiles.get(file);
if (sf.isGenerated()) continue;
var classes = generateBytecode(file); 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 * @return
*/ */
public Map<JavaClassName, byte[]> generateBytecode(File sourceFile) throws ClassNotFoundException, IOException { public Map<JavaClassName, byte[]> generateBytecode(File sourceFile) throws ClassNotFoundException, IOException {
var sf = sourceFiles.get(sourceFile);
if (sf.isGenerated()) return null;
List<ResultSet> typeinferenceResult = this.typeInference(sourceFile); List<ResultSet> typeinferenceResult = this.typeInference(sourceFile);
return generateBytecode(sourceFiles.get(sourceFile), typeinferenceResult); return generateBytecode(sf, typeinferenceResult);
} }
private Map<SourceFile, List<GenericsResult>> generatedGenerics = new HashMap<>(); private Map<SourceFile, List<GenericsResult>> generatedGenerics = new HashMap<>();
@ -788,6 +791,7 @@ public class JavaTXCompiler {
var subPath = preserveHierarchy ? path : Path.of(path.toString(), name.getPackageName().split("\\.")).toFile(); var subPath = preserveHierarchy ? path : Path.of(path.toString(), name.getPackageName().split("\\.")).toFile();
File outputFile = new File(subPath, name.getClassName() + ".class"); File outputFile = new File(subPath, name.getClassName() + ".class");
outputFile.getAbsoluteFile().getParentFile().mkdirs(); outputFile.getAbsoluteFile().getParentFile().mkdirs();
System.out.println(outputFile);
output = new FileOutputStream(outputFile); output = new FileOutputStream(outputFile);
output.write(bytecode); output.write(bytecode);
output.close(); output.close();

View File

@ -77,7 +77,7 @@ public class CompilationEnvironment {
// Set classLoader to include default package for this specific source file // Set classLoader to include default package for this specific source file
File dir = sourceFile.getAbsoluteFile().getParentFile(); File dir = sourceFile.getAbsoluteFile().getParentFile();
String dirPath = dir.toString() + "/"; String dirPath = dir.toString() + "/";
if (packageName.length() > 0) if (!packageName.isEmpty())
dirPath = dirPath.substring(0, dirPath.length() - packageName.length() - 1); dirPath = dirPath.substring(0, dirPath.length() - packageName.length() - 1);
String path = dirPath; String path = dirPath;
ArrayList<File> defaultPath = Lists.newArrayList(new File(path)); ArrayList<File> defaultPath = Lists.newArrayList(new File(path));
@ -89,7 +89,10 @@ public class CompilationEnvironment {
String className = classFile.getName().substring(0, classFile.getName().length() - 6); String className = classFile.getName().substring(0, classFile.getName().length() - 6);
if (className.matches("Fun\\d+\\$\\$.*")) if (className.matches("Fun\\d+\\$\\$.*"))
continue; continue;
ret.add(classLoader.loadClass(packageName + className)); var name = packageName;
if (!packageName.isEmpty()) name += ".";
name += className;
ret.add(classLoader.loadClass(name));
} }
return ret; return ret;
} }

View File

@ -15,6 +15,8 @@ public class SourceFile extends SyntaxTreeNode {
public final List<ClassOrInterface> KlassenVektor; public final List<ClassOrInterface> KlassenVektor;
public final Set<JavaClassName> imports; public final Set<JavaClassName> imports;
private boolean isGenerated;
public List<ClassOrInterface> availableClasses = new ArrayList<>(); public List<ClassOrInterface> availableClasses = new ArrayList<>();
/** /**
@ -38,6 +40,18 @@ public class SourceFile extends SyntaxTreeNode {
this.imports = new HashSet<>(sf.imports); 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() { public String getPkgName() {
return this.pkgName; return this.pkgName;
} }