diff --git a/src/main/java/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java b/src/main/java/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java index 0565d933..02a07fb8 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java @@ -13,10 +13,14 @@ import java.util.HashMap; import java.util.Iterator; import java.util.LinkedList; import java.util.List; +import java.util.stream.Collectors; import de.dhbwstuttgart.bytecode.utilities.*; import de.dhbwstuttgart.environment.DirectoryClassLoader; import de.dhbwstuttgart.exceptions.NotImplementedException; +import de.dhbwstuttgart.intermediate.convert.ASTToIntermediate; +import de.dhbwstuttgart.intermediate.generation.FunN; +import de.dhbwstuttgart.intermediate.types.IntermediateInnerType; import de.dhbwstuttgart.parser.scope.JavaClassName; import de.dhbwstuttgart.syntaxtree.statement.*; import de.dhbwstuttgart.syntaxtree.statement.BinaryExpr.Operator; @@ -814,7 +818,21 @@ public class BytecodeGenMethod implements StatementVisitor { } else if(!helper.isInCurrPkg(clazz)){ if(clazz.contains(CONSTANTS.$$)) { mDesc = helper.getDescriptorOfApplyMethod(methCallType); - helper.generateBCForFunN(mDesc); + //ToDo Etienne: check if it works + //helper.generateBCForFunN(mDesc); + ASTToIntermediate converter = new ASTToIntermediate(); + IntermediateInnerType returnType = converter.convert(methodCall.receiver.getType()); + List arguments = methodCall + .arglist + .getArguments() + .stream() + .map(TypableStatement::getType) + .map(converter::convert) + .collect(Collectors.toList());//ToDo + FunN f = new FunN(arguments, returnType); + FunN.writeClassFile(f.getSuperClassName(), f.getSuperBytecode(), path); + FunN.writeClassFile(f.getClassName(), f.getBytecode(), path); + //ToDo Etienne End // mDesc = helper.generateBCForFunN(methCallType,typesOfParams); }else { try { diff --git a/src/main/java/de/dhbwstuttgart/bytecode/descriptor/TypeToDescriptor.java b/src/main/java/de/dhbwstuttgart/bytecode/descriptor/TypeToDescriptor.java index aa3db5b1..613f672e 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/descriptor/TypeToDescriptor.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/descriptor/TypeToDescriptor.java @@ -1,6 +1,7 @@ package de.dhbwstuttgart.bytecode.descriptor; import de.dhbwstuttgart.exceptions.NotImplementedException; +import de.dhbwstuttgart.intermediate.convert.ASTToIntermediate; import de.dhbwstuttgart.syntaxtree.type.ExtendsWildcardType; import de.dhbwstuttgart.syntaxtree.type.GenericRefType; import de.dhbwstuttgart.syntaxtree.type.RefType; @@ -12,6 +13,9 @@ public class TypeToDescriptor implements TypeVisitor{ @Override public String visit(RefType refType) { + //ToDo Etienne: check if it works + if (refType.getName().toString().matches("Fun\\d+\\$\\$")) return new ASTToIntermediate().convert(refType).getDescriptor(); + return refType.getName().toString().replace(".", "/"); // String t = refType.getName().toString().replace(".", "/"); // return t.equals("Fun1")?(t+"$$"):t; diff --git a/src/main/java/de/dhbwstuttgart/bytecode/signature/TypeToSignature.java b/src/main/java/de/dhbwstuttgart/bytecode/signature/TypeToSignature.java index c8306f02..347d0d08 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/signature/TypeToSignature.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/signature/TypeToSignature.java @@ -6,6 +6,7 @@ import java.util.List; import java.util.Optional; import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.GenericsGeneratorResult; +import de.dhbwstuttgart.intermediate.convert.ASTToIntermediate; import de.dhbwstuttgart.syntaxtree.type.ExtendsWildcardType; import de.dhbwstuttgart.syntaxtree.type.GenericRefType; import de.dhbwstuttgart.syntaxtree.type.RefType; @@ -29,6 +30,9 @@ public class TypeToSignature implements TypeVisitor { public String visit(RefType refType) { if(refType.getName().toString().equals("void")) return "V"; + //ToDo Etienne: check if it works + if (refType.getName().toString().matches("Fun\\d+\\$\\$")) return new ASTToIntermediate().convert(refType).getSignature(); + // return refType.toString().replace(".", "/"); String params = ""; if(refType.getParaList().size()>0){ diff --git a/src/main/java/de/dhbwstuttgart/intermediate/convert/ASTToIntermediate.java b/src/main/java/de/dhbwstuttgart/intermediate/convert/ASTToIntermediate.java index f918c777..4e9ef6dc 100644 --- a/src/main/java/de/dhbwstuttgart/intermediate/convert/ASTToIntermediate.java +++ b/src/main/java/de/dhbwstuttgart/intermediate/convert/ASTToIntermediate.java @@ -1,14 +1,28 @@ package de.dhbwstuttgart.intermediate.convert; import de.dhbwstuttgart.exceptions.NotImplementedException; +import de.dhbwstuttgart.intermediate.generation.FunN; import de.dhbwstuttgart.intermediate.types.*; import de.dhbwstuttgart.syntaxtree.type.*; +import java.util.List; import java.util.stream.Collectors; public class ASTToIntermediate { public IntermediateRefType convert(RefType originalType){ + //ToDo Etienne: check if it works + if (originalType.getName().toString().matches("Fun\\d+\\$\\$")){ + ASTToIntermediate converter = new ASTToIntermediate(); + List parameters = originalType.getParaList(); + IntermediateInnerType returnType = convert(parameters.get(parameters.size() - 1)); + parameters.remove(parameters.size() - 1); + return new FunN(parameters + .stream() + .map(this::convert) + .collect(Collectors.toList()), returnType); + } + return new IntermediateRefType(originalType.getName(), originalType .getParaList() diff --git a/src/main/java/de/dhbwstuttgart/intermediate/generation/FunN.java b/src/main/java/de/dhbwstuttgart/intermediate/generation/FunN.java index 1d60f46a..fc8119a0 100644 --- a/src/main/java/de/dhbwstuttgart/intermediate/generation/FunN.java +++ b/src/main/java/de/dhbwstuttgart/intermediate/generation/FunN.java @@ -1,5 +1,6 @@ package de.dhbwstuttgart.intermediate.generation; +import de.dhbwstuttgart.bytecode.utilities.CONSTANTS; import de.dhbwstuttgart.intermediate.types.IntermediateGenericType; import de.dhbwstuttgart.intermediate.types.IntermediateInnerType; import de.dhbwstuttgart.intermediate.types.IntermediateRefType; @@ -9,6 +10,8 @@ import org.objectweb.asm.ClassWriter; import org.objectweb.asm.MethodVisitor; import org.objectweb.asm.Type; +import java.io.File; +import java.io.FileOutputStream; import java.util.Collections; import java.util.List; import java.util.stream.Collectors; @@ -67,6 +70,8 @@ public final class FunN extends IntermediateRefType { Stream.concat(typArguments.stream(), Stream.of(returnType)).collect(Collectors.toList())); } + public String getSuperClassName() { return superFunN.getClassName(); } + /** * @return the bytecode for the super FunN type */ @@ -131,4 +136,16 @@ public final class FunN extends IntermediateRefType { } return bytecode; } + + //ToDo Etienne: Auslagern in andere Klasse; + public static boolean writeClassFile(String className, byte[] bytecode, File directory) { + try (FileOutputStream output = new FileOutputStream(new File(directory , className + CONSTANTS.EXTENSIONCLASS))){ + output.write(bytecode); + output.flush(); + return true; + } catch (Exception e) { + e.printStackTrace(); + } + return false; + } }