Merged with bigRefactoring. Added method to generate signature to

FunN-Interfaces. Adapted jav-Files.
This commit is contained in:
Fayez Abu Alia 2019-06-06 12:15:51 +02:00
parent f6669f8c13
commit b801e144c3
6 changed files with 72 additions and 7 deletions

View File

@ -842,6 +842,7 @@ public class BytecodeGenMethod implements StatementVisitor {
} else if(!helper.isInCurrPkg(clazz)){
if(clazz.contains("$$")) {
mDesc = helper.generateBCForFunN();
// mDesc = helper.generateBCForFunN(methCallType,typesOfParams);
}else {
try {
cLoader2 = new URLClassLoader(new URL[] {new URL("file://"+path)});
@ -902,7 +903,9 @@ public class BytecodeGenMethod implements StatementVisitor {
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, clazz.replace(".", "/"), methodCall.name,
mDesc, isInterface);
}
// TODO: if called method is apply (from FI) then generate BC-instruction "CHECKCAST"
if(methodRefl != null && !methodRefl.getReturnType().isPrimitive()) {
if(methodRefl.getReturnType().equals(Object.class)) {
String checkCast = getResolvedType(methodCall.getType());

View File

@ -84,6 +84,30 @@ public class Signature {
sw = new SignatureWriter();
createSignatureForFunN(numberOfParams);
}
public Signature(int numberOfParams, String to, String[] paramTypes) {
sw = new SignatureWriter();
createSignatureForFunN(numberOfParams, to, paramTypes);
}
private void createSignatureForFunN(int numberOfParams, String to, String[] paramTypes) {
for(int i = 0;i<numberOfParams;i++) {
int j = i+1;
sw.visitFormalTypeParameter("T"+ j);
// getBounds von Params
sw.visitClassBound().visitClassType(paramTypes[i]);
sw.visitClassBound().visitEnd();
}
sw.visitFormalTypeParameter("R");
// getBounds vom Return-Type
sw.visitClassBound().visitClassType(to);
sw.visitClassBound().visitEnd();
// TODO: prüfe ob Return-Type = void,
sw.visitSuperclass().visitClassType(Type.getInternalName(Object.class));;
sw.visitEnd();
}
private void createSignatureForFunN(int numberOfParams) {

View File

@ -234,6 +234,37 @@ public class MethodCallHelper {
writeClassFile(classWriter.toByteArray(), name);
return methDesc;
}
public String generateBCForFunN(String returnType, String[] paramTypes) {
ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
SignatureWriter methSig = new SignatureWriter();
int numberOfParams = 0;
SignatureVisitor paramVisitor = methSig.visitParameterType();
Iterator<Expression> itr1 = methCall.arglist.getArguments().iterator();
String methDesc = "(";
while(itr1.hasNext()) {
numberOfParams++;
// getBounds
paramVisitor.visitTypeVariable("T" + numberOfParams);
methDesc += "L" + paramTypes[numberOfParams-1] + ";";
itr1.next();
}
methDesc += ")L" + returnType + ";";
methSig.visitReturnType().visitTypeVariable("R");
// ")"+lam.getReturn.getBounds
Signature sig = new Signature(numberOfParams,returnType,paramTypes);
String name = "Fun" + numberOfParams + "$$";
classWriter.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC+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);
return methDesc;
}
private void writeClassFile(byte[] bytecode, String name) {
FileOutputStream output;

View File

@ -1,6 +1,6 @@
import java.lang.Integer;
import java.lang.Long;
import java.lang.Short;
//import java.lang.Long;
//import java.lang.Short;
public class Faculty {
public fact;

View File

@ -7,17 +7,17 @@ public class Sorting{
a.addAll(b);
return a;
}
/*
sort(in){
var firstHalf = in;
var secondHalf = in;
return merge(sort(firstHalf), sort(secondHalf));
}
*/
/*
void sort(a){
a = merge(a,a);
}
*/
}

View File

@ -4,5 +4,12 @@ import java.util.Collection;
class Sorting{
void merge(a, b){
a.addAll(b);
return a;
}
sort(in){
var firstHalf = in;
var secondHalf = in;
return merge(sort(firstHalf), sort(secondHalf));
}
}