Mehrere Lösungen in einer Classfile

This commit is contained in:
Enrico Schrödter 2015-11-25 10:41:57 +01:00
parent a27b4f9d3b
commit 8ae7bda535
7 changed files with 36 additions and 62 deletions

View File

@ -68,17 +68,14 @@ public class ClassGenerator extends ClassGen{
public org.apache.commons.bcel6.generic.Type getNearestUsedType(TypePlaceholder toTPH){ public org.apache.commons.bcel6.generic.Type getNearestUsedType(TypePlaceholder toTPH){
return this.getNearestUsedType(toTPH, null); return this.getNearestUsedType(toTPH, null);
} }
public Type resolveTPH(TypePlaceholder typePlaceholder) { public Type resolveTPH(TypePlaceholder typePlaceholder) {
return resolveTPH(typePlaceholder, 0, null); return resolveTPH(typePlaceholder, null);
} }
public Type resolveTPH(TypePlaceholder typePlaceholder, Integer typeinferenceResultSetIndex) { public Type resolveTPH(TypePlaceholder typePlaceholder, Menge<TypePlaceholder> toOneOfTheseTypes) {
return resolveTPH(typePlaceholder, typeinferenceResultSetIndex, null); return tiResult.getTypeOfPlaceholder(typePlaceholder, toOneOfTheseTypes);
}
public Type resolveTPH(TypePlaceholder typePlaceholder, Integer typeinferenceResultSetIndex, Menge<TypePlaceholder> toOneOfTheseTypes) {
return tiResult.getTypeOfPlaceholder(typePlaceholder, typeinferenceResultSetIndex, toOneOfTheseTypes);
} }
public String createLambdaMethodName() { public String createLambdaMethodName() {

View File

@ -22,7 +22,7 @@ public class MethodGenerator extends MethodGen{
super(access_flags, return_type, arg_types, arg_names, method_name, class_name, il, cp); super(access_flags, return_type, arg_types, arg_names, method_name, class_name, il, cp);
} }
public Method createMethod(ClassGenerator cg, ParameterList parameter, de.dhbwstuttgart.syntaxtree.type.Type retType, Block block, Integer typeReconstructionSetIndex){ public Method createMethod(ClassGenerator cg, ParameterList parameter, de.dhbwstuttgart.syntaxtree.type.Type retType, Block block){
MethodGen method = this; MethodGen method = this;
DHBWInstructionFactory factory = cg.getInstructionFactory(); DHBWInstructionFactory factory = cg.getInstructionFactory();
@ -42,21 +42,12 @@ public class MethodGenerator extends MethodGen{
//Die korrekte Signatur für die Methode anhängen. Hier sind dann auch die Parameter von RefTypes enthalten: //Die korrekte Signatur für die Methode anhängen. Hier sind dann auch die Parameter von RefTypes enthalten:
String paramTypesSig = "("; String paramTypesSig = "(";
for(FormalParameter p : parameter){ for(FormalParameter p : parameter){
if(p.getType() instanceof TypePlaceholder){ paramTypesSig += p.getType().getBytecodeSignature(cg);
paramTypesSig += ((TypePlaceholder) p.getType()).getBytecodeSignature(cg, typeReconstructionSetIndex);
}else{
paramTypesSig += p.getType().getBytecodeSignature(cg);
}
Logger.getLogger("MethodGenerator").error(paramTypesSig, Section.CODEGEN); Logger.getLogger("MethodGenerator").error(paramTypesSig, Section.CODEGEN);
} }
paramTypesSig += ")"; paramTypesSig += ")";
String retTypeSig = ""; String retTypeSig = retType.getBytecodeSignature(cg);
if(retType instanceof TypePlaceholder){
retTypeSig = ((TypePlaceholder) retType).getBytecodeSignature(cg, typeReconstructionSetIndex);
}else{
retTypeSig = retType.getBytecodeSignature(cg);
}
method.addAttribute(factory.createSignatureAttribute(paramTypesSig+retTypeSig)); method.addAttribute(factory.createSignatureAttribute(paramTypesSig+retTypeSig));

View File

@ -78,7 +78,7 @@ public class Constructor extends Method {
//method.setMaxStack(); //Die Stack Größe automatisch berechnen lassen (erst nach dem alle Instructions angehängt wurden) //method.setMaxStack(); //Die Stack Größe automatisch berechnen lassen (erst nach dem alle Instructions angehängt wurden)
cg.addMethod(method.createMethod(cg, getParameterList(), this.getType(), get_Block(), 0)); cg.addMethod(method.createMethod(cg, getParameterList(), this.getType(), get_Block()));
} }
/** /**

View File

@ -746,9 +746,11 @@ public class Method extends Field implements IItemWithOffset, TypeInsertable
} }
public void genByteCode(ClassGenerator cg) { public void genByteCode(ClassGenerator cg) {
List<Integer> typeReconstructionSetIndexList = cg.getTypeinferenceResults().getTypeReconstructionSetIndexList(this, cg); List<Integer> typeReconstructionSetIndexList = cg.getTypeinferenceResults().getTypeReconstructionSetIndexList(this, cg);
for(Integer t: typeReconstructionSetIndexList){ for(Integer t: typeReconstructionSetIndexList){
cg.getTypeinferenceResults().setTypeReconstructionSetIndex(t);
DHBWConstantPoolGen _cp = cg.getConstantPool(); DHBWConstantPoolGen _cp = cg.getConstantPool();
InstructionList il = new InstructionList(); InstructionList il = new InstructionList();
Class parentClass = this.getParentClass(); Class parentClass = this.getParentClass();
@ -761,11 +763,7 @@ public class Method extends Field implements IItemWithOffset, TypeInsertable
argumentNames = new String[this.parameterlist.size()]; argumentNames = new String[this.parameterlist.size()];
int i = 0; int i = 0;
for(FormalParameter parameter : this.parameterlist){ for(FormalParameter parameter : this.parameterlist){
if(parameter.getType() instanceof TypePlaceholder){ argumentTypes[i] = parameter.getType().getBytecodeType(cg);
argumentTypes[i] = ((TypePlaceholder) parameter.getType()).getBytecodeType(cg, t);
}else{
argumentTypes[i] = parameter.getType().getBytecodeType(cg);
}
argumentNames[i] = parameter.getIdentifier(); argumentNames[i] = parameter.getIdentifier();
i++; i++;
} }
@ -773,18 +771,13 @@ public class Method extends Field implements IItemWithOffset, TypeInsertable
short constants = Constants.ACC_PUBLIC; //Per Definition ist jede Methode public short constants = Constants.ACC_PUBLIC; //Per Definition ist jede Methode public
if(this.modifiers != null && this.modifiers.includesModifier(new Static())) constants += Constants.ACC_STATIC; if(this.modifiers != null && this.modifiers.includesModifier(new Static())) constants += Constants.ACC_STATIC;
Type returnType; Type returnType = this.getType();
if(this.getType() instanceof TypePlaceholder){
returnType = cg.resolveTPH((TypePlaceholder) this.getType(), t);
}else{
returnType = this.getType();
}
//Methode generieren: //Methode generieren:
MethodGenerator method = new MethodGenerator(constants, returnType.getBytecodeType(cg), argumentTypes , argumentNames, this.get_Method_Name(), parentClass.name, il, _cp); MethodGenerator method = new MethodGenerator(constants, returnType.getBytecodeType(cg), argumentTypes , argumentNames, this.get_Method_Name(), parentClass.name, il, _cp);
//Methode generieren und anfügen: //Methode generieren und anfügen:
cg.addMethod(method.createMethod(cg, getParameterList(), returnType, get_Block(), t)); cg.addMethod(method.createMethod(cg, getParameterList(), returnType, get_Block()));
} }
} }
} }

View File

@ -437,15 +437,7 @@ public class TypePlaceholder extends ObjectType
@Override @Override
public org.apache.commons.bcel6.generic.Type getBytecodeType(ClassGenerator cg) { public org.apache.commons.bcel6.generic.Type getBytecodeType(ClassGenerator cg) {
Type resolvedType = cg.resolveTPH(this, 0); Type resolvedType = cg.resolveTPH(this);
if(resolvedType instanceof TypePlaceholder){
return DHBWInstructionFactory.createObjectType();
}
return resolvedType.getBytecodeType(cg);
}
public org.apache.commons.bcel6.generic.Type getBytecodeType(ClassGenerator cg, Integer typeReconstructionSetIndex) {
Type resolvedType = cg.resolveTPH(this, typeReconstructionSetIndex);
if(resolvedType instanceof TypePlaceholder){ if(resolvedType instanceof TypePlaceholder){
return DHBWInstructionFactory.createObjectType(); return DHBWInstructionFactory.createObjectType();
} }
@ -454,11 +446,7 @@ public class TypePlaceholder extends ObjectType
@Override @Override
public String getBytecodeSignature(ClassGenerator cg) { public String getBytecodeSignature(ClassGenerator cg) {
return getBytecodeSignature(cg, 0); Type resolvedType = cg.resolveTPH(this);
}
public String getBytecodeSignature(ClassGenerator cg, Integer typeReconstructionSetIndex) {
Type resolvedType = cg.resolveTPH(this, typeReconstructionSetIndex);
if(resolvedType instanceof TypePlaceholder){ if(resolvedType instanceof TypePlaceholder){
cg.addUsedTPH((TypePlaceholder)resolvedType); cg.addUsedTPH((TypePlaceholder)resolvedType);
return new TypePlaceholderType((TypePlaceholder)resolvedType).getSignature(); return new TypePlaceholderType((TypePlaceholder)resolvedType).getSignature();

View File

@ -17,6 +17,7 @@ import de.dhbwstuttgart.typeinference.Menge.Equal;
public class TypeinferenceResults { public class TypeinferenceResults {
private Menge<TypeinferenceResultSet> typeReconstructions; private Menge<TypeinferenceResultSet> typeReconstructions;
private Integer typeReconstructionSetIndex = 0;
public TypeinferenceResults() { public TypeinferenceResults() {
typeReconstructions = new Menge<>(); typeReconstructions = new Menge<>();
@ -26,18 +27,22 @@ public class TypeinferenceResults {
this.typeReconstructions = typeReconstructions; this.typeReconstructions = typeReconstructions;
} }
public Type getTypeOfPlaceholder(TypePlaceholder typePlaceholder, Integer typeinferenceResultSetIndex, Menge<TypePlaceholder> toOneOfTheseTypes) { public Type getTypeOfPlaceholder(TypePlaceholder typePlaceholder, Menge<TypePlaceholder> toOneOfTheseTypes) {
return typeReconstructions.get(typeinferenceResultSetIndex).getTypeOfPlaceholder(typePlaceholder, toOneOfTheseTypes); return typeReconstructions.get(typeReconstructionSetIndex).getTypeOfPlaceholder(typePlaceholder, toOneOfTheseTypes);
} }
public Type getTypeOfPlaceholder(TypePlaceholder typePlaceholder, Integer typeinferenceResultSetIndex) { public Type getTypeOfPlaceholder(TypePlaceholder typePlaceholder) {
return getTypeOfPlaceholder(typePlaceholder, typeinferenceResultSetIndex, null); return getTypeOfPlaceholder(typePlaceholder, null);
} }
public Menge<TypeinferenceResultSet> getTypeReconstructions() { public Menge<TypeinferenceResultSet> getTypeReconstructions() {
return typeReconstructions; return typeReconstructions;
} }
public void setTypeReconstructionSetIndex(Integer typeReconstructionSetIndex) {
this.typeReconstructionSetIndex = typeReconstructionSetIndex;
}
public List<Integer> getTypeReconstructionSetIndexList(Method method, ClassGenerator cg) { public List<Integer> getTypeReconstructionSetIndexList(Method method, ClassGenerator cg) {
Menge<FormalParameter> parameters = method.parameterlist.formalparameter; Menge<FormalParameter> parameters = method.parameterlist.formalparameter;
Menge<TypePlaceholder> typePlaceholders = new Menge<>(); Menge<TypePlaceholder> typePlaceholders = new Menge<>();
@ -51,10 +56,12 @@ public class TypeinferenceResults {
} }
for(int i = 0; i < typeReconstructions.size(); i++){ for(int i = 0; i < typeReconstructions.size(); i++){
setTypeReconstructionSetIndex(i);
Type[] reconstructionTypes = new Type[typePlaceholders.size()]; Type[] reconstructionTypes = new Type[typePlaceholders.size()];
for(int t = 0; t < typePlaceholders.size(); t++){ for(int t = 0; t < typePlaceholders.size(); t++){
reconstructionTypes[t] = getTypeOfPlaceholder(typePlaceholders.get(t), i); reconstructionTypes[t] = getTypeOfPlaceholder(typePlaceholders.get(t));
} }
if(!types.contains(reconstructionTypes, new TypeArrayEqual(cg))){ if(!types.contains(reconstructionTypes, new TypeArrayEqual(cg))){
@ -70,15 +77,10 @@ public class TypeinferenceResults {
class TypeArrayEqual implements Equal<Type[]>{ class TypeArrayEqual implements Equal<Type[]>{
private ClassGenerator cg; private ClassGenerator cg;
public TypeArrayEqual(ClassGenerator cg) { public TypeArrayEqual(ClassGenerator cg) {
super();
this.cg = cg; this.cg = cg;
} }
@Override @Override
public boolean equal(Type[] a, Type[] b) { public boolean equal(Type[] a, Type[] b) {
if(a.length != b.length){ if(a.length != b.length){
@ -86,6 +88,9 @@ class TypeArrayEqual implements Equal<Type[]>{
} }
for(int i = 0; i < a.length; i++){ for(int i = 0; i < a.length; i++){
Logger.getLogger("TypeArrayEqual").error(a[i].toString(), Section.CODEGEN);
Logger.getLogger("TypeArrayEqual").error(b[i].toString(), Section.CODEGEN);
if(!a[i].getBytecodeType(cg).equals(b[i].getBytecodeType(cg))){ if(!a[i].getBytecodeType(cg).equals(b[i].getBytecodeType(cg))){
Logger.getLogger("TypeArrayEqual").error(a[i].toString(), Section.CODEGEN); Logger.getLogger("TypeArrayEqual").error(a[i].toString(), Section.CODEGEN);
Logger.getLogger("TypeArrayEqual").error(b[i].toString(), Section.CODEGEN); Logger.getLogger("TypeArrayEqual").error(b[i].toString(), Section.CODEGEN);

View File

@ -69,13 +69,13 @@ public class AutoOverloadingTest extends SourceFileBytecodeTest{
URL url = file.toURL(); URL url = file.toURL();
URL[] urls = new URL[]{url}; URL[] urls = new URL[]{url};
Class integer = classLoader.loadClass("java.lang.Integer"); Integer integer = new Integer(123);
Class[] params = new Class[1]; Class[] params = new Class[1];
params[0] = integer; params[0] = integer.getClass();
Method method = cls.getDeclaredMethod("method", params); Method method = cls.getDeclaredMethod("method", params);
method.invoke(obj, integer.newInstance()); method.invoke(obj, integer);
assertTrue(true); assertTrue(true);
}catch(Exception e){ }catch(Exception e){
throw new RuntimeException(e); throw new RuntimeException(e);