From fe1154d21293959e1440aa5fcd579e975c07276c Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Thu, 19 Mar 2020 16:50:49 +0100 Subject: [PATCH] Start fixing error where classes are loaded before java files --- .../dhbwstuttgart/bytecode/BytecodeGen.java | 5 +- .../bytecode/BytecodeGenMethod.java | 14 +-- .../de/dhbwstuttgart/core/JavaTXCompiler.java | 2 +- .../environment/CompilationEnvironment.java | 4 +- .../environment/DirectoryClassLoader.java | 20 +++- .../SyntaxTreeGenerator/FCGenerator.java | 9 +- .../SyntaxTreeGenerator.java | 3 +- .../parser/scope/GatherNames.java | 5 +- .../sat/asp/writer/ASPFactory.java | 5 +- .../sat/asp/writer/ASPGencayFactory.java | 5 +- .../syntaxtree/factory/UnifyTypeFactory.java | 3 +- src/test/java/asp/ClingoTest.java | 52 --------- src/test/java/asp/UnifyWithoutWildcards.java | 103 ------------------ src/test/java/asp/gencay/GeneratorTest.java | 63 ----------- src/test/java/asp/typeinference/ASPTest.java | 70 ------------ .../java/asp/typeinference/GenericsTest.java | 9 -- .../java/asp/typeinference/VectorTest.java | 9 -- .../asp/unifywithoutwildcards/ASPTests.java | 10 -- 18 files changed, 48 insertions(+), 343 deletions(-) delete mode 100644 src/test/java/asp/ClingoTest.java delete mode 100644 src/test/java/asp/UnifyWithoutWildcards.java delete mode 100644 src/test/java/asp/gencay/GeneratorTest.java delete mode 100644 src/test/java/asp/typeinference/ASPTest.java delete mode 100644 src/test/java/asp/typeinference/GenericsTest.java delete mode 100644 src/test/java/asp/typeinference/VectorTest.java delete mode 100644 src/test/java/asp/unifywithoutwildcards/ASPTests.java diff --git a/src/main/java/de/dhbwstuttgart/bytecode/BytecodeGen.java b/src/main/java/de/dhbwstuttgart/bytecode/BytecodeGen.java index 04f408af..545e8d81 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/BytecodeGen.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/BytecodeGen.java @@ -8,6 +8,7 @@ import java.util.Iterator; import java.util.List; import java.util.Optional; +import de.dhbwstuttgart.environment.DirectoryClassLoader; import de.dhbwstuttgart.parser.scope.JavaClassName; import org.objectweb.asm.ClassWriter; import org.objectweb.asm.FieldVisitor; @@ -120,10 +121,10 @@ public class BytecodeGen implements ASTVisitor { private GenericsGeneratorResultForClass generatedGenerics; private Resolver resolver; - private final ClassLoader classLoader; + private final DirectoryClassLoader classLoader; public BytecodeGen(HashMap classFiles, Collection listOfResultSets, List simplifyResultsForAllSourceFiles, SourceFile sf, - File path, ClassLoader classLoader) { + File path, DirectoryClassLoader classLoader) { this.classFiles = classFiles; this.listOfResultSets = listOfResultSets; this.simplifyResultsForAllSourceFiles = simplifyResultsForAllSourceFiles; diff --git a/src/main/java/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java b/src/main/java/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java index 0565d933..7a777459 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java @@ -79,13 +79,13 @@ public class BytecodeGenMethod implements StatementVisitor { private int constructorPos = 0; private ArrayList varsFunInterface = new ArrayList<>();; - private final ClassLoader classLoader; + private final DirectoryClassLoader 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, File path, Block block, int constructorPos, ClassLoader classLoader) { + SourceFile sf, File path, Block block, int constructorPos, DirectoryClassLoader classLoader) { this.className = className; this.superClass = superClass; @@ -111,7 +111,7 @@ public class BytecodeGenMethod implements StatementVisitor { 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,File path, ClassLoader classLoader) { + HashMap genericsAndBounds, boolean isInterface, HashMap classFiles, SourceFile sf,File path, DirectoryClassLoader classLoader) { this.className = className; this.superClass = superClass; @@ -135,7 +135,7 @@ public class BytecodeGenMethod implements StatementVisitor { public BytecodeGenMethod(JavaClassName className, ClassWriter cw, LambdaExpression lambdaExpression, ArrayList usedVars, ResultSet resultSet, MethodVisitor mv, int indexOfFirstParamLam, boolean isInterface, HashMap classFiles, File path, int lamCounter, SourceFile sf,HashMap genericsAndBoundsMethod, - HashMap genericsAndBounds, ClassLoader classLoader) { + HashMap genericsAndBounds, DirectoryClassLoader classLoader) { this.className = className; this.cw = cw; this.resultSet = resultSet; @@ -762,10 +762,10 @@ public class BytecodeGenMethod implements StatementVisitor { String mDesc = ""; MethodCallHelper helper = new MethodCallHelper(methodCall, sf, resultSet, path); - - ClassLoader cLoader = this.classLoader; + + DirectoryClassLoader cLoader = this.classLoader; // This will be used if the class is not standard class (not in API) - ClassLoader cLoader2; + DirectoryClassLoader cLoader2; String methCallType = resultSet.resolveType(methodCall.getType()).resolvedType.acceptTV(new TypeToDescriptor()); String[] typesOfParams = getTypes(methodCall.arglist.getArguments()); diff --git a/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java b/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java index 157c6b82..8497e49e 100644 --- a/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java +++ b/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java @@ -77,7 +77,7 @@ public class JavaTXCompiler { public final Map sourceFiles = new HashMap<>(); Boolean log = true; //gibt an ob ein Log-File nach System.getProperty("user.dir")+"src/test/java/logFiles" geschrieben werden soll? public volatile UnifyTaskModel usedTasks = new UnifyTaskModel(); - private final ClassLoader classLoader; + private final DirectoryClassLoader classLoader; public JavaTXCompiler(File sourceFile) throws IOException, ClassNotFoundException { this(Arrays.asList(sourceFile), null); diff --git a/src/main/java/de/dhbwstuttgart/environment/CompilationEnvironment.java b/src/main/java/de/dhbwstuttgart/environment/CompilationEnvironment.java index 746d1e85..b1367e60 100644 --- a/src/main/java/de/dhbwstuttgart/environment/CompilationEnvironment.java +++ b/src/main/java/de/dhbwstuttgart/environment/CompilationEnvironment.java @@ -60,7 +60,7 @@ public class CompilationEnvironment { this.packageCrawler = new PackageCrawler(librarys); } - public JavaClassRegistry getRegistry(File forSourceFile, ClassLoader classLoader) throws ClassNotFoundException, IOException { + public JavaClassRegistry getRegistry(File forSourceFile, DirectoryClassLoader classLoader) throws ClassNotFoundException, IOException { Map allNames; CompilationUnitContext tree = JavaTXParser.parse(forSourceFile); allNames = GatherNames.getNames(tree, packageCrawler, classLoader); @@ -70,7 +70,7 @@ public class CompilationEnvironment { return new JavaClassRegistry(allNames); } - public static List loadDefaultPackageClasses(File forSourceFile, ClassLoader classLoader) throws IOException, ClassNotFoundException { + public static List loadDefaultPackageClasses(File forSourceFile, DirectoryClassLoader classLoader) throws IOException, ClassNotFoundException { List ret = new ArrayList<>(); String packageName = getPackageName(JavaTXParser.parse(forSourceFile)); //Set classLoader to include default package for this specific source file diff --git a/src/main/java/de/dhbwstuttgart/environment/DirectoryClassLoader.java b/src/main/java/de/dhbwstuttgart/environment/DirectoryClassLoader.java index a74dbc49..04ce3f55 100644 --- a/src/main/java/de/dhbwstuttgart/environment/DirectoryClassLoader.java +++ b/src/main/java/de/dhbwstuttgart/environment/DirectoryClassLoader.java @@ -7,13 +7,23 @@ import java.net.URLClassLoader; import java.util.List; import java.util.stream.Collectors; -public class DirectoryClassLoader extends URLClassLoader { +public class DirectoryClassLoader { + private final URLClassLoader classLoader; + public DirectoryClassLoader(File directory, java.lang.ClassLoader parent) { - super(generateURLArray(dirToURL(directory)), parent); + classLoader = new URLClassLoader(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); + classLoader = new URLClassLoader(directory.stream().map(DirectoryClassLoader::dirToURL).collect(Collectors.toList()).toArray(new URL[0]), parent); + } + + public DirectoryClassLoader(File directory, DirectoryClassLoader parent) { + classLoader = new URLClassLoader(generateURLArray(dirToURL(directory)), parent.classLoader); + } + + public DirectoryClassLoader(List directory, DirectoryClassLoader parent) { + classLoader = new URLClassLoader(directory.stream().map(DirectoryClassLoader::dirToURL).collect(Collectors.toList()).toArray(new URL[0]), parent.classLoader); } private static URL[] generateURLArray(URL url) { @@ -28,4 +38,8 @@ public class DirectoryClassLoader extends URLClassLoader { throw new RuntimeException(e); } } + + public Class loadClass(String className) throws ClassNotFoundException { + return classLoader.loadClass(className); + } } diff --git a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/FCGenerator.java b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/FCGenerator.java index 71dcef52..428c60bb 100644 --- a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/FCGenerator.java +++ b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/FCGenerator.java @@ -1,5 +1,6 @@ package de.dhbwstuttgart.parser.SyntaxTreeGenerator; +import de.dhbwstuttgart.environment.DirectoryClassLoader; import de.dhbwstuttgart.exceptions.DebugException; import de.dhbwstuttgart.parser.NullToken; import de.dhbwstuttgart.syntaxtree.ClassOrInterface; @@ -22,11 +23,11 @@ public class FCGenerator { * * @param availableClasses - Alle geparsten Klassen */ - public static Set toUnifyFC(Collection availableClasses, ClassLoader classLoader) throws ClassNotFoundException { + public static Set toUnifyFC(Collection availableClasses, DirectoryClassLoader classLoader) throws ClassNotFoundException { return toFC(availableClasses, classLoader).stream().map(t -> UnifyTypeFactory.convert(t)).collect(Collectors.toSet()); } - public static Set toFC(Collection availableClasses, ClassLoader classLoader) throws ClassNotFoundException { + public static Set toFC(Collection availableClasses, DirectoryClassLoader 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<>(); @@ -48,7 +49,7 @@ public class FCGenerator { * @param forType * @return */ - private static List getSuperTypes(ClassOrInterface forType, Collection availableClasses, ClassLoader classLoader) throws ClassNotFoundException { + private static List getSuperTypes(ClassOrInterface forType, Collection availableClasses, DirectoryClassLoader classLoader) throws ClassNotFoundException { return getSuperTypes(forType, availableClasses, new HashMap<>(), classLoader); } @@ -61,7 +62,7 @@ public class FCGenerator { * @throws ClassNotFoundException */ private static List getSuperTypes(ClassOrInterface forType, Collection availableClasses, - HashMap gtvs, ClassLoader classLoader) throws ClassNotFoundException { + HashMap gtvs, DirectoryClassLoader classLoader) throws ClassNotFoundException { List params = new ArrayList<>(); //Die GTVs, die in forType hinzukommen: HashMap newGTVs = new HashMap<>(); diff --git a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java index c374dd06..97f8bc34 100644 --- a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java +++ b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java @@ -1,5 +1,6 @@ package de.dhbwstuttgart.parser.SyntaxTreeGenerator; +import de.dhbwstuttgart.environment.DirectoryClassLoader; import de.dhbwstuttgart.environment.PackageCrawler; import de.dhbwstuttgart.exceptions.NotImplementedException; import java.lang.ClassNotFoundException; @@ -75,7 +76,7 @@ public class SyntaxTreeGenerator{ return ret; } - public SourceFile convert(Java8Parser.CompilationUnitContext ctx, PackageCrawler packageCrawler, ClassLoader classLoader) throws ClassNotFoundException{ + public SourceFile convert(Java8Parser.CompilationUnitContext ctx, PackageCrawler packageCrawler, DirectoryClassLoader classLoader) throws ClassNotFoundException{ if(ctx.packageDeclaration()!=null)this.pkgName = convert(ctx.packageDeclaration()); List classes = new ArrayList<>(); Map imports = GatherNames.getImports(ctx, packageCrawler, classLoader); diff --git a/src/main/java/de/dhbwstuttgart/parser/scope/GatherNames.java b/src/main/java/de/dhbwstuttgart/parser/scope/GatherNames.java index 8e93355b..7edf2f22 100644 --- a/src/main/java/de/dhbwstuttgart/parser/scope/GatherNames.java +++ b/src/main/java/de/dhbwstuttgart/parser/scope/GatherNames.java @@ -4,6 +4,7 @@ import java.net.URL; import java.net.URLClassLoader; import java.util.*; +import de.dhbwstuttgart.environment.DirectoryClassLoader; import de.dhbwstuttgart.parser.antlr.Java8BaseListener; import de.dhbwstuttgart.syntaxtree.AbstractASTWalker; import org.antlr.v4.runtime.tree.AbstractParseTreeVisitor; @@ -15,7 +16,7 @@ import de.dhbwstuttgart.parser.antlr.Java8Parser; public class GatherNames { - public static Map getNames(Java8Parser.CompilationUnitContext ctx, PackageCrawler packages, ClassLoader classLoader) throws ClassNotFoundException{ + public static Map getNames(Java8Parser.CompilationUnitContext ctx, PackageCrawler packages, DirectoryClassLoader classLoader) throws ClassNotFoundException{ Map ret = new HashMap<>(); String pkgName = getPackageName(ctx); String nameString = ""; @@ -68,7 +69,7 @@ public class GatherNames { return ret; } - public static Map getImports(Java8Parser.CompilationUnitContext ctx, PackageCrawler packages, ClassLoader classLoader) throws ClassNotFoundException { + public static Map getImports(Java8Parser.CompilationUnitContext ctx, PackageCrawler packages, DirectoryClassLoader classLoader) throws ClassNotFoundException { Map ret = new HashMap<>(); //ret.putAll(packages.getClassNames("java.lang")); for(Java8Parser.ImportDeclarationContext importDeclCtx : ctx.importDeclaration()){ 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 8cb61fee..5e60a24b 100644 --- a/src/main/java/de/dhbwstuttgart/sat/asp/writer/ASPFactory.java +++ b/src/main/java/de/dhbwstuttgart/sat/asp/writer/ASPFactory.java @@ -1,5 +1,6 @@ package de.dhbwstuttgart.sat.asp.writer; +import de.dhbwstuttgart.environment.DirectoryClassLoader; import de.dhbwstuttgart.exceptions.NotImplementedException; import de.dhbwstuttgart.parser.SyntaxTreeGenerator.FCGenerator; import de.dhbwstuttgart.sat.asp.ASPStringConverter; @@ -16,7 +17,7 @@ import java.util.*; public class ASPFactory implements TypeVisitor{ - public static String generateASP(ConstraintSet constraints, Collection fcClasses, ClassLoader classLoader) throws ClassNotFoundException{ + public static String generateASP(ConstraintSet constraints, Collection fcClasses, DirectoryClassLoader classLoader) throws ClassNotFoundException{ ASPFactory factory = new ASPFactory(); factory.convertFC(fcClasses, classLoader); List> constraints1 = constraints.cartesianProduct().iterator().next(); @@ -32,7 +33,7 @@ public class ASPFactory implements TypeVisitor{ ASPWriter writer = new ASPWriter(); boolean isFCType = false; - private void convertFC(Collection classes, ClassLoader classLoader) throws ClassNotFoundException { + private void convertFC(Collection classes, DirectoryClassLoader classLoader) throws ClassNotFoundException { Set fc = FCGenerator.toFC(classes, classLoader); isFCType = true; for(Pair fcp : fc){ 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 242c63e6..148fff64 100644 --- a/src/main/java/de/dhbwstuttgart/sat/asp/writer/ASPGencayFactory.java +++ b/src/main/java/de/dhbwstuttgart/sat/asp/writer/ASPGencayFactory.java @@ -1,5 +1,6 @@ package de.dhbwstuttgart.sat.asp.writer; +import de.dhbwstuttgart.environment.DirectoryClassLoader; import de.dhbwstuttgart.exceptions.NotImplementedException; import de.dhbwstuttgart.parser.SyntaxTreeGenerator.FCGenerator; import de.dhbwstuttgart.sat.asp.ASPStringConverter; @@ -55,7 +56,7 @@ public class ASPGencayFactory implements TypeVisitor { return ret; } - public static String generateASP(ConstraintSet constraints, Collection fcClasses, ClassLoader classLoader) throws ClassNotFoundException{ + public static String generateASP(ConstraintSet constraints, Collection fcClasses, DirectoryClassLoader classLoader) throws ClassNotFoundException{ ASPGencayFactory factory = new ASPGencayFactory(); factory.convertFC(fcClasses, classLoader); List> constraints1 = constraints.cartesianProduct().iterator().next(); @@ -69,7 +70,7 @@ public class ASPGencayFactory implements TypeVisitor { return factory.writer.getASPFile(); } - private void convertFC(Collection classes, ClassLoader classLoader) throws ClassNotFoundException { + private void convertFC(Collection classes, DirectoryClassLoader classLoader) throws ClassNotFoundException { Set fc = FCGenerator.toFC(classes, classLoader); isFCType = true; for(Pair fcp : fc){ diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java b/src/main/java/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java index 84d0f1e5..d643bcdc 100644 --- a/src/main/java/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java @@ -7,6 +7,7 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; +import de.dhbwstuttgart.environment.DirectoryClassLoader; import de.dhbwstuttgart.exceptions.DebugException; import de.dhbwstuttgart.exceptions.NotImplementedException; import de.dhbwstuttgart.parser.NullToken; @@ -31,7 +32,7 @@ public class UnifyTypeFactory { private static ArrayList PLACEHOLDERS = new ArrayList<>(); - public static FiniteClosure generateFC(List fromClasses, Writer logFile, ClassLoader classLoader) throws ClassNotFoundException { + public static FiniteClosure generateFC(List fromClasses, Writer logFile, DirectoryClassLoader classLoader) throws ClassNotFoundException { /* Die transitive Hülle muss funktionieren. Man darf schreiben List extends AL diff --git a/src/test/java/asp/ClingoTest.java b/src/test/java/asp/ClingoTest.java deleted file mode 100644 index ea09add3..00000000 --- a/src/test/java/asp/ClingoTest.java +++ /dev/null @@ -1,52 +0,0 @@ -package asp; - -import de.dhbwstuttgart.parser.NullToken; -import de.dhbwstuttgart.sat.asp.parser.ASPParser; -import de.dhbwstuttgart.sat.asp.writer.ASPFactory; -import de.dhbwstuttgart.sat.asp.Clingo; -import de.dhbwstuttgart.syntaxtree.ClassOrInterface; -import de.dhbwstuttgart.syntaxtree.factory.ASTFactory; -import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; -import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; -import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; -import de.dhbwstuttgart.typeinference.constraints.Pair; -import de.dhbwstuttgart.typeinference.result.ResultSet; -import de.dhbwstuttgart.typeinference.unify.model.PairOperator; -import org.junit.Test; - -import javax.json.Json; -import javax.json.JsonObject; -import java.io.*; -import java.util.*; - -public class ClingoTest { - public static final String tempDirectory = "/tmp/"; - private final TypePlaceholder testType = TypePlaceholder.fresh(new NullToken()); - @Test - public void test() throws IOException, InterruptedException, ClassNotFoundException { - String content = ""; - content = ASPFactory.generateASP(this.getPairs(), this.getFC(), ClassLoader.getSystemClassLoader()); - - PrintWriter writer = new PrintWriter(tempDirectory + "test.lp", "UTF-8"); - writer.println(content); - writer.close(); - - Clingo clingo = new Clingo(Arrays.asList(new File(tempDirectory + "test.lp"))); - String result = clingo.runClingo(); - System.out.println(result); - ResultSet resultSet = ASPParser.parse(result, Arrays.asList(testType)); - RefTypeOrTPHOrWildcardOrGeneric resolvedType = resultSet.resolveType(testType).resolvedType; - assert resolvedType.toString().equals(ASTFactory.createObjectType().toString()); - } - - public Collection getFC() { - Set ret = new HashSet<>(); - return ret; - } - - public ConstraintSet getPairs() { - ConstraintSet ret = new ConstraintSet<>(); - ret.addUndConstraint(new Pair(testType, ASTFactory.createObjectType(), PairOperator.EQUALSDOT)); - return ret; - } -} diff --git a/src/test/java/asp/UnifyWithoutWildcards.java b/src/test/java/asp/UnifyWithoutWildcards.java deleted file mode 100644 index c1a84702..00000000 --- a/src/test/java/asp/UnifyWithoutWildcards.java +++ /dev/null @@ -1,103 +0,0 @@ -package asp; - -import de.dhbwstuttgart.parser.NullToken; -import de.dhbwstuttgart.parser.scope.JavaClassName; -import de.dhbwstuttgart.sat.asp.Clingo; -import de.dhbwstuttgart.sat.asp.parser.ASPParser; -import de.dhbwstuttgart.sat.asp.writer.ASPFactory; -import de.dhbwstuttgart.syntaxtree.ClassOrInterface; -import de.dhbwstuttgart.syntaxtree.factory.ASTFactory; -import de.dhbwstuttgart.syntaxtree.type.*; -import de.dhbwstuttgart.syntaxtree.visual.ResultSetPrinter; -import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; -import de.dhbwstuttgart.typeinference.constraints.Pair; -import de.dhbwstuttgart.typeinference.result.ResultSet; -import de.dhbwstuttgart.typeinference.unify.model.PairOperator; -import org.junit.Test; - -import java.io.File; -import java.io.IOException; -import java.io.PrintWriter; -import java.sql.Ref; -import java.util.*; - -public class UnifyWithoutWildcards { - - public static final String tempDirectory = "/tmp/"; - - @Test - public void adapt() throws InterruptedException, IOException, ClassNotFoundException { - ConstraintSet testSet = new ConstraintSet<>(); - List list1 = Arrays.asList(TypePlaceholder.fresh(new NullToken())); - List list2 = Arrays.asList(TypePlaceholder.fresh(new NullToken()),TypePlaceholder.fresh(new NullToken())); - 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)); - ResultSet resultSet = run(testSet); - System.out.println(ResultSetPrinter.print(resultSet)); - assert resultSet.results.size() > 0; - } - - public ResultSet run(ConstraintSet toTest) throws IOException, InterruptedException, ClassNotFoundException { - String content = ""; - content = ASPFactory.generateASP(toTest, this.getFC(), ClassLoader.getSystemClassLoader()); - - PrintWriter writer = new PrintWriter(tempDirectory + "test.lp", "UTF-8"); - writer.println(content); - writer.close(); - Clingo clingo = new Clingo(Arrays.asList(new File(tempDirectory + "test.lp"))); - String result = clingo.runClingo(); - ResultSet resultSet = ASPParser.parse(result, getInvolvedTPHS(toTest)); - return resultSet; - } - - private static class TPHExtractor implements TypeVisitor>{ - @Override - public List visit(RefType refType) { - ArrayList ret = new ArrayList<>(); - for(RefTypeOrTPHOrWildcardOrGeneric param : refType.getParaList()){ - ret.addAll(param.acceptTV(this)); - } - return ret; - } - - @Override - public List visit(SuperWildcardType superWildcardType) { - return superWildcardType.getInnerType().acceptTV(this); - } - - @Override - public List visit(TypePlaceholder typePlaceholder) { - return Arrays.asList(typePlaceholder); - } - - @Override - public List visit(ExtendsWildcardType extendsWildcardType) { - return extendsWildcardType.getInnerType().acceptTV(this); - } - - @Override - public List visit(GenericRefType genericRefType) { - return new ArrayList<>(); - } - } - - protected Collection getInvolvedTPHS(ConstraintSet toTest) { - List ret = new ArrayList<>(); - toTest.map((Pair p)-> { - ret.addAll(p.TA1.acceptTV(new TPHExtractor())); - ret.addAll(p.TA2.acceptTV(new TPHExtractor())); - return p; - }); - return ret; - } - - private Collection getFC() { - Set ret = new HashSet<>(); - ret.add(ASTFactory.createClass(Matrix.class)); - //ret.add(ASTFactory.createObjectClass()); - //ret.add(ASTFactory.createClass(java.util.List.class)); - return ret; - } - private class Matrix extends HashMap>{} -} diff --git a/src/test/java/asp/gencay/GeneratorTest.java b/src/test/java/asp/gencay/GeneratorTest.java deleted file mode 100644 index 36a74bde..00000000 --- a/src/test/java/asp/gencay/GeneratorTest.java +++ /dev/null @@ -1,63 +0,0 @@ -package asp.gencay; - -import asp.UnifyWithoutWildcards; -import de.dhbwstuttgart.parser.NullToken; -import de.dhbwstuttgart.parser.scope.JavaClassName; -import de.dhbwstuttgart.sat.asp.Clingo; -import de.dhbwstuttgart.sat.asp.parser.ASPParser; -import de.dhbwstuttgart.sat.asp.writer.ASPFactory; -import de.dhbwstuttgart.sat.asp.writer.ASPGencayFactory; -import de.dhbwstuttgart.syntaxtree.ClassOrInterface; -import de.dhbwstuttgart.syntaxtree.factory.ASTFactory; -import de.dhbwstuttgart.syntaxtree.type.RefType; -import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; -import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; -import de.dhbwstuttgart.syntaxtree.visual.ResultSetPrinter; -import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; -import de.dhbwstuttgart.typeinference.constraints.Pair; -import de.dhbwstuttgart.typeinference.result.ResultSet; -import de.dhbwstuttgart.typeinference.unify.model.PairOperator; -import org.junit.Test; - -import java.io.File; -import java.io.IOException; -import java.io.PrintWriter; -import java.util.*; -import java.util.concurrent.ArrayBlockingQueue; - -public class GeneratorTest extends UnifyWithoutWildcards{ - @Test - public void simple() throws ClassNotFoundException { - ConstraintSet testSet = new ConstraintSet<>(); - List list1 = Arrays.asList(TypePlaceholder.fresh(new NullToken())); - List list2 = Arrays.asList(TypePlaceholder.fresh(new NullToken())); - RefType t1 = new RefType(new JavaClassName("java.util.List"), list1, new NullToken()); - 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))), ClassLoader.getSystemClassLoader()); - System.out.println(resultSet); - } - - @Test - public void matrix() throws ClassNotFoundException { - ConstraintSet testSet = new ConstraintSet<>(); - List list1 = Arrays.asList(TypePlaceholder.fresh(new NullToken())); - List list2 = Arrays.asList(TypePlaceholder.fresh(new NullToken()),TypePlaceholder.fresh(new NullToken())); - 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(), ClassLoader.getSystemClassLoader()); - System.out.println(resultSet); - } - - private Collection getFC() { - Set ret = new HashSet<>(); - ret.add(ASTFactory.createClass(Matrix.class)); - //ret.add(ASTFactory.createObjectClass()); - //ret.add(ASTFactory.createClass(java.util.List.class)); - return ret; - } -} - -class Matrix extends Vector> {} \ No newline at end of file diff --git a/src/test/java/asp/typeinference/ASPTest.java b/src/test/java/asp/typeinference/ASPTest.java deleted file mode 100644 index 989e1233..00000000 --- a/src/test/java/asp/typeinference/ASPTest.java +++ /dev/null @@ -1,70 +0,0 @@ -package asp.typeinference; - -import de.dhbwstuttgart.core.JavaTXCompiler; -import de.dhbwstuttgart.sat.asp.writer.ASPFactory; -import de.dhbwstuttgart.syntaxtree.ClassOrInterface; -import de.dhbwstuttgart.syntaxtree.SourceFile; -import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; -import de.dhbwstuttgart.typeinference.constraints.Pair; -import org.junit.Test; - -import java.io.File; -import java.io.IOException; -import java.nio.charset.Charset; -import java.nio.file.Files; -import java.nio.file.Paths; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Set; - -public class ASPTest { - - public static final String rootDirectory = System.getProperty("user.dir")+"/src/test/resources/javFiles/"; - private static final List filesToTest = new ArrayList<>(); - protected File fileToTest = null; - - public ASPTest(){ - } - - - @Test - public void test() throws IOException, ClassNotFoundException { - if(fileToTest != null)filesToTest.add(fileToTest); - else return; - //filesToTest.add(new File(rootDirectory+"Faculty.jav")); - //filesToTest.add(new File(rootDirectory+"mathStruc.jav")); - //filesToTest.add(new File(rootDirectory+"test.jav")); - filesToTest.add(new File(rootDirectory+"EmptyMethod.jav")); - //filesToTest.add(new File(rootDirectory+"fc.jav")); - //filesToTest.add(new File(rootDirectory+"Lambda.jav")); - //filesToTest.add(new File(rootDirectory+"Lambda2.jav")); - //filesToTest.add(new File(rootDirectory+"Lambda3.jav")); - //filesToTest.add(new File(rootDirectory+"Vector.jav")); - //filesToTest.add(new File(rootDirectory+"Generics.jav")); - //filesToTest.add(new File(rootDirectory+"MethodsEasy.jav")); - //filesToTest.add(new File(rootDirectory+"Matrix.jav")); - //filesToTest.add(new File(rootDirectory+"Import.jav")); - JavaTXCompiler compiler = new JavaTXCompiler(fileToTest); - Set allClasses = new HashSet<>(); - for(SourceFile sf : compiler.sourceFiles.values()) { - allClasses.addAll(compiler.getAvailableClasses(sf)); - } - for(SourceFile sf : compiler.sourceFiles.values()) { - allClasses.addAll(sf.getClasses()); - } - - final ConstraintSet cons = compiler.getConstraints(); - String asp = ASPFactory.generateASP(cons, allClasses, ClassLoader.getSystemClassLoader()); - System.out.println(asp); - } - - static String readFile(String path, Charset encoding) - throws IOException - { - byte[] encoded = Files.readAllBytes(Paths.get(path)); - return new String(encoded, encoding); - } - -} - diff --git a/src/test/java/asp/typeinference/GenericsTest.java b/src/test/java/asp/typeinference/GenericsTest.java deleted file mode 100644 index b0f0330a..00000000 --- a/src/test/java/asp/typeinference/GenericsTest.java +++ /dev/null @@ -1,9 +0,0 @@ -package asp.typeinference; - -import java.io.File; - -public class GenericsTest extends ASPTest { - public GenericsTest() { - this.fileToTest = new File(rootDirectory+"Generics.jav"); - } -} \ No newline at end of file diff --git a/src/test/java/asp/typeinference/VectorTest.java b/src/test/java/asp/typeinference/VectorTest.java deleted file mode 100644 index 77ff4959..00000000 --- a/src/test/java/asp/typeinference/VectorTest.java +++ /dev/null @@ -1,9 +0,0 @@ -package asp.typeinference; - -import java.io.File; - -public class VectorTest extends ASPTest { - public VectorTest() { - this.fileToTest = new File(rootDirectory+"Vector.jav"); - } -} \ No newline at end of file diff --git a/src/test/java/asp/unifywithoutwildcards/ASPTests.java b/src/test/java/asp/unifywithoutwildcards/ASPTests.java deleted file mode 100644 index 80e7bb78..00000000 --- a/src/test/java/asp/unifywithoutwildcards/ASPTests.java +++ /dev/null @@ -1,10 +0,0 @@ -package asp.unifywithoutwildcards; - -import org.junit.Test; - -public class ASPTests { - @Test - public void test(){ - - } -}