From 141e1cbc94440c0b99f23d1162f54bd9e1f2f717 Mon Sep 17 00:00:00 2001 From: Daniel Holle Date: Thu, 23 May 2024 11:39:56 +0200 Subject: [PATCH] Make wildcard imports work again #330 --- pom.xml | 12 ++-- .../bytecode/javFiles/ImportWildcard.jav | 5 ++ .../de/dhbwstuttgart/core/JavaTXCompiler.java | 2 +- .../environment/CompilationEnvironment.java | 12 +--- .../environment/PackageCrawler.java | 69 ++++++------------- .../SyntaxTreeGenerator.java | 4 -- .../TypeInferenceBlockInformation.java | 1 - src/test/java/TestComplete.java | 7 ++ 8 files changed, 43 insertions(+), 69 deletions(-) create mode 100644 resources/bytecode/javFiles/ImportWildcard.jav diff --git a/pom.xml b/pom.xml index f356ce82..120f4204 100644 --- a/pom.xml +++ b/pom.xml @@ -29,14 +29,14 @@ http://maven.apache.org/maven-v4_0_0.xsd"> 2.6 - com.google.guava - guava - 22.0 + io.github.classgraph + classgraph + 4.8.172 - org.reflections - reflections - 0.9.11 + com.google.guava + guava + 33.2.0-jre diff --git a/resources/bytecode/javFiles/ImportWildcard.jav b/resources/bytecode/javFiles/ImportWildcard.jav new file mode 100644 index 00000000..a64e9ae2 --- /dev/null +++ b/resources/bytecode/javFiles/ImportWildcard.jav @@ -0,0 +1,5 @@ +import java.lang.*; + +public class ImportWildcard { + m(a, b) { return a * b; } +} \ No newline at end of file diff --git a/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java b/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java index 068f1784..62d69b16 100644 --- a/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java +++ b/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java @@ -98,7 +98,7 @@ public class JavaTXCompiler { } if (outputPath != null) path.add(outputPath); classLoader = new DirectoryClassLoader(path, ClassLoader.getSystemClassLoader()); - environment = new CompilationEnvironment(sources); + environment = new CompilationEnvironment(sources, classLoader); classPath = path; this.outputPath = outputPath; diff --git a/src/main/java/de/dhbwstuttgart/environment/CompilationEnvironment.java b/src/main/java/de/dhbwstuttgart/environment/CompilationEnvironment.java index 4b802fe5..ad16e766 100644 --- a/src/main/java/de/dhbwstuttgart/environment/CompilationEnvironment.java +++ b/src/main/java/de/dhbwstuttgart/environment/CompilationEnvironment.java @@ -34,7 +34,7 @@ public class CompilationEnvironment { * * @param sourceFiles die zu kompilierenden Dateien */ - public CompilationEnvironment(List sourceFiles) { + public CompilationEnvironment(List 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 * @@ -54,7 +54,7 @@ public class CompilationEnvironment { // librarys = Arrays.asList(loader.getURLs()); 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 { @@ -104,12 +104,4 @@ public class CompilationEnvironment { return packageName; } - public List getAllAvailableClasses() { - List ret = new ArrayList<>(); - for (Class c : new PackageCrawler(librarys).getAllAvailableClasses()) { - ret.add(ASTFactory.createClass(c)); - } - return ret; - } - } diff --git a/src/main/java/de/dhbwstuttgart/environment/PackageCrawler.java b/src/main/java/de/dhbwstuttgart/environment/PackageCrawler.java index 646440b1..f645c3e9 100644 --- a/src/main/java/de/dhbwstuttgart/environment/PackageCrawler.java +++ b/src/main/java/de/dhbwstuttgart/environment/PackageCrawler.java @@ -1,17 +1,10 @@ package de.dhbwstuttgart.environment; -import java.net.URL; +import io.github.classgraph.ClassGraph; +import io.github.classgraph.ScanResult; + 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 * Benutzt die Reflections-Library (https://github.com/ronmamo/reflections) @@ -19,48 +12,30 @@ import org.reflections.vfs.SystemDir; */ public class PackageCrawler { - final URL[] urls; - public PackageCrawler(List urlList) { - urls = urlList.toArray(new URL[0]); + final DirectoryClassLoader classLoader; + public PackageCrawler(DirectoryClassLoader classLoader) { + this.classLoader = classLoader; } - public Set> getClassesInPackage(String packageName){ - /* - List classLoadersList = new LinkedList(); - 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 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)))); + public Set> getClassesInPackage(String packageName) { + var res = new HashSet>(); - Set> 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> getAllAvailableClasses(){ - Reflections reflections = new Reflections(new ConfigurationBuilder() - .setScanners(new SubTypesScanner(false /* don't exclude Object.class */), new ResourcesScanner()) - .setUrls(urls)); - - Set> classes = reflections.getSubTypesOf(Object.class); - - return classes; + return res; } public Map getClassNames(String packageName){ diff --git a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java index 003f2580..de36c9f0 100644 --- a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java +++ b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java @@ -71,10 +71,6 @@ import de.dhbwstuttgart.syntaxtree.type.RefType; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; 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 { private JavaClassRegistry reg; diff --git a/src/main/java/de/dhbwstuttgart/typeinference/assumptions/TypeInferenceBlockInformation.java b/src/main/java/de/dhbwstuttgart/typeinference/assumptions/TypeInferenceBlockInformation.java index 6b9f54a5..7fe843d1 100644 --- a/src/main/java/de/dhbwstuttgart/typeinference/assumptions/TypeInferenceBlockInformation.java +++ b/src/main/java/de/dhbwstuttgart/typeinference/assumptions/TypeInferenceBlockInformation.java @@ -35,7 +35,6 @@ public class TypeInferenceBlockInformation extends TypeInferenceInformation { public ClassOrInterface getSuperClass() { for (var clazz : getAvailableClasses()) { - System.out.println(currentClass.getSuperClass().getName()); if (clazz.getClassName().equals(currentClass.getSuperClass().getName())) return clazz; } diff --git a/src/test/java/TestComplete.java b/src/test/java/TestComplete.java index 6b685b92..87c6f2aa 100644 --- a/src/test/java/TestComplete.java +++ b/src/test/java/TestComplete.java @@ -934,6 +934,13 @@ public class TestComplete { 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 public void testBug122() throws Exception { var classFiles = generateClassFiles(new ByteArrayClassLoader(), "Bug122.jav");