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

View File

@ -85,6 +85,30 @@ public class Signature {
createSignatureForFunN(numberOfParams); 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) { private void createSignatureForFunN(int numberOfParams) {
// sw.visitClassBound().visitEnd(); // sw.visitClassBound().visitEnd();

View File

@ -235,6 +235,37 @@ public class MethodCallHelper {
return methDesc; 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) { private void writeClassFile(byte[] bytecode, String name) {
FileOutputStream output; FileOutputStream output;
try { try {

View File

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

View File

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

View File

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