Fehler beheben und DirecotryClassLoader anfügen

This commit is contained in:
JanUlrich 2020-01-12 22:49:51 +01:00
parent f3d6fcb417
commit 0c1ca3d200
11 changed files with 57 additions and 27 deletions

View File

@ -1,5 +1,6 @@
package de.dhbwstuttgart.bytecode; package de.dhbwstuttgart.bytecode;
import java.io.File;
import java.lang.invoke.CallSite; import java.lang.invoke.CallSite;
import java.lang.invoke.MethodHandle; import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles; import java.lang.invoke.MethodHandles;
@ -14,6 +15,7 @@ import java.util.LinkedList;
import java.util.List; import java.util.List;
import de.dhbwstuttgart.bytecode.utilities.*; import de.dhbwstuttgart.bytecode.utilities.*;
import de.dhbwstuttgart.environment.DirectoryClassLoader;
import de.dhbwstuttgart.exceptions.NotImplementedException; import de.dhbwstuttgart.exceptions.NotImplementedException;
import de.dhbwstuttgart.parser.scope.JavaClassName; import de.dhbwstuttgart.parser.scope.JavaClassName;
import de.dhbwstuttgart.syntaxtree.statement.*; import de.dhbwstuttgart.syntaxtree.statement.*;
@ -814,7 +816,7 @@ public class BytecodeGenMethod implements StatementVisitor {
// mDesc = helper.generateBCForFunN(methCallType,typesOfParams); // mDesc = helper.generateBCForFunN(methCallType,typesOfParams);
}else { }else {
try { 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(); java.lang.reflect.Method[] methods = cLoader2.loadClass(clazz).getMethods();
System.out.println("Methods of " + receiverName + " "); System.out.println("Methods of " + receiverName + " ");
for(int i = 0; i<methods.length; i++) { for(int i = 0; i<methods.length; i++) {

View File

@ -11,7 +11,7 @@ public class ConsoleInterface {
public static void main(String[] args) throws IOException, ClassNotFoundException { public static void main(String[] args) throws IOException, ClassNotFoundException {
List<File> input = new ArrayList<>(); List<File> input = new ArrayList<>();
List<URL> classpath = new ArrayList<>(); List<File> classpath = new ArrayList<>();
String outputPath = null; String outputPath = null;
Iterator<String> it = Arrays.asList(args).iterator(); Iterator<String> it = Arrays.asList(args).iterator();
while(it.hasNext()){ while(it.hasNext()){
@ -23,7 +23,7 @@ public class ConsoleInterface {
}else if(arg.equals("-cp") || arg.equals("-classpath")){ }else if(arg.equals("-cp") || arg.equals("-classpath")){
String[] cps = it.next().split(":"); String[] cps = it.next().split(":");
for(String cp : cps){ for(String cp : cps){
classpath.add(new URL("file://"+cp)); classpath.add(new File(cp));
} }
}else{ }else{
input.add(new File(arg)); input.add(new File(arg));

View File

@ -7,6 +7,7 @@ import de.dhbwstuttgart.bytecode.Exception.BytecodeGeneratorError;
import de.dhbwstuttgart.bytecode.genericsGenerator.GeneratedGenericsFinder; import de.dhbwstuttgart.bytecode.genericsGenerator.GeneratedGenericsFinder;
import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.GenericGenratorResultForSourceFile; import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.GenericGenratorResultForSourceFile;
import de.dhbwstuttgart.environment.CompilationEnvironment; import de.dhbwstuttgart.environment.CompilationEnvironment;
import de.dhbwstuttgart.environment.DirectoryClassLoader;
import de.dhbwstuttgart.parser.JavaTXParser; import de.dhbwstuttgart.parser.JavaTXParser;
import de.dhbwstuttgart.parser.scope.GenericsRegistry; import de.dhbwstuttgart.parser.scope.GenericsRegistry;
import de.dhbwstuttgart.parser.SyntaxTreeGenerator.SyntaxTreeGenerator; import de.dhbwstuttgart.parser.SyntaxTreeGenerator.SyntaxTreeGenerator;
@ -75,12 +76,12 @@ public class JavaTXCompiler {
this(sourceFiles, null); this(sourceFiles, null);
INSTANCE = this; INSTANCE = this;
} }
public JavaTXCompiler(List<File> sources, List<URL> contextPath) throws IOException, ClassNotFoundException { public JavaTXCompiler(List<File> sources, List<File> contextPath) throws IOException, ClassNotFoundException {
if(contextPath == null || contextPath.isEmpty()){ if(contextPath == null || contextPath.isEmpty()){
//When no contextPaths are given, the working directory is the sources root //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); environment = new CompilationEnvironment(sources);
for (File s : sources) { for (File s : sources) {
sourceFiles.put(s, parse(s)); sourceFiles.put(s, parse(s));

View File

@ -1,24 +1,15 @@
package de.dhbwstuttgart.environment; package de.dhbwstuttgart.environment;
import java.io.File; import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException; import java.io.IOException;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.net.URLClassLoader;
import java.util.*; import java.util.*;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import de.dhbwstuttgart.syntaxtree.ClassOrInterface; import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
import de.dhbwstuttgart.syntaxtree.SourceFile;
import de.dhbwstuttgart.syntaxtree.factory.ASTFactory; import de.dhbwstuttgart.syntaxtree.factory.ASTFactory;
import org.antlr.v4.runtime.tree.TerminalNode; 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.exceptions.DebugException;
import de.dhbwstuttgart.parser.JavaTXParser; import de.dhbwstuttgart.parser.JavaTXParser;
@ -52,7 +43,7 @@ public class CompilationEnvironment {
* *
*/ */
//String bootClassPath = System.getProperty("sun.boot.class.path"); //String bootClassPath = System.getProperty("sun.boot.class.path");
// ClassLoader cl = ClassLoader.getPlatformClassLoader(); // DirectoryClassLoader cl = DirectoryClassLoader.getPlatformClassLoader();
String bootClassPath = System.getProperty("java.class.path"); String bootClassPath = System.getProperty("java.class.path");
librarys = new ArrayList<>(); librarys = new ArrayList<>();
for(String path : bootClassPath.split(File.pathSeparator)) { for(String path : bootClassPath.split(File.pathSeparator)) {
@ -86,9 +77,9 @@ public class CompilationEnvironment {
File dir = new File(forSourceFile.getParent()); File dir = new File(forSourceFile.getParent());
String dirPath = dir.toString() + "/"; String dirPath = dir.toString() + "/";
if(packageName.length()>0)dirPath = dirPath.substring(0,dirPath.length() - packageName.length()); if(packageName.length()>0)dirPath = dirPath.substring(0,dirPath.length() - packageName.length());
String path = "file://" + dirPath; String path = dirPath;
ArrayList<URL> defaultPath = Lists.newArrayList(new URL(path)); ArrayList<File> defaultPath = Lists.newArrayList(new File(path));
classLoader = new URLClassLoader(defaultPath.toArray(new URL[0]), classLoader); classLoader = new DirectoryClassLoader(defaultPath, classLoader);
//Gather all names in the default package for this source file (classes that are imported by default) //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")); File [] files = dir.listFiles((dir1, name) -> name.endsWith(".class"));
if(files != null)for (File classFile : files) { if(files != null)for (File classFile : files) {

View File

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

View File

@ -26,11 +26,11 @@ public class PackageCrawler {
public Set<Class<?>> getClassesInPackage(String packageName){ public Set<Class<?>> getClassesInPackage(String packageName){
/* /*
List<ClassLoader> classLoadersList = new LinkedList<ClassLoader>(); List<DirectoryClassLoader> classLoadersList = new LinkedList<DirectoryClassLoader>();
classLoadersList.add(Thread.currentThread().getContextClassLoader()); classLoadersList.add(Thread.currentThread().getContextClassLoader());
classLoadersList.add(ClasspathHelper.staticClassLoader()); classLoadersList.add(ClasspathHelper.staticClassLoader());
classLoadersList.add(Thread.currentThread().getContextClassLoader().getParent()); classLoadersList.add(Thread.currentThread().getContextClassLoader().getParent());
classLoadersList.add(ClassLoader.getSystemClassLoader()); classLoadersList.add(DirectoryClassLoader.getSystemClassLoader());
String bootClassPath = System.getProperty("sun.boot.class.path"); String bootClassPath = System.getProperty("sun.boot.class.path");
ArrayList<URL> urlList = new ArrayList<>(); ArrayList<URL> urlList = new ArrayList<>();
for(String path : bootClassPath.split(";")) { for(String path : bootClassPath.split(";")) {
@ -41,7 +41,7 @@ public class PackageCrawler {
} }
} }
URL[] urls = urlList.toArray(new URL[0]); 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() Reflections reflections = new Reflections(new ConfigurationBuilder()
.setScanners(new SubTypesScanner(false /* don't exclude Object.class */), new ResourcesScanner()) .setScanners(new SubTypesScanner(false /* don't exclude Object.class */), new ResourcesScanner())

View File

@ -46,7 +46,7 @@ public class TYPE {
/* /*
TODO: Hier eine Information erstellen nur mit den importierte Klassen einer einzigen SourceFile TODO: Hier eine Information erstellen nur mit den importierte Klassen einer einzigen SourceFile
private TypeInferenceInformation getTypeInferenceInformation(sourceFile) { private TypeInferenceInformation getTypeInferenceInformation(sourceFile) {
ClassLoader classLoader = ClassLoader.getSystemClassLoader(); DirectoryClassLoader classLoader = DirectoryClassLoader.getSystemClassLoader();
Set<ClassOrInterface> classes = new HashSet<>(); Set<ClassOrInterface> classes = new HashSet<>();
for(SourceFile sourceFile : sfs){ for(SourceFile sourceFile : sfs){

View File

@ -4,6 +4,7 @@ import de.dhbwstuttgart.core.JavaTXCompiler;
import de.dhbwstuttgart.syntaxtree.SourceFile; import de.dhbwstuttgart.syntaxtree.SourceFile;
import junit.framework.TestCase; import junit.framework.TestCase;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.ExpectedException;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;

View File

@ -48,7 +48,7 @@ public class ImportTest extends TestCase {
public void testSetPackageNameInBytecodeAndOutputFolder() throws IOException, ClassNotFoundException { public void testSetPackageNameInBytecodeAndOutputFolder() throws IOException, ClassNotFoundException {
JavaTXCompiler compiler = new JavaTXCompiler( JavaTXCompiler compiler = new JavaTXCompiler(
Lists.newArrayList(new File(rootDirectory+"ImportTest.jav")), Lists.newArrayList(new File(rootDirectory+"ImportTest.jav")),
Lists.newArrayList(new URL("file://"+rootDirectory+"output/"))); Lists.newArrayList(new File(rootDirectory+"output/")));
compiler.typeInference(); compiler.typeInference();
File f = new File(rootDirectory + "output/de/test/ImportTest.class"); File f = new File(rootDirectory + "output/de/test/ImportTest.class");
if(f.exists() && !f.isDirectory()) { if(f.exists() && !f.isDirectory()) {
@ -63,7 +63,7 @@ public class ImportTest extends TestCase {
public void testSetPackageNameInBytecodeAndStandardOutputFolder() throws IOException, ClassNotFoundException { public void testSetPackageNameInBytecodeAndStandardOutputFolder() throws IOException, ClassNotFoundException {
JavaTXCompiler compiler = new JavaTXCompiler( JavaTXCompiler compiler = new JavaTXCompiler(
Lists.newArrayList(new File(rootDirectory+"ImportTest.jav")), Lists.newArrayList(new File(rootDirectory+"ImportTest.jav")),
Lists.newArrayList(new URL("file://"+rootDirectory+"output/"))); Lists.newArrayList(new File(rootDirectory+"output/")));
compiler.typeInference(); compiler.typeInference();
File f = new File(rootDirectory + "ImportTest.class"); File f = new File(rootDirectory + "ImportTest.class");
if(f.exists() && !f.isDirectory()) { if(f.exists() && !f.isDirectory()) {
@ -79,7 +79,7 @@ public class ImportTest extends TestCase {
public void testImportTwoClasses() throws IOException, ClassNotFoundException { public void testImportTwoClasses() throws IOException, ClassNotFoundException {
JavaTXCompiler compiler = new JavaTXCompiler( JavaTXCompiler compiler = new JavaTXCompiler(
Lists.newArrayList(new File(rootDirectory+"ImportTest2.jav")), Lists.newArrayList(new File(rootDirectory+"ImportTest2.jav")),
Lists.newArrayList(new URL("file://"+rootDirectory+"output/"))); Lists.newArrayList(new File(rootDirectory+"output/")));
compiler.typeInference(); compiler.typeInference();
File f = new File(rootDirectory + "ImportTest2.class"); File f = new File(rootDirectory + "ImportTest2.class");
if(f.exists() && !f.isDirectory()) { if(f.exists() && !f.isDirectory()) {

View File

@ -39,7 +39,7 @@ public class mathStrucVectorTest extends TestCase {
public void testSetPackageNameInBytecodeAndOutputFolder() throws IOException, ClassNotFoundException { public void testSetPackageNameInBytecodeAndOutputFolder() throws IOException, ClassNotFoundException {
JavaTXCompiler compiler = new JavaTXCompiler( JavaTXCompiler compiler = new JavaTXCompiler(
Lists.newArrayList(new File(rootDirectory+"mathStrucVector.jav")), Lists.newArrayList(new File(rootDirectory+"mathStrucVector.jav")),
Lists.newArrayList(new URL("file://"+rootDirectory+"output/"))); Lists.newArrayList(new File(rootDirectory+"output/")));
compiler.typeInference(); compiler.typeInference();
File f = new File(rootDirectory + "output/de/test/mathStrucVector.class"); File f = new File(rootDirectory + "output/de/test/mathStrucVector.class");
if(f.exists() && !f.isDirectory()) { if(f.exists() && !f.isDirectory()) {

View File

@ -72,3 +72,7 @@ TODO:
* wurde bereits erledigt -> TODO: Testen * wurde bereits erledigt -> TODO: Testen
## Console Interface um Parameter -classpath und -output directory erweitern ## 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