Doppelte Methodengenerierungen verhindert

This commit is contained in:
Enrico Schrödter 2015-11-27 18:10:46 +01:00
parent f7804b754d
commit 3c80cb275b
2 changed files with 70 additions and 20 deletions

View File

@ -6,6 +6,7 @@ package de.dhbwstuttgart.syntaxtree;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import org.apache.commons.bcel6.Constants;
@ -18,7 +19,9 @@ import org.apache.commons.bcel6.generic.InstructionList;
import org.apache.commons.bcel6.generic.MethodGen;
import de.dhbwstuttgart.typeinference.Menge;
import de.dhbwstuttgart.typeinference.Menge.Equal;
import de.dhbwstuttgart.logger.Logger;
import de.dhbwstuttgart.logger.Section;
import de.dhbwstuttgart.bytecode.ClassGenerator;
import de.dhbwstuttgart.bytecode.DHBWConstantPoolGen;
import de.dhbwstuttgart.bytecode.DHBWInstructionFactory;
@ -108,6 +111,8 @@ public class Method extends Field implements IItemWithOffset, TypeInsertable
// ino.end
// ino.attribute.parserlog.23518.declaration
protected static Logger parserlog = Logger.getLogger("parser");
protected Menge<org.apache.commons.bcel6.generic.Type[]> createdMethods = new Menge<>();
// ino.end
@ -755,7 +760,7 @@ public class Method extends Field implements IItemWithOffset, TypeInsertable
Class parentClass = this.getParentClass();
//Die Argumentliste generieren:
org.apache.commons.bcel6.generic.Type[] argumentTypes = org.apache.commons.bcel6.generic.Type.NO_ARGS;
org.apache.commons.bcel6.generic.Type[] argumentTypes = org.apache.commons.bcel6.generic.Type.NO_ARGS;
String[] argumentNames = new String[]{};
if(this.parameterlist != null && this.parameterlist.size() > 0){
argumentTypes = new org.apache.commons.bcel6.generic.Type[this.parameterlist.size()];
@ -767,21 +772,47 @@ public class Method extends Field implements IItemWithOffset, TypeInsertable
i++;
}
short constants = Constants.ACC_PUBLIC; //Per Definition ist jede Methode public
if(this.modifiers != null && this.modifiers.includesModifier(new Static())) constants += Constants.ACC_STATIC;
Type returnType = this.getType();
//Methode generieren:
MethodGenerator method = new MethodGenerator(constants, returnType.getBytecodeType(cg, t), argumentTypes , argumentNames, this.get_Method_Name(), parentClass.name, il, _cp);
//Methode generieren und anfügen:
cg.addMethod(method.createMethod(cg, getParameterList(), returnType, get_Block(), t));
if(!createdMethods.contains(argumentTypes, new ArgumentTypeEquals())){
createdMethods.add(argumentTypes);
short constants = Constants.ACC_PUBLIC; //Per Definition ist jede Methode public
if(this.modifiers != null && this.modifiers.includesModifier(new Static())) constants += Constants.ACC_STATIC;
Type returnType = this.getType();
//Methode generieren:
MethodGenerator method = new MethodGenerator(constants, returnType.getBytecodeType(cg, t), argumentTypes , argumentNames, this.get_Method_Name(), parentClass.name, il, _cp);
//Methode generieren und anfügen:
cg.addMethod(method.createMethod(cg, getParameterList(), returnType, get_Block(), t));
}
}
}
}
}
class ArgumentTypeEquals implements Equal<org.apache.commons.bcel6.generic.Type[]>{
@Override
public boolean equal(org.apache.commons.bcel6.generic.Type[] a, org.apache.commons.bcel6.generic.Type[] b) {
if(a.length != b.length){
Logger.getLogger("ArgumentTypeEquals").error("false(length)", Section.CODEGEN);
return false;
}
for(int i = 0; i < a.length; i++){
if(!a[i].equals(b[i])){
String as = a[i].toString();
String bs = b[i].toString();
Logger.getLogger("ArgumentTypeEquals").error("false "+as+" != "+bs, Section.CODEGEN);
return false;
}
}
Logger.getLogger("ArgumentTypeEquals").error("true", Section.CODEGEN);
return true;
}
}
// ino.end

View File

@ -1,18 +1,11 @@
package de.dhbwstuttgart.typeinference;
import java.util.LinkedList;
import java.util.List;
import de.dhbwstuttgart.bytecode.ClassGenerator;
import de.dhbwstuttgart.logger.Logger;
import de.dhbwstuttgart.logger.Section;
import de.dhbwstuttgart.syntaxtree.FormalParameter;
import de.dhbwstuttgart.syntaxtree.Method;
import de.dhbwstuttgart.syntaxtree.type.ExtendsWildcardType;
import de.dhbwstuttgart.syntaxtree.type.SuperWildcardType;
import de.dhbwstuttgart.syntaxtree.type.Type;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import de.dhbwstuttgart.typeinference.Menge.Equal;
public class TypeinferenceResults {
@ -25,6 +18,32 @@ public class TypeinferenceResults {
public TypeinferenceResults(Menge<TypeinferenceResultSet> typeReconstructions) {
//TODO: filter
int limit = typeReconstructions.size();
Logger.getLogger("TypeinferenceResults").error(new Integer(limit).toString(), Section.CODEGEN);
for(int i = 0; i < limit; i++){
Logger.getLogger("TypeinferenceResults").error(new Integer(i).toString(), Section.CODEGEN);
for(Pair p: typeReconstructions.get(i).getUnifiedConstraints()){
boolean flag = false;
Logger.getLogger("TypeinferenceResults").error(p.toString(), Section.CODEGEN);
if( p.TA1 instanceof SuperWildcardType ||
p.TA1 instanceof ExtendsWildcardType ||
p.TA2 instanceof SuperWildcardType ||
p.TA2 instanceof ExtendsWildcardType ||
flag){
Logger.getLogger("TypeinferenceResults").error("remove", Section.CODEGEN);
typeReconstructions.remove(i);
i--;
limit--;
flag = true;
}
}
}
this.typeReconstructions = typeReconstructions;
}