JavaPatternMatching/src/de/dhbwstuttgart/syntaxtree/Constructor.java

96 lines
3.7 KiB
Java
Raw Normal View History

2014-09-02 08:33:54 +00:00
package de.dhbwstuttgart.syntaxtree;
2013-10-18 11:33:46 +00:00
2016-07-21 14:36:33 +00:00
import org.apache.bcel.Constants;
import org.apache.bcel.generic.InstructionList;
2014-02-19 04:20:54 +00:00
import de.dhbwstuttgart.bytecode.ClassGenerator;
import de.dhbwstuttgart.bytecode.DHBWConstantPoolGen;
2015-09-23 13:30:35 +00:00
import de.dhbwstuttgart.bytecode.MethodGenerator;
2016-12-15 14:45:23 +00:00
import de.dhbwstuttgart.typecheck.JavaClassName;
2014-09-02 08:33:54 +00:00
import de.dhbwstuttgart.syntaxtree.statement.Block;
2015-06-17 10:03:54 +00:00
import de.dhbwstuttgart.syntaxtree.statement.SuperCall;
2014-09-02 08:33:54 +00:00
import de.dhbwstuttgart.syntaxtree.type.GenericTypeVar;
import de.dhbwstuttgart.syntaxtree.type.Type;
2016-10-07 12:12:18 +00:00
import de.dhbwstuttgart.typeinference.TypeinferenceResults;
2014-09-02 08:33:54 +00:00
import de.dhbwstuttgart.typeinference.assumptions.ConstructorAssumption;
import de.dhbwstuttgart.typeinference.assumptions.TypeAssumptions;
import de.dhbwstuttgart.typeinference.exceptions.DebugException;
import de.dhbwstuttgart.syntaxtree.type.Void;
2014-02-19 04:20:54 +00:00
public class Constructor extends Method {
/**
* Parser kann nicht zwischen einem Konstruktor und einer Methode unterscheiden.
* Diese Klasse beherbegt den als Methode geparsten Konstruktor und wandelt sein verhalten zu dem eines Konstruktors ab.
*/
public Constructor(Method methode, Class parent){
2016-09-16 11:25:20 +00:00
super(methode.get_Name(), methode.getType(), methode.getParameterList(),
methode.get_Block(), methode.getGenericDeclarationList(), methode.getOffset());
2015-06-17 10:03:54 +00:00
//Sicherstellen, dass das erste Statement in der Methode ein SuperCall ist:
2016-09-16 11:25:20 +00:00
if(this.get_Block().get_Statement().size() <1 || ! (this.get_Block().get_Statement().get(0) instanceof SuperCall)){
2015-06-17 10:03:54 +00:00
this.get_Block().statements.add(0, new SuperCall(this.get_Block()));
this.parserPostProcessing(parent);
2015-06-17 10:03:54 +00:00
}
}
@Override
public TypeAssumptions createTypeAssumptions(Class classmember) {
this.parent = classmember;
2016-09-16 11:25:20 +00:00
Class parentClass = classmember;
TypeAssumptions ret = new TypeAssumptions();
ret.addAssumption(new ConstructorAssumption(this, parentClass));
return ret;
}
public void genByteCode(ClassGenerator cg, InstructionList fieldInitializations){
DHBWConstantPoolGen _cp = cg.getConstantPool();
InstructionList il = new InstructionList(); //sollte nicht new sein sondern aus Block kommen
2015-12-08 12:34:16 +00:00
2016-09-16 11:25:20 +00:00
MethodGenerator method = new MethodGenerator(Constants.ACC_PUBLIC, this.getType().getBytecodeType(cg, null),
this.parameterlist.getBytecodeTypeList(cg,null) , this.parameterlist.getParameterNameArray(),
"<init>", cg.getClassName(), il, _cp);
2015-06-17 10:03:54 +00:00
2015-09-23 13:30:35 +00:00
//FieldInitializations an Block anfügen
2015-06-18 09:17:42 +00:00
Block block = this.get_Block();
2016-09-16 11:25:20 +00:00
if(! (block.statements.get(0) instanceof SuperCall)){
throw new DebugException("Fehlender SuperCall im Konstruktor");
}
2015-06-17 10:03:54 +00:00
2015-09-23 13:30:35 +00:00
//method.setMaxStack(); //Die Stack Größe automatisch berechnen lassen (erst nach dem alle Instructions angehängt wurden)
2015-06-17 10:03:54 +00:00
cg.addMethod(method.createMethod(cg, getParameterList(), this.getType(), get_Block(), null));
}
@Override
2016-10-07 12:12:18 +00:00
public void genByteCode(ClassGenerator cg, Class classObj, TypeinferenceResults resultSets) {
this.genByteCode(cg, new InstructionList());
}
2015-06-16 09:55:15 +00:00
// super statement muss drin sein
// stmt genByteCode + im block genByteCode implementieren & dann Hierarchie ausprobieren
// de.dhbw.systanxtree.stmts supercall
// Aufrufhierarchie: Class->Felder->Konstruktor->Methode innerhalb Konstruktor->Block->Statements (in diesem Fall nur super())->hier wird bytecode f<>r superaufruf generiert
2015-06-16 09:55:15 +00:00
@Override
public void parserPostProcessing(SyntaxTreeNode parent){
super.parserPostProcessing(parent);
if(this.parameterlist != null){
for(FormalParameter fp : this.parameterlist){
fp.parserPostProcessing(this);
}
}
for(GenericTypeVar gtv : this.getGenericParameter()){
gtv.parserPostProcessing(this);
}
}
2014-02-19 04:20:54 +00:00
@Override
2014-09-02 16:49:19 +00:00
public JavaClassName getTypeName() {
2014-02-19 04:20:54 +00:00
2014-02-19 16:32:43 +00:00
return this.getType().getName();
2014-02-19 04:20:54 +00:00
}
2013-10-18 11:33:46 +00:00
}