Fehler durch Änderung von Unify beheben. Merge mit bytecode

This commit is contained in:
JanUlrich 2015-11-13 13:11:10 +01:00
commit c646a34745
17 changed files with 293 additions and 121 deletions

View File

@ -32,7 +32,7 @@ public class JavaToBCEL {
new BCELifier(new ClassParser(rootDirectory+"LocalVarAccess.class").parse(), new FileOutputStream(new File(rootDirectory+"LocalVarAccessCreator.java"))).start();
new BCELifier(new ClassParser(rootDirectory+"Wildcard.class").parse(), new FileOutputStream(new File(rootDirectory+"WildcardCreator.java"))).start();
new BCELifier(new ClassParser(rootDirectory+"BooleanValue.class").parse(), new FileOutputStream(new File(rootDirectory+"BooleanValueCreator.java"))).start();
new BCELifier(new ClassParser(rootDirectory+"NewClass.class").parse(), new FileOutputStream(new File(rootDirectory+"NewClass.java"))).start();
new BCELifier(new ClassParser(rootDirectory+"NewClass.class").parse(), new FileOutputStream(new File(rootDirectory+"NewClassCreator.java"))).start();
} catch (ClassFormatException | IOException e) {
e.printStackTrace();

View File

@ -1,60 +1,10 @@
package bcelifier;
import org.apache.commons.bcel6.generic.*;
import org.apache.commons.bcel6.classfile.*;
import org.apache.commons.bcel6.*;
import java.io.*;
public class NewClassCreator implements Constants {
private InstructionFactory _factory;
private ConstantPoolGen _cp;
private ClassGen _cg;
public NewClassCreator() {
_cg = new ClassGen("bcelifier.NewClass", "java.lang.Object", "NewClass.java", ACC_SUPER, new String[] { });
_cp = _cg.getConstantPool();
_factory = new InstructionFactory(_cg, _cp);
}
public void create(OutputStream out) throws IOException {
createMethod_0();
createMethod_1();
_cg.getJavaClass().dump(out);
}
private void createMethod_0() {
InstructionList il = new InstructionList();
MethodGen method = new MethodGen(0, Type.VOID, Type.NO_ARGS, new String[] { }, "<init>", "bcelifier.NewClass", il, _cp);
InstructionHandle ih_0 = il.append(_factory.createLoad(Type.OBJECT, 0));
il.append(_factory.createInvoke("java.lang.Object", "<init>", Type.VOID, Type.NO_ARGS, Constants.INVOKESPECIAL));
InstructionHandle ih_4 = il.append(_factory.createReturn(Type.VOID));
method.setMaxStack();
method.setMaxLocals();
_cg.addMethod(method.getMethod());
il.dispose();
}
private void createMethod_1() {
InstructionList il = new InstructionList();
MethodGen method = new MethodGen(0, Type.STRING, new Type[] { Type.STRING }, new String[] { "arg0" }, "method", "bcelifier.NewClass", il, _cp);
InstructionHandle ih_0 = il.append(_factory.createNew("java.lang.String"));
il.append(InstructionConstants.DUP);
il.append(_factory.createLoad(Type.OBJECT, 1));
il.append(_factory.createInvoke("java.lang.String", "<init>", Type.VOID, new Type[] { Type.STRING }, Constants.INVOKESPECIAL));
il.append(_factory.createStore(Type.OBJECT, 2));
InstructionHandle ih_9 = il.append(_factory.createLoad(Type.OBJECT, 2));
InstructionHandle ih_10 = il.append(_factory.createReturn(Type.OBJECT));
method.setMaxStack();
method.setMaxLocals();
_cg.addMethod(method.getMethod());
il.dispose();
}
public static void main(String[] args) throws Exception {
bcelifier.NewClassCreator creator = new bcelifier.NewClassCreator();
creator.create(new FileOutputStream("bcelifier.NewClass.class"));
}
public class NewClass {
public NewClass(Integer i){}
void methode2(){
new NewClass(1);
}
}

View File

@ -0,0 +1,60 @@
package bcelifier;
import org.apache.commons.bcel6.generic.*;
import org.apache.commons.bcel6.classfile.*;
import org.apache.commons.bcel6.*;
import java.io.*;
public class NewClassCreator implements Constants {
private InstructionFactory _factory;
private ConstantPoolGen _cp;
private ClassGen _cg;
public NewClassCreator() {
_cg = new ClassGen("bcelifier.NewClass", "java.lang.Object", "NewClass.java", ACC_PUBLIC | ACC_SUPER, new String[] { });
_cp = _cg.getConstantPool();
_factory = new InstructionFactory(_cg, _cp);
}
public void create(OutputStream out) throws IOException {
createMethod_0();
createMethod_1();
_cg.getJavaClass().dump(out);
}
private void createMethod_0() {
InstructionList il = new InstructionList();
MethodGen method = new MethodGen(ACC_PUBLIC, Type.VOID, new Type[] { new ObjectType("java.lang.Integer") }, new String[] { "arg0" }, "<init>", "bcelifier.NewClass", il, _cp);
InstructionHandle ih_0 = il.append(_factory.createLoad(Type.OBJECT, 0));
il.append(_factory.createInvoke("java.lang.Object", "<init>", Type.VOID, Type.NO_ARGS, Constants.INVOKESPECIAL));
InstructionHandle ih_4 = il.append(_factory.createReturn(Type.VOID));
method.setMaxStack();
method.setMaxLocals();
_cg.addMethod(method.getMethod());
il.dispose();
}
private void createMethod_1() {
InstructionList il = new InstructionList();
MethodGen method = new MethodGen(0, Type.VOID, Type.NO_ARGS, new String[] { }, "methode2", "bcelifier.NewClass", il, _cp);
InstructionHandle ih_0 = il.append(_factory.createNew("bcelifier.NewClass"));
il.append(InstructionConstants.DUP);
il.append(new PUSH(_cp, 1));
il.append(_factory.createInvoke("java.lang.Integer", "valueOf", new ObjectType("java.lang.Integer"), new Type[] { Type.INT }, Constants.INVOKESTATIC));
il.append(_factory.createInvoke("bcelifier.NewClass", "<init>", Type.VOID, new Type[] { new ObjectType("java.lang.Integer") }, Constants.INVOKESPECIAL));
il.append(InstructionConstants.POP);
InstructionHandle ih_12 = il.append(_factory.createReturn(Type.VOID));
method.setMaxStack();
method.setMaxLocals();
_cg.addMethod(method.getMethod());
il.dispose();
}
public static void main(String[] args) throws Exception {
bcelifier.NewClassCreator creator = new bcelifier.NewClassCreator();
creator.create(new FileOutputStream("bcelifier.NewClass.class"));
}
}

View File

@ -1,39 +1,26 @@
// ino.module.ArgumentList.8621.package
package de.dhbwstuttgart.syntaxtree.statement;
// ino.end
// ino.module.ArgumentList.8621.import
import java.util.Iterator;
import de.dhbwstuttgart.typeinference.Menge;
import java.util.Iterator;
import org.apache.commons.bcel6.generic.InstructionList;
import de.dhbwstuttgart.typeinference.Menge;
import de.dhbwstuttgart.bytecode.ClassGenerator;
import de.dhbwstuttgart.myexception.JVMCodeException;
import de.dhbwstuttgart.syntaxtree.FormalParameter;
import de.dhbwstuttgart.syntaxtree.SyntaxTreeNode;
import de.dhbwstuttgart.typeinference.JavaCodeResult;
import de.dhbwstuttgart.typeinference.ResultSet;
// ino.class.ArgumentList.24911.declaration
public class ArgumentList extends SyntaxTreeNode
// ino.end
// ino.class.ArgumentList.24911.body
{
// ino.attribute.expr.24914.declaration
public Menge<Expr> expr = new Menge<Expr>();
// ino.end
// ino.method.get_firstElement.24923.definition
public Object get_firstElement()
// ino.end
// ino.method.get_firstElement.24923.body
{
return expr.firstElement();
}
// ino.end
/**
* @author Andreas Stadelmeier, a10023
@ -93,6 +80,22 @@ public class ArgumentList extends SyntaxTreeNode
public Menge<? extends SyntaxTreeNode> getChildren() {
return expr;
}
public InstructionList generateBytecode(ClassGenerator cg){
InstructionList ret = new InstructionList();
for(Expr e : expr){
ret.append(e.genByteCode(cg));
}
return ret;
}
public org.apache.commons.bcel6.generic.Type[] getBytecodeTypeList(ClassGenerator cg){
org.apache.commons.bcel6.generic.Type[] ret = new org.apache.commons.bcel6.generic.Type[expr.size()];
int i = 0;
for(Expr e : expr){
ret[i] = e.getType().getBytecodeType(cg);
i++;
}
return ret;
}
}
// ino.end

View File

@ -9,10 +9,10 @@ import java.util.Iterator;
import org.apache.commons.bcel6.Constants;
import org.apache.commons.bcel6.generic.ClassGen;
import org.apache.commons.bcel6.generic.InstructionConstants;
import org.apache.commons.bcel6.generic.InstructionFactory;
import org.apache.commons.bcel6.generic.InstructionHandle;
import org.apache.commons.bcel6.generic.InstructionList;
import org.apache.commons.bcel6.generic.NEW;
import org.apache.commons.bcel6.generic.ObjectType;
import de.dhbwstuttgart.typeinference.Menge;
import de.dhbwstuttgart.bytecode.ClassGenerator;
@ -60,6 +60,7 @@ public class NewClass extends Expr
// ino.attribute.arglist.25837.declaration
private ArgumentList arglist;
private boolean isStatement = false;
// ino.end
// ino.attribute.parserlog.25840.declaration
protected static Logger parserlog = Logger.getLogger("parser");
@ -134,15 +135,7 @@ public class NewClass extends Expr
//TODO: Das hier noch vervollständigen
ConstraintsSet ret = new ConstraintsSet();
UndConstraint callConstraints = new UndConstraint();
//Die Auskommentierten Zeilen gehÃren zu MethodRefNew
//Menge<Type> argumentTypeList = new Menge<Type>();
//for(Expr expr : this.arglist.expr){
// argumentTypeList.add(expr.getTypeVariable());
//}
//FunN funN= new FunN(null,argumentTypeList);
//Constraint newClassTypeConstraint = new Constraint(null,null);
//ret.add(newClassTypeConstraint);
int numArgs = 0;
if(this.arglist != null)numArgs = this.arglist.size();
ConstructorAssumption cA = assumptions.getConstructorAssumption(this.get_Name(), numArgs);
@ -152,22 +145,9 @@ public class NewClass extends Expr
ret.add(this.arglist.expr.elementAt(i).TYPEExpr(assumptions));
callConstraints.addConstraint( this.arglist.expr.elementAt(i).getType().TYPE(assumptions, this), cA.getParameterType(i).TYPE(assumptions, this));
}
//if(this.arglist != null && this.arglist.expr != null)for(Expr arg : this.arglist.expr){
// ret.add(arg.TYPEExpr(assumptions));
//}
Type thisT = assumptions.checkType(new RefType(this.get_Name(),this,0), (SyntaxTreeNode)this);
this.setType(thisT);
/*
//Ein new-Aufruf ist nichts anderes als ein MethodCall der Methode <init>
MethodCall newAufruf = new MethodCall(0,0);
this.setType(assumptions.getTypeFor(new RefType(this.get_Name(),0)));
newAufruf.type = this.getType();
newAufruf.set_Name("<init>");
newAufruf.set_Receiver(null);
ret.add(new Overloading(assumptions, newAufruf, this.getType()).generateConsstraints());
*/
return ret;
}
@ -181,6 +161,7 @@ public class NewClass extends Expr
public ConstraintsSet TYPEStmt(TypeAssumptions assumptions){
ConstraintsSet ret = this.TYPEExpr(assumptions); //TypeExpr aufrufen
this.setType(new Void(this,0)); //Typ des Statments auf Void setzen.
this.isStatement = true;
return ret;
}
@ -213,18 +194,21 @@ public class NewClass extends Expr
@Override
public InstructionList genByteCode(ClassGenerator _cg) {
//TODO bytecode
InstructionFactory _factory = _cg.getInstructionFactory();
InstructionList il = new InstructionList();
String name = type.get_Name();
il.append(_factory.createNew(name));
il.append(InstructionConstants.DUP);
il.append(_factory.createInvoke(name, "<init>", org.apache.commons.bcel6.generic.Type.VOID, org.apache.commons.bcel6.generic.Type.NO_ARGS, Constants.INVOKESPECIAL));
//il.append(InstructionConstants.POP);
return il;
if(arglist!=null){
il.append(arglist.generateBytecode(_cg));
il.append(_cg.getInstructionFactory().createInvoke(this.get_Name(), "<init>",
org.apache.commons.bcel6.generic.Type.VOID,
this.arglist.getBytecodeTypeList(_cg), Constants.INVOKESPECIAL));
}else{
il.append(_cg.getInstructionFactory().createInvoke(this.get_Name(), "<init>",
org.apache.commons.bcel6.generic.Type.VOID,
new org.apache.commons.bcel6.generic.Type[]{}, Constants.INVOKESPECIAL));
}
if(this.isStatement){
il.append(InstructionConstants.POP);
}
return il;
}

View File

@ -842,8 +842,8 @@ public class RefType extends ObjectType implements IMatchable
cg.addExtraClass(generatedClass.genByteCode(new TypeinferenceResults()).getByteCode());
}
return "L"+combinedType+";";
String ret = new org.apache.commons.bcel6.generic.ObjectType(combinedType).getSignature();
return ret;
}
public String getCombinedType(ClassGenerator cg){

View File

@ -1,16 +1,20 @@
//otth/pluemicke2.1.jav funktioniert nicht xxx anschauen
// ino.module.Unify.8721.package
package de.dhbwstuttgart.typeinference.unify;
import java.util.Collection;
// ino.end
// ino.module.Unify.8721.import
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.function.Function;
import java.util.stream.Stream;
import de.dhbwstuttgart.logger.Logger;
import de.dhbwstuttgart.logger.Section;
import de.dhbwstuttgart.logger.SectionLogger;
import de.dhbwstuttgart.logger.Timestamp;
import de.dhbwstuttgart.logger.Timewatch;
import de.dhbwstuttgart.core.MyCompiler;
import de.dhbwstuttgart.myexception.CTypeReconstructionException;
import de.dhbwstuttgart.myexception.MatchException;
@ -30,6 +34,7 @@ import de.dhbwstuttgart.syntaxtree.type.Type;
import de.dhbwstuttgart.syntaxtree.type.ObjectType;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import de.dhbwstuttgart.syntaxtree.type.WildcardType;
import de.dhbwstuttgart.typeinference.DeepCloneable;
import de.dhbwstuttgart.typeinference.Menge;
import de.dhbwstuttgart.typeinference.Pair;
import de.dhbwstuttgart.typeinference.Pair.PairOperator;
@ -3489,6 +3494,15 @@ tempKlasse.get_Superclass_Name() ); System.out.println( "P. S.:
return T instanceof FreshWildcardType;
}
public static <A extends DeepCloneable> Menge<A> deepClone(Menge<A> m){
Timewatch watch = Timewatch.getTimewatch();
Timestamp timer = watch.start("Unify - deepClone");
Menge<A> ret = m.stream().<A>map((Function<A,A>)(x -> x.deepClone())).<Menge<A>>collect(Menge::new, Menge::add, Menge::addAll);
timer.stop();
return ret;
}
}
// ino.end

10
test/bytecode/Binary.jav Normal file
View File

@ -0,0 +1,10 @@
class Binary{
void method() {
a;
a = 20;
b;
b=59 + a;
}
}

View File

@ -0,0 +1,8 @@
class Binary2 {
Integer m(Integer x) { return x + x; }
Boolean m(Boolean x) {return x || x; }
}

View File

@ -0,0 +1,34 @@
package bytecode;
import static org.junit.Assert.*;
import java.io.File;
import java.io.IOException;
import junit.framework.TestCase;
import org.junit.Test;
import plugindevelopment.TypeInsertTester;
import de.dhbwstuttgart.core.MyCompiler;
import de.dhbwstuttgart.core.MyCompilerAPI;
import de.dhbwstuttgart.logger.LoggerConfiguration;
import de.dhbwstuttgart.logger.Section;
import de.dhbwstuttgart.parser.JavaParser.yyException;
import de.dhbwstuttgart.typeinference.ByteCodeResult;
import de.dhbwstuttgart.typeinference.Menge;
import de.dhbwstuttgart.typeinference.TypeinferenceResultSet;
import de.dhbwstuttgart.typeinference.typedeployment.TypeInsertSet;
public class BinaryTest {
public final static String rootDirectory = System.getProperty("user.dir")+"/test/bytecode/";
public final static String testFile = "Binary.jav";
public final static String outputFile = "Binary.class";
@Test
public void test() {
SingleClassTester.compileToBytecode(rootDirectory+testFile, rootDirectory+outputFile);
}
}

View File

@ -0,0 +1,34 @@
package bytecode;
import static org.junit.Assert.*;
import java.io.File;
import java.io.IOException;
import junit.framework.TestCase;
import org.junit.Test;
import plugindevelopment.TypeInsertTester;
import de.dhbwstuttgart.core.MyCompiler;
import de.dhbwstuttgart.core.MyCompilerAPI;
import de.dhbwstuttgart.logger.LoggerConfiguration;
import de.dhbwstuttgart.logger.Section;
import de.dhbwstuttgart.parser.JavaParser.yyException;
import de.dhbwstuttgart.typeinference.ByteCodeResult;
import de.dhbwstuttgart.typeinference.Menge;
import de.dhbwstuttgart.typeinference.TypeinferenceResultSet;
import de.dhbwstuttgart.typeinference.typedeployment.TypeInsertSet;
public class BinaryTest2 {
public final static String rootDirectory = System.getProperty("user.dir")+"/test/bytecode/";
public final static String testFile = "Binary2.jav";
public final static String outputFile = "Binary2.class";
@Test
public void test() {
SingleClassTester.compileToBytecode(rootDirectory+testFile, rootDirectory);
}
}

View File

@ -0,0 +1,10 @@
class NewStatement{
public NewStatement(Integer i){}
public NewStatement(String i){}
method() {
NewStatement n;
n = new NewStatement("hallo");
n = new NewStatement(1);
}
}

View File

@ -0,0 +1,55 @@
package bytecode;
import static org.junit.Assert.*;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import junit.framework.TestCase;
import org.junit.Test;
import plugindevelopment.TypeInsertTester;
import de.dhbwstuttgart.core.MyCompiler;
import de.dhbwstuttgart.core.MyCompilerAPI;
import de.dhbwstuttgart.logger.LoggerConfiguration;
import de.dhbwstuttgart.logger.Section;
import de.dhbwstuttgart.parser.JavaParser.yyException;
import de.dhbwstuttgart.typeinference.ByteCodeResult;
import de.dhbwstuttgart.typeinference.Menge;
import de.dhbwstuttgart.typeinference.TypeinferenceResultSet;
import de.dhbwstuttgart.typeinference.typedeployment.TypeInsertSet;
public class NewStatementTest {
public final static String rootDirectory = System.getProperty("user.dir")+"/test/bytecode/";
public final static String testFile = "NewStatement.jav";
public final static String outputFile = "NewStatement.class";
@Test
public void test() {
SingleClassTester.compileToBytecode(rootDirectory+testFile, rootDirectory);
}
@Test
public void testUntypedVectorDeclaredMethods() {
try{
File file = new File(System.getProperty("user.dir")+"/test/bytecode/types/");
URL url = file.toURL();
URL[] urls = new URL[]{url};
ClassLoader classLoader = new URLClassLoader(urls);
Class untypedVectorTest = classLoader.loadClass("UntypedVector");
for(Method method: untypedVectorTest.getDeclaredMethods()){
System.out.println(method.toGenericString());
}
}catch(Exception e){
throw new RuntimeException(e);
}
}
}

6
test/bytecode/Test2.java Normal file
View File

@ -0,0 +1,6 @@
class Test2{
public static void main(java.lang.String[] args){
new NewStatement(1).method();
}
}

View File

@ -20,7 +20,6 @@ public class OverloadingTest extends SourceFileBytecodeTest{
}
@Test
public void testString() {
try{

View File

@ -1,3 +1,4 @@
class LambdaTest{
String var;
@ -7,4 +8,4 @@ op = () -> (f) -> {
return var;
};
}
}

View File

@ -14,6 +14,7 @@ import de.dhbwstuttgart.logger.Section;
import de.dhbwstuttgart.logger.SectionLogger;
import de.dhbwstuttgart.logger.Timewatch;
import de.dhbwstuttgart.parser.JavaParser.yyException;
import de.dhbwstuttgart.syntaxtree.SourceFile;
import de.dhbwstuttgart.typeinference.TypeinferenceResultSet;
import de.dhbwstuttgart.typeinference.typedeployment.TypeInsertPoint;
import de.dhbwstuttgart.typeinference.typedeployment.TypeInsertSet;
@ -46,8 +47,10 @@ public class MultipleTypesInsertTester extends TypeInsertTester{
MyCompilerAPI compiler = MyCompiler.getAPI(logConfig);
try {
compiler.parse(new File(rootDirectory + sourceFileToInfere));
Menge<TypeinferenceResultSet> results = compiler.typeReconstruction();
SourceFile parsedFile = compiler.parse(new File(rootDirectory + sourceFileToInfere));
Menge<SourceFile> parsedFiles = new Menge<>();
parsedFiles.add(parsedFile);
Menge<TypeinferenceResultSet> results = compiler.typeReconstruction(parsedFiles);
System.out.println("Typinferenz ausgeführt!");
//TestCase.assertTrue("Es darf nicht mehr als eine Lösungsmöglichkeit geben und nicht "+results.size(), results.size()==1);
for(TypeinferenceResultSet result : results){
@ -78,8 +81,9 @@ public class MultipleTypesInsertTester extends TypeInsertTester{
String inferedSource = "";
MyCompilerAPI compiler = MyCompiler.getAPI(logConfig);
try {
compiler.parse(new File(rootDirectory + sourceFileToInfere));
Menge<TypeinferenceResultSet> results = compiler.typeReconstruction();
Menge<SourceFile> parsedSource = new Menge<>();
parsedSource.add(compiler.parse(new File(rootDirectory + sourceFileToInfere)));
Menge<TypeinferenceResultSet> results = compiler.typeReconstruction(parsedSource);
//TestCase.assertTrue("Es darf nicht mehr als eine Lösungsmöglichkeit geben und nicht "+results.size(), results.size()==1);
for(TypeinferenceResultSet result : results){
TypeInsertSet point = result.getTypeInsertionPoints();