From c82abcaae5448b7d26d76afa32458cb3191c35e7 Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Sun, 15 Dec 2019 14:49:19 +0100 Subject: [PATCH] Generate Package names in .class files --- .../dhbwstuttgart/bytecode/BytecodeGen.java | 4 +-- .../bytecode/BytecodeGenMethod.java | 6 ++-- src/test/java/packages/Bytecode.java | 32 +++++++++++++------ src/test/java/packages/ParsePackageName.java | 2 +- .../typeinference/JavaTXCompilerTest.java | 4 +++ src/test/resources/javFiles/Import.jav | 2 +- .../packageTest/de/test/TestClass.jav | 3 ++ .../packageTest/de/test/packageNameTest.jav | 3 -- vorgehen.md | 7 ++++ 9 files changed, 43 insertions(+), 20 deletions(-) create mode 100644 src/test/resources/javFiles/packageTest/de/test/TestClass.jav delete mode 100644 src/test/resources/javFiles/packageTest/de/test/packageNameTest.jav diff --git a/src/main/java/de/dhbwstuttgart/bytecode/BytecodeGen.java b/src/main/java/de/dhbwstuttgart/bytecode/BytecodeGen.java index e0602731..fff65180 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/BytecodeGen.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/BytecodeGen.java @@ -161,7 +161,7 @@ public class BytecodeGen implements ASTVisitor { className = classOrInterface.getClassName(); - cw.visitSource(className + ".jav", null); + cw.visitSource(className.getClassName() + ".jav", null); isInterface = (classOrInterface.getModifiers() & 512) == 512; @@ -200,7 +200,7 @@ public class BytecodeGen implements ASTVisitor { System.out.println("Signature: => " + sig); } - cw.visit(Opcodes.V1_8, acc, classOrInterface.getClassName().toString(), sig, + cw.visit(Opcodes.V1_8, acc, classOrInterface.getClassName().toString().replace(".", "/"), sig, classOrInterface.getSuperClass().acceptTV(new TypeToDescriptor()), null); isVisited = true; diff --git a/src/main/java/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java b/src/main/java/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java index 9a485561..04f895fe 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java @@ -623,7 +623,7 @@ public class BytecodeGenMethod implements StatementVisitor { } String newDesc = addUsedVarsToDesugaredMethodDescriptor(lamDesc); // first check if capturing lambda then invokestatic or invokespecial - Handle arg2 = new Handle(staticOrSpecial, this.className.toString(), desugaredMethodName, newDesc, false); + Handle arg2 = new Handle(staticOrSpecial, this.className.getClassName(), desugaredMethodName, newDesc, false); // Descriptor of functional interface methode SamMethod samMethod = new SamMethod(kindOfLambda.getArgumentList(), lambdaExpression.getType()); // Desc: (this/nothing)TargetType @@ -798,7 +798,7 @@ public class BytecodeGenMethod implements StatementVisitor { } if(methodRefl == null) { - boolean toCreate = !receiverName.equals(className.toString()) && helper.isInCurrPkg(clazz); + boolean toCreate = !receiverName.equals(className.getClassName()) && helper.isInCurrPkg(clazz); if(toCreate) { try { mDesc = helper.getDesc(clazz); @@ -833,7 +833,7 @@ public class BytecodeGenMethod implements StatementVisitor { System.out.println("Methodcall type : " + resultSet.resolveType(methodCall.getType()).resolvedType.acceptTV(new TypeToDescriptor())); List argListMethCall = new LinkedList<>(); String receiverRefl=""; - if(methodRefl == null && receiverName.equals(className.toString())) { + if(methodRefl == null && receiverName.equals(className.getClassName())) { MethodFromMethodCall method = new MethodFromMethodCall(methodCall.arglist, methodCall.getType(), receiverName, genericsAndBoundsMethod, genericsAndBounds); mDesc = method.accept(new DescriptorToString(resultSet)); diff --git a/src/test/java/packages/Bytecode.java b/src/test/java/packages/Bytecode.java index ece46ac8..7a5c4c25 100644 --- a/src/test/java/packages/Bytecode.java +++ b/src/test/java/packages/Bytecode.java @@ -6,33 +6,45 @@ import org.junit.Test; import java.io.File; import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.net.URL; +import java.net.URLClassLoader; public class Bytecode extends TestCase { - public static final String rootDirectory = System.getProperty("user.dir")+"/src/test/resources/javFiles/packageTest/de/test/"; + public static final String rootDirectory = System.getProperty("user.dir")+"/src/test/resources/javFiles/packageTest/"; @Test - public void testSetPackageNameInBytecode() throws IOException, ClassNotFoundException { - JavaTXCompiler compiler = new JavaTXCompiler(new File(rootDirectory+"packageNameTest.jav")); + public void testSetPackageNameInBytecode() throws Exception { + JavaTXCompiler compiler = new JavaTXCompiler(new File(rootDirectory+"de/test/TestClass.jav")); compiler.typeInference(); - File f = new File(rootDirectory + "TestClass.class"); + File f = new File(rootDirectory + "de/test/TestClass.class"); if(f.exists() && !f.isDirectory()) { f.delete(); } compiler.generateBytecode(null); - f = new File(rootDirectory + "TestClass.class"); + f = new File(rootDirectory + "de/test/TestClass.class"); assertTrue(f.exists()); + + + URLClassLoader loader = new URLClassLoader(new URL[]{new URL("file://" + rootDirectory)}); + Class classToTest = loader.loadClass("de.test.TestClass"); + Object instanceOfClass = classToTest.getDeclaredConstructor().newInstance(); } @Test - public void testSetPackageNameInBytecodeAndOutputFolder() throws IOException, ClassNotFoundException { - JavaTXCompiler compiler = new JavaTXCompiler(new File(rootDirectory+"packageNameTest.jav")); + public void testSetPackageNameInBytecodeAndOutputFolder() throws Exception { + JavaTXCompiler compiler = new JavaTXCompiler(new File(rootDirectory+"de/test/TestClass.jav")); compiler.typeInference(); - File f = new File(rootDirectory + "output/de/test/TestClass.class"); + File f = new File(rootDirectory + "de/test/output/de/test/TestClass.class"); if(f.exists() && !f.isDirectory()) { f.delete(); } - compiler.generateBytecode(rootDirectory + "output/"); - f = new File(rootDirectory + "output/de/test/TestClass.class"); + compiler.generateBytecode(rootDirectory + "de/test/output/"); + f = new File(rootDirectory + "de/test/output/de/test/TestClass.class"); assertTrue(f.exists()); + + URLClassLoader loader = new URLClassLoader(new URL[]{new URL("file://" + rootDirectory + "de/test/output/")}); + Class classToTest = loader.loadClass("de.test.TestClass"); + Object instanceOfClass = classToTest.getDeclaredConstructor().newInstance(); } } diff --git a/src/test/java/packages/ParsePackageName.java b/src/test/java/packages/ParsePackageName.java index b54cf90e..9dc0ea9a 100644 --- a/src/test/java/packages/ParsePackageName.java +++ b/src/test/java/packages/ParsePackageName.java @@ -12,7 +12,7 @@ public class ParsePackageName { 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")); + JavaTXCompiler compiler = new JavaTXCompiler(new File(rootDirectory+"TestClass.jav")); for(File f : compiler.sourceFiles.keySet()){ SourceFile sf = compiler.sourceFiles.get(f); assert sf.getPkgName().equals("de.test"); diff --git a/src/test/java/typeinference/JavaTXCompilerTest.java b/src/test/java/typeinference/JavaTXCompilerTest.java index 87ae477d..ac724771 100644 --- a/src/test/java/typeinference/JavaTXCompilerTest.java +++ b/src/test/java/typeinference/JavaTXCompilerTest.java @@ -28,6 +28,10 @@ public class JavaTXCompilerTest { execute(new File(rootDirectory+"fc.jav")); } @Test + public void importTest() throws IOException, ClassNotFoundException { + execute(new File(rootDirectory+"Import.jav")); + } + @Test public void lambda() throws IOException, ClassNotFoundException { execute(new File(rootDirectory+"Lambda.jav")); } diff --git a/src/test/resources/javFiles/Import.jav b/src/test/resources/javFiles/Import.jav index c48082ba..2fc3a8b3 100644 --- a/src/test/resources/javFiles/Import.jav +++ b/src/test/resources/javFiles/Import.jav @@ -2,7 +2,7 @@ import java.util.Vector; class Import { void methode(){ - Vector v; + var v; v.add(v); } } \ No newline at end of file diff --git a/src/test/resources/javFiles/packageTest/de/test/TestClass.jav b/src/test/resources/javFiles/packageTest/de/test/TestClass.jav new file mode 100644 index 00000000..6681b36a --- /dev/null +++ b/src/test/resources/javFiles/packageTest/de/test/TestClass.jav @@ -0,0 +1,3 @@ +package de.test; + +public 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 deleted file mode 100644 index ad5b9f63..00000000 --- a/src/test/resources/javFiles/packageTest/de/test/packageNameTest.jav +++ /dev/null @@ -1,3 +0,0 @@ -package de.test; - -class TestClass{} \ No newline at end of file diff --git a/vorgehen.md b/vorgehen.md index f6a3f169..834c49e7 100644 --- a/vorgehen.md +++ b/vorgehen.md @@ -34,6 +34,10 @@ * Ohne Pfad, direkt neben die Source File legen * wenn Source File nicht in richtigem Ordner -> Warnung ausgeben +### Stand ### +* Beinahe abgeschlossen +* TODO: Tests schreiben + ## Class files einlesen * Wenn Classpath übergeben * Suchen in Classpath + packageName @@ -41,6 +45,9 @@ * dann currentDirectory + packageName * Für die Tests muss korrekter Classpath gesetzt werden +### Stand ### +TODO + ## Class files mit packageNamen versehen * In die Class file muss noch der korrekte name geschrieben werden * kann möglicherweise ASM