From 6d70464a1c43dd3066e04da331ed791e6990ef8a Mon Sep 17 00:00:00 2001 From: Fayez Abu Alia Date: Tue, 19 Jun 2018 13:31:39 +0200 Subject: [PATCH 1/5] TPH X < TPH Y <...< TPH Z vereinfacht => TPH X < TPH Z und Signature-Erzeugung wird angepasst --- .../dhbwstuttgart/bytecode/BytecodeGen.java | 101 ++++++++++++------ .../bytecode/signature/Signature.java | 10 -- 2 files changed, 71 insertions(+), 40 deletions(-) diff --git a/src/de/dhbwstuttgart/bytecode/BytecodeGen.java b/src/de/dhbwstuttgart/bytecode/BytecodeGen.java index 5115a29b..0d467b77 100644 --- a/src/de/dhbwstuttgart/bytecode/BytecodeGen.java +++ b/src/de/dhbwstuttgart/bytecode/BytecodeGen.java @@ -30,6 +30,7 @@ import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import de.dhbwstuttgart.syntaxtree.type.SuperWildcardType; import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; import de.dhbwstuttgart.typeinference.result.GenericInsertPair; +import de.dhbwstuttgart.typeinference.result.ResolvedType; import de.dhbwstuttgart.typeinference.result.ResultPair; import de.dhbwstuttgart.typeinference.result.ResultSet; @@ -54,8 +55,8 @@ public class BytecodeGen implements ASTVisitor { // stores generics and their bounds of method HashMap genericsAndBoundsMethod = new HashMap<>(); + private final TPHExtractor tphExtractor = new TPHExtractor(); private final ArrayList commonPairs = new ArrayList<>(); - private ArrayList ListOfMethodsAndTph = new ArrayList<>(); HashMap methodParamsAndTypes = new HashMap<>(); byte[] bytecode; @@ -115,11 +116,10 @@ public class BytecodeGen implements ASTVisitor { resultSet = rs; // Nur einmal ausführen!! if(!isVisited) { - TPHExtractor tphExtractor = new TPHExtractor(); classOrInterface.accept(tphExtractor); getCommonTPHS(tphExtractor); - ListOfMethodsAndTph = tphExtractor.ListOfMethodsAndTph; + String sig = null; /* if class has generics then creates signature * Signature looks like: @@ -164,26 +164,6 @@ public class BytecodeGen implements ASTVisitor { if(!tphExtractor.allTPHS.get(tph)) cTPHs.add(tph); } - - ArrayList tphsMethod = tphExtractor.ListOfMethodsAndTph; - // Für jede Methode speichere die gemeinsame TPHs: - // -> Für jedes Pair prüfe ob, auf der rechten Seite ein TPH steht, der - // in der Liste der TPHs der Methode enthalten ist. - // Wenn ja -> gemeinsamer TPH - for(MethodAndTPH m:tphsMethod){ - for(GenericInsertPair p : m.getPairs()){ - if(!m.getTphs().contains(p.TA2)) - cTPHs.add(p.TA2); - } - } - - for(TypePlaceholder tph : cTPHs) { - for(GenericInsertPair p : tphExtractor.allPairs) { - if(p.contains(tph)) - commonPairs.add(p); - } - } - } @Override @@ -268,14 +248,10 @@ public class BytecodeGen implements ASTVisitor { /* if method has generics or return type is TPH, create signature */ // zwite operand muss weggelassen werden if(hasGen||method.getReturnType().acceptTV(new TypeToString()).equals("TPH")) { - ArrayList pairs = new ArrayList<>(); - for(MethodAndTPH m : ListOfMethodsAndTph) { - if(m.getName().equals(method.name)) { - pairs = m.getPairs(); - break; - } - } + ArrayList pairs = simplifyPairs(method.name,tphExtractor.allPairs); + System.out.println(method.name + " => Simplified Pairs: "); + pairs.forEach(p->System.out.println(p.TA1.getName() + " -> "+p.TA2.getName())); Signature signature = new Signature(method, genericsAndBoundsMethod, genericsAndBounds,methodParamsAndTypes,resultSet, pairs); sig = signature.toString(); } @@ -294,6 +270,70 @@ public class BytecodeGen implements ASTVisitor { mv.visitEnd(); } + private ArrayList simplifyPairs(String methodName, ArrayList allPairs) { + ArrayList simplifiedPairs = new ArrayList<>(); + + MethodAndTPH method; + ArrayList methodTphs = new ArrayList<>(); + ArrayList methodPairs = new ArrayList<>(); + for(MethodAndTPH m : tphExtractor.ListOfMethodsAndTph) { + if(m.getName().equals(methodName)) { + methodTphs = m.getTphs(); + methodPairs = m.getPairs(); + break; + } + } + + HashMap subAndSuperTph = new HashMap<>(); + for(GenericInsertPair p : allPairs) { + subAndSuperTph.put(p.TA1, p.TA2); + } + + int numOfVisitedPairs = 0; + for(TypePlaceholder subTph: subAndSuperTph.keySet()) { + + if(numOfVisitedPairs>=subAndSuperTph.size()) + break; + + if(!methodTphs.contains(subTph)) + continue; + + HashMap tphsInRel= new HashMap<>(); + + tphsInRel.put(tphsInRel.size(), subTph); + TypePlaceholder superTph = subAndSuperTph.get(subTph); + tphsInRel.put(tphsInRel.size(), superTph); + + numOfVisitedPairs++; + + while(subAndSuperTph.containsKey(superTph)) { + superTph = subAndSuperTph.get(superTph); + tphsInRel.put(tphsInRel.size(), superTph); + numOfVisitedPairs++; + } + + // TODO: teste noch den Fall X < Y und Y nicht in TPHS der Methode + // Dann hat man nach der While-Schleife X < Y + // Y muss durch Object ersetzt. + + // Subtype + TypePlaceholder subTphRes = tphsInRel.get(0); + // Die größte Supertype + TypePlaceholder superTphRes = tphsInRel.get(tphsInRel.size()-1); + + int i = 2; + while(!methodTphs.contains(superTphRes) && (tphsInRel.size()-i) >0) { + superTphRes = tphsInRel.get(tphsInRel.size()-i); + i++; + } + + GenericInsertPair sPair = new GenericInsertPair(subTphRes, superTphRes); + simplifiedPairs.add(sPair); + } + + return simplifiedPairs; + } + @Override public void visit(ParameterList formalParameters) { paramsAndLocals = new HashMap<>(); @@ -553,6 +593,7 @@ public class BytecodeGen implements ASTVisitor { methodAndTph.getTphs().add(resolvedTPH); allTPHS.put(resolvedTPH,inMethod); + ResolvedType rst = resultSet.resolveType(tph); resultSet.resolveType(tph).additionalGenerics.forEach(ag ->{ if(ag.contains(resolvedTPH)&&ag.TA1.equals(resolvedTPH)&&!contains(allPairs,ag)) { if(inMethod) diff --git a/src/de/dhbwstuttgart/bytecode/signature/Signature.java b/src/de/dhbwstuttgart/bytecode/signature/Signature.java index 51329a55..2860cc83 100644 --- a/src/de/dhbwstuttgart/bytecode/signature/Signature.java +++ b/src/de/dhbwstuttgart/bytecode/signature/Signature.java @@ -147,16 +147,6 @@ public class Signature { } } -// methodPairs.forEach(p->{ -// String name = p.TA2.getName() + "$"; -// if(!genericsAndBoundsMethod.containsKey(name)) { -// sw.visitFormalTypeParameter(name); -// String bound = Type.getInternalName(Object.class); -// sw.visitClassBound().visitClassType(bound); -// sw.visitClassBound().visitEnd(); -// genericsAndBoundsMethod.put(name, bound); -// } -// }); } // visit each method-parameter to create the signature From 23eab0c9a4af07e9251942b59e19d11e8047508f Mon Sep 17 00:00:00 2001 From: Fayez Abu Alia Date: Tue, 19 Jun 2018 14:56:22 +0200 Subject: [PATCH 2/5] TPHs, die nicht in ParameterListe der Methode vorkommen, werden in Signature geschrieben --- .../dhbwstuttgart/bytecode/BytecodeGen.java | 10 ++++--- .../bytecode/signature/Signature.java | 27 +++++++++++++++++++ 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/src/de/dhbwstuttgart/bytecode/BytecodeGen.java b/src/de/dhbwstuttgart/bytecode/BytecodeGen.java index 0d467b77..69acdefc 100644 --- a/src/de/dhbwstuttgart/bytecode/BytecodeGen.java +++ b/src/de/dhbwstuttgart/bytecode/BytecodeGen.java @@ -283,12 +283,16 @@ public class BytecodeGen implements ASTVisitor { break; } } - + HashMap subAndSuperTph = new HashMap<>(); for(GenericInsertPair p : allPairs) { + // Tph2.jav + if(subAndSuperTph.containsKey(p.TA1)) { + if(methodTphs.contains(subAndSuperTph.get(p.TA1))) + continue; + } subAndSuperTph.put(p.TA1, p.TA2); } - int numOfVisitedPairs = 0; for(TypePlaceholder subTph: subAndSuperTph.keySet()) { @@ -320,7 +324,6 @@ public class BytecodeGen implements ASTVisitor { TypePlaceholder subTphRes = tphsInRel.get(0); // Die größte Supertype TypePlaceholder superTphRes = tphsInRel.get(tphsInRel.size()-1); - int i = 2; while(!methodTphs.contains(superTphRes) && (tphsInRel.size()-i) >0) { superTphRes = tphsInRel.get(tphsInRel.size()-i); @@ -330,7 +333,6 @@ public class BytecodeGen implements ASTVisitor { GenericInsertPair sPair = new GenericInsertPair(subTphRes, superTphRes); simplifiedPairs.add(sPair); } - return simplifiedPairs; } diff --git a/src/de/dhbwstuttgart/bytecode/signature/Signature.java b/src/de/dhbwstuttgart/bytecode/signature/Signature.java index 2860cc83..7b222770 100644 --- a/src/de/dhbwstuttgart/bytecode/signature/Signature.java +++ b/src/de/dhbwstuttgart/bytecode/signature/Signature.java @@ -147,6 +147,33 @@ public class Signature { } } + for(GenericInsertPair p:methodPairs) { + String name = p.TA1.getName()+"$"; + if(!genericsAndBoundsMethod.containsKey(name)) { + sw.visitFormalTypeParameter(name); + sw.visitClassBound().visitTypeVariable(p.TA2.getName()+"$"); + genericsAndBoundsMethod.put(name, p.TA2.getName()+"$"); + } + } + + ArrayList types = new ArrayList<>(); + ArrayList superTypes = new ArrayList<>(); + + for(GenericInsertPair p : methodPairs) { + types.add(p.TA1); + superTypes.add(p.TA2); + } + + for(GenericInsertPair p : methodPairs) { + String name = p.TA2.getName()+"$"; + if(!types.contains(p.TA2) && !genericsAndBoundsMethod.containsKey(name)) { + String bound = Type.getInternalName(Object.class); + sw.visitFormalTypeParameter(name); + sw.visitClassBound().visitClassType(bound); + genericsAndBoundsMethod.put(name, bound); + sw.visitClassBound().visitEnd(); + } + } } // visit each method-parameter to create the signature From 660ef68f7eb63ce55716194cf4d4c5e9510f9e41 Mon Sep 17 00:00:00 2001 From: Fayez Abu Alia Date: Wed, 20 Jun 2018 18:07:17 +0200 Subject: [PATCH 3/5] generateBytecode() um Parameter path erweitert. Alle Tests angepasst --- .../bytecode/BytecodeGenMethod.java | 2 ++ src/de/dhbwstuttgart/core/JavaTXCompiler.java | 11 +++--- test/bytecode/BinaryTest.java | 2 +- test/bytecode/FacTest.java | 2 +- test/bytecode/GenTest.java | 2 +- test/bytecode/GreaterEqualTest.java | 2 +- test/bytecode/GreaterThanTest.java | 2 +- test/bytecode/LambdaTest.java | 2 +- test/bytecode/LessEqualTest.java | 2 +- test/bytecode/LessThanTest.java | 2 +- test/bytecode/MatrixTest.java | 2 +- test/bytecode/OLTest.java | 2 +- test/bytecode/OpTest.java | 2 +- test/bytecode/OverloadingTest.java | 2 +- test/bytecode/PlusTest.java | 3 +- test/bytecode/PostIncTest.java | 2 +- test/bytecode/PreIncTest.java | 2 +- test/bytecode/RelOpsTest.java | 2 +- test/bytecode/StaticTest.java | 2 +- test/bytecode/Tph2Test.java | 2 +- test/bytecode/TphTest.java | 2 +- test/bytecode/WhileTest.java | 2 +- test/logFiles/.log.swp | Bin 0 -> 12288 bytes test/logFiles/log | 34 ++++++++++++++++++ 24 files changed, 62 insertions(+), 26 deletions(-) create mode 100644 test/logFiles/.log.swp create mode 100644 test/logFiles/log diff --git a/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java b/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java index edec62e7..bead57c1 100644 --- a/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java +++ b/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java @@ -636,6 +636,8 @@ public class BytecodeGenMethod implements StatementVisitor { @Override public void visit(MethodCall methodCall) { + //ClassLoader.getSystemClassLoader().loadClass(className).getMethod(name, parameterTypes) + System.out.println("Methodcall type : " + resultSet.resolveType(methodCall.getType()).resolvedType.acceptTV(new TypeToDescriptor())); methodCall.receiver.accept(this); methodCall.arglist.accept(this); diff --git a/src/de/dhbwstuttgart/core/JavaTXCompiler.java b/src/de/dhbwstuttgart/core/JavaTXCompiler.java index 8646b484..bccb88aa 100644 --- a/src/de/dhbwstuttgart/core/JavaTXCompiler.java +++ b/src/de/dhbwstuttgart/core/JavaTXCompiler.java @@ -240,8 +240,8 @@ public class JavaTXCompiler { SourceFile ret = generator.convert(tree, environment.packageCrawler); return ret; } - - public void generateBytecode() throws ClassNotFoundException, IOException { + // um pfad erweitern + public void generateBytecode(String path) throws ClassNotFoundException, IOException { for(File f : sourceFiles.keySet()) { HashMap classFiles = new HashMap<>(); SourceFile sf = sourceFiles.get(f); @@ -249,16 +249,17 @@ public class JavaTXCompiler { BytecodeGen bytecodeGen = new BytecodeGen(classFiles,typeinferenceResult); // BytecodeGen bytecodeGen = new BytecodeGen(classFiles,typeinferenceResult.get(0)); bytecodeGen.visit(sf); - this.writeClassFile(bytecodeGen.getClassFiles()); + this.writeClassFile(bytecodeGen.getClassFiles(), path); } } - private void writeClassFile(HashMap classFiles) throws IOException { + private void writeClassFile(HashMap classFiles, String path) throws IOException { FileOutputStream output; for(String name : classFiles.keySet()) { byte[] bytecode = classFiles.get(name); System.out.println("generating "+name+ ".class file ..."); - output = new FileOutputStream(new File(System.getProperty("user.dir") + "/testBytecode/generatedBC/" +name+".class")); + //output = new FileOutputStream(new File(System.getProperty("user.dir") + "/testBytecode/generatedBC/" +name+".class")); + output = new FileOutputStream(new File(path +name+".class")); output.write(bytecode); output.close(); System.out.println(name+".class file generated"); diff --git a/test/bytecode/BinaryTest.java b/test/bytecode/BinaryTest.java index 7f2f5fa4..07a70d54 100644 --- a/test/bytecode/BinaryTest.java +++ b/test/bytecode/BinaryTest.java @@ -25,8 +25,8 @@ public class BinaryTest { path = System.getProperty("user.dir")+"/test/bytecode/javFiles/BinaryInMeth.jav"; fileToTest = new File(path); compiler = new JavaTXCompiler(fileToTest); - compiler.generateBytecode(); pathToClassFile = System.getProperty("user.dir")+"/testBytecode/generatedBC/"; + compiler.generateBytecode(pathToClassFile); loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)}); classToTest = loader.loadClass("BinaryInMeth"); instanceOfClass = classToTest.getDeclaredConstructor().newInstance(); diff --git a/test/bytecode/FacTest.java b/test/bytecode/FacTest.java index c4cab24e..f5dd86ea 100644 --- a/test/bytecode/FacTest.java +++ b/test/bytecode/FacTest.java @@ -27,8 +27,8 @@ public class FacTest { path = System.getProperty("user.dir")+"/test/bytecode/javFiles/Fac.jav"; fileToTest = new File(path); compiler = new JavaTXCompiler(fileToTest); - compiler.generateBytecode(); pathToClassFile = System.getProperty("user.dir")+"/testBytecode/generatedBC/"; + compiler.generateBytecode(pathToClassFile); loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)}); classToTest = loader.loadClass("Fac"); instanceOfClass = classToTest.getDeclaredConstructor().newInstance(); diff --git a/test/bytecode/GenTest.java b/test/bytecode/GenTest.java index d0067d08..e8ca6938 100644 --- a/test/bytecode/GenTest.java +++ b/test/bytecode/GenTest.java @@ -26,8 +26,8 @@ public class GenTest { path = System.getProperty("user.dir")+"/test/bytecode/javFiles/Gen.jav"; fileToTest = new File(path); compiler = new JavaTXCompiler(fileToTest); - compiler.generateBytecode(); pathToClassFile = System.getProperty("user.dir")+"/testBytecode/generatedBC/"; + compiler.generateBytecode(pathToClassFile); loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)}); classToTest = loader.loadClass("Gen"); instanceOfClass = classToTest.getDeclaredConstructor().newInstance(); diff --git a/test/bytecode/GreaterEqualTest.java b/test/bytecode/GreaterEqualTest.java index c65a4015..b60c446c 100644 --- a/test/bytecode/GreaterEqualTest.java +++ b/test/bytecode/GreaterEqualTest.java @@ -28,8 +28,8 @@ public class GreaterEqualTest { path = System.getProperty("user.dir")+"/test/bytecode/javFiles/GreaterEqual.jav"; fileToTest = new File(path); compiler = new JavaTXCompiler(fileToTest); - compiler.generateBytecode(); pathToClassFile = System.getProperty("user.dir")+"/testBytecode/generatedBC/"; + compiler.generateBytecode(pathToClassFile); loader = new URLClassLoader(new URL[] {new URL("File://"+pathToClassFile)}); classToTest = loader.loadClass("GreaterEqual"); instanceOfClass = classToTest.getDeclaredConstructor().newInstance(); diff --git a/test/bytecode/GreaterThanTest.java b/test/bytecode/GreaterThanTest.java index 8ab07ff1..1a460a8c 100644 --- a/test/bytecode/GreaterThanTest.java +++ b/test/bytecode/GreaterThanTest.java @@ -28,8 +28,8 @@ public class GreaterThanTest { path = System.getProperty("user.dir")+"/test/bytecode/javFiles/GreaterThan.jav"; fileToTest = new File(path); compiler = new JavaTXCompiler(fileToTest); - compiler.generateBytecode(); pathToClassFile = System.getProperty("user.dir")+"/testBytecode/generatedBC/"; + compiler.generateBytecode(pathToClassFile); loader = new URLClassLoader(new URL[] {new URL("File://"+pathToClassFile)}); classToTest = loader.loadClass("GreaterThan"); instanceOfClass = classToTest.getDeclaredConstructor().newInstance(); diff --git a/test/bytecode/LambdaTest.java b/test/bytecode/LambdaTest.java index f29690cb..72d21f54 100644 --- a/test/bytecode/LambdaTest.java +++ b/test/bytecode/LambdaTest.java @@ -16,7 +16,7 @@ public class LambdaTest { path = System.getProperty("user.dir")+"/test/bytecode/javFiles/Lambda.jav"; fileToTest = new File(path); compiler = new JavaTXCompiler(fileToTest); - compiler.generateBytecode(); + compiler.generateBytecode(System.getProperty("user.dir")+"/testBytecode/generatedBC/"); } diff --git a/test/bytecode/LessEqualTest.java b/test/bytecode/LessEqualTest.java index de9cdb8c..dc439af9 100644 --- a/test/bytecode/LessEqualTest.java +++ b/test/bytecode/LessEqualTest.java @@ -28,8 +28,8 @@ public class LessEqualTest { path = System.getProperty("user.dir")+"/test/bytecode/javFiles/LessEqual.jav"; fileToTest = new File(path); compiler = new JavaTXCompiler(fileToTest); - compiler.generateBytecode(); pathToClassFile = System.getProperty("user.dir")+"/testBytecode/generatedBC/"; + compiler.generateBytecode(pathToClassFile); loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)}); classToTest = loader.loadClass("LessEqual"); instanceOfClass = classToTest.getDeclaredConstructor().newInstance(); diff --git a/test/bytecode/LessThanTest.java b/test/bytecode/LessThanTest.java index 6846f820..99eba80c 100644 --- a/test/bytecode/LessThanTest.java +++ b/test/bytecode/LessThanTest.java @@ -28,8 +28,8 @@ public class LessThanTest { path = System.getProperty("user.dir")+"/test/bytecode/javFiles/LessThan.jav"; fileToTest = new File(path); compiler = new JavaTXCompiler(fileToTest); - compiler.generateBytecode(); pathToClassFile = System.getProperty("user.dir")+"/testBytecode/generatedBC/"; + compiler.generateBytecode(pathToClassFile); loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)}); classToTest = loader.loadClass("LessThan"); instanceOfClass = classToTest.getDeclaredConstructor().newInstance(); diff --git a/test/bytecode/MatrixTest.java b/test/bytecode/MatrixTest.java index 98884151..db8dc8ab 100644 --- a/test/bytecode/MatrixTest.java +++ b/test/bytecode/MatrixTest.java @@ -25,8 +25,8 @@ public class MatrixTest { path = System.getProperty("user.dir")+"/test/bytecode/javFiles/Matrix.jav"; fileToTest = new File(path); compiler = new JavaTXCompiler(fileToTest); - compiler.generateBytecode(); pathToClassFile = System.getProperty("user.dir")+"/testBytecode/generatedBC/"; + compiler.generateBytecode(pathToClassFile); loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)}); classToTest = loader.loadClass("Matrix"); instanceOfClass = classToTest.getDeclaredConstructor().newInstance(); diff --git a/test/bytecode/OLTest.java b/test/bytecode/OLTest.java index ff91073e..288e580f 100644 --- a/test/bytecode/OLTest.java +++ b/test/bytecode/OLTest.java @@ -25,8 +25,8 @@ public class OLTest { path = System.getProperty("user.dir")+"/test/bytecode/javFiles/OL.jav"; fileToTest = new File(path); compiler = new JavaTXCompiler(fileToTest); - compiler.generateBytecode(); pathToClassFile = System.getProperty("user.dir")+"/testBytecode/generatedBC/"; + compiler.generateBytecode(pathToClassFile); loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)}); classToTest = loader.loadClass("OL"); instanceOfClass = classToTest.getDeclaredConstructor().newInstance(); diff --git a/test/bytecode/OpTest.java b/test/bytecode/OpTest.java index 1d213a78..3909f0cd 100644 --- a/test/bytecode/OpTest.java +++ b/test/bytecode/OpTest.java @@ -28,8 +28,8 @@ public class OpTest { path = System.getProperty("user.dir")+"/test/bytecode/javFiles/Op.jav"; fileToTest = new File(path); compiler = new JavaTXCompiler(fileToTest); - compiler.generateBytecode(); pathToClassFile = System.getProperty("user.dir")+"/testBytecode/generatedBC/"; + compiler.generateBytecode(pathToClassFile); loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)}); classToTest = loader.loadClass("Op"); instanceOfClass = classToTest.getDeclaredConstructor().newInstance(); diff --git a/test/bytecode/OverloadingTest.java b/test/bytecode/OverloadingTest.java index 294bf533..6b4bf8eb 100644 --- a/test/bytecode/OverloadingTest.java +++ b/test/bytecode/OverloadingTest.java @@ -30,8 +30,8 @@ public class OverloadingTest { path = System.getProperty("user.dir")+"/test/bytecode/javFiles/Overloading.jav"; fileToTest = new File(path); compiler = new JavaTXCompiler(fileToTest); - compiler.generateBytecode(); pathToClassFile = System.getProperty("user.dir")+"/testBytecode/generatedBC/"; + compiler.generateBytecode(pathToClassFile); loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)}); classToTest = loader.loadClass("Overloading"); instanceOfClass = classToTest.getDeclaredConstructor().newInstance(); diff --git a/test/bytecode/PlusTest.java b/test/bytecode/PlusTest.java index 51503c58..85f05b75 100644 --- a/test/bytecode/PlusTest.java +++ b/test/bytecode/PlusTest.java @@ -27,9 +27,8 @@ public class PlusTest { path = System.getProperty("user.dir")+"/test/bytecode/javFiles/Plus.jav"; fileToTest = new File(path); compiler = new JavaTXCompiler(fileToTest); - compiler.generateBytecode(); - pathToClassFile = System.getProperty("user.dir")+"/testBytecode/generatedBC/"; + compiler.generateBytecode(pathToClassFile); loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)}); classToTest = loader.loadClass("Plus"); instanceOfClass = classToTest.getDeclaredConstructor().newInstance(); diff --git a/test/bytecode/PostIncTest.java b/test/bytecode/PostIncTest.java index 36e63816..6c3ebc33 100644 --- a/test/bytecode/PostIncTest.java +++ b/test/bytecode/PostIncTest.java @@ -27,8 +27,8 @@ public class PostIncTest { path = System.getProperty("user.dir")+"/test/bytecode/javFiles/PostIncDec.jav"; fileToTest = new File(path); compiler = new JavaTXCompiler(fileToTest); - compiler.generateBytecode(); pathToClassFile = System.getProperty("user.dir")+"/testBytecode/generatedBC/"; + compiler.generateBytecode(pathToClassFile); loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)}); classToTest = loader.loadClass("PostIncDec"); instanceOfClass = classToTest.getDeclaredConstructor().newInstance(); diff --git a/test/bytecode/PreIncTest.java b/test/bytecode/PreIncTest.java index 434b88e1..29feac33 100644 --- a/test/bytecode/PreIncTest.java +++ b/test/bytecode/PreIncTest.java @@ -27,8 +27,8 @@ public class PreIncTest { path = System.getProperty("user.dir")+"/test/bytecode/javFiles/PreInc.jav"; fileToTest = new File(path); compiler = new JavaTXCompiler(fileToTest); - compiler.generateBytecode(); pathToClassFile = System.getProperty("user.dir")+"/testBytecode/generatedBC/"; + compiler.generateBytecode(pathToClassFile); loader = new URLClassLoader(new URL[] {new URL("file://" + pathToClassFile)}); classToTest = loader.loadClass("PreInc"); instanceOfClass = classToTest.getDeclaredConstructor().newInstance(); diff --git a/test/bytecode/RelOpsTest.java b/test/bytecode/RelOpsTest.java index 06b24630..1bdf1a1d 100644 --- a/test/bytecode/RelOpsTest.java +++ b/test/bytecode/RelOpsTest.java @@ -27,8 +27,8 @@ public class RelOpsTest { path = System.getProperty("user.dir")+"/test/bytecode/javFiles/RelOps.jav"; fileToTest = new File(path); compiler = new JavaTXCompiler(fileToTest); - compiler.generateBytecode(); pathToClassFile = System.getProperty("user.dir")+"/testBytecode/generatedBC/"; + compiler.generateBytecode(pathToClassFile); loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)}); classToTest = loader.loadClass("RelOps"); instanceOfClass = classToTest.getDeclaredConstructor().newInstance(); diff --git a/test/bytecode/StaticTest.java b/test/bytecode/StaticTest.java index f639645c..98642b4e 100644 --- a/test/bytecode/StaticTest.java +++ b/test/bytecode/StaticTest.java @@ -26,8 +26,8 @@ public class StaticTest { path = System.getProperty("user.dir")+"/test/bytecode/javFiles/StaticM.jav"; fileToTest = new File(path); compiler = new JavaTXCompiler(fileToTest); - compiler.generateBytecode(); pathToClassFile = System.getProperty("user.dir")+"/testBytecode/generatedBC/"; + compiler.generateBytecode(pathToClassFile); loader = new URLClassLoader(new URL[] {new URL("file://" + pathToClassFile)}); classToTest = loader.loadClass("StaticM"); instanceOfClass = classToTest.getDeclaredConstructor().newInstance(); diff --git a/test/bytecode/Tph2Test.java b/test/bytecode/Tph2Test.java index 4cda4148..de706431 100644 --- a/test/bytecode/Tph2Test.java +++ b/test/bytecode/Tph2Test.java @@ -26,8 +26,8 @@ public class Tph2Test { path = System.getProperty("user.dir")+"/test/bytecode/javFiles/Tph2.jav"; fileToTest = new File(path); compiler = new JavaTXCompiler(fileToTest); - compiler.generateBytecode(); pathToClassFile = System.getProperty("user.dir")+"/testBytecode/generatedBC/"; + compiler.generateBytecode(pathToClassFile); loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)}); classToTest = loader.loadClass("Tph2"); instanceOfClass = classToTest.getDeclaredConstructor().newInstance(); diff --git a/test/bytecode/TphTest.java b/test/bytecode/TphTest.java index 449db765..92b786ce 100644 --- a/test/bytecode/TphTest.java +++ b/test/bytecode/TphTest.java @@ -25,8 +25,8 @@ public class TphTest { path = System.getProperty("user.dir")+"/test/bytecode/javFiles/Tph.jav"; fileToTest = new File(path); compiler = new JavaTXCompiler(fileToTest); - compiler.generateBytecode(); pathToClassFile = System.getProperty("user.dir")+"/testBytecode/generatedBC/"; + compiler.generateBytecode(pathToClassFile); loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)}); classToTest = loader.loadClass("Tph"); instanceOfClass = classToTest.getDeclaredConstructor().newInstance(); diff --git a/test/bytecode/WhileTest.java b/test/bytecode/WhileTest.java index 5690facb..b3020ffe 100644 --- a/test/bytecode/WhileTest.java +++ b/test/bytecode/WhileTest.java @@ -27,8 +27,8 @@ public class WhileTest { path = System.getProperty("user.dir")+"/test/bytecode/javFiles/While.jav"; fileToTest = new File(path); compiler = new JavaTXCompiler(fileToTest); - compiler.generateBytecode(); pathToClassFile = System.getProperty("user.dir")+"/testBytecode/generatedBC/"; + compiler.generateBytecode(pathToClassFile); loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)}); classToTest = loader.loadClass("While"); instanceOfClass = classToTest.getDeclaredConstructor().newInstance(); diff --git a/test/logFiles/.log.swp b/test/logFiles/.log.swp new file mode 100644 index 0000000000000000000000000000000000000000..4741e023f9dbf0f22cc572b45749ba9d1a316006 GIT binary patch literal 12288 zcmeI2O>fgc5Qe85Y55TRfuV4)QR>9yf&vvP{SeS54M__Utq+c~Y12Bkv0c8ZQi(Gh zLF$!Xz=1PIIQ0Y)oVX(KD|pv9RT>F_dZ<)1mR{}d?9A-s+3_Jtnx%WUD}2=|5uUR| zU!%**U)+(Q;U7e8_o;fO?SFY@wO=w;alM;ixG`3XL(306*B=afo+T_!hmQk4bX!UB zj{C@+3cFFqSMgLBt758>wCIPcv#=z3GMGZvLO=){n!qW#GHz$D`o+-;eE!_hp)*OK z5D)@FKnMr{As_^VfDjM@$B#f74bcwDK9QF`nvX*RW4@F@2nYcoAOwVf5D)@FKnMr{ zAs_^VfDkx_1l$(U-btc|!zdoV|M$NC@0}rf550k2Lpx9#a-bFH0aS%5P!Sr1ex4@! z41Iz=Lhqnm=oR!5dI2TSD%6Hnpl_#$-a@<3CFsKl@}ap7{pttW58d_e?MK_C-e(}@ZL?fntli@K zm@_Avk%|q&G_PTal!j?+5vDz_Ma-FFm2SoX8_hhnk$EFYox9_P&eq`OK;+#6k!~J2 zuOpcaFJD)Af(;{G>m<2h!9vFUgDPfjuKkYFM^)MD9Y4q2?DTnd3#|7lc^RJXCfP0U z7HtzvtHjIhuof#lS;zm~E#D1Rt%c^g@=}Ko9<5Ex_^MmxYS>Z+mgauopnvA=&>yqS QPL)@-27~&u5AK=t3(Y4>{r~^~ literal 0 HcmV?d00001 diff --git a/test/logFiles/log b/test/logFiles/log new file mode 100644 index 00000000..72e1f029 --- /dev/null +++ b/test/logFiles/log @@ -0,0 +1,34 @@ +FC:\{java.lang.Object=Elem: Node(java.lang.Object) +Prec: [java.lang.Object] +Desc: [java.lang.Object, Tph] + +, Tph=Elem: Node(Tph) +Prec: [java.lang.Object] +Desc: [] + +} +class Tph { + + TPH K m(TPH L a, TPH M b)({ + TPH N c; + (c)::TPH N = ((this)::TPH O.m2((b)::TPH M))::TPH P; + return (a)::TPH L; + })::TPH Q + + TPH R m2(TPH S b)({ + return (b)::TPH S; + })::TPH T + + Tph()({ + super(()); + })::TPH W + +}0 AA: [] +1 Unifikation: [(L <. K, 1, -1), (R =. P, -1, -1), (O =. Tph, 0), (S <. R, 1, -1), (O <. Tph, 0), (P <. N, -1, -1), (M <. S, 1, 1)] +nextSet: [[(O =. Tph, 0)], [(O =. ? extends Tph, 0)]] +1 AA: [] +2 Unifikation: [(L <. K, 1, -1), (R =. P, -1, -1), (S <. P, 1, -1), (O =. Tph, 0), (P <. N, -1, -1), (M <. S, 1, 1)] +[(L <. K, 1, -1), (R =. P, -1, -1), (S <. P, 1, -1), (O =. Tph, 0), (P <. N, -1, -1), (M <. S, 1, 1)] +Result1 [[(L <. K, 1, -1), (R =. P, -1, -1), (S <. P, 1, -1), (O =. Tph, 0), (P <. N, -1, -1), (M <. S, 1, 1)]] +Result1 [[(L <. K, 1, -1), (R =. P, -1, -1), (S <. P, 1, -1), (O =. Tph, 0), (P <. N, -1, -1), (M <. S, 1, 1)]] +RES: [[(L <. K, 1, -1), (R =. P, -1, -1), (S <. P, 1, -1), (O =. Tph, 0), (P <. N, -1, -1), (M <. S, 1, 1)]] From 303cfa5409c159faa1a4f28b5f33026f4f7d284d Mon Sep 17 00:00:00 2001 From: Fayez Abu Alia Date: Wed, 20 Jun 2018 18:08:25 +0200 Subject: [PATCH 4/5] log entfernt --- test/logFiles/.log.swp | Bin 12288 -> 0 bytes test/logFiles/log | 34 ---------------------------------- 2 files changed, 34 deletions(-) delete mode 100644 test/logFiles/.log.swp delete mode 100644 test/logFiles/log diff --git a/test/logFiles/.log.swp b/test/logFiles/.log.swp deleted file mode 100644 index 4741e023f9dbf0f22cc572b45749ba9d1a316006..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 12288 zcmeI2O>fgc5Qe85Y55TRfuV4)QR>9yf&vvP{SeS54M__Utq+c~Y12Bkv0c8ZQi(Gh zLF$!Xz=1PIIQ0Y)oVX(KD|pv9RT>F_dZ<)1mR{}d?9A-s+3_Jtnx%WUD}2=|5uUR| zU!%**U)+(Q;U7e8_o;fO?SFY@wO=w;alM;ixG`3XL(306*B=afo+T_!hmQk4bX!UB zj{C@+3cFFqSMgLBt758>wCIPcv#=z3GMGZvLO=){n!qW#GHz$D`o+-;eE!_hp)*OK z5D)@FKnMr{As_^VfDjM@$B#f74bcwDK9QF`nvX*RW4@F@2nYcoAOwVf5D)@FKnMr{ zAs_^VfDkx_1l$(U-btc|!zdoV|M$NC@0}rf550k2Lpx9#a-bFH0aS%5P!Sr1ex4@! z41Iz=Lhqnm=oR!5dI2TSD%6Hnpl_#$-a@<3CFsKl@}ap7{pttW58d_e?MK_C-e(}@ZL?fntli@K zm@_Avk%|q&G_PTal!j?+5vDz_Ma-FFm2SoX8_hhnk$EFYox9_P&eq`OK;+#6k!~J2 zuOpcaFJD)Af(;{G>m<2h!9vFUgDPfjuKkYFM^)MD9Y4q2?DTnd3#|7lc^RJXCfP0U z7HtzvtHjIhuof#lS;zm~E#D1Rt%c^g@=}Ko9<5Ex_^MmxYS>Z+mgauopnvA=&>yqS QPL)@-27~&u5AK=t3(Y4>{r~^~ diff --git a/test/logFiles/log b/test/logFiles/log deleted file mode 100644 index 72e1f029..00000000 --- a/test/logFiles/log +++ /dev/null @@ -1,34 +0,0 @@ -FC:\{java.lang.Object=Elem: Node(java.lang.Object) -Prec: [java.lang.Object] -Desc: [java.lang.Object, Tph] - -, Tph=Elem: Node(Tph) -Prec: [java.lang.Object] -Desc: [] - -} -class Tph { - - TPH K m(TPH L a, TPH M b)({ - TPH N c; - (c)::TPH N = ((this)::TPH O.m2((b)::TPH M))::TPH P; - return (a)::TPH L; - })::TPH Q - - TPH R m2(TPH S b)({ - return (b)::TPH S; - })::TPH T - - Tph()({ - super(()); - })::TPH W - -}0 AA: [] -1 Unifikation: [(L <. K, 1, -1), (R =. P, -1, -1), (O =. Tph, 0), (S <. R, 1, -1), (O <. Tph, 0), (P <. N, -1, -1), (M <. S, 1, 1)] -nextSet: [[(O =. Tph, 0)], [(O =. ? extends Tph, 0)]] -1 AA: [] -2 Unifikation: [(L <. K, 1, -1), (R =. P, -1, -1), (S <. P, 1, -1), (O =. Tph, 0), (P <. N, -1, -1), (M <. S, 1, 1)] -[(L <. K, 1, -1), (R =. P, -1, -1), (S <. P, 1, -1), (O =. Tph, 0), (P <. N, -1, -1), (M <. S, 1, 1)] -Result1 [[(L <. K, 1, -1), (R =. P, -1, -1), (S <. P, 1, -1), (O =. Tph, 0), (P <. N, -1, -1), (M <. S, 1, 1)]] -Result1 [[(L <. K, 1, -1), (R =. P, -1, -1), (S <. P, 1, -1), (O =. Tph, 0), (P <. N, -1, -1), (M <. S, 1, 1)]] -RES: [[(L <. K, 1, -1), (R =. P, -1, -1), (S <. P, 1, -1), (O =. Tph, 0), (P <. N, -1, -1), (M <. S, 1, 1)]] From 11649b39d397cf47f36281c741c54061d20e6d5b Mon Sep 17 00:00:00 2001 From: Fayez Abu Alia Date: Wed, 20 Jun 2018 18:15:44 +0200 Subject: [PATCH 5/5] generateBCForFun() um Parameter path erweitert. --- src/de/dhbwstuttgart/bytecode/BytecodeGen.java | 12 ++++++++---- .../dhbwstuttgart/bytecode/BytecodeGenMethod.java | 14 ++++++++------ src/de/dhbwstuttgart/core/JavaTXCompiler.java | 2 +- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/de/dhbwstuttgart/bytecode/BytecodeGen.java b/src/de/dhbwstuttgart/bytecode/BytecodeGen.java index 69acdefc..0dd57374 100644 --- a/src/de/dhbwstuttgart/bytecode/BytecodeGen.java +++ b/src/de/dhbwstuttgart/bytecode/BytecodeGen.java @@ -44,6 +44,9 @@ public class BytecodeGen implements ASTVisitor { private boolean isInterface; private List listOfResultSets; private ResultSet resultSet; + + private String path; + private int indexOfFirstParam = 0; private String superClass; @@ -64,16 +67,17 @@ public class BytecodeGen implements ASTVisitor { ArrayList methodNameAndParamsT = new ArrayList<>(); - public BytecodeGen(HashMap classFiles, List listOfResultSets) { + public BytecodeGen(HashMap classFiles, List listOfResultSets, String path) { this.classFiles = classFiles; this.listOfResultSets = listOfResultSets; + this.path = path; } @Override public void visit(SourceFile sourceFile) { for(ClassOrInterface cl : sourceFile.getClasses()) { System.out.println("in Class: " + cl.getClassName().toString()); - BytecodeGen classGen = new BytecodeGen(classFiles, listOfResultSets); + BytecodeGen classGen = new BytecodeGen(classFiles, listOfResultSets,path); cl.accept(classGen); classGen.writeClass(cl.getClassName().toString()); } @@ -190,7 +194,7 @@ public class BytecodeGen implements ASTVisitor { MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "", desc, sig, null); mv.visitCode(); BytecodeGenMethod gen = new BytecodeGenMethod(className,superClass,resultSet,field, mv,paramsAndLocals,cw, - genericsAndBoundsMethod,genericsAndBounds,isInterface,classFiles); + genericsAndBoundsMethod,genericsAndBounds,isInterface,classFiles, path); if(!field.getParameterList().iterator().hasNext()) { mv.visitInsn(Opcodes.RETURN); } @@ -264,7 +268,7 @@ public class BytecodeGen implements ASTVisitor { mv.visitCode(); BytecodeGenMethod gen = new BytecodeGenMethod(className,superClass,resultSet,method, mv,paramsAndLocals,cw, - genericsAndBoundsMethod,genericsAndBounds,isInterface,classFiles); + genericsAndBoundsMethod,genericsAndBounds,isInterface,classFiles, path); mv.visitMaxs(0, 0); mv.visitEnd(); diff --git a/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java b/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java index bead57c1..9d03979f 100644 --- a/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java +++ b/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java @@ -57,7 +57,7 @@ public class BytecodeGenMethod implements StatementVisitor { private HashMap genericsAndBounds; private boolean isBinaryExp = false; private String superClass; - + private String path; private IStatement statement = null; // for tests ** @@ -73,7 +73,7 @@ public class BytecodeGenMethod implements StatementVisitor { public BytecodeGenMethod(String className, String superClass,ResultSet resultSet, Method m, MethodVisitor mv, HashMap paramsAndLocals, ClassWriter cw, HashMap genericsAndBoundsMethod, - HashMap genericsAndBounds, boolean isInterface, HashMap classFiles) { + HashMap genericsAndBounds, boolean isInterface, HashMap classFiles, String path) { this.className = className; this.superClass = superClass; @@ -86,19 +86,21 @@ public class BytecodeGenMethod implements StatementVisitor { this.genericsAndBounds = genericsAndBounds; this.isInterface = isInterface; this.classFiles = classFiles; - + this.path = path; + if (!isInterface) this.m.block.accept(this); } public BytecodeGenMethod(LambdaExpression lambdaExpression, ResultSet resultSet, MethodVisitor mv, - int indexOfFirstParamLam, boolean isInterface, HashMap classFiles) { + int indexOfFirstParamLam, boolean isInterface, HashMap classFiles, String path) { this.resultSet = resultSet; this.mv = mv; this.isInterface = isInterface; this.classFiles = classFiles; + this.path = path; Iterator itr = lambdaExpression.params.iterator(); int i = indexOfFirstParamLam; @@ -534,7 +536,7 @@ public class BytecodeGenMethod implements StatementVisitor { methodName, arg3.toString(), null, null); new BytecodeGenMethod(lambdaExpression, this.resultSet, mvLambdaBody, indexOfFirstParamLam, isInterface, - classFiles); + classFiles,this.path); mvLambdaBody.visitMaxs(0, 0); mvLambdaBody.visitEnd(); @@ -575,7 +577,7 @@ public class BytecodeGenMethod implements StatementVisitor { try { System.out.println("generating " + name + ".class file..."); output = new FileOutputStream( - new File(System.getProperty("user.dir") + "/testBytecode/generatedBC/" + name + ".class")); + new File(path + name + ".class")); output.write(bytecode); output.close(); System.out.println(name + ".class file generated"); diff --git a/src/de/dhbwstuttgart/core/JavaTXCompiler.java b/src/de/dhbwstuttgart/core/JavaTXCompiler.java index bccb88aa..c4db69f2 100644 --- a/src/de/dhbwstuttgart/core/JavaTXCompiler.java +++ b/src/de/dhbwstuttgart/core/JavaTXCompiler.java @@ -246,7 +246,7 @@ public class JavaTXCompiler { HashMap classFiles = new HashMap<>(); SourceFile sf = sourceFiles.get(f); List typeinferenceResult = this.typeInference(); - BytecodeGen bytecodeGen = new BytecodeGen(classFiles,typeinferenceResult); + BytecodeGen bytecodeGen = new BytecodeGen(classFiles,typeinferenceResult,path); // BytecodeGen bytecodeGen = new BytecodeGen(classFiles,typeinferenceResult.get(0)); bytecodeGen.visit(sf); this.writeClassFile(bytecodeGen.getClassFiles(), path);