From 09a6aec65d693084f07f49a133c6f0472199a63b Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Sun, 24 Nov 2019 17:52:02 +0100 Subject: [PATCH] =?UTF-8?q?Erste=20Tests=20und=20die=20M=C3=B6glichkeit=20?= =?UTF-8?q?generateBytecode=20null=20als=20Pfad=20zu=20=C3=BCbergeben?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- doc/Generics/generics.tex | 25 +++++++++++++++++++ .../de/dhbwstuttgart/core/JavaTXCompiler.java | 8 ++++-- src/test/java/packages/Bytecode.java | 25 +++++++++++++++++++ .../java/packages/CheckPackageFolder.java | 20 +++++++++++++++ src/test/java/packages/ParsePackageName.java | 17 ++----------- .../resources/javFiles/packageNameTest.jav | 3 --- .../packageTest/de/test/packageNameTest.jav | 3 +++ .../de/test/packageNameTestWrongPackage.jav | 3 +++ vorgehen.md | 19 ++++++++++++++ 9 files changed, 103 insertions(+), 20 deletions(-) create mode 100644 doc/Generics/generics.tex create mode 100644 src/test/java/packages/Bytecode.java create mode 100644 src/test/java/packages/CheckPackageFolder.java delete mode 100644 src/test/resources/javFiles/packageNameTest.jav create mode 100644 src/test/resources/javFiles/packageTest/de/test/packageNameTest.jav create mode 100644 src/test/resources/javFiles/packageTest/de/test/packageNameTestWrongPackage.jav diff --git a/doc/Generics/generics.tex b/doc/Generics/generics.tex new file mode 100644 index 00000000..bacd9f92 --- /dev/null +++ b/doc/Generics/generics.tex @@ -0,0 +1,25 @@ +\documentclass{article} + +\begin{document} + +\section{Generics sind notwendig} +Generics können nicht ignoriert werden. +Folgender Fall ist Typisierbar: + +\begin{program} + T m1(T x){ + return m2(x); +} + +m2(x){ + m1(1); + m2("Test"); + return m1(x); +} +\end{program} + +Beim weglassen des Generics T wäre es aber nicht mehr möglich. +Dann erhält jeder Constraint, welcher in Verbindung mit der Methode m1 steht +den selben TPH. + +\end{document} diff --git a/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java b/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java index d44ac77f..5f107ed0 100644 --- a/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java +++ b/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java @@ -762,7 +762,7 @@ public class JavaTXCompiler { } /** - * @param path + * @param path - can be null, then class file output is in the same directory as the parsed source files * @param typeinferenceResult * @param simplifyResultsForAllSourceFiles * @throws IOException @@ -775,7 +775,11 @@ public class JavaTXCompiler { BytecodeGen bytecodeGen = new BytecodeGen(classFiles, typeinferenceResult, simplifyResultsForAllSourceFiles, sf, path); bytecodeGen.visit(sf); - writeClassFile(bytecodeGen.getClassFiles(), path); + String packagePath = sf.getPkgName().replace(".","/"); + if(path == null){ + path = f.getPath(); + } + writeClassFile(bytecodeGen.getClassFiles(), path + packagePath); } } diff --git a/src/test/java/packages/Bytecode.java b/src/test/java/packages/Bytecode.java new file mode 100644 index 00000000..f02c83fa --- /dev/null +++ b/src/test/java/packages/Bytecode.java @@ -0,0 +1,25 @@ +package packages; + +import de.dhbwstuttgart.core.JavaTXCompiler; +import junit.framework.TestCase; +import org.junit.Test; + +import java.io.File; +import java.io.IOException; + +public class Bytecode extends TestCase { + + public static final String rootDirectory = System.getProperty("user.dir")+"/src/test/resources/javFiles/packageTest/de/test/"; + @Test + public void testSetPackageNameInBytecode() throws IOException, ClassNotFoundException { + JavaTXCompiler compiler = new JavaTXCompiler(new File(rootDirectory+"packageNameTest.jav")); + compiler.typeInference(); + File f = new File(rootDirectory + "Test.class"); + if(f.exists() && !f.isDirectory()) { + f.delete(); + } + compiler.generateBytecode(null); + f = new File(rootDirectory + "Test.class"); + assertTrue(f.exists()); + } +} diff --git a/src/test/java/packages/CheckPackageFolder.java b/src/test/java/packages/CheckPackageFolder.java new file mode 100644 index 00000000..9644d5da --- /dev/null +++ b/src/test/java/packages/CheckPackageFolder.java @@ -0,0 +1,20 @@ +package packages; + +import de.dhbwstuttgart.core.JavaTXCompiler; +import de.dhbwstuttgart.syntaxtree.SourceFile; +import junit.framework.TestCase; +import org.junit.Test; + +import java.io.File; +import java.io.IOException; + +public class CheckPackageFolder extends TestCase { + + public static final String rootDirectory = System.getProperty("user.dir")+"/src/test/resources/javFiles/packageTest/de/test/"; + + @Test + public void testCorrectFolder1File() throws IOException, ClassNotFoundException { + JavaTXCompiler compiler = new JavaTXCompiler(new File(rootDirectory+"packageNameTestWrongPackage.jav")); + assertTrue(true); //Es ist erlaubt falsche package Namen zu verwenden. Warnung wäre optional + } +} diff --git a/src/test/java/packages/ParsePackageName.java b/src/test/java/packages/ParsePackageName.java index e2fe1682..b54cf90e 100644 --- a/src/test/java/packages/ParsePackageName.java +++ b/src/test/java/packages/ParsePackageName.java @@ -2,33 +2,20 @@ package packages; import de.dhbwstuttgart.core.JavaTXCompiler; import de.dhbwstuttgart.syntaxtree.SourceFile; -import de.dhbwstuttgart.syntaxtree.visual.ASTPrinter; -import de.dhbwstuttgart.syntaxtree.visual.ASTTypePrinter; -import de.dhbwstuttgart.typedeployment.TypeInsert; -import de.dhbwstuttgart.typedeployment.TypeInsertFactory; -import de.dhbwstuttgart.typeinference.result.ResultSet; import org.junit.Test; -import typeinference.JavaTXCompilerTest; import java.io.File; import java.io.IOException; -import java.nio.charset.StandardCharsets; -import java.util.HashSet; -import java.util.List; -import java.util.Set; public class ParsePackageName { - public static final String rootDirectory = System.getProperty("user.dir")+"/src/test/resources/javFiles/"; + public static final String rootDirectory = System.getProperty("user.dir")+"/src/test/resources/javFiles/packageTest/de/test/"; @Test public void parsePackage() throws IOException, ClassNotFoundException { JavaTXCompiler compiler = new JavaTXCompiler(new File(rootDirectory+"packageNameTest.jav")); for(File f : compiler.sourceFiles.keySet()){ SourceFile sf = compiler.sourceFiles.get(f); - System.out.println(ASTTypePrinter.print(sf)); - System.out.println(ASTPrinter.print(sf)); + assert sf.getPkgName().equals("de.test"); } - List results = compiler.typeInference(); - } } diff --git a/src/test/resources/javFiles/packageNameTest.jav b/src/test/resources/javFiles/packageNameTest.jav deleted file mode 100644 index b55d1071..00000000 --- a/src/test/resources/javFiles/packageNameTest.jav +++ /dev/null @@ -1,3 +0,0 @@ -package Test; - -class TestClass{} \ No newline at end of file diff --git a/src/test/resources/javFiles/packageTest/de/test/packageNameTest.jav b/src/test/resources/javFiles/packageTest/de/test/packageNameTest.jav new file mode 100644 index 00000000..ad5b9f63 --- /dev/null +++ b/src/test/resources/javFiles/packageTest/de/test/packageNameTest.jav @@ -0,0 +1,3 @@ +package de.test; + +class TestClass{} \ No newline at end of file diff --git a/src/test/resources/javFiles/packageTest/de/test/packageNameTestWrongPackage.jav b/src/test/resources/javFiles/packageTest/de/test/packageNameTestWrongPackage.jav new file mode 100644 index 00000000..a86d22aa --- /dev/null +++ b/src/test/resources/javFiles/packageTest/de/test/packageNameTestWrongPackage.jav @@ -0,0 +1,3 @@ +package de.tesst; + +class TestClass{} \ No newline at end of file diff --git a/vorgehen.md b/vorgehen.md index d3d17653..61b62c4f 100644 --- a/vorgehen.md +++ b/vorgehen.md @@ -9,3 +9,22 @@ * macht es hier einen Unterschied? * es ändert sich nur der Namen der Typen * das ist schon in JavaClassName enthalten + +## Java verhalten +* das verhalten von Java soll kopiert werden +* Java kompiliert in den gleichen Ordner wie die source file +* Java erhält eine liste von Source Files, die es kompilieren soll +* anhand von deren Standort lassen sich relativ die Position von kompilierten Class-files bestimmen + +* -cp path or -classpath path + Specifies where to find user class files, and (optionally) annotation processors and source files. This class path overrides the user class path in the CLASSPATH environment variable. If neither CLASSPATH, -cp nor -classpath is specified, then the + user class path is the current directory. See Setting the Class Path. + +* Compiler kontrolliert nicht, ob package im korrekten Ordner ist + * auch keine Warnung + * unser kompiler könnte aber trotzdem zumindes eine Warnung ausgeben + +### -d Parameter +* Optional +* damit lässt sich ein andere ort zur Ausgabe der Class-files bestimmen +