Bytecode für arithmetische Operatoren mit Parametern unterschiedlicher Typen. Testfälle bereinigt und aufgeräumt.
This commit is contained in:
parent
35b99a4095
commit
f453343f1c
@ -104,7 +104,6 @@ public class BytecodeGenMethod implements StatementVisitor{
|
||||
return resultSet.resolveType(type).resolvedType.acceptTV(new TypeToDescriptor());
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void visit(Block block) {
|
||||
for (Statement stmt : block.getStatements()) {
|
||||
@ -116,8 +115,8 @@ public class BytecodeGenMethod implements StatementVisitor{
|
||||
public void visit(SuperCall superCall) {
|
||||
superCall.receiver.accept(this);
|
||||
superCall.arglist.accept(this);
|
||||
mv.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(Object.class),
|
||||
superCall.name, "()V",isInterface);
|
||||
mv.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(Object.class), superCall.name, "()V",
|
||||
isInterface);
|
||||
}
|
||||
|
||||
// ??
|
||||
@ -132,6 +131,7 @@ public class BytecodeGenMethod implements StatementVisitor{
|
||||
getVlaueIns(getResolvedType(localVar.getType()));
|
||||
}
|
||||
}
|
||||
|
||||
// ??
|
||||
@Override
|
||||
public void visit(LocalVarDecl localVarDecl) {
|
||||
@ -162,20 +162,23 @@ public class BytecodeGenMethod implements StatementVisitor{
|
||||
}
|
||||
assign.lefSide.accept(this);
|
||||
}
|
||||
|
||||
/*
|
||||
* Die folgeneden Fälle müssen noch betrachtet werden:
|
||||
* - Long OPARATION Integer usw.
|
||||
* */
|
||||
* Die folgeneden Fälle müssen noch betrachtet werden: - Long OPARATION Integer
|
||||
* usw.
|
||||
*/
|
||||
@Override
|
||||
public void visit(BinaryExpr binary) {
|
||||
String typeOfBinary = getResolvedType(binary.getType());
|
||||
if (typeOfBinary.equals(Type.getInternalName(String.class))) {
|
||||
mv.visitTypeInsn(Opcodes.NEW, Type.getInternalName(StringBuilder.class));
|
||||
mv.visitInsn(Opcodes.DUP);
|
||||
mv.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(StringBuilder.class),
|
||||
"<init>", "()V", false);
|
||||
mv.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(StringBuilder.class), "<init>", "()V",
|
||||
false);
|
||||
}
|
||||
binary.lexpr.accept(this);
|
||||
if(!getResolvedType(binary.lexpr.getType()).equals(typeOfBinary))
|
||||
doCast(getResolvedType(binary.lexpr.getType()), typeOfBinary);
|
||||
binary.rexpr.accept(this);
|
||||
switch (binary.operation.toString()) {
|
||||
case "ADD":
|
||||
@ -207,6 +210,28 @@ public class BytecodeGenMethod implements StatementVisitor{
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void doCast(String typeOfExp, String typeOfBinary) {
|
||||
switch (typeOfBinary) {
|
||||
case "java/lang/Long":
|
||||
mv.visitInsn(Opcodes.I2L);
|
||||
break;
|
||||
|
||||
case "java/lang/Double":
|
||||
if(typeOfExp.equals(Type.getInternalName(Long.class))) {
|
||||
mv.visitInsn(Opcodes.L2D);
|
||||
} else if(typeOfExp.equals(Type.getInternalName(Float.class))) {
|
||||
mv.visitInsn(Opcodes.F2D);
|
||||
} else {
|
||||
mv.visitInsn(Opcodes.I2D);
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// TODO
|
||||
private void doVisitLessOpInsn(String typeOfBinary) {
|
||||
switch (typeOfBinary) {
|
||||
@ -332,17 +357,18 @@ public class BytecodeGenMethod implements StatementVisitor{
|
||||
|
||||
Lambda lam = new Lambda(lambdaExpression);
|
||||
String lamDesc = lam.accept(new DescriptorToString(resultSet));
|
||||
//Call site, which, when invoked, returns an instance of the functional interface to which
|
||||
// Call site, which, when invoked, returns an instance of the functional
|
||||
// interface to which
|
||||
// the lambda is being converted
|
||||
MethodType mt = MethodType.methodType(CallSite.class, MethodHandles.Lookup.class, String.class,
|
||||
MethodType.class, MethodType.class, MethodHandle.class, MethodType.class);
|
||||
|
||||
|
||||
Handle bootstrap = new Handle(Opcodes.H_INVOKESTATIC, "java/lang/invoke/LambdaMetafactory",
|
||||
"metafactory", mt.toMethodDescriptorString(), false);
|
||||
Handle bootstrap = new Handle(Opcodes.H_INVOKESTATIC, "java/lang/invoke/LambdaMetafactory", "metafactory",
|
||||
mt.toMethodDescriptorString(), false);
|
||||
String methodName = "lambda$new$" + this.lamCounter;
|
||||
|
||||
// Für die Parameter-Typen und Return-Typ braucht man die Bounds (für die Typlöschung)
|
||||
// Für die Parameter-Typen und Return-Typ braucht man die Bounds (für die
|
||||
// Typlöschung)
|
||||
|
||||
String typeErasure = "(";
|
||||
Iterator<FormalParameter> itr = lambdaExpression.params.iterator();
|
||||
@ -373,8 +399,7 @@ public class BytecodeGenMethod implements StatementVisitor{
|
||||
}
|
||||
|
||||
// first check if capturing lambda then invokestatic or invokespecial
|
||||
Handle arg2 = new Handle(staticOrSpecial, this.className, methodName,
|
||||
arg3.toString(),false);
|
||||
Handle arg2 = new Handle(staticOrSpecial, this.className, methodName, arg3.toString(), false);
|
||||
// Descriptor of functional interface methode
|
||||
SamMethod samMethod = new SamMethod(kindOfLambda.getArgumentList(), lambdaExpression.getType());
|
||||
// Desc: (this/nothing)TargetType
|
||||
@ -413,10 +438,10 @@ public class BytecodeGenMethod implements StatementVisitor{
|
||||
// ")"+lam.getReturn.getBounds
|
||||
Signature sig = new Signature(lambdaExpression, numberOfParams);
|
||||
String name = "Fun" + numberOfParams;
|
||||
classWriter.visit(Opcodes.V1_8, Opcodes.ACC_INTERFACE+Opcodes.ACC_ABSTRACT, name,
|
||||
sig.toString(), Type.getInternalName(Object.class), null);
|
||||
MethodVisitor mvApply = classWriter.visitMethod(Opcodes.ACC_PUBLIC+Opcodes.ACC_ABSTRACT, "apply",
|
||||
methDesc, methSig.toString(), null);
|
||||
classWriter.visit(Opcodes.V1_8, Opcodes.ACC_INTERFACE + Opcodes.ACC_ABSTRACT, name, sig.toString(),
|
||||
Type.getInternalName(Object.class), null);
|
||||
MethodVisitor mvApply = classWriter.visitMethod(Opcodes.ACC_PUBLIC + Opcodes.ACC_ABSTRACT, "apply", methDesc,
|
||||
methSig.toString(), null);
|
||||
mvApply.visitEnd();
|
||||
writeClassFile(classWriter.toByteArray(), name);
|
||||
}
|
||||
@ -425,7 +450,8 @@ public class BytecodeGenMethod implements StatementVisitor{
|
||||
FileOutputStream output;
|
||||
try {
|
||||
System.out.println("generating " + name + ".class file...");
|
||||
output = new FileOutputStream(new File(System.getProperty("user.dir") + "/testBytecode/generatedBC/examples/" +name+".class"));
|
||||
output = new FileOutputStream(
|
||||
new File(System.getProperty("user.dir") + "/testBytecode/generatedBC/examples/" + name + ".class"));
|
||||
output.write(bytecode);
|
||||
output.close();
|
||||
System.out.println(name + ".class file generated");
|
||||
@ -458,11 +484,11 @@ public class BytecodeGenMethod implements StatementVisitor{
|
||||
fieldVar.receiver.accept(this);
|
||||
// test (if)
|
||||
if (!fieldVar.receiver.getClass().equals(StaticClassName.class)) {
|
||||
mv.visitFieldInsn(Opcodes.GETFIELD,getResolvedType(fieldVar.receiver.getType()),
|
||||
fieldName ,fieldDesc);
|
||||
mv.visitFieldInsn(Opcodes.GETFIELD, getResolvedType(fieldVar.receiver.getType()), fieldName, fieldDesc);
|
||||
}
|
||||
|
||||
// mv.visitFieldInsn(Opcodes.GETSTATIC, fieldVar.receiver.getType().toString().replace(".", "/"),
|
||||
// mv.visitFieldInsn(Opcodes.GETSTATIC,
|
||||
// fieldVar.receiver.getType().toString().replace(".", "/"),
|
||||
// fieldVar.fieldVarName, fieldVar.getType().toString());
|
||||
}
|
||||
|
||||
@ -494,11 +520,11 @@ public class BytecodeGenMethod implements StatementVisitor{
|
||||
|
||||
// is methodCall.receiver functional Interface)?
|
||||
if (varsFunInterface.contains(methodCall.receiver.getType())) {
|
||||
mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, getResolvedType(methodCall.receiver.getType()),
|
||||
methodCall.name, mDesc, false);
|
||||
mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, getResolvedType(methodCall.receiver.getType()), methodCall.name,
|
||||
mDesc, false);
|
||||
} else {
|
||||
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, getResolvedType(methodCall.receiver.getType()),
|
||||
methodCall.name, mDesc, isInterface);
|
||||
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, getResolvedType(methodCall.receiver.getType()), methodCall.name,
|
||||
mDesc, isInterface);
|
||||
}
|
||||
// test
|
||||
// if(!methodCall.getType().toString().equals("V")) {
|
||||
@ -551,10 +577,10 @@ public class BytecodeGenMethod implements StatementVisitor{
|
||||
|
||||
@Override
|
||||
public void visit(StaticClassName staticClassName) {
|
||||
// mv.visitMethodInsn(Opcodes.INVOKESTATIC, staticClassName.getType().toString().replace(".", "/"),
|
||||
// mv.visitMethodInsn(Opcodes.INVOKESTATIC,
|
||||
// staticClassName.getType().toString().replace(".", "/"),
|
||||
// staticClassName.toString(), staticClassName.getType().toString(), false);
|
||||
mv.visitFieldInsn(Opcodes.GETSTATIC, getResolvedType(staticClassName.getType()),
|
||||
fieldName, fieldDesc);
|
||||
mv.visitFieldInsn(Opcodes.GETSTATIC, getResolvedType(staticClassName.getType()), fieldName, fieldDesc);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -582,8 +608,7 @@ public class BytecodeGenMethod implements StatementVisitor{
|
||||
@Override
|
||||
public void visit(Literal literal) {
|
||||
Object value = literal.value;
|
||||
String typeOfLiteral = resultSet.resolveType(
|
||||
literal.getType()).resolvedType.acceptTV(new TypeToDescriptor());
|
||||
String typeOfLiteral = resultSet.resolveType(literal.getType()).resolvedType.acceptTV(new TypeToDescriptor());
|
||||
|
||||
doAssign(typeOfLiteral, value);
|
||||
|
||||
@ -592,35 +617,29 @@ public class BytecodeGenMethod implements StatementVisitor{
|
||||
private void getVlaueIns(String type) {
|
||||
switch (type) {
|
||||
case "java/lang/String":
|
||||
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(StringBuilder.class),
|
||||
"append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false);
|
||||
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(StringBuilder.class), "append",
|
||||
"(Ljava/lang/String;)Ljava/lang/StringBuilder;", false);
|
||||
|
||||
break;
|
||||
case "java/lang/Boolean":
|
||||
break;
|
||||
case "java/lang/Byte":
|
||||
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Byte", "byteValue",
|
||||
"()B", false);
|
||||
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Byte", "byteValue", "()B", false);
|
||||
break;
|
||||
case "java/lang/Short":
|
||||
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Short", "shortValue",
|
||||
"()S", false);
|
||||
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Short", "shortValue", "()S", false);
|
||||
break;
|
||||
case "java/lang/Integer":
|
||||
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Integer", "intValue",
|
||||
"()I", false);
|
||||
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Integer", "intValue", "()I", false);
|
||||
break;
|
||||
case "java/lang/Long":
|
||||
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Long", "longValue",
|
||||
"()J", false);
|
||||
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Long", "longValue", "()J", false);
|
||||
break;
|
||||
case "java/lang/Float":
|
||||
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Float", "floatValue",
|
||||
"()F", false);
|
||||
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Float", "floatValue", "()F", false);
|
||||
break;
|
||||
case "java/lang/Double":
|
||||
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Double", "doubleValue",
|
||||
"()D", false);
|
||||
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Double", "doubleValue", "()D", false);
|
||||
break;
|
||||
case "java/lang/Character":
|
||||
break;
|
||||
@ -675,40 +694,33 @@ public class BytecodeGenMethod implements StatementVisitor{
|
||||
private void getValueOfIns(String type) {
|
||||
switch (type) {
|
||||
case "java/lang/String":
|
||||
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder", "toString",
|
||||
"()Ljava/lang/String;", false);
|
||||
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder", "toString", "()Ljava/lang/String;",
|
||||
false);
|
||||
break;
|
||||
case "java/lang/Boolean":
|
||||
mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Boolean", "valueOf",
|
||||
"(Z)Ljava/lang/Boolean;", false);
|
||||
mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Boolean", "valueOf", "(Z)Ljava/lang/Boolean;", false);
|
||||
break;
|
||||
case "java/lang/Byte":
|
||||
mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Byte", "valueOf",
|
||||
"(B)Ljava/lang/Byte;", false);
|
||||
mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Byte", "valueOf", "(B)Ljava/lang/Byte;", false);
|
||||
break;
|
||||
case "java/lang/Short":
|
||||
mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Short", "valueOf",
|
||||
"(S)Ljava/lang/Short;", false);
|
||||
mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Short", "valueOf", "(S)Ljava/lang/Short;", false);
|
||||
break;
|
||||
case "java/lang/Integer":
|
||||
mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Integer", "valueOf",
|
||||
"(I)Ljava/lang/Integer;", false);
|
||||
mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Integer", "valueOf", "(I)Ljava/lang/Integer;", false);
|
||||
break;
|
||||
case "java/lang/Long":
|
||||
mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Long", "valueOf",
|
||||
"(J)Ljava/lang/Long;", false);
|
||||
mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Long", "valueOf", "(J)Ljava/lang/Long;", false);
|
||||
break;
|
||||
case "java/lang/Float":
|
||||
mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Float", "valueOf",
|
||||
"(F)Ljava/lang/Float;", false);
|
||||
mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Float", "valueOf", "(F)Ljava/lang/Float;", false);
|
||||
break;
|
||||
case "java/lang/Double":
|
||||
mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Double", "valueOf",
|
||||
"(D)Ljava/lang/Double;", false);
|
||||
mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Double", "valueOf", "(D)Ljava/lang/Double;", false);
|
||||
break;
|
||||
case "java/lang/Character":
|
||||
mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Character", "valueOf",
|
||||
"(C)Ljava/lang/Character;", false);
|
||||
mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Character", "valueOf", "(C)Ljava/lang/Character;",
|
||||
false);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
@ -790,8 +802,7 @@ public class BytecodeGenMethod implements StatementVisitor{
|
||||
mv.visitInsn(Opcodes.ICONST_0);
|
||||
}
|
||||
// muss noch getestet werden.
|
||||
mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Boolean", "valueOf",
|
||||
"(Z)Ljava/lang/Boolean;", false);
|
||||
mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Boolean", "valueOf", "(Z)Ljava/lang/Boolean;", false);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -1,6 +1,7 @@
|
||||
package de.dhbwstuttgart.core;
|
||||
|
||||
|
||||
import de.dhbwstuttgart.bytecode.BytecodeGen;
|
||||
import de.dhbwstuttgart.environment.CompilationEnvironment;
|
||||
import de.dhbwstuttgart.parser.JavaTXParser;
|
||||
import de.dhbwstuttgart.parser.scope.GenericsRegistry;
|
||||
@ -23,6 +24,8 @@ import de.dhbwstuttgart.typeinference.unify.model.FiniteClosure;
|
||||
import de.dhbwstuttgart.typeinference.unify.model.UnifyPair;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
@ -103,7 +106,7 @@ public class JavaTXCompiler {
|
||||
System.out.println(xConsSet);
|
||||
Set<Set<UnifyPair>> result = unify.unify(xConsSet, finiteClosure);
|
||||
System.out.println("RESULT: " + result.size());
|
||||
//results.addAll(result);
|
||||
results.addAll(result);
|
||||
}
|
||||
return results.stream().map((unifyPairs ->
|
||||
new ResultSet(UnifyTypeFactory.convert(unifyPairs, generateTPHMap(cons))))).collect(Collectors.toList());
|
||||
@ -130,4 +133,26 @@ public class JavaTXCompiler {
|
||||
return ret;
|
||||
}
|
||||
|
||||
public void generateBytecode() throws ClassNotFoundException, IOException {
|
||||
for(File f : sourceFiles.keySet()) {
|
||||
HashMap<String,byte[]> classFiles = new HashMap<>();
|
||||
SourceFile sf = sourceFiles.get(f);
|
||||
List<ResultSet> typeinferenceResult = this.typeInference();
|
||||
BytecodeGen bytecodeGen = new BytecodeGen(classFiles,typeinferenceResult.get(0));
|
||||
bytecodeGen.visit(sf);
|
||||
this.writeClassFile(bytecodeGen.getClassFiles());
|
||||
}
|
||||
}
|
||||
|
||||
private void writeClassFile(HashMap<String, byte[]> classFiles) 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.write(bytecode);
|
||||
output.close();
|
||||
System.out.println(name+".class file generated");
|
||||
}
|
||||
}
|
||||
}
|
@ -1,8 +0,0 @@
|
||||
package bytecode;
|
||||
|
||||
public class ATest extends JavaTXCompilerTest {
|
||||
public ATest() {
|
||||
fileName = "Example";
|
||||
}
|
||||
|
||||
}
|
@ -1,7 +0,0 @@
|
||||
package bytecode;
|
||||
|
||||
public class AssignToLitTest extends JavaTXCompilerTest {
|
||||
public AssignToLitTest() {
|
||||
this.fileName = "AssignToLit";
|
||||
}
|
||||
}
|
@ -1,7 +0,0 @@
|
||||
package bytecode;
|
||||
|
||||
public class DuMethodTest extends JavaTXCompilerTest{
|
||||
public DuMethodTest() {
|
||||
this.fileName = "DuMethod";
|
||||
}
|
||||
}
|
@ -1,11 +0,0 @@
|
||||
package bytecode;
|
||||
|
||||
import org.objectweb.asm.Opcodes;
|
||||
|
||||
public class ForTest extends JavaTXCompilerTest {
|
||||
|
||||
public ForTest() {
|
||||
this.fileName = "For";
|
||||
}
|
||||
|
||||
}
|
@ -1,7 +0,0 @@
|
||||
package bytecode;
|
||||
|
||||
public class Generics2Test extends JavaTXCompilerTest{
|
||||
public Generics2Test() {
|
||||
this.fileName = "Generics2";
|
||||
}
|
||||
}
|
@ -1,7 +0,0 @@
|
||||
package bytecode;
|
||||
|
||||
public class GenericsTest extends JavaTXCompilerTest {
|
||||
public GenericsTest() {
|
||||
this.fileName = "Generics";
|
||||
}
|
||||
}
|
@ -1,7 +0,0 @@
|
||||
package bytecode;
|
||||
|
||||
public class ImportTest extends JavaTXCompilerTest{
|
||||
public ImportTest() {
|
||||
this.fileName = "Import";
|
||||
}
|
||||
}
|
@ -1,7 +0,0 @@
|
||||
package bytecode;
|
||||
|
||||
public class InterfaceTest extends JavaTXCompilerTest{
|
||||
public InterfaceTest() {
|
||||
this.fileName = "Interface1";
|
||||
}
|
||||
}
|
@ -1,90 +0,0 @@
|
||||
package bytecode;
|
||||
|
||||
import de.dhbwstuttgart.bytecode.BytecodeGen;
|
||||
import de.dhbwstuttgart.core.JavaTXCompiler;
|
||||
import de.dhbwstuttgart.syntaxtree.SourceFile;
|
||||
import de.dhbwstuttgart.typeinference.result.ResultPair;
|
||||
import de.dhbwstuttgart.typeinference.result.ResultSet;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class JavaTXCompilerTest {
|
||||
|
||||
private static final String rootDirectory = System.getProperty("user.dir")+"/test/bytecode/";
|
||||
private static final List<File> filesToTest = new ArrayList<>();
|
||||
|
||||
protected String fileName = "";
|
||||
|
||||
@Test
|
||||
public void test() throws IOException, java.lang.ClassNotFoundException {
|
||||
System.out.println(rootDirectory);
|
||||
filesToTest.add(new File(rootDirectory+fileName+".jav"));
|
||||
System.out.println(rootDirectory+fileName+".jav");
|
||||
JavaTXCompiler compiler = new JavaTXCompiler(filesToTest);
|
||||
for(File f : filesToTest){
|
||||
String content = readFile(f.getPath(), StandardCharsets.UTF_8);
|
||||
List<ResultSet> typeinferenceResult = compiler.typeInference();
|
||||
HashMap<String,byte[]> bytecode = this.getBytecode(compiler.sourceFiles.get(f), typeinferenceResult.get(0));
|
||||
|
||||
// for(ResultPair ep : typeinferenceResult.get(0).results) {
|
||||
// System.out.println(ep.getLeft() + " ->" + ep.getRight());
|
||||
// }
|
||||
|
||||
String name;
|
||||
int pos = f.getName().lastIndexOf(".");
|
||||
if(pos != -1) {
|
||||
name = f.getName().substring(0, pos);
|
||||
}
|
||||
this.writeClassFile(bytecode);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
public HashMap<String,byte[]> getBytecode(SourceFile sf, ResultSet resultSet) {
|
||||
HashMap<String,byte[]> classFiles = new HashMap<>();
|
||||
BytecodeGen bytecodeGen = new BytecodeGen(classFiles,resultSet);
|
||||
bytecodeGen.visit(sf);
|
||||
return bytecodeGen.getClassFiles();
|
||||
}
|
||||
|
||||
public void writeClassFile(HashMap<String,byte[]> classFiles) {
|
||||
FileOutputStream output;
|
||||
for(String name : classFiles.keySet()) {
|
||||
byte[] bytecode = classFiles.get(name);
|
||||
try {
|
||||
System.out.println("generating "+name+ ".class file ...");
|
||||
output = new FileOutputStream(new File(System.getProperty("user.dir") + "/testBytecode/generatedBC/" +name+".class"));
|
||||
output.write(bytecode);
|
||||
output.close();
|
||||
System.out.println(name+".class file generated");
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static String readFile(String path, Charset encoding)
|
||||
throws IOException
|
||||
{
|
||||
byte[] encoded = Files.readAllBytes(Paths.get(path));
|
||||
return new String(encoded, encoding);
|
||||
}
|
||||
|
||||
}
|
@ -1,7 +0,0 @@
|
||||
package bytecode;
|
||||
|
||||
public class LamAssignTest extends JavaTXCompilerTest{
|
||||
public LamAssignTest() {
|
||||
this.fileName = "LamAssign";
|
||||
}
|
||||
}
|
@ -1,7 +0,0 @@
|
||||
package bytecode;
|
||||
|
||||
public class MethodsTest extends JavaTXCompilerTest {
|
||||
public MethodsTest() {
|
||||
this.fileName = "Methods";
|
||||
}
|
||||
}
|
@ -1,46 +0,0 @@
|
||||
import java.lang.Integer;
|
||||
import java.lang.String;
|
||||
import java.lang.Long;
|
||||
import java.lang.Float;
|
||||
import java.lang.Double;
|
||||
import java.lang.Boolean;
|
||||
|
||||
class Op {
|
||||
addInt(Integer a, Integer b) {
|
||||
Integer c = a+b;
|
||||
return c;
|
||||
}
|
||||
addString(String a, String b) {
|
||||
String c = a+b;
|
||||
return c;
|
||||
}
|
||||
addLong(Long a, Long b) {
|
||||
Long c = a+b;
|
||||
return c;
|
||||
}
|
||||
addFloat(Float a, Float b) {
|
||||
Float c = a+b;
|
||||
return c;
|
||||
}
|
||||
addDouble(Double a, Double b) {
|
||||
Double c = a+b;
|
||||
return c;
|
||||
}
|
||||
|
||||
subInt(Integer a, Integer b) {
|
||||
Integer c = a-b;
|
||||
return c;
|
||||
}
|
||||
subLong(Long a, Long b) {
|
||||
Long c = a-b;
|
||||
return c;
|
||||
}
|
||||
subFloat(Float a, Float b) {
|
||||
Float c = a-b;
|
||||
return c;
|
||||
}
|
||||
subDouble(Double a, Double b) {
|
||||
Double c = a-b;
|
||||
return c;
|
||||
}
|
||||
}
|
@ -1,7 +1,157 @@
|
||||
package bytecode;
|
||||
|
||||
public class OpTest extends JavaTXCompilerTest {
|
||||
public OpTest() {
|
||||
this.fileName = "Op";
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.io.File;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.objectweb.asm.Opcodes;
|
||||
|
||||
import de.dhbwstuttgart.core.JavaTXCompiler;
|
||||
|
||||
public class OpTest {
|
||||
private static String path;
|
||||
private static File fileToTest;
|
||||
private static JavaTXCompiler compiler;
|
||||
private static ClassLoader loader;
|
||||
private static Class<?> classToTest;
|
||||
private static String pathToClassFile;
|
||||
private static Object instanceOfClass;
|
||||
|
||||
@BeforeClass
|
||||
public static void setUpBeforeClass() throws Exception {
|
||||
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/";
|
||||
loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)});
|
||||
classToTest = loader.loadClass("Op");
|
||||
instanceOfClass = classToTest.getDeclaredConstructor().newInstance();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClassname() {
|
||||
assertEquals("Op", classToTest.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClassModifiers() {
|
||||
assertEquals(Opcodes.ACC_PUBLIC, classToTest.getModifiers());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNumberOfMethods() {
|
||||
int numOfMeth = classToTest.getDeclaredMethods().length;
|
||||
assertEquals(5, numOfMeth);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddString() throws NoSuchMethodException, SecurityException, IllegalAccessException,
|
||||
IllegalArgumentException, InvocationTargetException, InstantiationException {
|
||||
Method addString = classToTest.getDeclaredMethod("addString", String.class,String.class);
|
||||
String result = (String) addString.invoke(instanceOfClass, "Byte","Code");
|
||||
assertEquals("ByteCode", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddInt() throws NoSuchMethodException, SecurityException, IllegalAccessException,
|
||||
IllegalArgumentException, InvocationTargetException, InstantiationException {
|
||||
Method addInt = classToTest.getDeclaredMethod("addInt", Integer.class,Integer.class);
|
||||
Integer result = (Integer) addInt.invoke(instanceOfClass, 7,3);
|
||||
assertEquals(10, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddLong() throws NoSuchMethodException, SecurityException, IllegalAccessException,
|
||||
IllegalArgumentException, InvocationTargetException, InstantiationException {
|
||||
Method addLong = classToTest.getDeclaredMethod("addLong", Long.class,Long.class);
|
||||
Long result = (Long) addLong.invoke(instanceOfClass, 7L,3L);
|
||||
assertEquals(10L, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddFloat() throws NoSuchMethodException, SecurityException, IllegalAccessException,
|
||||
IllegalArgumentException, InvocationTargetException, InstantiationException {
|
||||
Method addFloat = classToTest.getDeclaredMethod("addFloat", Float.class,Float.class);
|
||||
Float result = (Float) addFloat.invoke(instanceOfClass, 7f,3f);
|
||||
assertEquals(10f, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddDouble() throws NoSuchMethodException, SecurityException, IllegalAccessException,
|
||||
IllegalArgumentException, InvocationTargetException, InstantiationException {
|
||||
Method addDouble = classToTest.getDeclaredMethod("addDouble", Double.class,Double.class);
|
||||
Double result = (Double) addDouble.invoke(instanceOfClass, 7.0,3.0);
|
||||
assertEquals(10.0, result);
|
||||
}
|
||||
|
||||
// @Test
|
||||
// public void testAddIntLong() throws NoSuchMethodException, SecurityException, IllegalAccessException,
|
||||
// IllegalArgumentException, InvocationTargetException, InstantiationException {
|
||||
// Method add = classToTest.getDeclaredMethod("add", Integer.class,Long.class);
|
||||
// Long result = (Long) add.invoke(instanceOfClass, 7,3L);
|
||||
// assertEquals(10L, result);
|
||||
// }
|
||||
|
||||
// @Test
|
||||
// public void testAddDLong() throws NoSuchMethodException, SecurityException, IllegalAccessException,
|
||||
// IllegalArgumentException, InvocationTargetException, InstantiationException {
|
||||
// Method add = classToTest.getDeclaredMethod("add", Double.class,Long.class);
|
||||
// Double result = (Double) add.invoke(instanceOfClass, 7d,3L);
|
||||
// assertEquals(10d, result);
|
||||
// }
|
||||
//
|
||||
// @Test
|
||||
// public void testAddIntShort() throws NoSuchMethodException, SecurityException, IllegalAccessException,
|
||||
// IllegalArgumentException, InvocationTargetException, InstantiationException {
|
||||
// Method add = classToTest.getDeclaredMethod("add", Integer.class,Short.class);
|
||||
// Integer result = (Integer) add.invoke(instanceOfClass, 7,3);
|
||||
// assertEquals(10, result);
|
||||
// }
|
||||
//
|
||||
// @Test
|
||||
// public void testAddIntByte() throws NoSuchMethodException, SecurityException, IllegalAccessException,
|
||||
// IllegalArgumentException, InvocationTargetException, InstantiationException {
|
||||
// Method add = classToTest.getDeclaredMethod("add", Integer.class,Byte.class);
|
||||
// Integer result = (Integer) add.invoke(instanceOfClass, 7,3);
|
||||
// assertEquals(10, result);
|
||||
// }
|
||||
//
|
||||
// @Test
|
||||
// public void testAddDFloat() throws NoSuchMethodException, SecurityException, IllegalAccessException,
|
||||
// IllegalArgumentException, InvocationTargetException, InstantiationException {
|
||||
// Method add = classToTest.getDeclaredMethod("add", Float.class,Double.class);
|
||||
// Double result = (Double) add.invoke(instanceOfClass, 7f,3d);
|
||||
// assertEquals(10d, result);
|
||||
// }
|
||||
//
|
||||
// @Test
|
||||
// public void testAddIntD() throws NoSuchMethodException, SecurityException, IllegalAccessException,
|
||||
// IllegalArgumentException, InvocationTargetException, InstantiationException {
|
||||
// Method add = classToTest.getDeclaredMethod("add", Integer.class,Double.class);
|
||||
// Double result = (Double) add.invoke(instanceOfClass, 7,3d);
|
||||
// assertEquals(10d, result);
|
||||
// }
|
||||
//
|
||||
// @Test
|
||||
// public void testAddShortD() throws NoSuchMethodException, SecurityException, IllegalAccessException,
|
||||
// IllegalArgumentException, InvocationTargetException, InstantiationException {
|
||||
// Method add = classToTest.getDeclaredMethod("add", Short.class,Double.class);
|
||||
// Double result = (Double) add.invoke(instanceOfClass, 7,3d);
|
||||
// assertEquals(10d, result);
|
||||
// }
|
||||
//
|
||||
// @Test
|
||||
// public void testAddByteD() throws NoSuchMethodException, SecurityException, IllegalAccessException,
|
||||
// IllegalArgumentException, InvocationTargetException, InstantiationException {
|
||||
// Method add = classToTest.getDeclaredMethod("add", Byte.class,Double.class);
|
||||
// Double result = (Double) add.invoke(instanceOfClass, 7,3d);
|
||||
// assertEquals(10d, result);
|
||||
// }
|
||||
}
|
||||
|
@ -1,7 +0,0 @@
|
||||
package bytecode;
|
||||
|
||||
public class OverlaodGenTest extends JavaTXCompilerTest {
|
||||
public OverlaodGenTest() {
|
||||
this.fileName = "OverlaodGen";
|
||||
}
|
||||
}
|
@ -1,7 +0,0 @@
|
||||
package bytecode;
|
||||
|
||||
public class TestIfTest extends JavaTXCompilerTest{
|
||||
public TestIfTest() {
|
||||
this.fileName = "IfTest";
|
||||
}
|
||||
}
|
88
test/bytecode/javFiles/Op.jav
Normal file
88
test/bytecode/javFiles/Op.jav
Normal file
@ -0,0 +1,88 @@
|
||||
import java.lang.Integer;
|
||||
import java.lang.String;
|
||||
import java.lang.Long;
|
||||
import java.lang.Float;
|
||||
import java.lang.Double;
|
||||
import java.lang.Boolean;
|
||||
import java.lang.Short;
|
||||
import java.lang.Byte;
|
||||
|
||||
public class Op {
|
||||
addInt(Integer a, Integer b) {
|
||||
Integer c = a+b;
|
||||
return c;
|
||||
}
|
||||
addString(String a, String b) {
|
||||
String c = a+b;
|
||||
return c;
|
||||
}
|
||||
addLong(Long a, Long b) {
|
||||
Long c = a+b;
|
||||
return c;
|
||||
}
|
||||
addFloat(Float a, Float b) {
|
||||
Float c = a+b;
|
||||
return c;
|
||||
}
|
||||
addDouble(Double a, Double b) {
|
||||
Double c = a+b;
|
||||
return c;
|
||||
}
|
||||
|
||||
// subInt(Integer a, Integer b) {
|
||||
// Integer c = a-b;
|
||||
// return c;
|
||||
// }
|
||||
// subLong(Long a, Long b) {
|
||||
// Long c = a-b;
|
||||
// return c;
|
||||
// }
|
||||
// subFloat(Float a, Float b) {
|
||||
// Float c = a-b;
|
||||
// return c;
|
||||
// }
|
||||
// subDouble(Double a, Double b) {
|
||||
// Double c = a-b;
|
||||
// return c;
|
||||
// }
|
||||
|
||||
// Long add(Integer a, Long b) {
|
||||
// Long c = a+b;
|
||||
// return c;
|
||||
// }
|
||||
|
||||
// add(Double a, Long b) {
|
||||
// Double c = a+b;
|
||||
// return c;
|
||||
// }
|
||||
//
|
||||
// add(Integer a, Short b) {
|
||||
// Integer c = a+b;
|
||||
// return c;
|
||||
// }
|
||||
//
|
||||
// add(Integer a, Byte b) {
|
||||
// Integer c = a+b;
|
||||
// return c;
|
||||
// }
|
||||
//
|
||||
// add(Float a, Double b) {
|
||||
// Double c = a+b;
|
||||
// return c;
|
||||
//}
|
||||
//
|
||||
// add(Integer a, Double b) {
|
||||
// Double c = a+b;
|
||||
// return c;
|
||||
// }
|
||||
//
|
||||
// add(Short a, Double b) {
|
||||
// Double c = a+b;
|
||||
// return c;
|
||||
// }
|
||||
//
|
||||
// add(Byte a, Double b) {
|
||||
// Double c = a+b;
|
||||
// return c;
|
||||
// }
|
||||
}
|
Loading…
Reference in New Issue
Block a user