Merge branch 'bigRefactoring'

This commit is contained in:
JanUlrich 2017-10-10 15:51:13 +02:00
commit c442e2ac17
164 changed files with 4196 additions and 16816 deletions

View File

@ -1,22 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="test"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="lib" path="lib/bcel-6.1-SNAPSHOT.jar" sourcepath="lib/bcel-6.1-SNAPSHOT-sources.jar"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
<classpathentry kind="lib" path="lib/antlr-complete.jar"/>
<classpathentry kind="lib" path="lib/annotations-2.0.1.jar"/>
<classpathentry kind="lib" path="lib/bcel-6.1-SNAPSHOT-sources.jar"/>
<classpathentry kind="lib" path="lib/cloning.jar"/>
<classpathentry kind="lib" path="lib/commons-bcel6-6.0-SNAPSHOT.jar"/>
<classpathentry kind="lib" path="lib/javassist-3.19.0-GA.jar"/>
<classpathentry kind="lib" path="lib/junit-4.0.jar"/>
<classpathentry kind="lib" path="lib/log4j-1.2.12.jar"/>
<classpathentry kind="lib" path="lib/reflections-0.9.10-javadoc.jar"/>
<classpathentry kind="lib" path="lib/reflections-0.9.10-sources.jar"/>
<classpathentry kind="lib" path="lib/reflections-0.9.10.jar" sourcepath="/reflections/src"/>
<classpathentry kind="lib" path="lib/guava-22.0.jar" sourcepath="lib/guava-22.0-sources.jar"/>
<classpathentry kind="lib" path="lib/guava-15.0.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>

10
.gitignore vendored
View File

@ -10,3 +10,13 @@ bin
*.jar
*.war
*.ear
# IDEs
.classpath
*.iml
.idea/
/target/
.DS_Store
.project
.settings/

View File

@ -1,22 +0,0 @@
<component name="libraryTable">
<library name="lib">
<CLASSES>
<root url="jar://$PROJECT_DIR$/lib/log4j-1.2.12.jar!/" />
<root url="jar://$PROJECT_DIR$/lib/guava-10.0.1.jar!/" />
<root url="jar://$PROJECT_DIR$/lib/bcel-6.1-SNAPSHOT.jar!/" />
<root url="jar://$PROJECT_DIR$/lib/bcel-6.1-SNAPSHOT-sources.jar!/" />
<root url="jar://$PROJECT_DIR$/lib/annotations-2.0.1.jar!/" />
<root url="jar://$PROJECT_DIR$/lib/guava-15.0.jar!/" />
<root url="jar://$PROJECT_DIR$/lib/junit-4.0.jar!/" />
<root url="jar://$PROJECT_DIR$/lib/commons-bcel6-6.0-SNAPSHOT.jar!/" />
<root url="jar://$PROJECT_DIR$/lib/reflections-0.9.10-javadoc.jar!/" />
<root url="jar://$PROJECT_DIR$/lib/reflections-0.9.10.jar!/" />
<root url="jar://$PROJECT_DIR$/lib/cloning.jar!/" />
<root url="jar://$PROJECT_DIR$/lib/javassist-3.19.0-GA.jar!/" />
<root url="jar://$PROJECT_DIR$/lib/reflections-0.9.10-sources.jar!/" />
<root url="jar://$PROJECT_DIR$/lib/antlr-complete.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</component>

View File

@ -1,17 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>JavaCompilerCore</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>

View File

@ -1,12 +0,0 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.8

View File

@ -1,9 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

View File

@ -1,9 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

30
pom.xml
View File

@ -31,6 +31,11 @@
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
<version>0.9.11</version>
</dependency>
<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm-all</artifactId>
<version>[4.0.0,)</version>
</dependency>
</dependencies>
@ -41,7 +46,30 @@
<testOutputDirectory>target/test-classes</testOutputDirectory>
<sourceDirectory>src/</sourceDirectory>
<testSourceDirectory>test/</testSourceDirectory>
</build>
<plugins>
<plugin>
<groupId>org.antlr</groupId>
<artifactId>antlr4-maven-plugin</artifactId>
<version>4.7</version>
<executions>
<execution>
<id>antlr</id>
<goals>
<goal>antlr4</goal>
</goals>
<configuration>
<sourceDirectory>src/de/dhbwstuttgart/parser/antlr/</sourceDirectory>
<outputDirectory>src/de/dhbwstuttgart/parser/antlr/</outputDirectory>
<arguments>
<argument>-package</argument>
<argument>de.dhbwstuttgart.parser.antlr</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>

View File

@ -1,11 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

BIN
src/de/dhbwstuttgart/.DS_Store vendored Normal file

Binary file not shown.

View File

@ -0,0 +1,386 @@
package de.dhbwstuttgart.bytecode;
import java.awt.List;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.FieldVisitor;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import de.dhbwstuttgart.parser.SyntaxTreeGenerator.AssignToLocal;
import de.dhbwstuttgart.syntaxtree.*;
import de.dhbwstuttgart.syntaxtree.statement.ArgumentList;
import de.dhbwstuttgart.syntaxtree.statement.Assign;
import de.dhbwstuttgart.syntaxtree.statement.AssignToField;
import de.dhbwstuttgart.syntaxtree.statement.Binary;
import de.dhbwstuttgart.syntaxtree.statement.Block;
import de.dhbwstuttgart.syntaxtree.statement.CastExpr;
import de.dhbwstuttgart.syntaxtree.statement.DoStmt;
import de.dhbwstuttgart.syntaxtree.statement.EmptyStmt;
import de.dhbwstuttgart.syntaxtree.statement.FieldVar;
import de.dhbwstuttgart.syntaxtree.statement.ForStmt;
import de.dhbwstuttgart.syntaxtree.statement.IfStmt;
import de.dhbwstuttgart.syntaxtree.statement.InstanceOf;
import de.dhbwstuttgart.syntaxtree.statement.LambdaExpression;
import de.dhbwstuttgart.syntaxtree.statement.LocalVar;
import de.dhbwstuttgart.syntaxtree.statement.LocalVarDecl;
import de.dhbwstuttgart.syntaxtree.statement.MethodCall;
import de.dhbwstuttgart.syntaxtree.statement.NewArray;
import de.dhbwstuttgart.syntaxtree.statement.NewClass;
import de.dhbwstuttgart.syntaxtree.statement.Receiver;
import de.dhbwstuttgart.syntaxtree.statement.Return;
import de.dhbwstuttgart.syntaxtree.statement.ReturnVoid;
import de.dhbwstuttgart.syntaxtree.statement.StaticClassName;
import de.dhbwstuttgart.syntaxtree.statement.Super;
import de.dhbwstuttgart.syntaxtree.statement.SuperCall;
import de.dhbwstuttgart.syntaxtree.statement.This;
import de.dhbwstuttgart.syntaxtree.statement.UnaryPlus;
import de.dhbwstuttgart.syntaxtree.statement.WhileStmt;
import de.dhbwstuttgart.syntaxtree.statement.literal.Literal;
import de.dhbwstuttgart.syntaxtree.statement.literal.Null;
import de.dhbwstuttgart.syntaxtree.type.ExtendsWildcardType;
import de.dhbwstuttgart.syntaxtree.type.GenericRefType;
import de.dhbwstuttgart.syntaxtree.type.RefType;
import de.dhbwstuttgart.syntaxtree.type.SuperWildcardType;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
public class BytecodeGen implements ASTVisitor {
ClassWriter cw =new ClassWriter(ClassWriter.COMPUTE_FRAMES|ClassWriter.COMPUTE_MAXS);
// String methDesc;
String type;
String className;
// stores parameter, local vars and the next index on the local variable table, which use for aload_i, astore_i,...
HashMap<String, Integer> paramsAndLocals;// = new HashMap<>();
byte[] bytecode;
HashMap<String,byte[]> classFiles;
public BytecodeGen(HashMap<String,byte[]> classFiles) {
this.classFiles = classFiles;
paramsAndLocals = new HashMap<>();
}
@Override
public void visit(SourceFile sourceFile) {
for(ClassOrInterface cl : sourceFile.getClasses()) {
BytecodeGen classGen = new BytecodeGen(classFiles);
cl.accept(classGen);
classGen.writeClass(cl.getClassName().toString());
}
}
private void writeClass(String name) {
bytecode = cw.toByteArray();
classFiles.put(name, bytecode);
}
public HashMap<String,byte[]> getClassFiles() {
return classFiles;
}
@Override
public void visit(ClassOrInterface classOrInterface) {
className = classOrInterface.getClassName().toString();
// access flages??
cw.visit(Opcodes.V1_8, classOrInterface.getModifiers()+Opcodes.ACC_SUPER, classOrInterface.getClassName().toString()
, null, classOrInterface.getSuperClass().toString(), null);
// for each field in the class
for(Field f : classOrInterface.getFieldDecl()) {
System.out.println("get Fields");
System.out.println(f.getName());
f.accept(this);
}
for(Constructor c : classOrInterface.getConstructors()) {
c.accept(this);
}
for(Method m : classOrInterface.getMethods()) {
m.accept(this);
}
cw.visitSource(classOrInterface.getClassName().toString()+".jav", null);
}
@Override
public void visit(Constructor field) {
Descriptor desc = new Descriptor(field);
MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", desc.getDesc(), null, null);
mv.visitCode();
System.out.println("-----Constructor-----");
BytecodeGenMethod gen = new BytecodeGenMethod(className,field, mv,paramsAndLocals,desc.getDesc(),cw);
mv.visitInsn(Opcodes.RETURN);
mv.visitMaxs(0, 0);
mv.visitEnd();
}
@Override
public void visit(Method method) {
method.getParameterList().accept(this);
Descriptor methDesc = new Descriptor(method);
System.out.println("-----Method-----");
MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, method.getName(), methDesc.getDesc(), null, null);
mv.visitCode();
BytecodeGenMethod gen = new BytecodeGenMethod(className,method, mv,paramsAndLocals,methDesc.getDesc(),cw);
mv.visitMaxs(0, 0);
mv.visitEnd();
}
@Override
public void visit(ParameterList formalParameters) {
Iterator<FormalParameter> itr = formalParameters.iterator();
int i = 1;
while(itr.hasNext()) {
FormalParameter fp = itr.next();
paramsAndLocals.put(fp.getName(), i);
fp.accept(this);
i++;
}
}
@Override
public void visit(FormalParameter formalParameter) {
formalParameter.getType().accept(this);
}
@Override
public void visit(RefType refType) {
type = "L"+refType.toString()+";";
}
@Override
public void visit(SuperWildcardType superWildcardType) {
// TODO Auto-generated method stub
}
@Override
public void visit(TypePlaceholder typePlaceholder) {
// TODO Auto-generated method stub
}
@Override
public void visit(ExtendsWildcardType extendsWildcardType) {
// TODO Auto-generated method stub
}
@Override
public void visit(GenericRefType genericRefType) {
// TODO Auto-generated method stub
}
// ??
@Override
public void visit(FieldVar fieldVar) {
System.out.println("in fieldvar");
// cw.newField(fieldVar.receiver.toString(), fieldVar.fieldVarName.toString(), fieldVar.getType().toString());
FieldVisitor fv = cw.visitField(Opcodes.ACC_PRIVATE, fieldVar.fieldVarName, "L"+fieldVar.getType()+";", null, null);
fv.visitEnd();
}
// access flages?? modifiers
@Override
public void visit(Field field) {
System.out.println("in field");
FieldVisitor fv = cw.visitField(Opcodes.ACC_PRIVATE, field.getName(), "L"+field.getType().toString().replace(".", "/")+";", null, null);
fv.visitEnd();
}
@Override
public void visit(LambdaExpression lambdaExpression) {
// TODO Auto-generated method stub
}
@Override
public void visit(Assign assign) {
// TODO Auto-generated method stub
}
@Override
public void visit(Binary binary) {
// TODO Auto-generated method stub
}
@Override
public void visit(Block block) {
// TODO Auto-generated method stub
}
@Override
public void visit(CastExpr castExpr) {
// TODO Auto-generated method stub
}
@Override
public void visit(EmptyStmt emptyStmt) {
// TODO Auto-generated method stub
}
@Override
public void visit(ForStmt forStmt) {
// TODO Auto-generated method stub
}
@Override
public void visit(IfStmt ifStmt) {
// TODO Auto-generated method stub
}
@Override
public void visit(InstanceOf instanceOf) {
// TODO Auto-generated method stub
}
@Override
public void visit(LocalVar localVar) {
// TODO Auto-generated method stub
}
@Override
public void visit(LocalVarDecl localVarDecl) {
// TODO Auto-generated method stub
}
@Override
public void visit(MethodCall methodCall) {
// TODO Auto-generated method stub
}
@Override
public void visit(NewClass methodCall) {
// TODO Auto-generated method stub
}
@Override
public void visit(NewArray newArray) {
// TODO Auto-generated method stub
}
@Override
public void visit(Receiver receiver) {
// TODO Auto-generated method stub
}
@Override
public void visit(Return aReturn) {
// TODO Auto-generated method stub
}
@Override
public void visit(ReturnVoid aReturn) {
// TODO Auto-generated method stub
}
@Override
public void visit(StaticClassName staticClassName) {
// TODO Auto-generated method stub
}
@Override
public void visit(Super aSuper) {
// TODO Auto-generated method stub
}
@Override
public void visit(This aThis) {
// TODO Auto-generated method stub
}
@Override
public void visit(UnaryPlus unaryPlus) {
// TODO Auto-generated method stub
}
@Override
public void visit(WhileStmt whileStmt) {
// TODO Auto-generated method stub
}
@Override
public void visit(DoStmt whileStmt) {
// TODO Auto-generated method stub
}
@Override
public void visit(Null aNull) {
// TODO Auto-generated method stub
}
// ???
@Override
public void visit(Literal literal) {
// TODO Auto-generated method stub
}
@Override
public void visit(ArgumentList argumentList) {
// TODO Auto-generated method stub
}
@Override
public void visit(GenericTypeVar genericTypeVar) {
// TODO Auto-generated method stub
}
@Override
public void visit(GenericDeclarationList genericTypeVars) {
// TODO Auto-generated method stub
}
@Override
public void visit(AssignToField assignLeftSide) {
// TODO Auto-generated method stub
}
@Override
public void visit(AssignToLocal assignLeftSide) {
// TODO Auto-generated method stub
}
@Override
public void visit(SuperCall superCall) {
// TODO Auto-generated method stub
}
}

View File

@ -0,0 +1,227 @@
package de.dhbwstuttgart.bytecode;
import org.objectweb.asm.MethodVisitor;
import de.dhbwstuttgart.parser.SyntaxTreeGenerator.AssignToLocal;
import de.dhbwstuttgart.syntaxtree.StatementVisitor;
import de.dhbwstuttgart.syntaxtree.statement.ArgumentList;
import de.dhbwstuttgart.syntaxtree.statement.Assign;
import de.dhbwstuttgart.syntaxtree.statement.AssignToField;
import de.dhbwstuttgart.syntaxtree.statement.Binary;
import de.dhbwstuttgart.syntaxtree.statement.Block;
import de.dhbwstuttgart.syntaxtree.statement.CastExpr;
import de.dhbwstuttgart.syntaxtree.statement.DoStmt;
import de.dhbwstuttgart.syntaxtree.statement.EmptyStmt;
import de.dhbwstuttgart.syntaxtree.statement.FieldVar;
import de.dhbwstuttgart.syntaxtree.statement.ForStmt;
import de.dhbwstuttgart.syntaxtree.statement.IfStmt;
import de.dhbwstuttgart.syntaxtree.statement.InstanceOf;
import de.dhbwstuttgart.syntaxtree.statement.LambdaExpression;
import de.dhbwstuttgart.syntaxtree.statement.LocalVar;
import de.dhbwstuttgart.syntaxtree.statement.LocalVarDecl;
import de.dhbwstuttgart.syntaxtree.statement.MethodCall;
import de.dhbwstuttgart.syntaxtree.statement.NewArray;
import de.dhbwstuttgart.syntaxtree.statement.NewClass;
import de.dhbwstuttgart.syntaxtree.statement.Receiver;
import de.dhbwstuttgart.syntaxtree.statement.Return;
import de.dhbwstuttgart.syntaxtree.statement.ReturnVoid;
import de.dhbwstuttgart.syntaxtree.statement.StaticClassName;
import de.dhbwstuttgart.syntaxtree.statement.Super;
import de.dhbwstuttgart.syntaxtree.statement.SuperCall;
import de.dhbwstuttgart.syntaxtree.statement.This;
import de.dhbwstuttgart.syntaxtree.statement.UnaryPlus;
import de.dhbwstuttgart.syntaxtree.statement.WhileStmt;
import de.dhbwstuttgart.syntaxtree.statement.literal.Literal;
import de.dhbwstuttgart.syntaxtree.statement.literal.Null;
public class BytecodeGenLambda implements StatementVisitor{
private LambdaExpression lambdaExpression;
private MethodVisitor mv;
public BytecodeGenLambda(LambdaExpression lambdaExpression, MethodVisitor mv) {
this.lambdaExpression = lambdaExpression;
this.mv = mv;
}
@Override
public void visit(ArgumentList argumentList) {
// TODO Auto-generated method stub
}
@Override
public void visit(LambdaExpression lambdaExpression) {
// TODO Auto-generated method stub
}
@Override
public void visit(Assign assign) {
// TODO Auto-generated method stub
}
@Override
public void visit(Binary binary) {
// TODO Auto-generated method stub
}
@Override
public void visit(Block block) {
// TODO Auto-generated method stub
}
@Override
public void visit(CastExpr castExpr) {
// TODO Auto-generated method stub
}
@Override
public void visit(EmptyStmt emptyStmt) {
// TODO Auto-generated method stub
}
@Override
public void visit(FieldVar fieldVar) {
// TODO Auto-generated method stub
}
@Override
public void visit(ForStmt forStmt) {
// TODO Auto-generated method stub
}
@Override
public void visit(IfStmt ifStmt) {
// TODO Auto-generated method stub
}
@Override
public void visit(InstanceOf instanceOf) {
// TODO Auto-generated method stub
}
@Override
public void visit(LocalVar localVar) {
// TODO Auto-generated method stub
}
@Override
public void visit(LocalVarDecl localVarDecl) {
// TODO Auto-generated method stub
}
@Override
public void visit(MethodCall methodCall) {
// TODO Auto-generated method stub
}
@Override
public void visit(NewClass methodCall) {
// TODO Auto-generated method stub
}
@Override
public void visit(NewArray newArray) {
// TODO Auto-generated method stub
}
@Override
public void visit(Receiver receiver) {
// TODO Auto-generated method stub
}
@Override
public void visit(Return aReturn) {
// TODO Auto-generated method stub
}
@Override
public void visit(ReturnVoid aReturn) {
// TODO Auto-generated method stub
}
@Override
public void visit(StaticClassName staticClassName) {
// TODO Auto-generated method stub
}
@Override
public void visit(Super aSuper) {
// TODO Auto-generated method stub
}
@Override
public void visit(This aThis) {
// TODO Auto-generated method stub
}
@Override
public void visit(UnaryPlus unaryPlus) {
// TODO Auto-generated method stub
}
@Override
public void visit(WhileStmt whileStmt) {
// TODO Auto-generated method stub
}
@Override
public void visit(DoStmt whileStmt) {
// TODO Auto-generated method stub
}
@Override
public void visit(Null aNull) {
// TODO Auto-generated method stub
}
@Override
public void visit(Literal literal) {
// TODO Auto-generated method stub
}
@Override
public void visit(AssignToField assignLeftSide) {
// TODO Auto-generated method stub
}
@Override
public void visit(AssignToLocal assignLeftSide) {
// TODO Auto-generated method stub
}
@Override
public void visit(SuperCall superCall) {
// TODO Auto-generated method stub
}
}

View File

@ -0,0 +1,360 @@
package de.dhbwstuttgart.bytecode;
import java.io.PrintStream;
import java.lang.invoke.CallSite;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.security.GeneralSecurityException;
import java.util.HashMap;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Handle;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;
import de.dhbwstuttgart.parser.SyntaxTreeGenerator.AssignToLocal;
import de.dhbwstuttgart.syntaxtree.Method;
import de.dhbwstuttgart.syntaxtree.StatementVisitor;
import de.dhbwstuttgart.syntaxtree.statement.ArgumentList;
import de.dhbwstuttgart.syntaxtree.statement.Assign;
import de.dhbwstuttgart.syntaxtree.statement.AssignToField;
import de.dhbwstuttgart.syntaxtree.statement.Binary;
import de.dhbwstuttgart.syntaxtree.statement.Block;
import de.dhbwstuttgart.syntaxtree.statement.CastExpr;
import de.dhbwstuttgart.syntaxtree.statement.DoStmt;
import de.dhbwstuttgart.syntaxtree.statement.EmptyStmt;
import de.dhbwstuttgart.syntaxtree.statement.Expression;
import de.dhbwstuttgart.syntaxtree.statement.FieldVar;
import de.dhbwstuttgart.syntaxtree.statement.ForStmt;
import de.dhbwstuttgart.syntaxtree.statement.IfStmt;
import de.dhbwstuttgart.syntaxtree.statement.InstanceOf;
import de.dhbwstuttgart.syntaxtree.statement.LambdaExpression;
import de.dhbwstuttgart.syntaxtree.statement.LocalVar;
import de.dhbwstuttgart.syntaxtree.statement.LocalVarDecl;
import de.dhbwstuttgart.syntaxtree.statement.MethodCall;
import de.dhbwstuttgart.syntaxtree.statement.NewArray;
import de.dhbwstuttgart.syntaxtree.statement.NewClass;
import de.dhbwstuttgart.syntaxtree.statement.Receiver;
import de.dhbwstuttgart.syntaxtree.statement.Return;
import de.dhbwstuttgart.syntaxtree.statement.ReturnVoid;
import de.dhbwstuttgart.syntaxtree.statement.Statement;
import de.dhbwstuttgart.syntaxtree.statement.StaticClassName;
import de.dhbwstuttgart.syntaxtree.statement.Super;
import de.dhbwstuttgart.syntaxtree.statement.SuperCall;
import de.dhbwstuttgart.syntaxtree.statement.This;
import de.dhbwstuttgart.syntaxtree.statement.UnaryPlus;
import de.dhbwstuttgart.syntaxtree.statement.WhileStmt;
import de.dhbwstuttgart.syntaxtree.statement.literal.Literal;
import de.dhbwstuttgart.syntaxtree.statement.literal.Null;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
public class BytecodeGenMethod implements StatementVisitor{
private Method m;
private MethodVisitor mv;
private HashMap<String, Integer> paramsAndLocals = new HashMap<>();
private String desc;
private String className;
private int lamCounter;
private ClassWriter cw;
//for tests **
private String fieldName;
private String fieldDesc;
private Expression rightSideTemp;
private String where;
public BytecodeGenMethod(String className, Method m, MethodVisitor mv, HashMap<String, Integer> paramsAndLocals,
String desc, ClassWriter cw) {
this.where = "NORMAL METHOD";
this.className = className;
this.m = m;
this.mv = mv;
this.paramsAndLocals = paramsAndLocals;
this.desc = desc;
this.cw = cw;
this.lamCounter = -1;
this.m.block.accept(this);
}
public BytecodeGenMethod(LambdaExpression lambdaExpression, MethodVisitor mv,
HashMap<String, Integer> paramsAndLocals, String desc) {
System.out.println("++++++IN LAMBDA -------");
this.where = "&&&&&&&& LAMBDA METHOD";
this.mv = mv;
this.paramsAndLocals = paramsAndLocals;
this.desc = desc;
this.lamCounter = -1;
lambdaExpression.methodBody.accept(this);
}
@Override
public void visit(Block block) {
for(Statement stmt : block.getStatements()) {
System.out.println(where);
System.out.println("Stmt : " + stmt.toString());
stmt.accept(this);
System.out.println("--------------------------\n");
}
}
@Override
public void visit(SuperCall superCall) {
superCall.receiver.accept(this);
superCall.arglist.accept(this);
// mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", superCall.name, desc,false);
mv.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(Object.class), superCall.name, desc,false);
}
// ??
@Override
public void visit(LocalVar localVar) {
System.out.println("in Local Var");
}
// ??
@Override
public void visit(LocalVarDecl localVarDecl) {
// Integer i;
paramsAndLocals.put(localVarDecl.getName(), paramsAndLocals.size()+1);
System.out.println("In localVarDecl");
}
@Override
public void visit(Assign assign) {
System.out.println("Assign : \nright = "+assign.rightSide + "\nLeft = " + assign.lefSide);
if(assign.lefSide.getClass().equals(AssignToField.class)) {
// load_0, ldc or .. then putfield
this.rightSideTemp = assign.rightSide;
assign.lefSide.accept(this);
}else {
assign.rightSide.accept(this);
assign.lefSide.accept(this);
}
}
@Override
public void visit(Binary binary) {
System.out.println("++ In Binary: ");
}
@Override
public void visit(LambdaExpression lambdaExpression) {
System.out.println("\n++ In Lambda: ");
this.lamCounter++;
//Call site, which, when invoked, returns an instance of the functional interface to which
//the lambda is being converted
MethodType mt = MethodType.methodType(CallSite.class, MethodHandles.Lookup.class, String.class,
MethodType.class, MethodType.class, MethodHandle.class, MethodType.class);
Handle bootstrap = new Handle(Opcodes.H_INVOKESTATIC, "java/lang/invoke/LambdaMetafactory",
"metafactory", mt.toMethodDescriptorString(), false);
String methodName = "lambda$new$" + this.lamCounter;
// Type erasure
Type arg1 = Type.getMethodType("()V");
// real Type
Type arg3 = Type.getMethodType("()V");
Handle arg2 = new Handle(Opcodes.H_INVOKESTATIC, this.className, methodName,
arg3.toString(),false);
mv.visitInvokeDynamicInsn("run", "()Ljava/lang/Runnable;", bootstrap,
arg1, arg2,arg3);
MethodVisitor mvLambdaBody = cw.visitMethod(Opcodes.ACC_PRIVATE+ Opcodes.ACC_STATIC + Opcodes.ACC_SYNTHETIC,
methodName, arg3.toString(), null, null);
// new BytecodeGenLambda(lambdaExpression, mvLambdaBody);
new BytecodeGenMethod(lambdaExpression, mvLambdaBody, new HashMap<>(), arg3.toString());
mvLambdaBody.visitMaxs(0, 0);
mvLambdaBody.visitEnd();
}
@Override
public void visit(CastExpr castExpr) {
// TODO Auto-generated method stub
}
@Override
public void visit(EmptyStmt emptyStmt) {
// TODO Auto-generated method stub
}
@Override
public void visit(FieldVar fieldVar) {
System.out.println("in fieldVar " + fieldVar.fieldVarName + " ** receiver: "+fieldVar.receiver);
fieldName = fieldVar.fieldVarName;
fieldDesc = fieldVar.getType().toString();
fieldVar.receiver.accept(this);
// test (if)
if(!fieldVar.receiver.getClass().equals(StaticClassName.class)) {
mv.visitFieldInsn(Opcodes.GETFIELD,fieldVar.getType().toString(),fieldName ,fieldDesc);
}
// mv.visitFieldInsn(Opcodes.GETSTATIC, fieldVar.receiver.getType().toString().replace(".", "/"),
// fieldVar.fieldVarName, fieldVar.getType().toString());
}
@Override
public void visit(ForStmt forStmt) {
// TODO Auto-generated method stub
}
@Override
public void visit(IfStmt ifStmt) {
System.out.println("++ IF-Statment: ");
}
@Override
public void visit(InstanceOf instanceOf) {
// TODO Auto-generated method stub
}
@Override
public void visit(MethodCall methodCall) {
System.out.println(" In Methodcall: (" +methodCall.name+")" );
System.out.println(" Method-Receiver: "+methodCall.receiver.expr);
methodCall.receiver.accept(this);
methodCall.arglist.accept(this);
Descriptor mDesc = new Descriptor(methodCall.arglist, methodCall.getType());
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, methodCall.receiver.expr.getType().toString(),
methodCall.name, mDesc.getDesc(), false);
// test
if(!methodCall.getType().toString().equals("V")) {
mv.visitInsn(Opcodes.POP);
}
}
@Override
public void visit(NewClass methodCall) {
System.out.println("In NewClass: ");
System.out.println("name: " + methodCall.name + " *** " + "Receiver: " + methodCall.receiver);
mv.visitTypeInsn(Opcodes.NEW, methodCall.name.replace(".", "/"));
mv.visitInsn(Opcodes.DUP);
// creates Descriptor
methodCall.arglist.accept(this);
String d = "(";
for(Expression e : methodCall.arglist.getArguments()) {
d = d + "L"+e.getType().toString().replace(".", "/") + ";";
}
d += ")V";
mv.visitMethodInsn(Opcodes.INVOKESPECIAL, methodCall.name.replace(".", "/"), "<init>", d, false);
}
@Override
public void visit(NewArray newArray) {
// TODO Auto-generated method stub
}
@Override
public void visit(Receiver receiver) {
System.out.println(" in Receiver");
System.out.println(" expr : " + receiver.expr);
receiver.expr.accept(this);
}
@Override
public void visit(Return aReturn) {
mv.visitInsn(Opcodes.ARETURN);
}
@Override
public void visit(ReturnVoid aReturn) {
mv.visitInsn(Opcodes.RETURN);
}
@Override
public void visit(StaticClassName staticClassName) {
System.out.println("In StaticClassName: ");
// mv.visitMethodInsn(Opcodes.INVOKESTATIC, staticClassName.getType().toString().replace(".", "/"),
// staticClassName.toString(), staticClassName.getType().toString(), false);
mv.visitFieldInsn(Opcodes.GETSTATIC, staticClassName.getType().toString().replace(".", "/"),
fieldName, fieldDesc);
}
@Override
public void visit(Super aSuper) {
System.out.println(">> In Super: ");
}
@Override
public void visit(This aThis) {
System.out.println("-> IN This");
mv.visitVarInsn(Opcodes.ALOAD, 0);
}
@Override
public void visit(UnaryPlus unaryPlus) {
System.out.println("++ In UnaryPlus: ");
}
@Override
public void visit(WhileStmt whileStmt) {
// TODO Auto-generated method stub
}
@Override
public void visit(DoStmt whileStmt) {
// TODO Auto-generated method stub
}
@Override
public void visit(Null aNull) {
mv.visitInsn(Opcodes.ACONST_NULL);
}
@Override
public void visit(Literal literal) {
// value?
mv.visitLdcInsn(literal.getType().toString());
}
@Override
public void visit(ArgumentList argumentList) {
for(Expression al : argumentList.getArguments()) {
al.accept(this);
}
}
@Override
public void visit(AssignToField assignLeftSide) {
// Loads the an object reference from the local variable
// array slot onto the top of the operand stack.
assignLeftSide.field.receiver.accept(this);
this.rightSideTemp.accept(this);
mv.visitFieldInsn(Opcodes.PUTFIELD, assignLeftSide.field.receiver.getType().toString(),
assignLeftSide.field.fieldVarName, assignLeftSide.field.getType().toString());
}
@Override
public void visit(AssignToLocal assignLeftSide) {
paramsAndLocals.put(assignLeftSide.localVar.name, paramsAndLocals.size()+1);
mv.visitVarInsn(Opcodes.ASTORE, paramsAndLocals.size());
}
}

View File

@ -0,0 +1,14 @@
package de.dhbwstuttgart.bytecode;
public class ClassFile {
String name;
byte[] bytecode;
public ClassFile(String name, byte[] bytecode) {
this.name = name;
this.bytecode = bytecode;
}
}

View File

@ -0,0 +1,54 @@
package de.dhbwstuttgart.bytecode;
import java.awt.List;
import java.util.Iterator;
import de.dhbwstuttgart.syntaxtree.Constructor;
import de.dhbwstuttgart.syntaxtree.FormalParameter;
import de.dhbwstuttgart.syntaxtree.Method;
import de.dhbwstuttgart.syntaxtree.statement.ArgumentList;
import de.dhbwstuttgart.syntaxtree.statement.Expression;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
public class Descriptor {
String desc;
public Descriptor(Method method) {
desc = "(";
Iterator<FormalParameter> itr = method.getParameterList().iterator();
while(itr.hasNext()) {
FormalParameter fp = itr.next();
desc = desc + "L"+fp.getType().toString().replace(".", "/") + ";";
}
if(method.getReturnType().toString().equals("void")){
desc = desc + ")V";
}else {
desc = desc + ")" + "L"+method.getReturnType().toString().replace(".", "/")+";";
}
}
public Descriptor(Constructor constructor) {
desc = "(";
Iterator<FormalParameter> itr = constructor.getParameterList().iterator();
while(itr.hasNext()) {
FormalParameter fp = itr.next();
desc = desc + "L"+fp.getType().toString().replace(".", "/") + ";";
}
desc = desc + ")V";
}
public Descriptor(ArgumentList argList, RefTypeOrTPHOrWildcardOrGeneric returnType) {
desc = "(";
for(Expression e : argList.getArguments()) {
desc = desc + "L"+e.getType().toString().replace(".", "/") + ";";
}
desc = desc + ")"+returnType.toString();
}
public String getDesc() {
return this.desc;
}
}

View File

@ -0,0 +1,150 @@
package de.dhbwstuttgart.bytecode;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.invoke.CallSite;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Handle;
import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;
public class Test {
private static final String rootDirectory = System.getProperty("user.dir") + "/bin/de/dhbwstuttgart/bytecode/";
protected static ClassLoader getClassLoader() throws Exception {
File file = new File(rootDirectory);
URL url = file.toURI().toURL();
URL[] urls = new URL[] { url };
System.out.println(urls[0]);
return new URLClassLoader(urls);
}
public static void main(String[] args) {
// Test Lambda
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
cw.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, "TestClass", null, "java/lang/Object", null);
cw.visitSource("TestClass.java", null);
// Create Constructor
MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
mv.visitVarInsn(Opcodes.ALOAD, 0);
mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
// mv.visitMethodInsn(INVOKEDYNAMIC, "#0", "run", "()Ljava/lang/Runnable");
//Call site, which, when invoked, returns an instance of the functional interface to which
//the lambda is being converted
MethodType mt = MethodType.methodType(CallSite.class, MethodHandles.Lookup.class, String.class,
MethodType.class, MethodType.class, MethodHandle.class, MethodType.class);
Handle bootstrap = new Handle(Opcodes.H_INVOKESTATIC, "java/lang/invoke/LambdaMetafactory", "metafactory",
mt.toMethodDescriptorString());
Handle arg2 = new Handle(Opcodes.H_INVOKESTATIC, "TestClass", "lambda$0", "()V");
mv.visitInvokeDynamicInsn("run", "()Ljava/lang/Runnable;", bootstrap,
Type.getMethodType("()V"), arg2,
Type.getMethodType("()V"));
mv.visitVarInsn(Opcodes.ASTORE, 1);
mv.visitVarInsn(Opcodes.ALOAD, 1);
mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/lang/Runnable", "run", "()V");
mv.visitInsn(Opcodes.RETURN);
// creates bridge method, contains lambdas body
MethodVisitor mvl = cw.visitMethod(Opcodes.ACC_PRIVATE + Opcodes.ACC_STATIC + Opcodes.ACC_SYNTHETIC, "lambda$0",
"()V", null, null);
mvl.visitCode();
mvl.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
mvl.visitLdcInsn("lambda");
mvl.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V");
mvl.visitInsn(Opcodes.RETURN);
mvl.visitMaxs(2, 0);
mvl.visitEnd();
mv.visitMaxs(1, 2);
mv.visitEnd();
cw.visitInnerClass("java/lang/invoke/MethodHandles$Lookup", "java/lang/invoke/MethodHandles", "Lookup",
Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC + Opcodes.ACC_FINAL);
cw.visitEnd();
byte[] b = cw.toByteArray();
// Test if statement
/*
* ClassWriter cw = new
* ClassWriter(ClassWriter.COMPUTE_FRAMES|ClassWriter.COMPUTE_MAXS);
*
* cw.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC+Opcodes.ACC_SUPER, "TestIf", null,
* "java/lang/Object", null); MethodVisitor mv =
* cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "(Ljava/lang/Boolean;)V", null,
* null); mv.visitCode();
*
* // Label l0 = new Label(); // mv.visitLabel(l0);
*
* mv.visitVarInsn(Opcodes.ALOAD, 0);
*
* mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>",
* "()V");
*
* // Label l1 = new Label(); // mv.visitLabel(l1);
* mv.visitVarInsn(Opcodes.ALOAD, 1); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL,
* "java/lang/Boolean", "booleanValue", "()Z");
*
* Label label = new Label(); mv.visitJumpInsn(Opcodes.IFEQ, label);
*
* mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "out",
* "Ljava/io/PrintStream;"); mv.visitLdcInsn("1");
* mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println",
* "(Ljava/lang/String;)V");
*
* Label endLabel = new Label(); mv.visitJumpInsn(Opcodes.GOTO, endLabel);
*
* mv.visitLabel(label); mv.visitFieldInsn(Opcodes.GETSTATIC,
* "java/lang/System", "out", "Ljava/io/PrintStream;"); mv.visitLdcInsn("0");
* mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println",
* "(Ljava/lang/String;)V");
*
*
*
* mv.visitLabel(endLabel); mv.visitInsn(Opcodes.RETURN);
*
* // Label l2 = new Label(); // mv.visitLabel(l2);
*
* // mv.visitLocalVariable("this", "LTestIf;", null, l0, l2, 0); //
* mv.visitLocalVariable("b", "Ljava/lang/Boolean;", null, l0, l2, 1);
* mv.visitMaxs(2, 2); mv.visitEnd();
*
* cw.visitEnd(); byte[] b = cw.toByteArray();
*/
FileOutputStream output;
try {
output = new FileOutputStream(new File(System.getProperty("user.dir") + "/testBytecode/TestClass.class"));
output.write(b);
output.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,8 @@
package de.dhbwstuttgart.bytecode;
public class TestClass {
public TestClass() {
Runnable lam = () -> System.out.println("lambda");
lam.run();
}
}

View File

@ -0,0 +1,59 @@
package de.dhbwstuttgart.bytecode;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.FieldVisitor;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;
public class TestFields {
private static final String rootDirectory = System.getProperty("user.dir") + "/bin/de/dhbwstuttgart/bytecode/";
public static void main(String[] args) {
// TODO Auto-generated method stub
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES|ClassWriter.COMPUTE_MAXS);
cw.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, "TetsF", null, "java/lang/Object", null);
cw.visitSource("TetsF.java", null);
FieldVisitor fv = cw.visitField(Opcodes.ACC_PRIVATE, "z", Type.INT_TYPE.getDescriptor(), null, null);
fv.visitEnd();
FieldVisitor fvS = cw.visitField(Opcodes.ACC_PUBLIC, "s", "Ljava/lang/String;", null, null);
fvS.visitEnd();
// Create Constructor
MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
mv.visitCode();
mv.visitVarInsn(Opcodes.ALOAD, 0);
mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
mv.visitVarInsn(Opcodes.ALOAD, 0);
mv.visitLdcInsn("");
mv.visitFieldInsn(Opcodes.PUTFIELD, "TetsF", "s", "Ljava/lang/String;");
mv.visitInsn(Opcodes.RETURN);
mv.visitMaxs(2, 1);
mv.visitEnd();
byte[] b = cw.toByteArray();
FileOutputStream output;
try {
output = new FileOutputStream(new File(System.getProperty("user.dir") + "/testBytecode/TetsF.class"));
output.write(b);
output.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,11 @@
package de.dhbwstuttgart.bytecode;
public class TestIf {
public TestIf(Boolean b) {
if(b) {
System.out.println("1");
}else {
System.out.println("0");
}
}
}

View File

@ -0,0 +1,18 @@
package de.dhbwstuttgart.bytecode;
public class TestMeth {
private int z;
public String s;
public TestMeth(int temp) {
this.z = temp;
}
public void m1(int a, int b) {
int y = m2(1,2,3,4);
}
public int m2(int a, int b, int x, int y) {
Integer c = 55;
Integer g;
return a+b+y+c;
}
}

View File

@ -0,0 +1,6 @@
package de.dhbwstuttgart.bytecode;
public class TetsF {
private int z;
public String s = "";
}

View File

@ -1,12 +1,16 @@
package de.dhbwstuttgart.core;
import de.dhbwstuttgart.environment.CompilationEnvironment;
import de.dhbwstuttgart.parser.JavaTXParser;
import de.dhbwstuttgart.parser.SyntaxTreeGenerator.GenericsRegistry;
import de.dhbwstuttgart.parser.scope.GenericsRegistry;
import de.dhbwstuttgart.parser.SyntaxTreeGenerator.SyntaxTreeGenerator;
import de.dhbwstuttgart.parser.antlr.Java8Parser.CompilationUnitContext;
import de.dhbwstuttgart.parser.scope.JavaClassName;
import de.dhbwstuttgart.parser.scope.JavaClassRegistry;
import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
import de.dhbwstuttgart.syntaxtree.SourceFile;
import de.dhbwstuttgart.syntaxtree.factory.ASTFactory;
import de.dhbwstuttgart.syntaxtree.factory.UnifyTypeFactory;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import de.dhbwstuttgart.typeinference.constraints.Constraint;
@ -39,13 +43,23 @@ public class JavaTXCompiler {
}
}
public List<ResultSet> typeInference(){
List<ClassOrInterface> allClasses = new ArrayList<>();
public List<ResultSet> typeInference() throws ClassNotFoundException {
List<ClassOrInterface> allClasses = new ArrayList<>();//environment.getAllAvailableClasses();
for(SourceFile sf : sourceFiles.values()){
allClasses.addAll(sf.getClasses());
}
FiniteClosure finiteClosure = UnifyTypeFactory.generateFC(allClasses);
final ConstraintSet<Pair> cons = new TYPE(sourceFiles.values()).getConstraints();
List<ClassOrInterface> importedClasses = new ArrayList<>();
//Alle Importierten Klassen in allen geparsten Sourcefiles kommen ins FC
for(File forSourceFile : sourceFiles.keySet())
for(JavaClassName name : sourceFiles.get(forSourceFile).getImports()){
ClassOrInterface importedClass = ASTFactory.createClass(
ClassLoader.getSystemClassLoader().loadClass(name.toString()));
importedClasses.add(importedClass);
}
allClasses.addAll(importedClasses);
FiniteClosure finiteClosure = UnifyTypeFactory.generateFC(allClasses);
final ConstraintSet<Pair> cons = new TYPE(sourceFiles.values(), allClasses).getConstraints();
ConstraintSet<UnifyPair> unifyCons = UnifyTypeFactory.convert(cons);
TypeUnify unify = new TypeUnify();
@ -56,9 +70,9 @@ public class JavaTXCompiler {
xConsSet.addAll(constraint);
}
System.out.println(xConsSet);
//System.out.println(xConsSet);
Set<Set<UnifyPair>> result = unify.unify(xConsSet, finiteClosure);
System.out.println("RESULT: " + result);
//System.out.println("RESULT: " + result);
results.addAll(result);
}
return results.stream().map((unifyPairs ->

View File

@ -10,6 +10,9 @@ import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
import de.dhbwstuttgart.syntaxtree.SourceFile;
import de.dhbwstuttgart.syntaxtree.factory.ASTFactory;
import org.reflections.Reflections;
import org.reflections.scanners.ResourcesScanner;
import org.reflections.scanners.SubTypesScanner;
@ -31,6 +34,7 @@ import de.dhbwstuttgart.parser.scope.JavaClassRegistry;
*/
public class CompilationEnvironment {
private final List<URL> librarys;
private final List<File> sourceFiles;
/**
* Imitiert die Environment beim Aufruf des JavaCompilers auf einer Menge von java-Dateien
@ -47,10 +51,11 @@ public class CompilationEnvironment {
new DebugException("Fehler im Classpath auf diesem System");
}
}
this.sourceFiles = sourceFiles;
}
public JavaClassRegistry getRegistry(File forSourceFile) throws ClassNotFoundException, IOException {
List<String> allNames = new ArrayList<>();
List<String> allNames;
CompilationUnitContext tree = JavaTXParser.parse(forSourceFile);
allNames = GatherNames.getNames(tree, new PackageCrawler(librarys));
return new JavaClassRegistry(allNames);

View File

@ -3,6 +3,7 @@ package de.dhbwstuttgart.parser.SyntaxTreeGenerator;
import de.dhbwstuttgart.exceptions.NotImplementedException;
import de.dhbwstuttgart.parser.NullToken;
import de.dhbwstuttgart.parser.antlr.Java8Parser;
import de.dhbwstuttgart.parser.scope.GenericsRegistry;
import de.dhbwstuttgart.parser.scope.JavaClassRegistry;
import de.dhbwstuttgart.syntaxtree.*;
import de.dhbwstuttgart.syntaxtree.statement.*;

View File

@ -2,8 +2,9 @@ package de.dhbwstuttgart.parser.SyntaxTreeGenerator;
import de.dhbwstuttgart.exceptions.NotImplementedException;
import java.lang.ClassNotFoundException;
import de.dhbwstuttgart.parser.NullToken;
import de.dhbwstuttgart.parser.antlr.Java8Parser;
import de.dhbwstuttgart.parser.scope.GenericsRegistry;
import de.dhbwstuttgart.parser.scope.JavaClassName;
import de.dhbwstuttgart.parser.scope.JavaClassRegistry;
import de.dhbwstuttgart.syntaxtree.*;
@ -13,9 +14,7 @@ import de.dhbwstuttgart.syntaxtree.type.RefType;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import java.io.File;
import java.lang.reflect.Modifier;
import java.sql.Ref;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@ -77,13 +76,13 @@ public class SyntaxTreeGenerator{
newImports.add(convertSingleTypeImportDeclaration(importDeclCtx.singleTypeImportDeclaration()));
}
else if(importDeclCtx.typeImportOnDemandDeclaration() != null){
newImports.add(convertTypeImportOnDemandDeclaration(importDeclCtx.typeImportOnDemandDeclaration()));
newImports.addAll(convertTypeImportOnDemandDeclaration(importDeclCtx.typeImportOnDemandDeclaration()));
}
else if(importDeclCtx.singleStaticImportDeclaration() != null){
newImports.add(convertSingleStaticImportDeclaration(importDeclCtx.singleStaticImportDeclaration()));
}
else{
newImports.add(convertStaticImportOnDemandDeclaration(importDeclCtx.staticImportOnDemandDeclaration()));
newImports.addAll(convertStaticImportOnDemandDeclaration(importDeclCtx.staticImportOnDemandDeclaration()));
}
}
this.imports.addAll(newImports);
@ -95,16 +94,16 @@ public class SyntaxTreeGenerator{
return ret;
}
private JavaClassName convertTypeImportOnDemandDeclaration(Java8Parser.TypeImportOnDemandDeclarationContext ctx){
return null;
private List<JavaClassName> convertTypeImportOnDemandDeclaration(Java8Parser.TypeImportOnDemandDeclarationContext ctx){
return reg.getAllFromPackage(ctx.packageOrTypeName().getText());
}
private JavaClassName convertSingleStaticImportDeclaration(Java8Parser.SingleStaticImportDeclarationContext ctx){
return null;
throw new NotImplementedException();
}
private JavaClassName convertStaticImportOnDemandDeclaration(Java8Parser.StaticImportOnDemandDeclarationContext ctx){
return null;
private List<JavaClassName> convertStaticImportOnDemandDeclaration(Java8Parser.StaticImportOnDemandDeclarationContext ctx){
return reg.getAllFromPackage(ctx.typeName().getText());
}
private String getPackageFromClass(String cls){

View File

@ -2,25 +2,21 @@ package de.dhbwstuttgart.parser.SyntaxTreeGenerator;
import de.dhbwstuttgart.exceptions.NotImplementedException;
import de.dhbwstuttgart.exceptions.TypeinferenceException;
import de.dhbwstuttgart.parser.NullToken;
import de.dhbwstuttgart.parser.antlr.Java8Parser;
import de.dhbwstuttgart.parser.scope.GenericTypeName;
import de.dhbwstuttgart.parser.scope.GenericsRegistry;
import de.dhbwstuttgart.parser.scope.JavaClassName;
import de.dhbwstuttgart.parser.scope.JavaClassRegistry;
import de.dhbwstuttgart.syntaxtree.GenericDeclarationList;
import de.dhbwstuttgart.syntaxtree.GenericTypeVar;
import de.dhbwstuttgart.syntaxtree.factory.ASTFactory;
import de.dhbwstuttgart.syntaxtree.statement.ArgumentList;
import de.dhbwstuttgart.syntaxtree.type.GenericRefType;
import de.dhbwstuttgart.syntaxtree.type.RefType;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import javassist.bytecode.stackmap.TypeData;
import org.antlr.v4.runtime.Token;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class TypeGenerator {

View File

@ -1,4 +1,4 @@
// Generated from Java8.g4 by ANTLR 4.5.3
// Generated from Java8.g4 by ANTLR 4.7
package de.dhbwstuttgart.parser.antlr;
import org.antlr.v4.runtime.ParserRuleContext;
@ -1571,6 +1571,18 @@ public class Java8BaseListener implements Java8Listener {
* <p>The default implementation does nothing.</p>
*/
@Override public void exitLocalVariableDeclarationStatement(Java8Parser.LocalVariableDeclarationStatementContext ctx) { }
/**
* {@inheritDoc}
*
* <p>The default implementation does nothing.</p>
*/
@Override public void enterUnannTypeOrAuto(Java8Parser.UnannTypeOrAutoContext ctx) { }
/**
* {@inheritDoc}
*
* <p>The default implementation does nothing.</p>
*/
@Override public void exitUnannTypeOrAuto(Java8Parser.UnannTypeOrAutoContext ctx) { }
/**
* {@inheritDoc}
*

View File

@ -1,4 +1,4 @@
// Generated from /home/janulrich/Development/intellijworkspace/JavaCompilerCore/src/de/dhbwstuttgart/parser/antlr/Java8.g4 by ANTLR 4.5.1
// Generated from Java8.g4 by ANTLR 4.7
package de.dhbwstuttgart.parser.antlr;
import org.antlr.v4.runtime.Lexer;
import org.antlr.v4.runtime.CharStream;
@ -11,7 +11,7 @@ import org.antlr.v4.runtime.misc.*;
@SuppressWarnings({"all", "warnings", "unchecked", "unused", "cast"})
public class Java8Lexer extends Lexer {
static { RuntimeMetaData.checkVersion("4.5.1", RuntimeMetaData.VERSION); }
static { RuntimeMetaData.checkVersion("4.7", RuntimeMetaData.VERSION); }
protected static final DFA[] _decisionToDFA;
protected static final PredictionContextCache _sharedContextCache =
@ -34,6 +34,10 @@ public class Java8Lexer extends Lexer {
OR_ASSIGN=97, XOR_ASSIGN=98, MOD_ASSIGN=99, LSHIFT_ASSIGN=100, RSHIFT_ASSIGN=101,
URSHIFT_ASSIGN=102, Identifier=103, AT=104, ELLIPSIS=105, WS=106, COMMENT=107,
LINE_COMMENT=108;
public static String[] channelNames = {
"DEFAULT_TOKEN_CHANNEL", "HIDDEN"
};
public static String[] modeNames = {
"DEFAULT_MODE"
};
@ -148,6 +152,9 @@ public class Java8Lexer extends Lexer {
@Override
public String getSerializedATN() { return _serializedATN; }
@Override
public String[] getChannelNames() { return channelNames; }
@Override
public String[] getModeNames() { return modeNames; }
@ -184,7 +191,7 @@ public class Java8Lexer extends Lexer {
}
public static final String _serializedATN =
"\3\u0430\ud6d1\u8206\uad2d\u4417\uaef1\u8d80\uaadd\2n\u044e\b\1\4\2\t"+
"\3\u608b\ua72a\u8133\ub9ed\u417c\u3be7\u7786\u5964\2n\u044e\b\1\4\2\t"+
"\2\4\3\t\3\4\4\t\4\4\5\t\5\4\6\t\6\4\7\t\7\4\b\t\b\4\t\t\t\4\n\t\n\4\13"+
"\t\13\4\f\t\f\4\r\t\r\4\16\t\16\4\17\t\17\4\20\t\20\4\21\t\21\4\22\t\22"+
"\4\23\t\23\4\24\t\24\4\25\t\25\4\26\t\26\4\27\t\27\4\30\t\30\4\31\t\31"+
@ -275,8 +282,8 @@ public class Java8Lexer extends Lexer {
"DDdd\3\2\62\63\4\2GGgg\4\2--//\6\2FFHHffhh\4\2RRrr\4\2))^^\4\2$$^^\n\2"+
"$$))^^ddhhppttvv\3\2\62\65\6\2&&C\\aac|\4\2\2\u0081\ud802\udc01\3\2\ud802"+
"\udc01\3\2\udc02\ue001\7\2&&\62;C\\aac|\5\2\13\f\16\17\"\"\4\2\f\f\17"+
"\17\u045c\2\3\3\2\2\2\2\5\3\2\2\2\2\7\3\2\2\2\2\t\3\2\2\2\2\13\3\2\2\2"+
"\2\r\3\2\2\2\2\17\3\2\2\2\2\21\3\2\2\2\2\23\3\2\2\2\2\25\3\2\2\2\2\27"+
"\17\2\u045c\2\3\3\2\2\2\2\5\3\2\2\2\2\7\3\2\2\2\2\t\3\2\2\2\2\13\3\2\2"+
"\2\2\r\3\2\2\2\2\17\3\2\2\2\2\21\3\2\2\2\2\23\3\2\2\2\2\25\3\2\2\2\2\27"+
"\3\2\2\2\2\31\3\2\2\2\2\33\3\2\2\2\2\35\3\2\2\2\2\37\3\2\2\2\2!\3\2\2"+
"\2\2#\3\2\2\2\2%\3\2\2\2\2\'\3\2\2\2\2)\3\2\2\2\2+\3\2\2\2\2-\3\2\2\2"+
"\2/\3\2\2\2\2\61\3\2\2\2\2\63\3\2\2\2\2\65\3\2\2\2\2\67\3\2\2\2\29\3\2"+

View File

@ -1,4 +1,4 @@
// Generated from Java8.g4 by ANTLR 4.5.3
// Generated from Java8.g4 by ANTLR 4.7
package de.dhbwstuttgart.parser.antlr;
import org.antlr.v4.runtime.tree.ParseTreeListener;
@ -1307,6 +1307,16 @@ public interface Java8Listener extends ParseTreeListener {
* @param ctx the parse tree
*/
void exitLocalVariableDeclarationStatement(Java8Parser.LocalVariableDeclarationStatementContext ctx);
/**
* Enter a parse tree produced by {@link Java8Parser#unannTypeOrAuto}.
* @param ctx the parse tree
*/
void enterUnannTypeOrAuto(Java8Parser.UnannTypeOrAutoContext ctx);
/**
* Exit a parse tree produced by {@link Java8Parser#unannTypeOrAuto}.
* @param ctx the parse tree
*/
void exitUnannTypeOrAuto(Java8Parser.UnannTypeOrAutoContext ctx);
/**
* Enter a parse tree produced by {@link Java8Parser#localVariableDeclaration}.
* @param ctx the parse tree

File diff suppressed because it is too large Load Diff

View File

@ -1,7 +1,8 @@
package de.dhbwstuttgart.parser.SyntaxTreeGenerator;
package de.dhbwstuttgart.parser.scope;
import de.dhbwstuttgart.parser.SyntaxTreeGenerator.GenericContext;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Optional;

View File

@ -1,5 +1,7 @@
package de.dhbwstuttgart.parser.scope;
import com.sun.xml.internal.bind.v2.runtime.reflect.Lister;
import java.util.ArrayList;
import java.util.List;
@ -35,7 +37,7 @@ public class JavaClassName {
}
/**
* Gibt von einem Klassennamen nur den Namen der Klasse zurück
* Gibt von einem Klassennamen nur den Namen der Klasse zur<EFBFBD>ck
* Beispiel:
* java.lang.Object wird zu: Object
*/

View File

@ -1,13 +1,14 @@
package de.dhbwstuttgart.parser.scope;
import de.dhbwstuttgart.exceptions.NotImplementedException;
import java.util.*;
/**
* Speichert die Klassen für einen bestimmten Projektscope
* Speichert die Klassen f<EFBFBD>r einen bestimmten Projektscope
*/
public class JavaClassRegistry {
final List<JavaClassName> existingClasses = new ArrayList<>();
final List<String> importedPackages = new ArrayList<>();
public JavaClassRegistry(List<String> initialNames){
for(String name : initialNames){
@ -23,13 +24,7 @@ public class JavaClassRegistry {
for(JavaClassName name : existingClasses){
if(name.equals(new JavaClassName(className)))return name;
}
//Jetzt noch alle importierten Packages durchsuchen:
JavaClassName ret = getClassFromImportedPackages(className);
if(ret == null){
throw new TypeNotPresentException(className, new Throwable());
}else{
return ret;
}
throw new NotImplementedException();
}
@Override
@ -37,35 +32,18 @@ public class JavaClassRegistry {
return existingClasses.toString();
}
public List<JavaClassName> getAllFromPackage(String packageName) {
List<JavaClassName> ret = new ArrayList<>();
for(JavaClassName className : this.existingClasses){
JavaClassName toCompare = new JavaClassName(packageName + "." + JavaClassName.stripClassName(className.toString()));
if(toCompare.toString().equals(className.toString())){
ret.add(className);
}
}
return ret;
}
public boolean contains(String whole) {
boolean ret = existingClasses.contains(new JavaClassName(whole));
if(ret == false){
JavaClassName imported = getClassFromImportedPackages(whole);
if(imported != null){
existingClasses.add(imported);
return true;
}else {
return false;
}
}else {
return true;
}
return existingClasses.contains(new JavaClassName(whole));
}
private JavaClassName getClassFromImportedPackages(String className){
ClassLoader loader = Thread.currentThread().getContextClassLoader();
String shortName = JavaClassName.stripClassName(className);
for(String packageName : importedPackages) {
try {
loader.loadClass(packageName+"."+shortName);
//Keine Exception! Die Klasse existiert:
JavaClassName ret = new JavaClassName(packageName+"."+shortName);
if(ret.equals(new JavaClassName(className)))return ret;
} catch (ClassNotFoundException e) {
//Die Klasse wurde nicht gefunden!
}
}
return null;
}
}

View File

@ -24,13 +24,13 @@ public class ClassOrInterface extends SyntaxTreeNode implements TypeScope{
private List<Field> fields = new ArrayList<>();
private List<Method> methods = new ArrayList<>();
private GenericDeclarationList genericClassParameters;
private RefTypeOrTPHOrWildcardOrGeneric superClass;
private RefType superClass;
protected boolean isInterface;
private List<? extends RefTypeOrTPHOrWildcardOrGeneric> implementedInterfaces;
private List<Constructor> constructors;
public ClassOrInterface(int modifiers, JavaClassName name, List<Field> fielddecl, List<Method> methods, List<Constructor> constructors, GenericDeclarationList genericClassParameters,
RefTypeOrTPHOrWildcardOrGeneric superClass, Boolean isInterface, List<? extends RefTypeOrTPHOrWildcardOrGeneric> implementedInterfaces, Token offset){
RefType superClass, Boolean isInterface, List<? extends RefTypeOrTPHOrWildcardOrGeneric> implementedInterfaces, Token offset){
super(offset);
this.modifiers = modifiers;
this.name = name;
@ -74,7 +74,7 @@ public class ClassOrInterface extends SyntaxTreeNode implements TypeScope{
return new RefType(name, params, offset);
}
public RefTypeOrTPHOrWildcardOrGeneric getSuperClass() {
public RefType getSuperClass() {
return superClass;
}

View File

@ -2,6 +2,7 @@ package de.dhbwstuttgart.syntaxtree.statement;
import de.dhbwstuttgart.syntaxtree.ASTVisitor;
import de.dhbwstuttgart.syntaxtree.StatementVisitor;
import de.dhbwstuttgart.syntaxtree.SyntaxTreeNode;
import org.antlr.v4.runtime.Token;
@ -20,8 +21,13 @@ public class ArgumentList extends SyntaxTreeNode
return expr;
}
@Override
public void accept(ASTVisitor visitor) {
visitor.visit(this);
}
public void accept(StatementVisitor visitor) {
visitor.visit(this);
}
}

View File

@ -11,13 +11,6 @@ import java.util.List;
public class RefType extends RefTypeOrTPHOrWildcardOrGeneric
{
/**
* Ist IsArray auf true, muss beim Codegen ein Zeichen [ gesetzt werden
* Bsp.: 15| tag = CONSTANT_Utf8, length = 22, ([Ljava/lang/String;)V
* Ist IsArray auf false, muss beim Codegen ein Zeichen [ gesetzt werden
* Bsp.: 9| tag = CONSTANT_Utf8, length = 21, (Ljava/lang/String;)V
*/
private boolean IsArray = false;
protected final JavaClassName name;
protected final List<RefTypeOrTPHOrWildcardOrGeneric> parameter;
/**

View File

@ -9,6 +9,7 @@ import de.dhbwstuttgart.syntaxtree.TypeScope;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Set;
import java.util.Stack;
import java.util.stream.Collectors;
@ -17,7 +18,7 @@ public class TypeInferenceBlockInformation extends TypeInferenceInformation {
private TypeScope methodContext;
private ClassOrInterface currentClass;
public TypeInferenceBlockInformation(Set<ClassOrInterface> availableClasses,
public TypeInferenceBlockInformation(Collection<ClassOrInterface> availableClasses,
ClassOrInterface currentClass, TypeScope methodContext) {
super(availableClasses);
this.methodContext = new TypeScopeContainer(currentClass, methodContext);

View File

@ -12,6 +12,7 @@ import de.dhbwstuttgart.typeinference.constraints.Constraint;
import de.dhbwstuttgart.typeinference.constraints.Pair;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;
@ -26,9 +27,9 @@ Zweiteres hat den Vorteil, dass bei der Entwicklung leichter Dinge hinzugefügt
Die ganze Logik steckt in dieser Klasse.
*/
public class TypeInferenceInformation {
private Set<ClassOrInterface> classes;
private Collection<ClassOrInterface> classes;
public TypeInferenceInformation(Set<ClassOrInterface> availableClasses){
public TypeInferenceInformation(Collection<ClassOrInterface> availableClasses){
classes = availableClasses;
}
@ -52,7 +53,7 @@ public class TypeInferenceInformation {
return ret;
}
public Set<ClassOrInterface> getAvailableClasses() {
public Collection<ClassOrInterface> getAvailableClasses() {
return classes;
}

View File

@ -13,18 +13,19 @@ import java.util.*;
public class TYPE {
private final Collection<SourceFile> sfs;
private final TypeInferenceInformation typeInferenceInformation;
public TYPE(Collection<SourceFile> sourceFiles){
public TYPE(Collection<SourceFile> sourceFiles, Collection<ClassOrInterface> allAvailableClasses){
sfs = sourceFiles;
this.typeInferenceInformation = new TypeInferenceInformation(allAvailableClasses);
}
public ConstraintSet getConstraints() {
ConstraintSet ret = new ConstraintSet();
TypeInferenceInformation info = getTypeInferenceInformation();
for(SourceFile sf : sfs)
for (ClassOrInterface cl : sf.KlassenVektor) {
ret.addAll(getConstraintsClass(cl ,info));
ret.addAll(getConstraintsClass(cl ,typeInferenceInformation));
}
return ret;
}
@ -39,25 +40,28 @@ public class TYPE {
}
return ret;
}
/*
TODO: Hier eine Information erstellen nur mit den importierte Klassen einer einzigen SourceFile
private TypeInferenceInformation getTypeInferenceInformation(sourceFile) {
ClassLoader classLoader = ClassLoader.getSystemClassLoader();
Set<ClassOrInterface> classes = new HashSet<>();
private TypeInferenceInformation getTypeInferenceInformation() {
ClassLoader classLoader = ClassLoader.getSystemClassLoader();
Set<ClassOrInterface> classes = new HashSet<>();
for(SourceFile sourceFile : sfs){
for(JavaClassName importName : sourceFile.imports){
System.out.println(importName);
try {
classes.add(ASTFactory.createClass(classLoader.loadClass(importName.toString())));
} catch (ClassNotFoundException e) {
throw new DebugException("Klasse " + importName + " konnte nicht geladen werden");
for(SourceFile sourceFile : sfs){
for(JavaClassName importName : sourceFile.imports){
System.out.println(importName);
try {
classes.add(ASTFactory.createClass(classLoader.loadClass(importName.toString())));
} catch (ClassNotFoundException e) {
throw new DebugException("Klasse " + importName + " konnte nicht geladen werden");
}
}
classes.addAll(sourceFile.KlassenVektor);
}
classes.addAll(sourceFile.KlassenVektor);
return new TypeInferenceInformation(classes);
}
return new TypeInferenceInformation(classes);
}
*/
private ConstraintSet getConstraintsMethod(Method m, TypeInferenceInformation info, ClassOrInterface currentClass) {
if(m.block == null)return new ConstraintSet(); //Abstrakte Methoden generieren keine Constraints
TypeInferenceBlockInformation blockInfo = new TypeInferenceBlockInformation(info.getAvailableClasses(), currentClass, m);

View File

@ -6,7 +6,6 @@ import de.dhbwstuttgart.exceptions.NotImplementedException;
import de.dhbwstuttgart.exceptions.TypeinferenceException;
import de.dhbwstuttgart.parser.NullToken;
import de.dhbwstuttgart.parser.SyntaxTreeGenerator.AssignToLocal;
import de.dhbwstuttgart.parser.SyntaxTreeGenerator.GenericsRegistry;
import de.dhbwstuttgart.syntaxtree.*;
import de.dhbwstuttgart.syntaxtree.statement.*;
import de.dhbwstuttgart.syntaxtree.statement.literal.Literal;

View File

@ -575,8 +575,9 @@ public class RuleSet implements IRuleSet{
if(pair.getPairOp() == PairOperator.EQUALSDOT
&& pair.getLhsType() instanceof PlaceholderType)
lhsType = (PlaceholderType) pair.getLhsType();
rhsType = pair.getRhsType(); //PL eingefuegt 2017-09-29 statt !((rhsType = pair.getRhsType()) instanceof PlaceholderType)
if(lhsType != null
&& !((rhsType = pair.getRhsType()) instanceof PlaceholderType)
//&& !((rhsType = pair.getRhsType()) instanceof PlaceholderType) //PL geloescht am 2017-09-29 Begründung: auch Typvariablen muessen ersetzt werden.
&& typeMap.get(lhsType) > 1 // The type occurs in more pairs in the set than just the recent pair.
&& !rhsType.getTypeParams().occurs(lhsType)) {
Unifier uni = new Unifier(lhsType, rhsType);
@ -688,7 +689,10 @@ public class RuleSet implements IRuleSet{
@Override
public Optional<Set<UnifyPair>> reduceFunN(UnifyPair pair) {
if(pair.getPairOp() != PairOperator.SMALLERDOT)
if((pair.getPairOp() != PairOperator.SMALLERDOT)
&& (pair.getPairOp() != PairOperator.EQUALSDOT)) //PL 2017-10-03 hinzugefuegt
//da Regel auch fuer EQUALSDOT anwendbar
//TODO: fuer allen anderen Relationen noch pruefen
return Optional.empty();
UnifyType lhsType = pair.getLhsType();

View File

@ -119,7 +119,10 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
// Sets of the "second level"
Set<UnifyPair> undefinedPairs = new HashSet<>();
Set<Set<Set<Set<UnifyPair>>>> secondLevelSets = calculatePairSets(eq2s, fc, undefinedPairs);
//PL 2017-09-20: Im calculatePairSets wird möglicherweise O .< java.lang.Integer
//nicht ausgewertet Faculty Beispiel im 1. Schritt
//PL 2017-10-03 geloest, muesste noch mit FCs mit kleineren
//Typen getestet werden.
// If pairs occured that did not match one of the cartesian product cases,
// those pairs are contradictory and the unification is impossible.
if(!undefinedPairs.isEmpty())
@ -149,7 +152,6 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
Set<Set<Set<UnifyPair>>> eqPrimeSet = setOps.cartesianProduct(topLevelSets)
.stream().map(x -> new HashSet<>(x))
.collect(Collectors.toCollection(HashSet::new));
//System.out.println(result);
Set<Set<UnifyPair>> eqPrimePrimeSet = new HashSet<>();
@ -164,12 +166,14 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
*/
Optional<Set<UnifyPair>> eqPrimePrime = rules.subst(eqPrime);
/*
* Step 6 a) Restart (fork) for pairs where subst was applied
*/
if(parallel) {
if (eqPrime.equals(eq))
if //(eqPrime.equals(eq)) //PL 2017-09-29 auskommentiert und durch
(!eqPrimePrime.isPresent()) //PL 2071-09-29 dies ersetzt
//Begruendung: Wenn in der Substitution keine Veraenderung
//(!eqPrimePrime.isPresent()) erfolgt ist, ist das Ergebnis erzielt.
eqPrimePrimeSet.add(eqPrime);
else if(eqPrimePrime.isPresent()) {
TypeUnifyTask fork = new TypeUnifyTask(eqPrimePrime.get(), fc, true);
@ -183,7 +187,10 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
}
}
else { // sequentiell (Step 6b is included)
if (eqPrime.equals(eq))
if //(eqPrime.equals(eq)) //PL 2017-09-29 auskommentiert und durch
(!eqPrimePrime.isPresent()) //PL 2071-09-29 dies ersetzt
//Begruendung: Wenn in der Substitution keine Veraenderung
//(!eqPrimePrime.isPresent()) erfolgt ist, ist das Ergebnis erzielt.
eqPrimePrimeSet.add(eqPrime);
else if(eqPrimePrime.isPresent())
eqPrimePrimeSet.addAll(unify(eqPrimePrime.get(), fc, false));
@ -440,7 +447,8 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
for(UnifyType c : cs) {
Set<UnifyType> thetaQs = fc.getChildren(c).stream().collect(Collectors.toCollection(HashSet::new));
//thetaQs.add(thetaPrime);
thetaQs.add(thetaPrime); //PL 2017-10-03: War auskommentiert habe ich wieder einkommentiert,
//da children offensichtlich ein echtes kleiner und kein kleinergleich ist
Set<UnifyType> thetaQPrimes = new HashSet<>();
TypeParams cParams = c.getTypeParams();
if(cParams.size() == 0)

View File

@ -0,0 +1,72 @@
package bytecode;
import de.dhbwstuttgart.bytecode.BytecodeGen;
import de.dhbwstuttgart.core.JavaTXCompiler;
import de.dhbwstuttgart.syntaxtree.SourceFile;
import org.junit.Test;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import static org.junit.Assert.*;
public class JavaTXCompilerTest {
private static final String rootDirectory = System.getProperty("user.dir")+"/test/javFiles/";
private static final List<File> filesToTest = new ArrayList<>();
@Test
public void test() throws IOException, java.lang.ClassNotFoundException {
System.out.println(rootDirectory);
filesToTest.add(new File(rootDirectory+"EmptyClass.jav"));
JavaTXCompiler compiler = new JavaTXCompiler(filesToTest);
System.out.println("test");
for(File f : filesToTest){
String content = readFile(f.getPath(), StandardCharsets.UTF_8);
HashMap<String,byte[]> bytecode = this.getBytecode(compiler.sourceFiles.get(f));
this.writeClassFile(bytecode, "EmptyClass");
}
}
public HashMap<String,byte[]> getBytecode(SourceFile sf) {
HashMap<String,byte[]> classFiles = new HashMap<>();
BytecodeGen bytecodeGen = new BytecodeGen(classFiles);
bytecodeGen.visit(sf);
return bytecodeGen.getClassFiles();
}
public void writeClassFile(HashMap<String,byte[]> classFiles, String name) {
FileOutputStream output;
byte[] bytecode = classFiles.get(name);
try {
System.out.println("generating .class file");
output = new FileOutputStream(new File(System.getProperty("user.dir") + "/testBytecode/" +name+".class"));
output.write(bytecode);
output.close();
System.out.println(".class file generated");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
static String readFile(String path, Charset encoding)
throws IOException
{
byte[] encoded = Files.readAllBytes(Paths.get(path));
return new String(encoded, encoding);
}
}

View File

@ -0,0 +1,3 @@
public class EmptyClass{
}

View File

@ -0,0 +1,37 @@
public class EmptyMethod{
static String s1 ="";
String s2;
public void m1(){
//String s = "";
System.out.println("test");
//Integer ab = Math.abs(1);
//Math.abs(1);
//String lV = "local";
//s1 = "1";
//s1.concat("2");
s2 = s1;
//m2();
Clazz i = new Clazz();
Integer i = new Integer(1);
}
public void m2(){}
}
class Clazz{}
/*
public class EmptyMethod2{
public static test = "5";
public void m1(Integer i, String j, Boolean b){
//String s = "";
EmptyMethod em = new EmptyMethod();
em.m1();
em.s1 = "";
//Integer ab = Math.abs(1);
//Math.abs(1);
//String lV = "local";
//s1 = "1";
//s1.concat("2");
//s2 = s1;
}
}*/

11
test/javFiles/Op1.jav Normal file
View File

@ -0,0 +1,11 @@
public class Op1{
public Op1() {
Runnable lam = () -> {
String test = "";
String b = "b";
test = b;
System.out.println(test);};
//lam.run();
}
}

View File

@ -0,0 +1,6 @@
public class Subclass extends Superclass {
public void printMethod() {
super.printMethod();
}
}

View File

@ -0,0 +1,6 @@
public class Superclass {
public void printMethod() {
System.out.println("Printed in Superclass.");
}
}

17
test/javFiles/fc.jav Normal file
View File

@ -0,0 +1,17 @@
import java.util.*;
class Test{
methode(param1, param2, param3) {
return param1.meth(param2.add(param3));
}
}
interface Klasse1{
Klasse1 meth(Klasse1 p);
Klasse1 meth(Klasse2 p);
}
interface Klasse2{
Klasse1 meth(Klasse1 p);
Klasse2 meth(Klasse2 p);
}

View File

@ -10,6 +10,6 @@ interface Klasse1{
}
interface Klasse2{
Klasse1 meth(Klasse1 p):
Klasse2 meth(Klasse2 p):
Klasse1 meth(Klasse1 p);
Klasse2 meth(Klasse2 p);
}

7
test/javFiles/test1.jav Normal file
View File

@ -0,0 +1,7 @@
class Faculty {
int a;
m (int x) {
return a+x;
}
}

View File

@ -1,11 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$" isTestSource="true" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

View File

@ -0,0 +1,9 @@
package typeinference;
import java.io.File;
public class FiniteClosureTest extends JavaTXCompilerTest{
public FiniteClosureTest() {
this.fileToTest = new File(rootDirectory+"fc.jav");
}
}

View File

@ -36,6 +36,7 @@ public class JavaTXCompilerTest {
//filesToTest.add(new File(rootDirectory+"Faculty.jav"));
//filesToTest.add(new File(rootDirectory+"mathStruc.jav"));
//filesToTest.add(new File(rootDirectory+"test.jav"));
filesToTest.add(new File(rootDirectory+"EmptyMethod.jav"));
//filesToTest.add(new File(rootDirectory+"Lambda.jav"));
//filesToTest.add(new File(rootDirectory+"Lambda2.jav"));
//filesToTest.add(new File(rootDirectory+"Lambda3.jav"));
@ -45,12 +46,12 @@ public class JavaTXCompilerTest {
//filesToTest.add(new File(rootDirectory+"Matrix.jav"));
//filesToTest.add(new File(rootDirectory+"Import.jav"));
JavaTXCompiler compiler = new JavaTXCompiler(fileToTest);
compiler.typeInference();
List<ResultSet> results = compiler.typeInference();
for(File f : compiler.sourceFiles.keySet()){
SourceFile sf = compiler.sourceFiles.get(f);
System.out.println(ASTTypePrinter.print(sf));
List<ResultSet> results = compiler.typeInference();
//List<ResultSet> results = compiler.typeInference(); PL 2017-10-03 vor die For-Schleife gezogen
assert results.size()>0;
for(ResultSet resultSet : results){
Set<TypeInsert> result = TypeInsertFactory.createTypeInsertPoints(sf, resultSet);

6
testBytecode/Field.java Normal file
View File

@ -0,0 +1,6 @@
public class Field{
public void m(){
MethFieldVar mF = new MethFieldVar();
mF.s1 = "Field S1";
}
}

8
testBytecode/Import.java Normal file
View File

@ -0,0 +1,8 @@
import java.util.Vector;
class Import {
void methode(){
Vector v = new Vector();
v.add(v);
}
}

10
testBytecode/Lam1.java Normal file
View File

@ -0,0 +1,10 @@
import java.util.function.Function;
public class Lam1{
public Lam1() {
Function<String,String> fun = (x) -> x+"1";
fun.apply("2");
Runnable lam = () -> System.out.println("lambda");
lam.run();
}
}

8
testBytecode/LamRun.java Normal file
View File

@ -0,0 +1,8 @@
public class LamRun{
public LamRun(){
Runnable lam = () -> System.out.println("lambda");
lam.run();
}
}

View File

@ -0,0 +1,38 @@
public class MethFieldVar{
String s1;// = "";
String s2;
/* public void meth(Integer i, String j, Boolean b){
//String local = "a";
//int localL = local.length();
//int l = s.length();
String s = null;
//s = "";
//return s.length();//l+localL;
}
*/
public void mm(){
// return "mm";
}
public void m2(){
System.out.println("");
// Math.abs(1);
// String lV = "local";
// s1 = "1";
// s1.concat("2");
s2 = s1;
mm();
Clazz i = new Clazz();
Runnable lam = ()->{
String test = "";
String b = "b";
test = b;
System.out.println(test);
};
}
}
class Clazz{}

View File

@ -0,0 +1,6 @@
public class Subclass extends Superclass {
public void printMethod() {
super.printMethod();
}
}

View File

@ -0,0 +1,14 @@
public class Superclass {
public void printMethod() {
System.out.println("Printed in Superclass.");
}
}
public class Subclass extends Superclass {
public void printMethod() {
super.printMethod();
}
}

View File

@ -0,0 +1,6 @@
public class Superclass {
public void printMethod() {
System.out.println("Printed in Superclass.");
}
}

View File

@ -0,0 +1,10 @@
class TestMyTest{
public static void main(String[] a){
//test1
//new TestClass();
//test if statement
//new TestIf(new Boolean(true));
// test lambda
new TestClass();
}
}

1
testBytecode/public Normal file
View File

@ -0,0 +1 @@

5
testBytecode/testF.java Normal file
View File

@ -0,0 +1,5 @@
public class testTets(){
public static void main(String[] args){
new tetsF();
}
}

View File

@ -0,0 +1,5 @@
public class testTets{
public static void main(String[] args){
new TetsF();
}
}

View File

@ -0,0 +1,5 @@
public class testTets(){
public static void main(String[] args){
new tetsF();
}
}

View File

@ -1,3 +0,0 @@
y.output
*.java
*.class

View File

@ -1,46 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project name="JavaCompiler.buildParser" default="JavaParser" basedir=".">
<property name="ParserSrcDir" value="./../src/mycompiler/myparser/"/>
<target name ="JavaParser" depends="JavaLexer" description="Create the file JavaParser.java">
<exec executable="${basedir}/RunJay.sh" os="linux" failonerror="true"/>
<exec executable="${basedir}/RunJayDarwin.sh" os="mac" failonerror="true"/>
<exec executable="${basedir}/RunJay.bat" os="windows" failonerror="true"/>
<!--
<echo message="Hallo Welt: ${ParserSrcDir}JavaParser.jay"/>
<exec executable="ipconfig" failonerror="true">
<arg value="/all"/>
</exec>
-->
<!--
<exec executable="${basedir}/jay.cygwin32" dir="${ParserSrcDir}" failonerror="true">
<arg value="-v ${ParserSrcDir}JavaParser.jay &lt;skeleton&gt; ${ParserSrcDir}JavaParser.java"/>
<arg value="-v"/>
<arg value="${ParserSrcDir}JavaParser.jay"/>
<arg value="&lt;"/>
<arg value="skeleton"/>
<arg value="&gt;"/>
<arg value="${ParserSrcDir}JavaParser.java"/>
</exec>
-->
</target>
<target name ="JavaLexer" depends="Lexer" description="Move the file JavaParser.lex.java to JavaParser.java">
<copy file="${ParserSrcDir}JavaLexer.lex.java" tofile="${ParserSrcDir}JavaLexer.java" failonerror="true"/>
</target>
<target name ="Lexer" description="Create the file JavaLexer.lex.java">
<java classname="JLex.Main"
classpath="${basedir}/JLex_neu.jar"
fork="true"
failonerror="true">
<arg value="${ParserSrcDir}JavaLexer.lex"/>
</java>
</target>
</project>

View File

@ -1,43 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project name="JavaCompiler.buildParser" default="JavaParser" basedir=".">
<property name="ParserSrcDir" value="./../src/de/dhbwstuttgart/parser/"/>
<target name ="JavaParser" depends="JavaLexer" description="Create the file JavaParser.java">
<exec executable="${basedir}/RunJayDarwin.sh" failonerror="true"/>
<!--
<echo message="Hallo Welt: ${ParserSrcDir}JavaParser.jay"/>
<exec executable="ipconfig" failonerror="true">
<arg value="/all"/>
</exec>
-->
<!--
<exec executable="${basedir}/jay.cygwin32" dir="${ParserSrcDir}" failonerror="true">
<arg value="-v ${ParserSrcDir}JavaParser.jay &lt;skeleton&gt; ${ParserSrcDir}JavaParser.java"/>
<arg value="-v"/>
<arg value="${ParserSrcDir}JavaParser.jay"/>
<arg value="&lt;"/>
<arg value="skeleton"/>
<arg value="&gt;"/>
<arg value="${ParserSrcDir}JavaParser.java"/>
</exec>
-->
</target>
<target name ="JavaLexer" depends="Lexer" description="Move the file JavaParser.lex.java to JavaParser.java">
<move file="${ParserSrcDir}JavaLexer.lex.java" tofile="${ParserSrcDir}JavaLexer.java" failonerror="true"/>
</target>
<target name ="Lexer" description="Create the file JavaLexer.lex.java">
<java classname="JLex.Main"
classpath="${basedir}/JLex_neu.jar"
fork="true"
failonerror="true">
<arg value="${ParserSrcDir}JavaLexer.lex"/>
</java>
</target>
</project>

View File

@ -1,42 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project name="JavaCompiler.buildParser" default="JavaParser" basedir=".">
<property name="ParserSrcDir" value="./../src/mycompiler/myparser/"/>
<target name ="JavaParser" depends="JavaLexer" description="Create the file JavaParser.java">
<exec executable="${basedir}/RunJay.sh" failonerror="true"/>
<!--
<echo message="Hallo Welt: ${ParserSrcDir}JavaParser.jay"/>
<exec executable="ipconfig" failonerror="true">
<arg value="/all"/>
</exec>
-->
<!--
<exec executable="${basedir}/jay.cygwin32" dir="${ParserSrcDir}" failonerror="true">
<arg value="-v ${ParserSrcDir}JavaParser.jay &lt;skeleton&gt; ${ParserSrcDir}JavaParser.java"/>
<arg value="-v"/>
<arg value="${ParserSrcDir}JavaParser.jay"/>
<arg value="&lt;"/>
<arg value="skeleton"/>
<arg value="&gt;"/>
<arg value="${ParserSrcDir}JavaParser.java"/>
</exec>
-->
</target>
<target name ="JavaLexer" depends="Lexer" description="Move the file JavaParser.lex.java to JavaParser.java">
<move file="${ParserSrcDir}JavaLexer.lex.java" tofile="${ParserSrcDir}JavaLexer.java" failonerror="true"/>
</target>
<target name ="Lexer" description="Create the file JavaLexer.lex.java">
<java classname="JLex.Main"
classpath="${basedir}/JLex_neu.jar"
fork="true"
failonerror="true">
<arg value="${ParserSrcDir}JavaLexer.lex"/>
</java>
</target>
</project>

View File

@ -1,45 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project name="JavaCompiler.buildParser" default="JavaParser" basedir=".">
<property name="ParserSrcDir" value="./../src/de/dhbwstuttgart/parser/"/>
<target name ="JavaParser" depends="JavaLexer" description="Create the file JavaParser.java">
<!-- Diese Buildfile wird ausgeführt, auch unter linux... -->
<exec executable="${basedir}/RunJay.sh" failonerror="true"/>
<!--
<echo message="Hallo Welt: ${ParserSrcDir}JavaParser.jay"/>
<exec executable="ipconfig" failonerror="true">
<arg value="/all"/>
</exec>
-->
<!--
<exec executable="${basedir}/jay.cygwin32" dir="${ParserSrcDir}" failonerror="true">
<arg value="-v ${ParserSrcDir}JavaParser.jay &lt;skeleton&gt; ${ParserSrcDir}JavaParser.java"/>
<arg value="-v"/>
<arg value="${ParserSrcDir}JavaParser.jay"/>
<arg value="&lt;"/>
<arg value="skeleton"/>
<arg value="&gt;"/>
<arg value="${ParserSrcDir}JavaParser.java"/>
</exec>
-->
</target>
<target name ="JavaLexer" depends="Lexer" description="Move the file JavaParser.lex.java to JavaParser.java">
<move file="${ParserSrcDir}JavaLexer.lex.java" tofile="${ParserSrcDir}JavaLexer.java" failonerror="true"/>
</target>
<target name ="Lexer" description="Create the file JavaLexer.lex.java">
<java classname="JLex.Main"
classpath="${basedir}/JLex_neu.jar"
fork="true"
failonerror="true">
<arg value="${ParserSrcDir}JavaLexer.lex"/>
</java>
</target>
</project>

Binary file not shown.

Binary file not shown.

File diff suppressed because it is too large Load Diff

View File

@ -1,12 +0,0 @@
#!/bin/bash
if [ $1 ] ;
then
/usr/lib/jvm/jdk1.8.0/bin/javac -d './' $1
echo $classfile
classfile=`echo $1 | sed 's/^.*\/\([^/]*\)\.java/\1/'`
#classfile=`pwd`"/$classfile"
echo "Klasse erfolgreich generiert: $classfile"
/usr/lib/jvm/jdk1.8.0/bin/java $classfile
else echo "Fehler: Bitte den Pfad zu einer .java Datei als Parameter übergeben!"
fi

View File

@ -1 +0,0 @@
jay.cygwin32 -v .\..\src\mycompiler\myparser\JavaParser.jay < skeleton > .\..\src\mycompiler\myparser\JavaParser.java

View File

@ -1 +0,0 @@
./../tools/jay -v ./../src/de/dhbwstuttgart/parser/JavaParser.jay < skeleton > ./../src/de/dhbwstuttgart/parser/JavaParser.java

View File

@ -1 +0,0 @@
./../tools/jay.darwin -v ./../src/de/dhbwstuttgart/parser/JavaParser.jay < skeleton > ./../src/de/dhbwstuttgart/parser/JavaParser.java

Some files were not shown because too many files have changed in this diff Show More