Identity implementiert

This commit is contained in:
JanUlrich 2015-09-23 15:30:35 +02:00
parent 99a7510810
commit c5f0e73e50
14 changed files with 59 additions and 73 deletions

View File

@ -4,6 +4,7 @@ import java.awt.List;
import java.util.ArrayList;
import org.apache.commons.bcel6.Constants;
import org.apache.commons.bcel6.classfile.Attribute;
import org.apache.commons.bcel6.classfile.BootstrapMethod;
import org.apache.commons.bcel6.classfile.ConstantInvokeDynamic;
import org.apache.commons.bcel6.classfile.ConstantMethodHandle;
@ -139,7 +140,11 @@ public class DHBWInstructionFactory extends InstructionFactory{
return InstructionFactory.createLoad(bytecodeType, index);
}
public Type createObjectType() {
public static Type createObjectType() {
return new org.apache.commons.bcel6.generic.ObjectType("java.lang.Object");
}
public Attribute createSignatureAttribute(String signature) {
return new Signature(cp.addUtf8("Signature"),2,cp.addUtf8(signature),cp.getConstantPool());
}
}

View File

@ -39,11 +39,11 @@ public class MethodGenerator extends MethodGen{
//Die korrekte Signatur für die Methode anhängen. Hier sind dann auch die Parameter von RefTypes enthalten:
String paramTypesSig = "(";
for(FormalParameter p : parameter){
paramTypesSig += p.getType().getBytecodeMethodSignature(cg);
paramTypesSig += p.getType().getBytecodeSignature(cg);
}
paramTypesSig += ")";
String retTypeSig = retType.getBytecodeMethodSignature(cg);
method.addAttribute(new Signature(cp.addUtf8("Signature"),2,cp.addUtf8(paramTypesSig+retTypeSig),cp.getConstantPool()));
String retTypeSig = retType.getBytecodeSignature(cg);
method.addAttribute(factory.createSignatureAttribute(paramTypesSig+retTypeSig));
return method.getMethod();
}

View File

@ -13,6 +13,7 @@ import de.dhbwstuttgart.typeinference.Menge;
import de.dhbwstuttgart.bytecode.ClassGenerator;
import de.dhbwstuttgart.bytecode.DHBWConstantPoolGen;
import de.dhbwstuttgart.bytecode.DHBWInstructionFactory;
import de.dhbwstuttgart.bytecode.MethodGenerator;
import de.dhbwstuttgart.myexception.JVMCodeException;
import de.dhbwstuttgart.parser.JavaClassName;
import de.dhbwstuttgart.syntaxtree.misc.DeclId;
@ -65,25 +66,19 @@ public class Constructor extends Method {
InstructionList il = new InstructionList(); //sollte nicht new sein sondern aus Block kommen
Class parentClass = this.getParentClass();
//TODO: Alles dynamisch gestalten
//init darf nur drin stehen, wenn Konstruktor;
this.method = new MethodGen(Constants.ACC_PUBLIC, this.getType().getBytecodeType(cg), org.apache.commons.bcel6.generic.Type.NO_ARGS , new String[] { }, "<init>", parentClass.name, il, _cp);
MethodGenerator method = new MethodGenerator(Constants.ACC_PUBLIC, this.getType().getBytecodeType(cg), org.apache.commons.bcel6.generic.Type.NO_ARGS , new String[] { }, "<init>", parentClass.name, il, _cp);
//Iteration <EFBFBD>ber Block starten
//FieldInitializations an Block anfügen
Block block = this.get_Block();
if(block.statements.firstElement() instanceof SuperCall){
block.statements.insertElementAt(new BytecodeInstructionBlock(fieldInitializations), 1);
}else{
throw new DebugException("Fehlender SuperCall im Konstruktor");
}
InstructionList blockInstructions = block.genByteCode(cg);
il.append(blockInstructions);
il.append(DHBWInstructionFactory.createReturn( org.apache.commons.bcel6.generic.Type.VOID)); //Konstruktor hat immer VOID Return type
//method.setMaxStack(); //Die Stack Größe automatisch berechnen lassen (erst nach dem alle Instructions angehängt wurden)
this.method.setMaxStack(); //Die Stack Größe automatisch berechnen lassen (erst nach dem alle Instructions angehängt wurden)
cg.addMethod(this.method.getMethod());
cg.addMethod(method.createMethod(cg, getParameterList(), this.getType(), get_Block()));
}
/**

View File

@ -173,7 +173,9 @@ public class FieldDeclaration extends Field{
public InstructionList genByteCode(ClassGenerator cg) {
//Das Feld an die Klasse anfügen:
FieldGen field = new FieldGen(0, this.getType().getBytecodeType(cg), this.getDescription(), cg.getConstantPool());
field.addAttribute(cg.getInstructionFactory().createSignatureAttribute(this.getType().getBytecodeSignature(cg)));
cg.addField(field.getField());
//Die Felddekleration an den Konstruktor anhängen:
InstructionList il = new InstructionList();
il.append(new This(this).genByteCode(cg));
@ -181,7 +183,6 @@ public class FieldDeclaration extends Field{
FieldInstruction putFieldInstruction =
cg.getInstructionFactory().createFieldAccess(this.getParentClass().getName().toString(),
this.getDescription(), this.getType().getBytecodeType(cg), Constants.PUTFIELD);
il.append(putFieldInstruction );
return il;
}

View File

@ -73,7 +73,7 @@ public class Method extends Field implements IItemWithOffset, TypeInsertable
// ino.attribute.returntype.23497.declaration
private Type returntype;
// ino.end
protected MethodGen method;
// ino.attribute.genericMethodParameters.23500.decldescription type=javadoc
/**
* HOTI 4.5.06 Dieser Vektor beinhaltet alle Generischen Typen und v.a. die

View File

@ -243,21 +243,8 @@ public class LambdaExpression extends Expr{
InstructionList il = new InstructionList();
org.apache.commons.bcel6.generic.Type[] paramTypes = params.getBytecodeTypeList(cg);
org.apache.commons.bcel6.generic.Type[] paramTypesTemp = new org.apache.commons.bcel6.generic.Type[paramTypes.length];
for(int i = 0; i < paramTypes.length ; i++) {
if(paramTypes[i] instanceof TypePlaceholderType){
//TODO: Das kann auch ein spezifischer Typ als Object sein
paramTypesTemp[i] = cg.getNearestNonTPHType(((TypePlaceholderType)paramTypes[i]).getTPH());
}else{
paramTypesTemp[i] = paramTypes[i];
}
}
paramTypes = paramTypesTemp;
org.apache.commons.bcel6.generic.Type retType = method_body.getType().getBytecodeType(cg);
if(retType instanceof TypePlaceholderType){
//Die TypePlaceholderTypen aussortieren. Sie werden nur in der Signatur erwähnt. Kommen im Bytecode sonst eigentlich nicht vor
retType = cg.getNearestNonTPHType(((TypePlaceholderType) retType).getTPH());
}
MethodGenerator lambdaMethod = new MethodGenerator(0, retType,
paramTypes, params.getParameterNameArray(), cg.createLambdaMethodName(),
this.getParentClass().getName().toString(), new InstructionList(), cg.getConstantPool());
@ -277,9 +264,9 @@ public class LambdaExpression extends Expr{
private String getLambdaSignature(ClassGenerator cg){
String typeSignature = "(";
typeSignature+=this.getParentClass().getType().getBytecodeSignature(cg);
typeSignature+=this.getParentClass().getType().getBytecodeType(cg).getSignature();
typeSignature += ")";
typeSignature += this.lambdaType.getBytecodeSignature(cg);
typeSignature += this.lambdaType.getBytecodeType(cg).getSignature();
return typeSignature;
}
}

View File

@ -157,8 +157,10 @@ public class ExtendsWildcardType extends WildcardType implements ITypeContainer,
return super.TYPE(ass, parent);
}
/*
@Override
public org.apache.commons.bcel6.generic.Type getBytecodeType(ClassGenerator cg) {
return new de.dhbwstuttgart.bytecode.WildcardType(this.innerType.get_Name(), "+");
}
*/
}

View File

@ -119,10 +119,10 @@ public class FunN extends RefType {
public String getBytecodeInvokeDynamicSignature(ClassGenerator cg) {
String ret = "(";
for(Type t : T){
ret+=t.getBytecodeMethodSignature(cg);
ret+=t.getBytecodeType(cg).getSignature();
}
ret +=")";
ret+= R.getBytecodeMethodSignature(cg);
ret+= R.getBytecodeType(cg).getSignature();
return ret;
}

View File

@ -818,29 +818,24 @@ public class RefType extends ObjectType implements IMatchable
}
@Override
public String getBytecodeMethodSignature(ClassGenerator cg) {
public String getBytecodeSignature(ClassGenerator cg) {
String paramString = "";
if(this.parameter != null && this.parameter.size()>0){
paramString+="<";
Iterator<Type> it = this.parameter.iterator();
while(it.hasNext()){
Type param = it.next();
paramString+=param.getBytecodeMethodSignature(cg);
paramString+=param.getBytecodeSignature(cg);
//if(it.hasNext())
//paramString+=";"; //kein Delimiter zwischen den Typen
}
paramString+=">";
}
String typeSignature = this.getBytecodeSignature(cg);
String typeSignature = this.getBytecodeType(cg).getSignature();
typeSignature = typeSignature.substring(0, typeSignature.length()-1);
return typeSignature+paramString+";";
}
@Override
public String getBytecodeSignature(ClassGenerator cg) {
return this.getBytecodeType(cg).getSignature();
}
}
// ino.end

View File

@ -154,19 +154,18 @@ public class SuperWildcardType extends WildcardType implements ITypeContainer, I
return super.TYPE(ass, parent);
}
/*
@Override
public org.apache.commons.bcel6.generic.Type getBytecodeType(ClassGenerator cg) {
return new de.dhbwstuttgart.bytecode.WildcardType(this.innerType.get_Name(), "-");
//return new de.dhbwstuttgart.bytecode.WildcardType(this.innerType.get_Name(), "-");
return this.innerType.getBytecodeType(cg);
}
*/
@Override
public String getBytecodeSignature(ClassGenerator cg) {
return this.innerType.getBytecodeSignature(cg);
return "-" + this.innerType.getBytecodeSignature(cg);
}
@Override
public String getBytecodeMethodSignature(ClassGenerator cg) {
return "-" + this.innerType.getBytecodeMethodSignature(cg);
}
}

View File

@ -307,16 +307,22 @@ public abstract class Type extends SyntaxTreeNode implements IItemWithOffset
public abstract org.apache.commons.bcel6.generic.Type getBytecodeType(ClassGenerator cg);
/**
* Erzeugt einen String, welcher den Typ genauer angibt.
* Dieser kann dann in Methoden und Feldersignaturen verwendet werden
* @param cg
* @return
*/
public String getBytecodeSignature(ClassGenerator cg) {
return this.getBytecodeType(cg).getSignature();
}
public String getBytecodeMethodSignature(ClassGenerator cg) {
return this.getBytecodeSignature(cg);
}
/**
* @param cg
* @return Die Signatur wie sie in Klassensignaturen verwendet wird
*/
public String getClassSignature(ClassGenerator cg){
return this.getBytecodeMethodSignature(cg);
return this.getBytecodeSignature(cg);
}
}
// ino.end

View File

@ -9,6 +9,7 @@ import java.util.Iterator;
import de.dhbwstuttgart.typeinference.Menge;
import de.dhbwstuttgart.logger.*;
import de.dhbwstuttgart.bytecode.ClassGenerator;
import de.dhbwstuttgart.bytecode.DHBWInstructionFactory;
import de.dhbwstuttgart.bytecode.TypePlaceholderType;
import de.dhbwstuttgart.core.MyCompiler;
import de.dhbwstuttgart.parser.JavaClassName;
@ -434,26 +435,26 @@ public class TypePlaceholder extends ObjectType
public org.apache.commons.bcel6.generic.Type getBytecodeType(ClassGenerator cg) {
Type resolvedType = cg.resolveTPH(this);
if(resolvedType instanceof TypePlaceholder){
cg.addUsedTPH((TypePlaceholder)resolvedType);
return new TypePlaceholderType((TypePlaceholder)resolvedType);
return DHBWInstructionFactory.createObjectType();
}
return resolvedType.getBytecodeType(cg);
}
@Override
public String getBytecodeSignature(ClassGenerator cg) {
return cg.resolveTPH(this).getBytecodeMethodSignature(cg);
Type resolvedType = cg.resolveTPH(this);
if(resolvedType instanceof TypePlaceholder){
cg.addUsedTPH((TypePlaceholder)resolvedType);
return new TypePlaceholderType((TypePlaceholder)resolvedType).getSignature();
}
public String getBytecodeMethodSignature(ClassGenerator cg) {
return super.getBytecodeSignature(cg);
return resolvedType.getBytecodeSignature(cg);
}
@Override
public String getClassSignature(ClassGenerator cg) {
//Die Signaturen in der Klasse bauen sich für Generische Variabeln (also TPHs) folgendermaßen auf: "GVAR:SuperClass"
String ret = this.getBytecodeMethodSignature(cg);
ret = ret.substring(0, ret.length()-1) + ":"; //";" mit ":" ersetzen
String ret = this.getBytecodeSignature(cg);
ret = ret.substring(1, ret.length()-1) + ":"; //";" mit ":" ersetzen
org.apache.commons.bcel6.generic.Type nearestType = cg.getNearestNonTPHType(this);
if(nearestType instanceof TypePlaceholderType){ //Handelt es sich um einen weiteren TPH als nächsten Typ, so ist es ein allgemeiner Typ und wir nehmen Object als Superklasse
ret += cg.getInstructionFactory().createObjectType().getSignature();

View File

@ -107,15 +107,9 @@ public class WildcardType extends Type{
@Override
public String getBytecodeSignature(ClassGenerator cg) {
return this.innerType.getBytecodeMethodSignature(cg);
}
@Override
public String getBytecodeMethodSignature(ClassGenerator cg) {
return this.innerType.getBytecodeMethodSignature(cg);
return this.innerType.getBytecodeSignature(cg);
}
}

View File

@ -10,6 +10,7 @@ public static void main(String[] args){
System.out.println(new FieldDeclaration().field);
System.out.println(new Runnable().method().apply());
Runnable r = new Runnable().method().apply();
Test t = new Identity<Test,Test>().op.apply(this);
Test t = new Identity<Test,Test>().op.apply(new Test());
System.out.println(t);
}
}