Manage imports per source file instead of globally, should fix #290

This commit is contained in:
Daniel Holle 2024-03-14 13:51:20 +01:00
parent f9188e65ca
commit f57d89c966
5 changed files with 23 additions and 14 deletions

View File

@ -1435,6 +1435,8 @@ public class Codegen {
var access = method.access();
if (method.block() == null)
access |= ACC_ABSTRACT;
if (clazz instanceof TargetInterface)
access |= ACC_PUBLIC;
// TODO The older codegen has set ACC_PUBLIC for all methods, good for testing but bad for everything else
MethodVisitor mv = cw.visitMethod(access, method.name(), method.getDescriptor(), method.getSignature(), null);

View File

@ -57,6 +57,7 @@ import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.sql.Array;
import java.util.*;
import java.util.Map.Entry;
import java.util.function.Function;
@ -128,11 +129,11 @@ public class JavaTXCompiler {
}
public ConstraintSet<Pair> getConstraints() throws ClassNotFoundException, IOException {
List<ClassOrInterface> allClasses = new ArrayList<>();// environment.getAllAvailableClasses();
List<ClassOrInterface> importedClasses = new ArrayList<>();
Set<ClassOrInterface> allClasses = new HashSet<>();// environment.getAllAvailableClasses();
ClassOrInterface objectClass = ASTFactory.createClass(classLoader.loadClass(new JavaClassName("java.lang.Object").toString()));
// Alle Importierten Klassen in allen geparsten Sourcefiles kommen ins FC
for (Entry<File, SourceFile> source : sourceFiles.entrySet()) {
var importedClasses = new ArrayList<ClassOrInterface>();
for (JavaClassName name : source.getValue().getImports()) {
importedClasses.addAll(getAvailableClasses(name));
}
@ -140,18 +141,17 @@ public class JavaTXCompiler {
ClassOrInterface importedClass = ASTFactory.createClass(c);
importedClasses.add(importedClass);
}
source.getValue().availableClasses.addAll(importedClasses);
}
for (File f : this.sourceFiles.keySet()) {
SourceFile sf = sourceFiles.get(f);
sf = new SourceFile(sf.getPkgName(), sf.KlassenVektor.stream().map(cl -> new ClassOrInterface(cl)).collect(Collectors.toCollection(ArrayList::new)), sf.imports);
SourceFile sf_new = new SourceFile(sf.getPkgName(), sf.KlassenVektor.stream().map(cl -> new ClassOrInterface(cl)).collect(Collectors.toCollection(ArrayList::new)), sf.imports);
// sf enthaelt neues Source-File, neue Klassen-Objekte und neue
// ArrayListen-Objekte fuer Fields, Construktoren und Methoden
// Alle anderen Objekte werden nur kopiert.
SourceFile sf_new = sf;
sf.KlassenVektor.forEach(cl -> addMethods(sf_new, cl, importedClasses, objectClass));
allClasses.addAll(sf.getClasses());
sf_new.KlassenVektor.forEach(cl -> addMethods(sf_new, cl, sf.availableClasses, objectClass));
allClasses.addAll(sf_new.getClasses());
}
allClasses.addAll(importedClasses);
TYPE ty = new TYPE(sourceFiles.values(), allClasses);
return ty.getConstraints();
}

View File

@ -143,7 +143,8 @@ public class SyntaxTreeGenerator {
if (srcfile.packageDeclaration() != null)
this.pkgName = convert(srcfile.packageDeclaration());
Map<String, Integer> imports = GatherNames.getImports(srcfile, packageCrawler, compiler);
this.imports = imports.keySet().stream().map(name -> reg.getName(name)).collect(Collectors.toSet());
this.imports.addAll(imports.keySet().stream().map(name -> reg.getName(name)).collect(Collectors.toSet()));
for (Java17Parser.ClassOrInterfaceContext type : srcfile.classOrInterface()) {
ClassorinterfacedeclContext clsoif;
if (type instanceof NoclassorinterfaceContext) {

View File

@ -15,6 +15,8 @@ public class SourceFile extends SyntaxTreeNode {
public final List<ClassOrInterface> KlassenVektor;
public final Set<JavaClassName> imports;
public List<ClassOrInterface> availableClasses = new ArrayList<>();
/**
* Die SourceFile repräsntiert eine zu einem Syntaxbaum eingelesene Java-Datei.
* SourceFile stellt dabei den Wurzelknoten des Syntaxbaumes dar.

View File

@ -16,19 +16,23 @@ import java.util.*;
public class TYPE {
private final Collection<SourceFile> sfs;
private final TypeInferenceInformation typeInferenceInformation;
private final Set<ClassOrInterface> allAvailableClasses;
public TYPE(Collection<SourceFile> sourceFiles, Collection<ClassOrInterface> allAvailableClasses){
public TYPE(Collection<SourceFile> sourceFiles, Set<ClassOrInterface> allAvailableClasses){
sfs = sourceFiles;
this.typeInferenceInformation = new TypeInferenceInformation(allAvailableClasses);
this.allAvailableClasses = allAvailableClasses;
}
public ConstraintSet getConstraints() {
ConstraintSet ret = new ConstraintSet();
for(SourceFile sf : sfs)
for (ClassOrInterface cl : sf.KlassenVektor) {
ret.addAll(getConstraintsClass(cl ,typeInferenceInformation));
}
for (ClassOrInterface cl : sf.KlassenVektor) {
System.out.println(sf.availableClasses);
var allClasses = new HashSet<ClassOrInterface>();
allClasses.addAll(allAvailableClasses);
allClasses.addAll(sf.availableClasses);
ret.addAll(getConstraintsClass(cl, new TypeInferenceInformation(allClasses)));
}
return ret;
}