Make wildcard imports work again #330

This commit is contained in:
Daniel Holle 2024-05-23 11:39:56 +02:00
parent 5b4ea5a0c5
commit 141e1cbc94
8 changed files with 43 additions and 69 deletions

12
pom.xml
View File

@ -29,14 +29,14 @@ http://maven.apache.org/maven-v4_0_0.xsd">
<version>2.6</version> <version>2.6</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.google.guava</groupId> <groupId>io.github.classgraph</groupId>
<artifactId>guava</artifactId> <artifactId>classgraph</artifactId>
<version>22.0</version> <version>4.8.172</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.reflections</groupId> <groupId>com.google.guava</groupId>
<artifactId>reflections</artifactId> <artifactId>guava</artifactId>
<version>0.9.11</version> <version>33.2.0-jre</version>
</dependency> </dependency>
<!-- https://mvnrepository.com/artifact/org.ow2.asm/asm --> <!-- https://mvnrepository.com/artifact/org.ow2.asm/asm -->
<dependency> <dependency>

View File

@ -0,0 +1,5 @@
import java.lang.*;
public class ImportWildcard {
m(a, b) { return a * b; }
}

View File

@ -98,7 +98,7 @@ public class JavaTXCompiler {
} }
if (outputPath != null) path.add(outputPath); if (outputPath != null) path.add(outputPath);
classLoader = new DirectoryClassLoader(path, ClassLoader.getSystemClassLoader()); classLoader = new DirectoryClassLoader(path, ClassLoader.getSystemClassLoader());
environment = new CompilationEnvironment(sources); environment = new CompilationEnvironment(sources, classLoader);
classPath = path; classPath = path;
this.outputPath = outputPath; this.outputPath = outputPath;

View File

@ -34,7 +34,7 @@ public class CompilationEnvironment {
* *
* @param sourceFiles die zu kompilierenden Dateien * @param sourceFiles die zu kompilierenden Dateien
*/ */
public CompilationEnvironment(List<File> sourceFiles) { public CompilationEnvironment(List<File> sourceFiles, DirectoryClassLoader classLoader) {
/** /**
* Java 9 bringt einige Änderungen am Classloader So funktioniert der BootClassLoader nicht mehr. hier gibts ein paar Quellen zum nachlesen: http://java9.wtf/class-loading/ https://stackoverflow.com/questions/46494112/classloaders-hierarchy-in-java-9 * Java 9 bringt einige Änderungen am Classloader So funktioniert der BootClassLoader nicht mehr. hier gibts ein paar Quellen zum nachlesen: http://java9.wtf/class-loading/ https://stackoverflow.com/questions/46494112/classloaders-hierarchy-in-java-9
* *
@ -54,7 +54,7 @@ public class CompilationEnvironment {
// librarys = Arrays.asList(loader.getURLs()); // librarys = Arrays.asList(loader.getURLs());
this.sourceFiles = sourceFiles; this.sourceFiles = sourceFiles;
this.packageCrawler = new PackageCrawler(librarys); this.packageCrawler = new PackageCrawler(classLoader);
} }
public void addClassesToRegistry(JavaClassRegistry registry, SourceFileContext tree, File sourceFile, JavaTXCompiler compiler) throws ClassNotFoundException, IOException { public void addClassesToRegistry(JavaClassRegistry registry, SourceFileContext tree, File sourceFile, JavaTXCompiler compiler) throws ClassNotFoundException, IOException {
@ -104,12 +104,4 @@ public class CompilationEnvironment {
return packageName; return packageName;
} }
public List<ClassOrInterface> getAllAvailableClasses() {
List<ClassOrInterface> ret = new ArrayList<>();
for (Class c : new PackageCrawler(librarys).getAllAvailableClasses()) {
ret.add(ASTFactory.createClass(c));
}
return ret;
}
} }

View File

@ -1,17 +1,10 @@
package de.dhbwstuttgart.environment; package de.dhbwstuttgart.environment;
import java.net.URL; import io.github.classgraph.ClassGraph;
import io.github.classgraph.ScanResult;
import java.util.*; import java.util.*;
import org.reflections.Reflections;
import org.reflections.scanners.ResourcesScanner;
import org.reflections.scanners.SubTypesScanner;
import org.reflections.util.ConfigurationBuilder;
import org.reflections.util.FilterBuilder;
import de.dhbwstuttgart.parser.scope.JavaClassName;
import org.reflections.vfs.SystemDir;
/** /**
* Hilft beim Durchsuchen von Packages * Hilft beim Durchsuchen von Packages
* Benutzt die Reflections-Library (https://github.com/ronmamo/reflections) * Benutzt die Reflections-Library (https://github.com/ronmamo/reflections)
@ -19,48 +12,30 @@ import org.reflections.vfs.SystemDir;
*/ */
public class PackageCrawler { public class PackageCrawler {
final URL[] urls; final DirectoryClassLoader classLoader;
public PackageCrawler(List<URL> urlList) { public PackageCrawler(DirectoryClassLoader classLoader) {
urls = urlList.toArray(new URL[0]); this.classLoader = classLoader;
} }
public Set<Class<?>> getClassesInPackage(String packageName){ public Set<Class<?>> getClassesInPackage(String packageName) {
/* var res = new HashSet<Class<?>>();
List<DirectoryClassLoader> classLoadersList = new LinkedList<DirectoryClassLoader>();
classLoadersList.add(Thread.currentThread().getContextClassLoader());
classLoadersList.add(ClasspathHelper.staticClassLoader());
classLoadersList.add(Thread.currentThread().getContextClassLoader().getParent());
classLoadersList.add(DirectoryClassLoader.getSystemClassLoader());
String bootClassPath = System.getProperty("sun.boot.class.path");
ArrayList<URL> urlList = new ArrayList<>();
for(String path : bootClassPath.split(";")) {
try {
urlList.add(new URL("file:"+path));
} catch (MalformedURLException e) {
new DebugException("Fehler im Classpath auf diesem System");
}
}
URL[] urls = urlList.toArray(new URL[0]);
classLoadersList.add(new URLClassLoader(urls, DirectoryClassLoader.getSystemClassLoader()));
*/
Reflections reflections = new Reflections(new ConfigurationBuilder()
.setScanners(new SubTypesScanner(false /* don't exclude Object.class */), new ResourcesScanner())
.setUrls(urls)
.filterInputsBy(new FilterBuilder().include(FilterBuilder.prefix(packageName))));
Set<Class<?>> classes = reflections.getSubTypesOf(Object.class); try (ScanResult result = new ClassGraph()
.enableClassInfo()
.enableSystemJarsAndModules()
.addClassLoader(classLoader)
.acceptPackages(packageName)
.scan()) {
return classes; for (var info : result.getAllClasses()) {
} try {
var clazz = Class.forName(info.getName());
res.add(clazz);
} catch (ClassNotFoundException ignored) {}
}
};
public Set<Class<?>> getAllAvailableClasses(){ return res;
Reflections reflections = new Reflections(new ConfigurationBuilder()
.setScanners(new SubTypesScanner(false /* don't exclude Object.class */), new ResourcesScanner())
.setUrls(urls));
Set<Class<?>> classes = reflections.getSubTypesOf(Object.class);
return classes;
} }
public Map<String, Integer> getClassNames(String packageName){ public Map<String, Integer> getClassNames(String packageName){

View File

@ -71,10 +71,6 @@ import de.dhbwstuttgart.syntaxtree.type.RefType;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import de.dhbwstuttgart.syntaxtree.type.Void; import de.dhbwstuttgart.syntaxtree.type.Void;
import de.dhbwstuttgart.typeinference.constraints.GenericsResolver;
import javassist.compiler.SyntaxError;
import javax.swing.text.html.Option;
public class SyntaxTreeGenerator { public class SyntaxTreeGenerator {
private JavaClassRegistry reg; private JavaClassRegistry reg;

View File

@ -35,7 +35,6 @@ public class TypeInferenceBlockInformation extends TypeInferenceInformation {
public ClassOrInterface getSuperClass() { public ClassOrInterface getSuperClass() {
for (var clazz : getAvailableClasses()) { for (var clazz : getAvailableClasses()) {
System.out.println(currentClass.getSuperClass().getName());
if (clazz.getClassName().equals(currentClass.getSuperClass().getName())) if (clazz.getClassName().equals(currentClass.getSuperClass().getName()))
return clazz; return clazz;
} }

View File

@ -934,6 +934,13 @@ public class TestComplete {
assertEquals(clazz.getDeclaredMethod("m2").invoke(instance), 10); assertEquals(clazz.getDeclaredMethod("m2").invoke(instance), 10);
} }
@Test
public void testImportWildcard() throws Exception {
var classFiles = generateClassFiles(new ByteArrayClassLoader(), "ImportWildcard.jav");
var clazz = classFiles.get("ImportWildcard");
var instance = clazz.getDeclaredConstructor().newInstance();
}
@Test @Test
public void testBug122() throws Exception { public void testBug122() throws Exception {
var classFiles = generateClassFiles(new ByteArrayClassLoader(), "Bug122.jav"); var classFiles = generateClassFiles(new ByteArrayClassLoader(), "Bug122.jav");