Generate Package names in .class files

This commit is contained in:
JanUlrich 2019-12-15 14:49:19 +01:00
parent a3b4ea9b1c
commit c82abcaae5
9 changed files with 43 additions and 20 deletions

View File

@ -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;

View File

@ -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<Boolean> 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));

View File

@ -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();
}
}

View File

@ -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");

View File

@ -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"));
}

View File

@ -2,7 +2,7 @@ import java.util.Vector;
class Import {
void methode(){
Vector v;
var v;
v.add(v);
}
}

View File

@ -0,0 +1,3 @@
package de.test;
public class TestClass{}

View File

@ -1,3 +0,0 @@
package de.test;
class TestClass{}

View File

@ -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