From 0c1ca3d20080bf5cce1a82ec63b5798eda0882d9 Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Sun, 12 Jan 2020 22:49:51 +0100 Subject: [PATCH] =?UTF-8?q?Fehler=20beheben=20und=20DirecotryClassLoader?= =?UTF-8?q?=20anf=C3=BCgen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bytecode/BytecodeGenMethod.java | 4 ++- .../dhbwstuttgart/core/ConsoleInterface.java | 4 +-- .../de/dhbwstuttgart/core/JavaTXCompiler.java | 7 +++-- .../environment/CompilationEnvironment.java | 17 +++------- .../environment/DirectoryClassLoader.java | 31 +++++++++++++++++++ .../environment/PackageCrawler.java | 6 ++-- .../typeinference/typeAlgo/TYPE.java | 2 +- .../java/packages/CheckPackageFolder.java | 1 + src/test/java/packages/ImportTest.java | 6 ++-- .../java/packages/mathStrucVectorTest.java | 2 +- vorgehen.md | 4 +++ 11 files changed, 57 insertions(+), 27 deletions(-) create mode 100644 src/main/java/de/dhbwstuttgart/environment/DirectoryClassLoader.java diff --git a/src/main/java/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java b/src/main/java/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java index a7664c6a..1c32748d 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java @@ -1,5 +1,6 @@ package de.dhbwstuttgart.bytecode; +import java.io.File; import java.lang.invoke.CallSite; import java.lang.invoke.MethodHandle; import java.lang.invoke.MethodHandles; @@ -14,6 +15,7 @@ import java.util.LinkedList; import java.util.List; import de.dhbwstuttgart.bytecode.utilities.*; +import de.dhbwstuttgart.environment.DirectoryClassLoader; import de.dhbwstuttgart.exceptions.NotImplementedException; import de.dhbwstuttgart.parser.scope.JavaClassName; import de.dhbwstuttgart.syntaxtree.statement.*; @@ -814,7 +816,7 @@ public class BytecodeGenMethod implements StatementVisitor { // mDesc = helper.generateBCForFunN(methCallType,typesOfParams); }else { try { - cLoader2 = new URLClassLoader(new URL[] {new URL("file://" + path + "/")}, classLoader); + cLoader2 = new DirectoryClassLoader(new File(path), classLoader); java.lang.reflect.Method[] methods = cLoader2.loadClass(clazz).getMethods(); System.out.println("Methods of " + receiverName + " "); for(int i = 0; i input = new ArrayList<>(); - List classpath = new ArrayList<>(); + List classpath = new ArrayList<>(); String outputPath = null; Iterator it = Arrays.asList(args).iterator(); while(it.hasNext()){ @@ -23,7 +23,7 @@ public class ConsoleInterface { }else if(arg.equals("-cp") || arg.equals("-classpath")){ String[] cps = it.next().split(":"); for(String cp : cps){ - classpath.add(new URL("file://"+cp)); + classpath.add(new File(cp)); } }else{ input.add(new File(arg)); diff --git a/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java b/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java index 04f7aa27..2ed5a67d 100644 --- a/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java +++ b/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java @@ -7,6 +7,7 @@ import de.dhbwstuttgart.bytecode.Exception.BytecodeGeneratorError; import de.dhbwstuttgart.bytecode.genericsGenerator.GeneratedGenericsFinder; import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.GenericGenratorResultForSourceFile; import de.dhbwstuttgart.environment.CompilationEnvironment; +import de.dhbwstuttgart.environment.DirectoryClassLoader; import de.dhbwstuttgart.parser.JavaTXParser; import de.dhbwstuttgart.parser.scope.GenericsRegistry; import de.dhbwstuttgart.parser.SyntaxTreeGenerator.SyntaxTreeGenerator; @@ -75,12 +76,12 @@ public class JavaTXCompiler { this(sourceFiles, null); INSTANCE = this; } - public JavaTXCompiler(List sources, List contextPath) throws IOException, ClassNotFoundException { + public JavaTXCompiler(List sources, List contextPath) throws IOException, ClassNotFoundException { if(contextPath == null || contextPath.isEmpty()){ //When no contextPaths are given, the working directory is the sources root - contextPath = Lists.newArrayList(new URL("file://" + System.getProperty("user.dir"))); + contextPath = Lists.newArrayList(new File(System.getProperty("user.dir"))); } - classLoader = new URLClassLoader(contextPath.toArray(new URL[0]), ClassLoader.getSystemClassLoader()); + classLoader = new DirectoryClassLoader(contextPath, ClassLoader.getSystemClassLoader()); environment = new CompilationEnvironment(sources); for (File s : sources) { sourceFiles.put(s, parse(s)); diff --git a/src/main/java/de/dhbwstuttgart/environment/CompilationEnvironment.java b/src/main/java/de/dhbwstuttgart/environment/CompilationEnvironment.java index 682e1eea..ca879cfd 100644 --- a/src/main/java/de/dhbwstuttgart/environment/CompilationEnvironment.java +++ b/src/main/java/de/dhbwstuttgart/environment/CompilationEnvironment.java @@ -1,24 +1,15 @@ package de.dhbwstuttgart.environment; import java.io.File; -import java.io.FilenameFilter; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; -import java.net.URLClassLoader; import java.util.*; import com.google.common.collect.Lists; import de.dhbwstuttgart.syntaxtree.ClassOrInterface; -import de.dhbwstuttgart.syntaxtree.SourceFile; import de.dhbwstuttgart.syntaxtree.factory.ASTFactory; import org.antlr.v4.runtime.tree.TerminalNode; -import org.reflections.Reflections; -import org.reflections.scanners.ResourcesScanner; -import org.reflections.scanners.SubTypesScanner; -import org.reflections.util.ClasspathHelper; -import org.reflections.util.ConfigurationBuilder; -import org.reflections.util.FilterBuilder; import de.dhbwstuttgart.exceptions.DebugException; import de.dhbwstuttgart.parser.JavaTXParser; @@ -52,7 +43,7 @@ public class CompilationEnvironment { * */ //String bootClassPath = System.getProperty("sun.boot.class.path"); - // ClassLoader cl = ClassLoader.getPlatformClassLoader(); + // DirectoryClassLoader cl = DirectoryClassLoader.getPlatformClassLoader(); String bootClassPath = System.getProperty("java.class.path"); librarys = new ArrayList<>(); for(String path : bootClassPath.split(File.pathSeparator)) { @@ -86,9 +77,9 @@ public class CompilationEnvironment { File dir = new File(forSourceFile.getParent()); String dirPath = dir.toString() + "/"; if(packageName.length()>0)dirPath = dirPath.substring(0,dirPath.length() - packageName.length()); - String path = "file://" + dirPath; - ArrayList defaultPath = Lists.newArrayList(new URL(path)); - classLoader = new URLClassLoader(defaultPath.toArray(new URL[0]), classLoader); + String path = dirPath; + ArrayList defaultPath = Lists.newArrayList(new File(path)); + classLoader = new DirectoryClassLoader(defaultPath, classLoader); //Gather all names in the default package for this source file (classes that are imported by default) File [] files = dir.listFiles((dir1, name) -> name.endsWith(".class")); if(files != null)for (File classFile : files) { diff --git a/src/main/java/de/dhbwstuttgart/environment/DirectoryClassLoader.java b/src/main/java/de/dhbwstuttgart/environment/DirectoryClassLoader.java new file mode 100644 index 00000000..a74dbc49 --- /dev/null +++ b/src/main/java/de/dhbwstuttgart/environment/DirectoryClassLoader.java @@ -0,0 +1,31 @@ +package de.dhbwstuttgart.environment; + +import java.io.File; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLClassLoader; +import java.util.List; +import java.util.stream.Collectors; + +public class DirectoryClassLoader extends URLClassLoader { + public DirectoryClassLoader(File directory, java.lang.ClassLoader parent) { + super(generateURLArray(dirToURL(directory)), parent); + } + + public DirectoryClassLoader(List directory, java.lang.ClassLoader parent) { + super(directory.stream().map(DirectoryClassLoader::dirToURL).collect(Collectors.toList()).toArray(new URL[0]), parent); + } + + private static URL[] generateURLArray(URL url) { + return new URL[]{url}; + } + + private static URL dirToURL(File url){ + if(!url.isDirectory())throw new RuntimeException(url.toString() + " is not a directory"); + try { + return url.toURI().toURL(); + } catch (MalformedURLException e) { + throw new RuntimeException(e); + } + } +} diff --git a/src/main/java/de/dhbwstuttgart/environment/PackageCrawler.java b/src/main/java/de/dhbwstuttgart/environment/PackageCrawler.java index 7c63a0b6..646440b1 100644 --- a/src/main/java/de/dhbwstuttgart/environment/PackageCrawler.java +++ b/src/main/java/de/dhbwstuttgart/environment/PackageCrawler.java @@ -26,11 +26,11 @@ public class PackageCrawler { public Set> getClassesInPackage(String packageName){ /* - List classLoadersList = new LinkedList(); + List classLoadersList = new LinkedList(); classLoadersList.add(Thread.currentThread().getContextClassLoader()); classLoadersList.add(ClasspathHelper.staticClassLoader()); classLoadersList.add(Thread.currentThread().getContextClassLoader().getParent()); - classLoadersList.add(ClassLoader.getSystemClassLoader()); + classLoadersList.add(DirectoryClassLoader.getSystemClassLoader()); String bootClassPath = System.getProperty("sun.boot.class.path"); ArrayList urlList = new ArrayList<>(); for(String path : bootClassPath.split(";")) { @@ -41,7 +41,7 @@ public class PackageCrawler { } } URL[] urls = urlList.toArray(new URL[0]); - classLoadersList.add(new URLClassLoader(urls, ClassLoader.getSystemClassLoader())); + classLoadersList.add(new URLClassLoader(urls, DirectoryClassLoader.getSystemClassLoader())); */ Reflections reflections = new Reflections(new ConfigurationBuilder() .setScanners(new SubTypesScanner(false /* don't exclude Object.class */), new ResourcesScanner()) diff --git a/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPE.java b/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPE.java index 246c3691..c84fa143 100644 --- a/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPE.java +++ b/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPE.java @@ -46,7 +46,7 @@ public class TYPE { /* TODO: Hier eine Information erstellen nur mit den importierte Klassen einer einzigen SourceFile private TypeInferenceInformation getTypeInferenceInformation(sourceFile) { - ClassLoader classLoader = ClassLoader.getSystemClassLoader(); + DirectoryClassLoader classLoader = DirectoryClassLoader.getSystemClassLoader(); Set classes = new HashSet<>(); for(SourceFile sourceFile : sfs){ diff --git a/src/test/java/packages/CheckPackageFolder.java b/src/test/java/packages/CheckPackageFolder.java index b0cfbb8e..4f545485 100644 --- a/src/test/java/packages/CheckPackageFolder.java +++ b/src/test/java/packages/CheckPackageFolder.java @@ -4,6 +4,7 @@ import de.dhbwstuttgart.core.JavaTXCompiler; import de.dhbwstuttgart.syntaxtree.SourceFile; import junit.framework.TestCase; import org.junit.Test; +import org.junit.rules.ExpectedException; import java.io.File; import java.io.IOException; diff --git a/src/test/java/packages/ImportTest.java b/src/test/java/packages/ImportTest.java index 4ae04779..92d3f5bb 100644 --- a/src/test/java/packages/ImportTest.java +++ b/src/test/java/packages/ImportTest.java @@ -48,7 +48,7 @@ public class ImportTest extends TestCase { public void testSetPackageNameInBytecodeAndOutputFolder() throws IOException, ClassNotFoundException { JavaTXCompiler compiler = new JavaTXCompiler( Lists.newArrayList(new File(rootDirectory+"ImportTest.jav")), - Lists.newArrayList(new URL("file://"+rootDirectory+"output/"))); + Lists.newArrayList(new File(rootDirectory+"output/"))); compiler.typeInference(); File f = new File(rootDirectory + "output/de/test/ImportTest.class"); if(f.exists() && !f.isDirectory()) { @@ -63,7 +63,7 @@ public class ImportTest extends TestCase { public void testSetPackageNameInBytecodeAndStandardOutputFolder() throws IOException, ClassNotFoundException { JavaTXCompiler compiler = new JavaTXCompiler( Lists.newArrayList(new File(rootDirectory+"ImportTest.jav")), - Lists.newArrayList(new URL("file://"+rootDirectory+"output/"))); + Lists.newArrayList(new File(rootDirectory+"output/"))); compiler.typeInference(); File f = new File(rootDirectory + "ImportTest.class"); if(f.exists() && !f.isDirectory()) { @@ -79,7 +79,7 @@ public class ImportTest extends TestCase { public void testImportTwoClasses() throws IOException, ClassNotFoundException { JavaTXCompiler compiler = new JavaTXCompiler( Lists.newArrayList(new File(rootDirectory+"ImportTest2.jav")), - Lists.newArrayList(new URL("file://"+rootDirectory+"output/"))); + Lists.newArrayList(new File(rootDirectory+"output/"))); compiler.typeInference(); File f = new File(rootDirectory + "ImportTest2.class"); if(f.exists() && !f.isDirectory()) { diff --git a/src/test/java/packages/mathStrucVectorTest.java b/src/test/java/packages/mathStrucVectorTest.java index 8911ce9c..a9d07684 100644 --- a/src/test/java/packages/mathStrucVectorTest.java +++ b/src/test/java/packages/mathStrucVectorTest.java @@ -39,7 +39,7 @@ public class mathStrucVectorTest extends TestCase { public void testSetPackageNameInBytecodeAndOutputFolder() throws IOException, ClassNotFoundException { JavaTXCompiler compiler = new JavaTXCompiler( Lists.newArrayList(new File(rootDirectory+"mathStrucVector.jav")), - Lists.newArrayList(new URL("file://"+rootDirectory+"output/"))); + Lists.newArrayList(new File(rootDirectory+"output/"))); compiler.typeInference(); File f = new File(rootDirectory + "output/de/test/mathStrucVector.class"); if(f.exists() && !f.isDirectory()) { diff --git a/vorgehen.md b/vorgehen.md index a83ca18f..c779c036 100644 --- a/vorgehen.md +++ b/vorgehen.md @@ -72,3 +72,7 @@ TODO: * wurde bereits erledigt -> TODO: Testen ## Console Interface um Parameter -classpath und -output directory erweitern + +# TODO 11.1.20 : +* Es muss überall, wo der URLClassloader mit einem Pfad instantiert wird, gecheckt werden, ob der Pfad mit "/" aufhört. + * Am besten eine Hilfsmethode schreiben