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){
return this.getNearestUsedType(toTPH, null);
}
public Type resolveTPH(TypePlaceholder typePlaceholder) {
return resolveTPH(typePlaceholder, 0, null);
return resolveTPH(typePlaceholder, null);
}
public Type resolveTPH(TypePlaceholder typePlaceholder, Integer typeinferenceResultSetIndex) {
return resolveTPH(typePlaceholder, typeinferenceResultSetIndex, null);
}
public Type resolveTPH(TypePlaceholder typePlaceholder, Integer typeinferenceResultSetIndex, Menge<TypePlaceholder> toOneOfTheseTypes) {
return tiResult.getTypeOfPlaceholder(typePlaceholder, typeinferenceResultSetIndex, toOneOfTheseTypes);
public Type resolveTPH(TypePlaceholder typePlaceholder, Menge<TypePlaceholder> toOneOfTheseTypes) {
return tiResult.getTypeOfPlaceholder(typePlaceholder, toOneOfTheseTypes);
}
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);
}
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;
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:
String paramTypesSig = "(";
for(FormalParameter p : parameter){
if(p.getType() instanceof TypePlaceholder){
paramTypesSig += ((TypePlaceholder) p.getType()).getBytecodeSignature(cg, typeReconstructionSetIndex);
}else{
paramTypesSig += p.getType().getBytecodeSignature(cg);
}
paramTypesSig += p.getType().getBytecodeSignature(cg);
Logger.getLogger("MethodGenerator").error(paramTypesSig, Section.CODEGEN);
}
paramTypesSig += ")";
String retTypeSig = "";
if(retType instanceof TypePlaceholder){
retTypeSig = ((TypePlaceholder) retType).getBytecodeSignature(cg, typeReconstructionSetIndex);
}else{
retTypeSig = retType.getBytecodeSignature(cg);
}
String retTypeSig = retType.getBytecodeSignature(cg);
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)
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) {
List<Integer> typeReconstructionSetIndexList = cg.getTypeinferenceResults().getTypeReconstructionSetIndexList(this, cg);
List<Integer> typeReconstructionSetIndexList = cg.getTypeinferenceResults().getTypeReconstructionSetIndexList(this, cg);
for(Integer t: typeReconstructionSetIndexList){
cg.getTypeinferenceResults().setTypeReconstructionSetIndex(t);
DHBWConstantPoolGen _cp = cg.getConstantPool();
InstructionList il = new InstructionList();
Class parentClass = this.getParentClass();
@ -761,11 +763,7 @@ public class Method extends Field implements IItemWithOffset, TypeInsertable
argumentNames = new String[this.parameterlist.size()];
int i = 0;
for(FormalParameter parameter : this.parameterlist){
if(parameter.getType() instanceof TypePlaceholder){
argumentTypes[i] = ((TypePlaceholder) parameter.getType()).getBytecodeType(cg, t);
}else{
argumentTypes[i] = parameter.getType().getBytecodeType(cg);
}
argumentTypes[i] = parameter.getType().getBytecodeType(cg);
argumentNames[i] = parameter.getIdentifier();
i++;
}
@ -773,18 +771,13 @@ public class Method extends Field implements IItemWithOffset, TypeInsertable
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;
if(this.getType() instanceof TypePlaceholder){
returnType = cg.resolveTPH((TypePlaceholder) this.getType(), t);
}else{
returnType = this.getType();
}
Type returnType = this.getType();
//Methode generieren:
MethodGenerator method = new MethodGenerator(constants, returnType.getBytecodeType(cg), 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));
cg.addMethod(method.createMethod(cg, getParameterList(), returnType, get_Block()));
}
}
}

View File

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

View File

@ -17,6 +17,7 @@ import de.dhbwstuttgart.typeinference.Menge.Equal;
public class TypeinferenceResults {
private Menge<TypeinferenceResultSet> typeReconstructions;
private Integer typeReconstructionSetIndex = 0;
public TypeinferenceResults() {
typeReconstructions = new Menge<>();
@ -26,18 +27,22 @@ public class TypeinferenceResults {
this.typeReconstructions = typeReconstructions;
}
public Type getTypeOfPlaceholder(TypePlaceholder typePlaceholder, Integer typeinferenceResultSetIndex, Menge<TypePlaceholder> toOneOfTheseTypes) {
return typeReconstructions.get(typeinferenceResultSetIndex).getTypeOfPlaceholder(typePlaceholder, toOneOfTheseTypes);
public Type getTypeOfPlaceholder(TypePlaceholder typePlaceholder, Menge<TypePlaceholder> toOneOfTheseTypes) {
return typeReconstructions.get(typeReconstructionSetIndex).getTypeOfPlaceholder(typePlaceholder, toOneOfTheseTypes);
}
public Type getTypeOfPlaceholder(TypePlaceholder typePlaceholder, Integer typeinferenceResultSetIndex) {
return getTypeOfPlaceholder(typePlaceholder, typeinferenceResultSetIndex, null);
public Type getTypeOfPlaceholder(TypePlaceholder typePlaceholder) {
return getTypeOfPlaceholder(typePlaceholder, null);
}
public Menge<TypeinferenceResultSet> getTypeReconstructions() {
return typeReconstructions;
}
public void setTypeReconstructionSetIndex(Integer typeReconstructionSetIndex) {
this.typeReconstructionSetIndex = typeReconstructionSetIndex;
}
public List<Integer> getTypeReconstructionSetIndexList(Method method, ClassGenerator cg) {
Menge<FormalParameter> parameters = method.parameterlist.formalparameter;
Menge<TypePlaceholder> typePlaceholders = new Menge<>();
@ -51,10 +56,12 @@ public class TypeinferenceResults {
}
for(int i = 0; i < typeReconstructions.size(); i++){
setTypeReconstructionSetIndex(i);
Type[] reconstructionTypes = new Type[typePlaceholders.size()];
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))){
@ -70,15 +77,10 @@ public class TypeinferenceResults {
class TypeArrayEqual implements Equal<Type[]>{
private ClassGenerator cg;
public TypeArrayEqual(ClassGenerator cg) {
super();
this.cg = cg;
}
@Override
public boolean equal(Type[] a, Type[] b) {
if(a.length != b.length){
@ -86,6 +88,9 @@ class TypeArrayEqual implements Equal<Type[]>{
}
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))){
Logger.getLogger("TypeArrayEqual").error(a[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[] urls = new URL[]{url};
Class integer = classLoader.loadClass("java.lang.Integer");
Integer integer = new Integer(123);
Class[] params = new Class[1];
params[0] = integer;
params[0] = integer.getClass();
Method method = cls.getDeclaredMethod("method", params);
method.invoke(obj, integer.newInstance());
method.invoke(obj, integer);
assertTrue(true);
}catch(Exception e){
throw new RuntimeException(e);