From 0378c17e7d61de3bf7c836e1aab823ff60038d58 Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Sun, 22 Dec 2019 14:27:54 +0100 Subject: [PATCH] ClassLoader austauschen gegen den Classloader, welcher in JavaCompilerCore erstellt wird --- .../de/dhbwstuttgart/bytecode/BytecodeGen.java | 4 ++-- .../bytecode/BytecodeGenMethod.java | 18 ++++++++++-------- .../de/dhbwstuttgart/core/JavaTXCompiler.java | 8 ++++---- .../environment/CompilationEnvironment.java | 1 - .../SyntaxTreeGenerator/FCGenerator.java | 18 +++++++++--------- .../sat/asp/writer/ASPFactory.java | 8 ++++---- .../sat/asp/writer/ASPGencayFactory.java | 8 ++++---- .../syntaxtree/factory/UnifyTypeFactory.java | 4 ++-- src/test/java/asp/ClingoTest.java | 2 +- src/test/java/asp/UnifyWithoutWildcards.java | 2 +- src/test/java/asp/gencay/GeneratorTest.java | 4 ++-- src/test/java/asp/typeinference/ASPTest.java | 2 +- .../finiteClosure/SuperInterfacesTest.java | 4 ++-- vorgehen.md | 2 ++ 14 files changed, 44 insertions(+), 41 deletions(-) diff --git a/src/main/java/de/dhbwstuttgart/bytecode/BytecodeGen.java b/src/main/java/de/dhbwstuttgart/bytecode/BytecodeGen.java index fff65180..b5ac3e0b 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/BytecodeGen.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/BytecodeGen.java @@ -270,7 +270,7 @@ public class BytecodeGen implements ASTVisitor { constructorPos += 1; BytecodeGenMethod gen = new BytecodeGenMethod(className, superClass, resultSet, field, mv, paramsAndLocals, cw, - genericsAndBoundsMethod, genericsAndBounds, isInterface, classFiles, sf, path, block, constructorPos); + genericsAndBoundsMethod, genericsAndBounds, isInterface, classFiles, sf, path, block, constructorPos, ClassLoader.getSystemClassLoader()); if (!field.getParameterList().iterator().hasNext() && !(field.block.statements.get(field.block.statements.size() - 1) instanceof ReturnVoid)) { mv.visitInsn(Opcodes.RETURN); @@ -351,7 +351,7 @@ public class BytecodeGen implements ASTVisitor { mv.visitCode(); BytecodeGenMethod gen = new BytecodeGenMethod(className, superClass, resultSet, method, mv, paramsAndLocals, cw, - genericsAndBoundsMethod, genericsAndBounds, isInterface, classFiles, sf, path); + genericsAndBoundsMethod, genericsAndBounds, isInterface, classFiles, sf, path, ClassLoader.getSystemClassLoader()); mv.visitMaxs(0, 0); mv.visitEnd(); diff --git a/src/main/java/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java b/src/main/java/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java index 04f895fe..b0ada708 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java @@ -77,11 +77,13 @@ public class BytecodeGenMethod implements StatementVisitor { private int constructorPos = 0; private ArrayList varsFunInterface = new ArrayList<>();; + private final ClassLoader classLoader; + // generate bytecode for constructor public BytecodeGenMethod(JavaClassName className, String superClass, ResultSet resultSet, Method m, MethodVisitor mv, HashMap paramsAndLocals, ClassWriter cw, HashMap genericsAndBoundsMethod, HashMap genericsAndBounds, boolean isInterface, HashMap classFiles, - SourceFile sf, String path, Block block, int constructorPos) { + SourceFile sf, String path, Block block, int constructorPos, ClassLoader classLoader) { this.className = className; this.superClass = superClass; @@ -99,16 +101,15 @@ public class BytecodeGenMethod implements StatementVisitor { this.path = path; this.lamCounter = -1; this.constructorPos = constructorPos; + this.classLoader = classLoader; if(block != null) this.blockFieldInit = block; this.m.block.accept(this); - - } public BytecodeGenMethod(JavaClassName className, String superClass,ResultSet resultSet, Method m, MethodVisitor mv, HashMap paramsAndLocals, ClassWriter cw, HashMap genericsAndBoundsMethod, - HashMap genericsAndBounds, boolean isInterface, HashMap classFiles, SourceFile sf,String path) { + HashMap genericsAndBounds, boolean isInterface, HashMap classFiles, SourceFile sf,String path, ClassLoader classLoader) { this.className = className; this.superClass = superClass; @@ -125,14 +126,14 @@ public class BytecodeGenMethod implements StatementVisitor { this.sf = sf; this.path = path; this.lamCounter = -1; + this.classLoader = classLoader; if (!isInterface) this.m.block.accept(this); - } public BytecodeGenMethod(JavaClassName className, ClassWriter cw, LambdaExpression lambdaExpression, ArrayList usedVars, ResultSet resultSet, MethodVisitor mv, int indexOfFirstParamLam, boolean isInterface, HashMap classFiles, String path, int lamCounter, SourceFile sf,HashMap genericsAndBoundsMethod, - HashMap genericsAndBounds) { + HashMap genericsAndBounds, ClassLoader classLoader) { this.className = className; this.cw = cw; this.resultSet = resultSet; @@ -145,6 +146,7 @@ public class BytecodeGenMethod implements StatementVisitor { this.sf = sf; this.genericsAndBoundsMethod = genericsAndBoundsMethod; this.genericsAndBounds = genericsAndBounds; + this.classLoader = classLoader; Iterator itr = lambdaExpression.params.iterator(); int i = indexOfFirstParamLam; @@ -637,7 +639,7 @@ public class BytecodeGenMethod implements StatementVisitor { new BytecodeGenMethod(className, cw,lambdaExpression, usedVars,this.resultSet, mvLambdaBody, indexOfFirstParamLam, isInterface, classFiles,this.path, lamCounter, sf, genericsAndBoundsMethod, - genericsAndBounds); + genericsAndBounds, classLoader); mvLambdaBody.visitMaxs(0, 0); mvLambdaBody.visitEnd(); @@ -757,7 +759,7 @@ public class BytecodeGenMethod implements StatementVisitor { MethodCallHelper helper = new MethodCallHelper(methodCall, sf, resultSet, path); - ClassLoader cLoader = ClassLoader.getSystemClassLoader(); + ClassLoader cLoader = this.classLoader; // This will be used if the class is not standard class (not in API) ClassLoader cLoader2; diff --git a/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java b/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java index 61f90e42..f79a3a35 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 { for (JavaClassName name : sourceFiles.get(forSourceFile).getImports()) { //TODO: Hier werden imports von eigenen (.jav) Klassen nicht beachtet ClassOrInterface importedClass = ASTFactory.createClass( - ClassLoader.getSystemClassLoader().loadClass(name.toString())); + classLoader.loadClass(name.toString())); importedClasses.add(importedClass); } allClasses.addAll(importedClasses); @@ -135,7 +135,7 @@ public class JavaTXCompiler { for (JavaClassName name : forSourceFile.getImports()) { // TODO: Hier werden imports von eigenen (.jav) Klassen nicht beachtet ClassOrInterface importedClass = ASTFactory - .createClass(ClassLoader.getSystemClassLoader().loadClass(name.toString())); + .createClass(classLoader.loadClass(name.toString())); importedClasses.add(importedClass); allClasses.addAll(importedClasses); } @@ -304,7 +304,7 @@ public class JavaTXCompiler { logFile = logFile == null ? new FileWriter(new File("log_" + sourceFiles.keySet().iterator().next().getName())) : logFile; - IFiniteClosure finiteClosure = UnifyTypeFactory.generateFC(allClasses, logFile); + IFiniteClosure finiteClosure = UnifyTypeFactory.generateFC(allClasses, logFile, classLoader); System.out.println(finiteClosure); urm = new UnifyResultModel(cons, finiteClosure); urm.addUnifyResultListener(resultListener); @@ -472,7 +472,7 @@ public class JavaTXCompiler { // File(System.getProperty("user.dir")+"/src/test/resources/logFiles/"+"log_"+sourceFiles.keySet().iterator().next().getName())); new FileWriter(new File(System.getProperty("user.dir") + "/logFiles/" + "log_" + sourceFiles.keySet().iterator().next().getName())); - IFiniteClosure finiteClosure = UnifyTypeFactory.generateFC(allClasses, logFile); + IFiniteClosure finiteClosure = UnifyTypeFactory.generateFC(allClasses, logFile, classLoader); System.out.println(finiteClosure); ConstraintSet unifyCons = UnifyTypeFactory.convert(cons); diff --git a/src/main/java/de/dhbwstuttgart/environment/CompilationEnvironment.java b/src/main/java/de/dhbwstuttgart/environment/CompilationEnvironment.java index 7b759e27..43196b6f 100644 --- a/src/main/java/de/dhbwstuttgart/environment/CompilationEnvironment.java +++ b/src/main/java/de/dhbwstuttgart/environment/CompilationEnvironment.java @@ -50,7 +50,6 @@ public class CompilationEnvironment { */ //String bootClassPath = System.getProperty("sun.boot.class.path"); // ClassLoader cl = ClassLoader.getPlatformClassLoader(); - ClassLoader cl = ClassLoader.getSystemClassLoader(); String bootClassPath = System.getProperty("java.class.path"); librarys = new ArrayList<>(); for(String path : bootClassPath.split(File.pathSeparator)) { diff --git a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/FCGenerator.java b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/FCGenerator.java index 29c53981..71dcef52 100644 --- a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/FCGenerator.java +++ b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/FCGenerator.java @@ -22,16 +22,16 @@ public class FCGenerator { * * @param availableClasses - Alle geparsten Klassen */ - public static Set toUnifyFC(Collection availableClasses) throws ClassNotFoundException { - return toFC(availableClasses).stream().map(t -> UnifyTypeFactory.convert(t)).collect(Collectors.toSet()); + public static Set toUnifyFC(Collection availableClasses, ClassLoader classLoader) throws ClassNotFoundException { + return toFC(availableClasses, classLoader).stream().map(t -> UnifyTypeFactory.convert(t)).collect(Collectors.toSet()); } - public static Set toFC(Collection availableClasses) throws ClassNotFoundException { + public static Set toFC(Collection availableClasses, ClassLoader classLoader) throws ClassNotFoundException { HashSet pairs = new HashSet<>(); //PL 2018-09-18: gtvs vor die for-Schleife gezogen, damit immer die gleichen Typeplaceholder eingesetzt werden. HashMap gtvs = new HashMap<>(); for(ClassOrInterface cly : availableClasses){ - pairs.addAll(getSuperTypes(cly, availableClasses, gtvs)); + pairs.addAll(getSuperTypes(cly, availableClasses, gtvs, classLoader)); } return pairs; } @@ -48,8 +48,8 @@ public class FCGenerator { * @param forType * @return */ - private static List getSuperTypes(ClassOrInterface forType, Collection availableClasses) throws ClassNotFoundException { - return getSuperTypes(forType, availableClasses, new HashMap<>()); + private static List getSuperTypes(ClassOrInterface forType, Collection availableClasses, ClassLoader classLoader) throws ClassNotFoundException { + return getSuperTypes(forType, availableClasses, new HashMap<>(), classLoader); } /** @@ -61,7 +61,7 @@ public class FCGenerator { * @throws ClassNotFoundException */ private static List getSuperTypes(ClassOrInterface forType, Collection availableClasses, - HashMap gtvs) throws ClassNotFoundException { + HashMap gtvs, ClassLoader classLoader) throws ClassNotFoundException { List params = new ArrayList<>(); //Die GTVs, die in forType hinzukommen: HashMap newGTVs = new HashMap<>(); @@ -86,7 +86,7 @@ public class FCGenerator { ClassOrInterface superClass; if(!hasSuperclass.isPresent()) //Wenn es die Klasse in den available Klasses nicht gibt wird sie im Classpath gesucht. Ansonsten Exception { - superClass = ASTFactory.createClass(ClassLoader.getSystemClassLoader().loadClass(superType.getName().toString())); + superClass = ASTFactory.createClass(classLoader.loadClass(superType.getName().toString())); }else{ superClass = hasSuperclass.get(); } @@ -120,7 +120,7 @@ public class FCGenerator { if(superClass.getClassName().equals(ASTFactory.createObjectClass().getClassName())){ superTypes = Arrays.asList(new Pair(ASTFactory.createObjectType(), ASTFactory.createObjectType(), PairOperator.SMALLER)); }else{ - superTypes = getSuperTypes(superClass, availableClasses, newGTVs); + superTypes = getSuperTypes(superClass, availableClasses, newGTVs, classLoader); } retList.add(ret); diff --git a/src/main/java/de/dhbwstuttgart/sat/asp/writer/ASPFactory.java b/src/main/java/de/dhbwstuttgart/sat/asp/writer/ASPFactory.java index 1777eabd..8cb61fee 100644 --- a/src/main/java/de/dhbwstuttgart/sat/asp/writer/ASPFactory.java +++ b/src/main/java/de/dhbwstuttgart/sat/asp/writer/ASPFactory.java @@ -16,9 +16,9 @@ import java.util.*; public class ASPFactory implements TypeVisitor{ - public static String generateASP(ConstraintSet constraints, Collection fcClasses) throws ClassNotFoundException{ + public static String generateASP(ConstraintSet constraints, Collection fcClasses, ClassLoader classLoader) throws ClassNotFoundException{ ASPFactory factory = new ASPFactory(); - factory.convertFC(fcClasses); + factory.convertFC(fcClasses, classLoader); List> constraints1 = constraints.cartesianProduct().iterator().next(); for(Constraint constraint : constraints1){ for(Pair p : constraint){ @@ -32,8 +32,8 @@ public class ASPFactory implements TypeVisitor{ ASPWriter writer = new ASPWriter(); boolean isFCType = false; - private void convertFC(Collection classes) throws ClassNotFoundException { - Set fc = FCGenerator.toFC(classes); + private void convertFC(Collection classes, ClassLoader classLoader) throws ClassNotFoundException { + Set fc = FCGenerator.toFC(classes, classLoader); isFCType = true; for(Pair fcp : fc){ convertPair(fcp); diff --git a/src/main/java/de/dhbwstuttgart/sat/asp/writer/ASPGencayFactory.java b/src/main/java/de/dhbwstuttgart/sat/asp/writer/ASPGencayFactory.java index 11408c25..242c63e6 100644 --- a/src/main/java/de/dhbwstuttgart/sat/asp/writer/ASPGencayFactory.java +++ b/src/main/java/de/dhbwstuttgart/sat/asp/writer/ASPGencayFactory.java @@ -55,9 +55,9 @@ public class ASPGencayFactory implements TypeVisitor { return ret; } - public static String generateASP(ConstraintSet constraints, Collection fcClasses) throws ClassNotFoundException{ + public static String generateASP(ConstraintSet constraints, Collection fcClasses, ClassLoader classLoader) throws ClassNotFoundException{ ASPGencayFactory factory = new ASPGencayFactory(); - factory.convertFC(fcClasses); + factory.convertFC(fcClasses, classLoader); List> constraints1 = constraints.cartesianProduct().iterator().next(); for(Constraint constraint : constraints1){ for(Pair p : constraint){ @@ -69,8 +69,8 @@ public class ASPGencayFactory implements TypeVisitor { return factory.writer.getASPFile(); } - private void convertFC(Collection classes) throws ClassNotFoundException { - Set fc = FCGenerator.toFC(classes); + private void convertFC(Collection classes, ClassLoader classLoader) throws ClassNotFoundException { + Set fc = FCGenerator.toFC(classes, classLoader); isFCType = true; for(Pair fcp : fc){ generateTheta((RefType) fcp.TA1); diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java b/src/main/java/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java index 4241af34..33d137eb 100644 --- a/src/main/java/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java @@ -31,7 +31,7 @@ public class UnifyTypeFactory { private static ArrayList PLACEHOLDERS = new ArrayList<>(); - public static FiniteClosure generateFC(List fromClasses, Writer logFile) throws ClassNotFoundException { + public static FiniteClosure generateFC(List fromClasses, Writer logFile, ClassLoader classLoader) throws ClassNotFoundException { /* Die transitive Hülle muss funktionieren. Man darf schreiben List extends AL @@ -42,7 +42,7 @@ public class UnifyTypeFactory { Generell dürfen sie immer die gleichen Namen haben. TODO: die transitive Hülle bilden */ - return new FiniteClosure(FCGenerator.toUnifyFC(fromClasses), logFile); + return new FiniteClosure(FCGenerator.toUnifyFC(fromClasses, classLoader), logFile); } public static UnifyPair generateSmallerPair(UnifyType tl, UnifyType tr){ diff --git a/src/test/java/asp/ClingoTest.java b/src/test/java/asp/ClingoTest.java index 90635bde..ea09add3 100644 --- a/src/test/java/asp/ClingoTest.java +++ b/src/test/java/asp/ClingoTest.java @@ -25,7 +25,7 @@ public class ClingoTest { @Test public void test() throws IOException, InterruptedException, ClassNotFoundException { String content = ""; - content = ASPFactory.generateASP(this.getPairs(), this.getFC()); + content = ASPFactory.generateASP(this.getPairs(), this.getFC(), ClassLoader.getSystemClassLoader()); PrintWriter writer = new PrintWriter(tempDirectory + "test.lp", "UTF-8"); writer.println(content); diff --git a/src/test/java/asp/UnifyWithoutWildcards.java b/src/test/java/asp/UnifyWithoutWildcards.java index 6f69d639..c1a84702 100644 --- a/src/test/java/asp/UnifyWithoutWildcards.java +++ b/src/test/java/asp/UnifyWithoutWildcards.java @@ -40,7 +40,7 @@ public class UnifyWithoutWildcards { public ResultSet run(ConstraintSet toTest) throws IOException, InterruptedException, ClassNotFoundException { String content = ""; - content = ASPFactory.generateASP(toTest, this.getFC()); + content = ASPFactory.generateASP(toTest, this.getFC(), ClassLoader.getSystemClassLoader()); PrintWriter writer = new PrintWriter(tempDirectory + "test.lp", "UTF-8"); writer.println(content); diff --git a/src/test/java/asp/gencay/GeneratorTest.java b/src/test/java/asp/gencay/GeneratorTest.java index 2c9fc46f..36a74bde 100644 --- a/src/test/java/asp/gencay/GeneratorTest.java +++ b/src/test/java/asp/gencay/GeneratorTest.java @@ -35,7 +35,7 @@ public class GeneratorTest extends UnifyWithoutWildcards{ RefType t2 = new RefType(new JavaClassName("java.util.List"), list2, new NullToken()); testSet.addUndConstraint(new Pair(t1, t2, PairOperator.SMALLERDOT)); String resultSet = ASPGencayFactory.generateASP(testSet, - new HashSet<>(Arrays.asList(ASTFactory.createClass(List.class)))); + new HashSet<>(Arrays.asList(ASTFactory.createClass(List.class))), ClassLoader.getSystemClassLoader()); System.out.println(resultSet); } @@ -47,7 +47,7 @@ public class GeneratorTest extends UnifyWithoutWildcards{ RefType t1 = new RefType(new JavaClassName("asp.UnifyWithoutWildcards$Matrix"), list1, new NullToken()); RefType t2 = new RefType(new JavaClassName("java.util.HashMap"), list2, new NullToken()); testSet.addUndConstraint(new Pair(t1, t2, PairOperator.SMALLERDOT)); - String resultSet = ASPGencayFactory.generateASP(testSet, this.getFC()); + String resultSet = ASPGencayFactory.generateASP(testSet, this.getFC(), ClassLoader.getSystemClassLoader()); System.out.println(resultSet); } diff --git a/src/test/java/asp/typeinference/ASPTest.java b/src/test/java/asp/typeinference/ASPTest.java index 5a43b6a9..989e1233 100644 --- a/src/test/java/asp/typeinference/ASPTest.java +++ b/src/test/java/asp/typeinference/ASPTest.java @@ -55,7 +55,7 @@ public class ASPTest { } final ConstraintSet cons = compiler.getConstraints(); - String asp = ASPFactory.generateASP(cons, allClasses); + String asp = ASPFactory.generateASP(cons, allClasses, ClassLoader.getSystemClassLoader()); System.out.println(asp); } diff --git a/src/test/java/finiteClosure/SuperInterfacesTest.java b/src/test/java/finiteClosure/SuperInterfacesTest.java index 0e1368c3..d74215b7 100644 --- a/src/test/java/finiteClosure/SuperInterfacesTest.java +++ b/src/test/java/finiteClosure/SuperInterfacesTest.java @@ -14,14 +14,14 @@ public class SuperInterfacesTest { public void test() throws ClassNotFoundException { Collection classes = new ArrayList<>(); classes.add(ASTFactory.createClass(TestClass.class)); - System.out.println(FCGenerator.toFC(classes)); + System.out.println(FCGenerator.toFC(classes, ClassLoader.getSystemClassLoader())); } @Test public void testGeneric() throws ClassNotFoundException { Collection classes = new ArrayList<>(); classes.add(ASTFactory.createClass(TestClassGeneric.class)); - System.out.println(FCGenerator.toFC(classes)); + System.out.println(FCGenerator.toFC(classes, ClassLoader.getSystemClassLoader())); } } diff --git a/vorgehen.md b/vorgehen.md index 7ee69df2..0854c152 100644 --- a/vorgehen.md +++ b/vorgehen.md @@ -59,3 +59,5 @@ TODO: * kann möglicherweise ASM * Kann ASM mittesl `cw.visit("name/mit/slashe/Getrennt"..` * wurde bereits erledigt -> TODO: Testen + +## Console Interface um Parameter -classpath und -output directory erweitern