From 3c80cb275b2cac5c7607a9a0625ce5130872097c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enrico=20Schr=C3=B6dter?= Date: Fri, 27 Nov 2015 18:10:46 +0100 Subject: [PATCH] Doppelte Methodengenerierungen verhindert --- src/de/dhbwstuttgart/syntaxtree/Method.java | 57 ++++++++++++++----- .../typeinference/TypeinferenceResults.java | 33 ++++++++--- 2 files changed, 70 insertions(+), 20 deletions(-) diff --git a/src/de/dhbwstuttgart/syntaxtree/Method.java b/src/de/dhbwstuttgart/syntaxtree/Method.java index 64d42c960..9ed039ae4 100755 --- a/src/de/dhbwstuttgart/syntaxtree/Method.java +++ b/src/de/dhbwstuttgart/syntaxtree/Method.java @@ -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 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{ + + @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 diff --git a/src/de/dhbwstuttgart/typeinference/TypeinferenceResults.java b/src/de/dhbwstuttgart/typeinference/TypeinferenceResults.java index 26ebd73ec..b2486c2cd 100644 --- a/src/de/dhbwstuttgart/typeinference/TypeinferenceResults.java +++ b/src/de/dhbwstuttgart/typeinference/TypeinferenceResults.java @@ -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 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; }