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 final DirectoryClassLoader classLoader;
private final List<File> classPath;
public final List<File> 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<ClassOrInterface>();
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<ClassOrInterface>();
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<JavaClassName, byte[]> generateBytecode(File sourceFile) throws ClassNotFoundException, IOException {
var sf = sourceFiles.get(sourceFile);
if (sf.isGenerated()) return null;
List<ResultSet> typeinferenceResult = this.typeInference(sourceFile);
return generateBytecode(sourceFiles.get(sourceFile), typeinferenceResult);
return generateBytecode(sf, typeinferenceResult);
}
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();
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();

View File

@ -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<File> 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;
}

View File

@ -15,6 +15,8 @@ public class SourceFile extends SyntaxTreeNode {
public final List<ClassOrInterface> KlassenVektor;
public final Set<JavaClassName> imports;
private boolean isGenerated;
public List<ClassOrInterface> 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;
}