Merge branch 'addPackages' of ssh://gohorb.ba-horb.de/bahome/projekt/git/JavaCompilerCore into addPackages

This commit is contained in:
pl@gohorb.ba-horb.de 2019-12-23 11:25:36 +01:00
commit 7fd03d505e
40 changed files with 453 additions and 125 deletions

3
.gitignore vendored
View File

@ -23,3 +23,6 @@ bin
#
manually/
logFiles/**
!logFiles/.gitkeep

19
abgabeprotokoll.md Normal file
View File

@ -0,0 +1,19 @@
# JavaTXCompiler Klasse
* Konstruktor hat einen weiteren Parameter
* contextPath
* Arrays aus URLs (file-urls)
* Parameter ist Optional
* wird er gesetzt, so werden Classfiles aus den übergebenen Pfaden geladen
* die übergebenen Pfade müssen dabei die Source-Roots sein
* Beispiel:
`import de.test.Klasse;`
* `Klasse.class` liegt in `output/de/test/Klasse.class`
* dann muss contextpath auf `output` gesetzt werden
* wird der Parameter nicht übergeben, so wird der Sourceroot auf das Verzeichnis gesetzt, in dem der Compiler ausgeführt wird
* dies ist das Verhalten vom javac Compiler
* generateBytecode - Methode hat neuen Parameter: path
* wird hier null übergeben, so wird die class-File in den gleichen Ordner wie die übergebene .jav File geschrieben
* wird hier ein Pfad übergeben, so gilt dieser als output root.
* Klassen werden in outputRoot/package/name/KlassenName.class geschrieben

0
logFiles/.gitkeep Normal file
View File

View File

@ -7,6 +7,7 @@ import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import de.dhbwstuttgart.parser.scope.JavaClassName;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.FieldVisitor;
import org.objectweb.asm.MethodVisitor;
@ -81,7 +82,7 @@ public class BytecodeGen implements ASTVisitor {
String type;
public static RefTypeOrTPHOrWildcardOrGeneric THISTYPE = null;
private String className;
private JavaClassName className;
private String pkgName;
private boolean isInterface;
private Collection<ResultSet> listOfResultSets;
@ -107,7 +108,7 @@ public class BytecodeGen implements ASTVisitor {
HashMap<String, RefTypeOrTPHOrWildcardOrGeneric> methodParamsAndTypes = new HashMap<>();
byte[] bytecode;
HashMap<String, byte[]> classFiles;
HashMap<JavaClassName, byte[]> classFiles;
private final ArrayList<String> methodNameAndParamsT = new ArrayList<>();
private final ArrayList<String> fieldNameAndParamsT = new ArrayList<>();
@ -118,24 +119,26 @@ public class BytecodeGen implements ASTVisitor {
private GenericsGeneratorResultForClass generatedGenerics;
private Resolver resolver;
private final ClassLoader classLoader;
public BytecodeGen(HashMap<String, byte[]> classFiles, Collection<ResultSet> listOfResultSets, List<GenericGenratorResultForSourceFile> simplifyResultsForAllSourceFiles, SourceFile sf,
String path) {
public BytecodeGen(HashMap<JavaClassName, byte[]> classFiles, Collection<ResultSet> listOfResultSets, List<GenericGenratorResultForSourceFile> simplifyResultsForAllSourceFiles, SourceFile sf,
String path, ClassLoader classLoader) {
this.classFiles = classFiles;
this.listOfResultSets = listOfResultSets;
this.simplifyResultsForAllSourceFiles = simplifyResultsForAllSourceFiles;
this.sf = sf;
this.path = path;
this.pkgName = sf.getPkgName();
this.classLoader = classLoader;
}
@Override
public void visit(SourceFile sourceFile) {
for (ClassOrInterface cl : sourceFile.getClasses()) {
System.out.println("in Class: " + cl.getClassName().toString());
BytecodeGen classGen = new BytecodeGen(classFiles, listOfResultSets, simplifyResultsForAllSourceFiles, sf, path);
BytecodeGen classGen = new BytecodeGen(classFiles, listOfResultSets, simplifyResultsForAllSourceFiles, sf, path, classLoader);
cl.accept(classGen);
classGen.writeClass(cl.getClassName().toString());
classGen.writeClass(cl.getClassName());
}
}
@ -145,22 +148,22 @@ public class BytecodeGen implements ASTVisitor {
*
* @param name name of the class with which the bytecode is to be associated
*/
private void writeClass(String name) {
private void writeClass(JavaClassName name) {
bytecode = cw.toByteArray();
classFiles.put(name, bytecode);
}
public HashMap<String, byte[]> getClassFiles() {
public HashMap<JavaClassName, byte[]> getClassFiles() {
return classFiles;
}
@Override
public void visit(ClassOrInterface classOrInterface) {
className = classOrInterface.getClassName().toString();
className = classOrInterface.getClassName();
cw.visitSource(className + ".jav", null);
cw.visitSource(className.getClassName() + ".jav", null);
isInterface = (classOrInterface.getModifiers() & 512) == 512;
@ -172,7 +175,7 @@ public class BytecodeGen implements ASTVisitor {
// resultSet = listOfResultSets.get(0);
boolean isVisited = false;
List<ResultSet> listOfResultSetsList = new ArrayList<>(listOfResultSets);
generatedGenerics = simplifyResultsForAllSourceFiles.stream().map(sr->sr.getSimplifyResultsByName(pkgName, className)).findFirst().get();
generatedGenerics = simplifyResultsForAllSourceFiles.stream().map(sr->sr.getSimplifyResultsByName(className)).findFirst().get();
for (int i = 0; i < listOfResultSetsList.size(); i++) {
//for (ResultSet rs : listOfResultSets) {
superClass = classOrInterface.getSuperClass().acceptTV(new TypeToDescriptor());
@ -199,7 +202,7 @@ public class BytecodeGen implements ASTVisitor {
System.out.println("Signature: => " + sig);
}
cw.visit(Opcodes.V1_8, acc, classOrInterface.getClassName().toString(), sig,
cw.visit(Opcodes.V1_8, acc, classOrInterface.getClassName().toString().replace(".", "/"), sig,
classOrInterface.getSuperClass().acceptTV(new TypeToDescriptor()), null);
isVisited = true;
@ -269,7 +272,7 @@ public class BytecodeGen implements ASTVisitor {
constructorPos += 1;
BytecodeGenMethod gen = new BytecodeGenMethod(className, superClass, resultSet, field, mv, paramsAndLocals, cw,
genericsAndBoundsMethod, genericsAndBounds, isInterface, classFiles, sf, path, block, constructorPos);
genericsAndBoundsMethod, genericsAndBounds, isInterface, classFiles, sf, path, block, constructorPos, classLoader);
if (!field.getParameterList().iterator().hasNext()
&& !(field.block.statements.get(field.block.statements.size() - 1) instanceof ReturnVoid)) {
mv.visitInsn(Opcodes.RETURN);
@ -350,7 +353,7 @@ public class BytecodeGen implements ASTVisitor {
mv.visitCode();
BytecodeGenMethod gen = new BytecodeGenMethod(className, superClass, resultSet, method, mv, paramsAndLocals, cw,
genericsAndBoundsMethod, genericsAndBounds, isInterface, classFiles, sf, path);
genericsAndBoundsMethod, genericsAndBounds, isInterface, classFiles, sf, path, classLoader);
mv.visitMaxs(0, 0);
mv.visitEnd();

View File

@ -15,6 +15,7 @@ import java.util.List;
import de.dhbwstuttgart.bytecode.utilities.*;
import de.dhbwstuttgart.exceptions.NotImplementedException;
import de.dhbwstuttgart.parser.scope.JavaClassName;
import de.dhbwstuttgart.syntaxtree.statement.*;
import de.dhbwstuttgart.syntaxtree.statement.BinaryExpr.Operator;
import de.dhbwstuttgart.syntaxtree.statement.UnaryExpr.Operation;
@ -45,7 +46,7 @@ public class BytecodeGenMethod implements StatementVisitor {
private Method m;
private MethodVisitor mv;
private HashMap<String, Integer> paramsAndLocals = new HashMap<>();
private String className;
private JavaClassName className;
private int lamCounter;
private ClassWriter cw;
private ResultSet resultSet;
@ -71,16 +72,18 @@ public class BytecodeGenMethod implements StatementVisitor {
private boolean isRightSideALambda = false;
private KindOfLambda kindOfLambda;
private HashMap<String, byte[]> classFiles;
private HashMap<JavaClassName, byte[]> classFiles;
private int constructorPos = 0;
private ArrayList<RefTypeOrTPHOrWildcardOrGeneric> varsFunInterface = new ArrayList<>();;
private final ClassLoader classLoader;
// generate bytecode for constructor
public BytecodeGenMethod(String className, String superClass,ResultSet resultSet, Method m, MethodVisitor mv,
HashMap<String, Integer> paramsAndLocals, ClassWriter cw, HashMap<String, String> genericsAndBoundsMethod,
HashMap<String, String> genericsAndBounds, boolean isInterface, HashMap<String, byte[]> classFiles,
SourceFile sf,String path, Block block, int constructorPos) {
public BytecodeGenMethod(JavaClassName className, String superClass, ResultSet resultSet, Method m, MethodVisitor mv,
HashMap<String, Integer> paramsAndLocals, ClassWriter cw, HashMap<String, String> genericsAndBoundsMethod,
HashMap<String, String> genericsAndBounds, boolean isInterface, HashMap<JavaClassName, byte[]> classFiles,
SourceFile sf, String path, Block block, int constructorPos, ClassLoader classLoader) {
this.className = className;
this.superClass = superClass;
@ -98,16 +101,15 @@ public class BytecodeGenMethod implements StatementVisitor {
this.path = path;
this.lamCounter = -1;
this.constructorPos = constructorPos;
this.classLoader = classLoader;
if(block != null)
this.blockFieldInit = block;
this.m.block.accept(this);
}
public BytecodeGenMethod(String className, String superClass,ResultSet resultSet, Method m, MethodVisitor mv,
public BytecodeGenMethod(JavaClassName className, String superClass,ResultSet resultSet, Method m, MethodVisitor mv,
HashMap<String, Integer> paramsAndLocals, ClassWriter cw, HashMap<String, String> genericsAndBoundsMethod,
HashMap<String, String> genericsAndBounds, boolean isInterface, HashMap<String, byte[]> classFiles, SourceFile sf,String path) {
HashMap<String, String> genericsAndBounds, boolean isInterface, HashMap<JavaClassName, byte[]> classFiles, SourceFile sf,String path, ClassLoader classLoader) {
this.className = className;
this.superClass = superClass;
@ -124,14 +126,14 @@ public class BytecodeGenMethod implements StatementVisitor {
this.sf = sf;
this.path = path;
this.lamCounter = -1;
this.classLoader = classLoader;
if (!isInterface)
this.m.block.accept(this);
}
public BytecodeGenMethod(String className, ClassWriter cw, LambdaExpression lambdaExpression, ArrayList<String> usedVars, ResultSet resultSet, MethodVisitor mv,
int indexOfFirstParamLam, boolean isInterface, HashMap<String, byte[]> classFiles, String path, int lamCounter, SourceFile sf,HashMap<String, String> genericsAndBoundsMethod,
HashMap<String, String> genericsAndBounds) {
public BytecodeGenMethod(JavaClassName className, ClassWriter cw, LambdaExpression lambdaExpression, ArrayList<String> usedVars, ResultSet resultSet, MethodVisitor mv,
int indexOfFirstParamLam, boolean isInterface, HashMap<JavaClassName, byte[]> classFiles, String path, int lamCounter, SourceFile sf,HashMap<String, String> genericsAndBoundsMethod,
HashMap<String, String> genericsAndBounds, ClassLoader classLoader) {
this.className = className;
this.cw = cw;
this.resultSet = resultSet;
@ -144,6 +146,7 @@ public class BytecodeGenMethod implements StatementVisitor {
this.sf = sf;
this.genericsAndBoundsMethod = genericsAndBoundsMethod;
this.genericsAndBounds = genericsAndBounds;
this.classLoader = classLoader;
Iterator<FormalParameter> itr = lambdaExpression.params.iterator();
int i = indexOfFirstParamLam;
@ -622,7 +625,7 @@ public class BytecodeGenMethod implements StatementVisitor {
}
String newDesc = addUsedVarsToDesugaredMethodDescriptor(lamDesc);
// first check if capturing lambda then invokestatic or invokespecial
Handle arg2 = new Handle(staticOrSpecial, this.className, desugaredMethodName, newDesc, false);
Handle arg2 = new Handle(staticOrSpecial, this.className.getClassName(), desugaredMethodName, newDesc, false);
// Descriptor of functional interface methode
SamMethod samMethod = new SamMethod(kindOfLambda.getArgumentList(), lambdaExpression.getType());
// Desc: (this/nothing)TargetType
@ -636,7 +639,7 @@ public class BytecodeGenMethod implements StatementVisitor {
new BytecodeGenMethod(className, cw,lambdaExpression, usedVars,this.resultSet, mvLambdaBody, indexOfFirstParamLam, isInterface,
classFiles,this.path, lamCounter, sf, genericsAndBoundsMethod,
genericsAndBounds);
genericsAndBounds, classLoader);
mvLambdaBody.visitMaxs(0, 0);
mvLambdaBody.visitEnd();
@ -756,7 +759,7 @@ public class BytecodeGenMethod implements StatementVisitor {
MethodCallHelper helper = new MethodCallHelper(methodCall, sf, resultSet, path);
ClassLoader cLoader = ClassLoader.getSystemClassLoader();
ClassLoader cLoader = this.classLoader;
// This will be used if the class is not standard class (not in API)
ClassLoader cLoader2;
@ -797,7 +800,7 @@ public class BytecodeGenMethod implements StatementVisitor {
}
if(methodRefl == null) {
boolean toCreate = !receiverName.equals(className) && helper.isInCurrPkg(clazz);
boolean toCreate = !receiverName.equals(className.getClassName()) && helper.isInCurrPkg(clazz);
if(toCreate) {
try {
mDesc = helper.getDesc(clazz);
@ -811,7 +814,7 @@ public class BytecodeGenMethod implements StatementVisitor {
// mDesc = helper.generateBCForFunN(methCallType,typesOfParams);
}else {
try {
cLoader2 = new URLClassLoader(new URL[] {new URL("file://"+path)});
cLoader2 = new URLClassLoader(new URL[] {new URL("file://"+path)}, classLoader);
java.lang.reflect.Method[] methods = cLoader2.loadClass(clazz).getMethods();
System.out.println("Methods of " + receiverName + " ");
for(int i = 0; i<methods.length; i++) {
@ -832,7 +835,7 @@ public class BytecodeGenMethod implements StatementVisitor {
System.out.println("Methodcall type : " + resultSet.resolveType(methodCall.getType()).resolvedType.acceptTV(new TypeToDescriptor()));
List<Boolean> argListMethCall = new LinkedList<>();
String receiverRefl="";
if(methodRefl == null && receiverName.equals(className)) {
if(methodRefl == null && receiverName.equals(className.getClassName())) {
MethodFromMethodCall method = new MethodFromMethodCall(methodCall.arglist, methodCall.getType(),
receiverName, genericsAndBoundsMethod, genericsAndBounds);
mDesc = method.accept(new DescriptorToString(resultSet));

View File

@ -16,6 +16,7 @@ import de.dhbwstuttgart.bytecode.utilities.MethodAndTPH;
import de.dhbwstuttgart.bytecode.utilities.MethodUtility;
import de.dhbwstuttgart.bytecode.utilities.Resolver;
import de.dhbwstuttgart.parser.SyntaxTreeGenerator.AssignToLocal;
import de.dhbwstuttgart.parser.scope.JavaClassName;
import de.dhbwstuttgart.syntaxtree.ASTVisitor;
import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
import de.dhbwstuttgart.syntaxtree.Constructor;
@ -75,7 +76,7 @@ public class GeneratedGenericsFinder implements ASTVisitor {
private final List<String> methodNameAndParamsT = new ArrayList<>();
private String pkgName;
private String className;
private JavaClassName className;
private Resolver resolver;
/**
@ -117,7 +118,7 @@ public class GeneratedGenericsFinder implements ASTVisitor {
*/
@Override
public void visit(ClassOrInterface classOrInterface) {
className = classOrInterface.getClassName().toString();
className = classOrInterface.getClassName();
List<ResultSet> listOfResultSetsList = new ArrayList<>(listOfResultSets);
boolean isVisited = false;

View File

@ -12,6 +12,7 @@ import java.util.Set;
import java.util.stream.Collectors;
import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.*;
import de.dhbwstuttgart.parser.scope.JavaClassName;
import org.objectweb.asm.Type;
import de.dhbwstuttgart.bytecode.TPHExtractor;
@ -30,7 +31,7 @@ public class GenericsGenerator {
class constraints: tphClass < tphMeth1, tphMeth1 < tphMeth2, tphMeth2 < Object
*/
public static GenericsGeneratorResultForClass generateConstraints(final String className, final TPHExtractor tphExtractor,
public static GenericsGeneratorResultForClass generateConstraints(final JavaClassName className, final TPHExtractor tphExtractor,
final List<String> tphsClass, final ConstraintsSimplierResult simplifiedConstraints) {
List<GenericsGeneratorResult> classConstraints = generateConstraintsForClass(tphExtractor,

View File

@ -3,6 +3,8 @@
*/
package de.dhbwstuttgart.bytecode.genericsGeneratorTypes;
import de.dhbwstuttgart.parser.scope.JavaClassName;
import java.util.ArrayList;
import java.util.List;
import java.util.NoSuchElementException;
@ -37,9 +39,9 @@ public class GenericGenratorResultForSourceFile {
genericGeneratorResultForAllClasses.add(sResClass);
}
public GenericsGeneratorResultForClass getSimplifyResultsByName(String pkgName, String name) {
public GenericsGeneratorResultForClass getSimplifyResultsByName(JavaClassName name) {
if (this.pkgName.equals(pkgName)) {
if (this.pkgName.equals(name.getPackageName())) {
return genericGeneratorResultForAllClasses.stream()
.filter(sr -> sr.getClassName().equals(name))
.findAny()

View File

@ -3,6 +3,8 @@
*/
package de.dhbwstuttgart.bytecode.genericsGeneratorTypes;
import de.dhbwstuttgart.parser.scope.JavaClassName;
import java.util.Collections;
import java.util.List;
@ -11,11 +13,11 @@ import java.util.List;
*
*/
public class GenericsGeneratorResultForClass {
private final String className;
private final JavaClassName className;
private final List<GenericsGeneratorResult> classConstraints;
private final GenericGeneratorResultsForAllMethods methodsAndTheirConstraints;
public GenericsGeneratorResultForClass(String className) {
public GenericsGeneratorResultForClass(JavaClassName className) {
this(className, Collections.emptyList(), new GenericGeneratorResultsForAllMethods());
}
/**
@ -23,8 +25,8 @@ public class GenericsGeneratorResultForClass {
* @param classConstraints
* @param methodsAndTheirConstraints
*/
public GenericsGeneratorResultForClass(String className, List<GenericsGeneratorResult> classConstraints,
GenericGeneratorResultsForAllMethods methodsAndTheirConstraints) {
public GenericsGeneratorResultForClass(JavaClassName className, List<GenericsGeneratorResult> classConstraints,
GenericGeneratorResultsForAllMethods methodsAndTheirConstraints) {
this.className = className;
this.classConstraints = classConstraints;
this.methodsAndTheirConstraints = methodsAndTheirConstraints;
@ -33,7 +35,7 @@ public class GenericsGeneratorResultForClass {
/**
* @return the className
*/
public String getClassName() {
public JavaClassName getClassName() {
return className;
}

View File

@ -1,6 +1,7 @@
//PL 2018-12-19: typeInferenceOld nach typeInference uebertragen
package de.dhbwstuttgart.core;
import com.google.common.collect.Lists;
import de.dhbwstuttgart.bytecode.BytecodeGen;
import de.dhbwstuttgart.bytecode.Exception.BytecodeGeneratorError;
import de.dhbwstuttgart.bytecode.genericsGenerator.GeneratedGenericsFinder;
@ -41,6 +42,8 @@ import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
@ -50,24 +53,34 @@ public class JavaTXCompiler {
public static JavaTXCompiler INSTANCE;
final CompilationEnvironment environment;
Boolean resultmodel = true;
Boolean resultmodel = true;
public final Map<File, SourceFile> sourceFiles = new HashMap<>();
Boolean log = true; //gibt an ob ein Log-File nach System.getProperty("user.dir")+"src/test/java/logFiles" geschrieben werden soll?
public volatile UnifyTaskModel usedTasks = new UnifyTaskModel();
public JavaTXCompiler(File sourceFile) throws IOException, ClassNotFoundException {
this(Arrays.asList(sourceFile));
INSTANCE = this;
}
private final ClassLoader classLoader;
public JavaTXCompiler(File sourceFile) throws IOException, ClassNotFoundException {
this(Arrays.asList(sourceFile), null);
INSTANCE = this;
}
public JavaTXCompiler(File sourceFile, Boolean log) throws IOException, ClassNotFoundException {
this(sourceFile);
this.log = log;
INSTANCE = this;
}
public JavaTXCompiler(List<File> sources) throws IOException, ClassNotFoundException {
environment = new CompilationEnvironment(sources);
public JavaTXCompiler(List<File> sourceFiles) throws IOException, ClassNotFoundException {
this(sourceFiles, null);
INSTANCE = this;
}
public JavaTXCompiler(List<File> sources, List<URL> contextPath) throws IOException, ClassNotFoundException {
if(contextPath == null || contextPath.isEmpty()){
//When no contextPaths are given, the working directory is the sources root
contextPath = Lists.newArrayList(new URL("file://" + System.getProperty("user.dir")));
}
classLoader = new URLClassLoader(contextPath.toArray(new URL[0]), ClassLoader.getSystemClassLoader());
environment = new CompilationEnvironment(sources);
for (File s : sources) {
sourceFiles.put(s, parse(s));
}
@ -85,7 +98,7 @@ public class JavaTXCompiler {
for (JavaClassName name : sourceFiles.get(forSourceFile).getImports()) {
//TODO: Hier werden imports von eigenen (.jav) Klassen nicht beachtet
ClassOrInterface importedClass = ASTFactory.createClass(
ClassLoader.getSystemClassLoader().loadClass(name.toString()));
classLoader.loadClass(name.toString()));
importedClasses.add(importedClass);
}
allClasses.addAll(importedClasses);
@ -122,7 +135,7 @@ public class JavaTXCompiler {
for (JavaClassName name : forSourceFile.getImports()) {
// TODO: Hier werden imports von eigenen (.jav) Klassen nicht beachtet
ClassOrInterface importedClass = ASTFactory
.createClass(ClassLoader.getSystemClassLoader().loadClass(name.toString()));
.createClass(classLoader.loadClass(name.toString()));
importedClasses.add(importedClass);
allClasses.addAll(importedClasses);
}
@ -291,7 +304,7 @@ public class JavaTXCompiler {
logFile = logFile == null
? new FileWriter(new File("log_" + sourceFiles.keySet().iterator().next().getName()))
: logFile;
IFiniteClosure finiteClosure = UnifyTypeFactory.generateFC(allClasses, logFile);
IFiniteClosure finiteClosure = UnifyTypeFactory.generateFC(allClasses, logFile, classLoader);
System.out.println(finiteClosure);
urm = new UnifyResultModel(cons, finiteClosure);
urm.addUnifyResultListener(resultListener);
@ -459,7 +472,7 @@ public class JavaTXCompiler {
// File(System.getProperty("user.dir")+"/src/test/resources/logFiles/"+"log_"+sourceFiles.keySet().iterator().next().getName()));
new FileWriter(new File(System.getProperty("user.dir") + "/logFiles/" + "log_"
+ sourceFiles.keySet().iterator().next().getName()));
IFiniteClosure finiteClosure = UnifyTypeFactory.generateFC(allClasses, logFile);
IFiniteClosure finiteClosure = UnifyTypeFactory.generateFC(allClasses, logFile, classLoader);
System.out.println(finiteClosure);
ConstraintSet<UnifyPair> unifyCons = UnifyTypeFactory.convert(cons);
@ -708,17 +721,17 @@ public class JavaTXCompiler {
private SourceFile parse(File sourceFile) throws IOException, java.lang.ClassNotFoundException {
CompilationUnitContext tree = JavaTXParser.parse(sourceFile);
SyntaxTreeGenerator generator = new SyntaxTreeGenerator(environment.getRegistry(sourceFile),
SyntaxTreeGenerator generator = new SyntaxTreeGenerator(environment.getRegistry(sourceFile, classLoader),
new GenericsRegistry(null));
SourceFile ret = generator.convert(tree, environment.packageCrawler);
SourceFile ret = generator.convert(tree, environment.packageCrawler, classLoader);
return ret;
}
public void generateBytecodForFile(String path, HashMap<String, byte[]> classFiles, SourceFile sf,
public void generateBytecodForFile(String path, HashMap<JavaClassName, byte[]> classFiles, SourceFile sf,
List<ResultSet> typeinferenceResult) throws IOException {
try {
List<GenericGenratorResultForSourceFile> genericResults = getGeneratedGenericResultsForAllSourceFiles(typeinferenceResult);
BytecodeGen bytecodeGen = new BytecodeGen(classFiles,typeinferenceResult, genericResults, sf,path);
BytecodeGen bytecodeGen = new BytecodeGen(classFiles,typeinferenceResult, genericResults, sf,path, classLoader);
bytecodeGen.visit(sf);
this.writeClassFile(bytecodeGen.getClassFiles(), path);
} catch (ClassNotFoundException e) {
@ -753,7 +766,13 @@ public class JavaTXCompiler {
return result;
}
// um pfad erweitern
public void generateBytecode() throws ClassNotFoundException, IOException, BytecodeGeneratorError {
generateBytecode(null);
}
/**
* @param path - can be null, then class file output is in the same directory as the parsed source files
*/
public void generateBytecode(String path) throws ClassNotFoundException, IOException, BytecodeGeneratorError {
List<ResultSet> typeinferenceResult = this.typeInference();
List<GenericGenratorResultForSourceFile> simplifyResultsForAllSourceFiles = getGeneratedGenericResultsForAllSourceFiles(
@ -770,27 +789,30 @@ public class JavaTXCompiler {
public void generateBytecode(String path, List<ResultSet> typeinferenceResult,
List<GenericGenratorResultForSourceFile> simplifyResultsForAllSourceFiles) throws IOException {
for (File f : sourceFiles.keySet()) {
HashMap<String, byte[]> classFiles = new HashMap<>();
HashMap<JavaClassName, byte[]> classFiles = new HashMap<>();
SourceFile sf = sourceFiles.get(f);
BytecodeGen bytecodeGen = new BytecodeGen(classFiles, typeinferenceResult, simplifyResultsForAllSourceFiles,
sf, path);
sf, path, classLoader);
bytecodeGen.visit(sf);
String packagePath = sf.getPkgName().replace(".","/");
if(path == null){
path = f.getPath();
path = f.getParent(); //Set path to path of the parsed .jav file
}else{
path += sf.getPkgName().replace(".","/"); //add package path to root path
}
writeClassFile(bytecodeGen.getClassFiles(), path + packagePath);
writeClassFile(bytecodeGen.getClassFiles(), path);
}
}
private void writeClassFile(HashMap<String, byte[]> classFiles, String path) throws IOException {
private void writeClassFile(HashMap<JavaClassName, byte[]> classFiles, String path) throws IOException {
FileOutputStream output;
for (String name : classFiles.keySet()) {
for (JavaClassName name : classFiles.keySet()) {
byte[] bytecode = classFiles.get(name);
System.out.println("generating " + name + ".class file ...");
// output = new FileOutputStream(new File(System.getProperty("user.dir") +
// "/testBytecode/generatedBC/" +name+".class"));
output = new FileOutputStream(new File(path + name + ".class"));
File outputFile = new File(path + File.separator + name.getClassName() + ".class");
outputFile.getParentFile().mkdirs();
output = new FileOutputStream(outputFile);
output.write(bytecode);
output.close();
System.out.println(name + ".class file generated");

View File

@ -50,7 +50,6 @@ public class CompilationEnvironment {
*/
//String bootClassPath = System.getProperty("sun.boot.class.path");
// ClassLoader cl = ClassLoader.getPlatformClassLoader();
ClassLoader cl = ClassLoader.getSystemClassLoader();
String bootClassPath = System.getProperty("java.class.path");
librarys = new ArrayList<>();
for(String path : bootClassPath.split(File.pathSeparator)) {
@ -67,10 +66,10 @@ public class CompilationEnvironment {
this.packageCrawler = new PackageCrawler(librarys);
}
public JavaClassRegistry getRegistry(File forSourceFile) throws ClassNotFoundException, IOException {
public JavaClassRegistry getRegistry(File forSourceFile, ClassLoader classLoader) throws ClassNotFoundException, IOException {
Map<String, Integer> allNames;
CompilationUnitContext tree = JavaTXParser.parse(forSourceFile);
allNames = GatherNames.getNames(tree, packageCrawler);
allNames = GatherNames.getNames(tree, packageCrawler, classLoader);
return new JavaClassRegistry(allNames);
}

View File

@ -22,16 +22,16 @@ public class FCGenerator {
*
* @param availableClasses - Alle geparsten Klassen
*/
public static Set<UnifyPair> toUnifyFC(Collection<ClassOrInterface> availableClasses) throws ClassNotFoundException {
return toFC(availableClasses).stream().map(t -> UnifyTypeFactory.convert(t)).collect(Collectors.toSet());
public static Set<UnifyPair> toUnifyFC(Collection<ClassOrInterface> availableClasses, ClassLoader classLoader) throws ClassNotFoundException {
return toFC(availableClasses, classLoader).stream().map(t -> UnifyTypeFactory.convert(t)).collect(Collectors.toSet());
}
public static Set<Pair> toFC(Collection<ClassOrInterface> availableClasses) throws ClassNotFoundException {
public static Set<Pair> toFC(Collection<ClassOrInterface> availableClasses, ClassLoader classLoader) throws ClassNotFoundException {
HashSet<Pair> pairs = new HashSet<>();
//PL 2018-09-18: gtvs vor die for-Schleife gezogen, damit immer die gleichen Typeplaceholder eingesetzt werden.
HashMap<String, RefTypeOrTPHOrWildcardOrGeneric> gtvs = new HashMap<>();
for(ClassOrInterface cly : availableClasses){
pairs.addAll(getSuperTypes(cly, availableClasses, gtvs));
pairs.addAll(getSuperTypes(cly, availableClasses, gtvs, classLoader));
}
return pairs;
}
@ -48,8 +48,8 @@ public class FCGenerator {
* @param forType
* @return
*/
private static List<Pair> getSuperTypes(ClassOrInterface forType, Collection<ClassOrInterface> availableClasses) throws ClassNotFoundException {
return getSuperTypes(forType, availableClasses, new HashMap<>());
private static List<Pair> getSuperTypes(ClassOrInterface forType, Collection<ClassOrInterface> availableClasses, ClassLoader classLoader) throws ClassNotFoundException {
return getSuperTypes(forType, availableClasses, new HashMap<>(), classLoader);
}
/**
@ -61,7 +61,7 @@ public class FCGenerator {
* @throws ClassNotFoundException
*/
private static List<Pair> getSuperTypes(ClassOrInterface forType, Collection<ClassOrInterface> availableClasses,
HashMap<String, RefTypeOrTPHOrWildcardOrGeneric> gtvs) throws ClassNotFoundException {
HashMap<String, RefTypeOrTPHOrWildcardOrGeneric> gtvs, ClassLoader classLoader) throws ClassNotFoundException {
List<RefTypeOrTPHOrWildcardOrGeneric> params = new ArrayList<>();
//Die GTVs, die in forType hinzukommen:
HashMap<String, RefTypeOrTPHOrWildcardOrGeneric> newGTVs = new HashMap<>();
@ -86,7 +86,7 @@ public class FCGenerator {
ClassOrInterface superClass;
if(!hasSuperclass.isPresent()) //Wenn es die Klasse in den available Klasses nicht gibt wird sie im Classpath gesucht. Ansonsten Exception
{
superClass = ASTFactory.createClass(ClassLoader.getSystemClassLoader().loadClass(superType.getName().toString()));
superClass = ASTFactory.createClass(classLoader.loadClass(superType.getName().toString()));
}else{
superClass = hasSuperclass.get();
}
@ -120,7 +120,7 @@ public class FCGenerator {
if(superClass.getClassName().equals(ASTFactory.createObjectClass().getClassName())){
superTypes = Arrays.asList(new Pair(ASTFactory.createObjectType(), ASTFactory.createObjectType(), PairOperator.SMALLER));
}else{
superTypes = getSuperTypes(superClass, availableClasses, newGTVs);
superTypes = getSuperTypes(superClass, availableClasses, newGTVs, classLoader);
}
retList.add(ret);

View File

@ -279,9 +279,21 @@ public class StatementGenerator {
}
private Statement convert(Java8Parser.ClassInstanceCreationExpressionContext stmt) {
//TODO
throw new NotImplementedException();
private Statement convert(Java8Parser.ClassInstanceCreationExpressionContext newExpression) {
Java8Parser.TypeArgumentsContext genericArgs = null;
if(newExpression.expressionName()!= null)throw new NotImplementedException();
if(newExpression.typeArgumentsOrDiamond()!= null){
if(newExpression.typeArgumentsOrDiamond().typeArguments()!=null){
genericArgs = newExpression.typeArgumentsOrDiamond().typeArguments();
}
}
if(newExpression.typeArguments()!= null)throw new NotImplementedException();
TerminalNode identifier = newExpression.Identifier(0);
RefType newClass = (RefType) TypeGenerator.convertTypeName(identifier.getText(),genericArgs,identifier.getSymbol(),reg,generics);
ArgumentList args = convert(newExpression.argumentList());
return new NewClass(newClass, args, newExpression.getStart());
}
private Statement convert(Java8Parser.PreIncrementExpressionContext stmt) {

View File

@ -19,6 +19,7 @@ import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import java.lang.reflect.Modifier;
import java.net.URL;
import java.sql.Ref;
import java.util.*;
import java.util.stream.Collectors;
@ -74,10 +75,10 @@ public class SyntaxTreeGenerator{
return ret;
}
public SourceFile convert(Java8Parser.CompilationUnitContext ctx, PackageCrawler packageCrawler) throws ClassNotFoundException{
public SourceFile convert(Java8Parser.CompilationUnitContext ctx, PackageCrawler packageCrawler, ClassLoader classLoader) throws ClassNotFoundException{
if(ctx.packageDeclaration()!=null)this.pkgName = convert(ctx.packageDeclaration());
List<ClassOrInterface> classes = new ArrayList<>();
Map<String, Integer> imports = GatherNames.getImports(ctx, packageCrawler);
Map<String, Integer> imports = GatherNames.getImports(ctx, packageCrawler, classLoader);
this.imports = imports.keySet().stream().map(name -> reg.getName(name)).collect(Collectors.toSet());
for(Java8Parser.TypeDeclarationContext typeDecl : ctx.typeDeclaration()){
ClassOrInterface newClass;

View File

@ -1,9 +1,8 @@
package de.dhbwstuttgart.parser.scope;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.*;
import de.dhbwstuttgart.parser.antlr.Java8BaseListener;
import de.dhbwstuttgart.syntaxtree.AbstractASTWalker;
@ -15,7 +14,7 @@ import de.dhbwstuttgart.parser.antlr.Java8Parser;
public class GatherNames {
public static Map<String, Integer> getNames(Java8Parser.CompilationUnitContext ctx, PackageCrawler packages) throws ClassNotFoundException{
public static Map<String, Integer> getNames(Java8Parser.CompilationUnitContext ctx, PackageCrawler packages, ClassLoader classLoader) throws ClassNotFoundException{
Map<String, Integer> ret = new HashMap<>();
String pkgName = getPackageName(ctx);
String nameString = "";
@ -64,14 +63,13 @@ public class GatherNames {
}
}
}
ret.putAll(getImports(ctx, packages));
ret.putAll(getImports(ctx, packages, classLoader));
return ret;
}
public static Map<String, Integer> getImports(Java8Parser.CompilationUnitContext ctx, PackageCrawler packages) throws ClassNotFoundException {
public static Map<String, Integer> getImports(Java8Parser.CompilationUnitContext ctx, PackageCrawler packages, ClassLoader classLoader) throws ClassNotFoundException {
Map<String, Integer> ret = new HashMap<>();
ClassLoader classLoader = ClassLoader.getSystemClassLoader();
//ret.putAll(packages.getClassNames("java.lang"));
for(Java8Parser.ImportDeclarationContext importDeclCtx : ctx.importDeclaration()){
if(importDeclCtx.singleTypeImportDeclaration() != null){

View File

@ -84,7 +84,15 @@ public class JavaClassName {
@Override
public String toString() {
return (packageName!=null ? packageName.toString() : "") + name;
return (packageName!=null ? packageName.toString() + "." : "") + name;
}
public String getPackageName() {
return (packageName!=null ? packageName.toString() : "");
}
public String getClassName(){
return name;
}
}
@ -130,6 +138,9 @@ class PackageName{
String ret = "";
if(names == null)return "";
for(String n : names)ret+=n+".";
if (ret != null && ret.length() > 0 && ret.charAt(ret.length() - 1) == '.') {
ret = ret.substring(0, ret.length() - 1);
}
return ret;
}
}

View File

@ -16,9 +16,9 @@ import java.util.*;
public class ASPFactory implements TypeVisitor<String>{
public static String generateASP(ConstraintSet<Pair> constraints, Collection<ClassOrInterface> fcClasses) throws ClassNotFoundException{
public static String generateASP(ConstraintSet<Pair> constraints, Collection<ClassOrInterface> fcClasses, ClassLoader classLoader) throws ClassNotFoundException{
ASPFactory factory = new ASPFactory();
factory.convertFC(fcClasses);
factory.convertFC(fcClasses, classLoader);
List<Constraint<Pair>> constraints1 = constraints.cartesianProduct().iterator().next();
for(Constraint<Pair> constraint : constraints1){
for(Pair p : constraint){
@ -32,8 +32,8 @@ public class ASPFactory implements TypeVisitor<String>{
ASPWriter writer = new ASPWriter();
boolean isFCType = false;
private void convertFC(Collection<ClassOrInterface> classes) throws ClassNotFoundException {
Set<Pair> fc = FCGenerator.toFC(classes);
private void convertFC(Collection<ClassOrInterface> classes, ClassLoader classLoader) throws ClassNotFoundException {
Set<Pair> fc = FCGenerator.toFC(classes, classLoader);
isFCType = true;
for(Pair fcp : fc){
convertPair(fcp);

View File

@ -55,9 +55,9 @@ public class ASPGencayFactory implements TypeVisitor<String> {
return ret;
}
public static String generateASP(ConstraintSet<Pair> constraints, Collection<ClassOrInterface> fcClasses) throws ClassNotFoundException{
public static String generateASP(ConstraintSet<Pair> constraints, Collection<ClassOrInterface> fcClasses, ClassLoader classLoader) throws ClassNotFoundException{
ASPGencayFactory factory = new ASPGencayFactory();
factory.convertFC(fcClasses);
factory.convertFC(fcClasses, classLoader);
List<Constraint<Pair>> constraints1 = constraints.cartesianProduct().iterator().next();
for(Constraint<Pair> constraint : constraints1){
for(Pair p : constraint){
@ -69,8 +69,8 @@ public class ASPGencayFactory implements TypeVisitor<String> {
return factory.writer.getASPFile();
}
private void convertFC(Collection<ClassOrInterface> classes) throws ClassNotFoundException {
Set<Pair> fc = FCGenerator.toFC(classes);
private void convertFC(Collection<ClassOrInterface> classes, ClassLoader classLoader) throws ClassNotFoundException {
Set<Pair> fc = FCGenerator.toFC(classes, classLoader);
isFCType = true;
for(Pair fcp : fc){
generateTheta((RefType) fcp.TA1);

View File

@ -31,7 +31,7 @@ public class UnifyTypeFactory {
private static ArrayList<PlaceholderType> PLACEHOLDERS = new ArrayList<>();
public static FiniteClosure generateFC(List<ClassOrInterface> fromClasses, Writer logFile) throws ClassNotFoundException {
public static FiniteClosure generateFC(List<ClassOrInterface> fromClasses, Writer logFile, ClassLoader classLoader) throws ClassNotFoundException {
/*
Die transitive Hülle muss funktionieren.
Man darf schreiben List<A> extends AL<A>
@ -42,7 +42,7 @@ public class UnifyTypeFactory {
Generell dürfen sie immer die gleichen Namen haben.
TODO: die transitive Hülle bilden
*/
return new FiniteClosure(FCGenerator.toUnifyFC(fromClasses), logFile);
return new FiniteClosure(FCGenerator.toUnifyFC(fromClasses, classLoader), logFile);
}
public static UnifyPair generateSmallerPair(UnifyType tl, UnifyType tr){

View File

@ -47,7 +47,7 @@ public class TypeInsertFactory {
new TypeToInsertString(resolvedType.resolvedType).insert);
List<GenericGenratorResultForSourceFile> simplifyResults = JavaTXCompiler.INSTANCE.getGeneratedGenericResultsForAllSourceFiles(newResults);
for (GenericGenratorResultForSourceFile simplifyResultsEntries : simplifyResults) {
GenericsGeneratorResultForClass genericResultsForClass = simplifyResultsEntries.getSimplifyResultsByName("", cl.getClassName().toString());
GenericsGeneratorResultForClass genericResultsForClass = simplifyResultsEntries.getSimplifyResultsByName(cl.getClassName());
return new TypeInsert(insertPoint, createGenericInsert(genericResultsForClass, cl, m, resultSet, offset), resolvedType.getResultPair());
}

View File

@ -25,7 +25,7 @@ public class ClingoTest {
@Test
public void test() throws IOException, InterruptedException, ClassNotFoundException {
String content = "";
content = ASPFactory.generateASP(this.getPairs(), this.getFC());
content = ASPFactory.generateASP(this.getPairs(), this.getFC(), ClassLoader.getSystemClassLoader());
PrintWriter writer = new PrintWriter(tempDirectory + "test.lp", "UTF-8");
writer.println(content);

View File

@ -40,7 +40,7 @@ public class UnifyWithoutWildcards {
public ResultSet run(ConstraintSet<Pair> toTest) throws IOException, InterruptedException, ClassNotFoundException {
String content = "";
content = ASPFactory.generateASP(toTest, this.getFC());
content = ASPFactory.generateASP(toTest, this.getFC(), ClassLoader.getSystemClassLoader());
PrintWriter writer = new PrintWriter(tempDirectory + "test.lp", "UTF-8");
writer.println(content);

View File

@ -35,7 +35,7 @@ public class GeneratorTest extends UnifyWithoutWildcards{
RefType t2 = new RefType(new JavaClassName("java.util.List"), list2, new NullToken());
testSet.addUndConstraint(new Pair(t1, t2, PairOperator.SMALLERDOT));
String resultSet = ASPGencayFactory.generateASP(testSet,
new HashSet<>(Arrays.asList(ASTFactory.createClass(List.class))));
new HashSet<>(Arrays.asList(ASTFactory.createClass(List.class))), ClassLoader.getSystemClassLoader());
System.out.println(resultSet);
}
@ -47,7 +47,7 @@ public class GeneratorTest extends UnifyWithoutWildcards{
RefType t1 = new RefType(new JavaClassName("asp.UnifyWithoutWildcards$Matrix"), list1, new NullToken());
RefType t2 = new RefType(new JavaClassName("java.util.HashMap"), list2, new NullToken());
testSet.addUndConstraint(new Pair(t1, t2, PairOperator.SMALLERDOT));
String resultSet = ASPGencayFactory.generateASP(testSet, this.getFC());
String resultSet = ASPGencayFactory.generateASP(testSet, this.getFC(), ClassLoader.getSystemClassLoader());
System.out.println(resultSet);
}

View File

@ -55,7 +55,7 @@ public class ASPTest {
}
final ConstraintSet<Pair> cons = compiler.getConstraints();
String asp = ASPFactory.generateASP(cons, allClasses);
String asp = ASPFactory.generateASP(cons, allClasses, ClassLoader.getSystemClassLoader());
System.out.println(asp);
}

View File

@ -14,14 +14,14 @@ public class SuperInterfacesTest {
public void test() throws ClassNotFoundException {
Collection<ClassOrInterface> classes = new ArrayList<>();
classes.add(ASTFactory.createClass(TestClass.class));
System.out.println(FCGenerator.toFC(classes));
System.out.println(FCGenerator.toFC(classes, ClassLoader.getSystemClassLoader()));
}
@Test
public void testGeneric() throws ClassNotFoundException {
Collection<ClassOrInterface> classes = new ArrayList<>();
classes.add(ASTFactory.createClass(TestClassGeneric.class));
System.out.println(FCGenerator.toFC(classes));
System.out.println(FCGenerator.toFC(classes, ClassLoader.getSystemClassLoader()));
}
}

View File

@ -6,20 +6,47 @@ import org.junit.Test;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import java.net.URLClassLoader;
public class Bytecode extends TestCase {
public static final String rootDirectory = System.getProperty("user.dir")+"/src/test/resources/javFiles/packageTest/de/test/";
public static final String rootDirectory = System.getProperty("user.dir")+"/src/test/resources/javFiles/packageTest/";
@Test
public void testSetPackageNameInBytecode() throws IOException, ClassNotFoundException {
JavaTXCompiler compiler = new JavaTXCompiler(new File(rootDirectory+"packageNameTest.jav"));
public void testSetPackageNameInBytecode() throws Exception {
JavaTXCompiler compiler = new JavaTXCompiler(new File(rootDirectory+"de/test/TestClass.jav"));
compiler.typeInference();
File f = new File(rootDirectory + "Test.class");
File f = new File(rootDirectory + "de/test/TestClass.class");
if(f.exists() && !f.isDirectory()) {
f.delete();
}
compiler.generateBytecode(null);
f = new File(rootDirectory + "Test.class");
f = new File(rootDirectory + "de/test/TestClass.class");
assertTrue(f.exists());
URLClassLoader loader = new URLClassLoader(new URL[]{new URL("file://" + rootDirectory)});
Class<?> classToTest = loader.loadClass("de.test.TestClass");
Object instanceOfClass = classToTest.getDeclaredConstructor().newInstance();
}
@Test
public void testSetPackageNameInBytecodeAndOutputFolder() throws Exception {
JavaTXCompiler compiler = new JavaTXCompiler(new File(rootDirectory+"de/test/TestClass.jav"));
compiler.typeInference();
File f = new File(rootDirectory + "de/test/output/de/test/TestClass.class");
if(f.exists() && !f.isDirectory()) {
f.delete();
}
compiler.generateBytecode(rootDirectory + "de/test/output/");
f = new File(rootDirectory + "de/test/output/de/test/TestClass.class");
assertTrue(f.exists());
URLClassLoader loader = new URLClassLoader(new URL[]{new URL("file://" + rootDirectory + "de/test/output/")});
Class<?> classToTest = loader.loadClass("de.test.TestClass");
Object instanceOfClass = classToTest.getDeclaredConstructor().newInstance();
}
}

View File

@ -7,14 +7,73 @@ import org.junit.Test;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
public class CheckPackageFolder extends TestCase {
public static final String rootDirectory = System.getProperty("user.dir")+"/src/test/resources/javFiles/packageTest/de/test/";
@Test
public void testCorrectFolder1File() throws IOException, ClassNotFoundException {
public void testCorrectFolder1FileWithWrongPackageName() throws IOException, ClassNotFoundException {
JavaTXCompiler compiler = new JavaTXCompiler(new File(rootDirectory+"packageNameTestWrongPackage.jav"));
assertTrue(true); //Es ist erlaubt falsche package Namen zu verwenden. Warnung wäre optional
compiler.typeInference();
File f = new File(rootDirectory + "TestClass.class");
if(f.exists() && !f.isDirectory()) {
f.delete();
}
compiler.generateBytecode();
f = new File(rootDirectory + "TestClass.class");
assertTrue(f.exists()); //Es ist erlaubt falsche package Namen zu verwenden. Warnung wäre optional
}
@Test
public void testCorrectFolder1File() throws IOException, ClassNotFoundException {
JavaTXCompiler compiler = new JavaTXCompiler(new File(rootDirectory+"TestClass.jav"));
compiler.typeInference();
File f = new File(rootDirectory + "TestClass.class");
if(f.exists() && !f.isDirectory()) {
f.delete();
}
compiler.generateBytecode();
f = new File(rootDirectory + "TestClass.class");
assertTrue(f.exists()); //Es ist erlaubt falsche package Namen zu verwenden. Warnung wäre optional
}
@Test
public void testCorrectFolder1FileAndOutputDirectory() throws IOException, ClassNotFoundException {
JavaTXCompiler compiler = new JavaTXCompiler(new File(rootDirectory+"TestClass.jav"));
compiler.typeInference();
File f = new File(rootDirectory + "output/de/test/TestClass.class");
if(f.exists() && !f.isDirectory()) {
f.delete();
}
compiler.generateBytecode(rootDirectory+"output/");
f = new File(rootDirectory + "output/de/test/TestClass.class");
assertTrue(f.exists()); //Es ist erlaubt falsche package Namen zu verwenden. Warnung wäre optional
}
/*
* Dieser Test wird übersprungen, da der Bytecode-Generator nicht mit zwei Eingabedateien gleichzeitig umgehen kann
@Test
public void testCorrectFolder2Files() throws IOException, ClassNotFoundException {
JavaTXCompiler compiler = new JavaTXCompiler(Arrays.asList(
new File(rootDirectory+"subpackage1/Test1.jav"),
new File(rootDirectory+"subpackage2/Test2.jav")
));
compiler.typeInference();
File f = new File(rootDirectory + "subpackage1/Test1.class");
if(f.exists() && !f.isDirectory()) {
f.delete();
}
File f2 = new File(rootDirectory + "subpackage2/Test2.class");
if(f.exists() && !f.isDirectory()) {
f.delete();
}
compiler.generateBytecode();
f = new File(rootDirectory + "subpackage1/Test1.class");
f2 = new File(rootDirectory + "subpackage2/Test2.class");
assertTrue(f.exists());
assertTrue(f2.exists());
}
*/
}

View File

@ -0,0 +1,86 @@
package packages;
import com.google.common.collect.Lists;
import de.dhbwstuttgart.core.JavaTXCompiler;
import junit.framework.TestCase;
import org.junit.Before;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
import java.net.URL;
public class ImportTest extends TestCase {
public static final String rootDirectory = System.getProperty("user.dir")+"/src/test/resources/javFiles/packageTest/de/test/";
public ImportTest() throws ClassNotFoundException, IOException {
/*
Generate ToImport class in rootDirectory and in output-Directory
*/
JavaTXCompiler compiler = new JavaTXCompiler(new File(rootDirectory+"ToImport.jav"));
compiler.typeInference();
compiler.generateBytecode(rootDirectory + "output/");
File f = new File(rootDirectory + "output/de/test/ToImport.class");
assertTrue(f.exists());
compiler = new JavaTXCompiler(new File(rootDirectory+"subpackage1/ToImport2.jav"));
compiler.typeInference();
compiler.generateBytecode(rootDirectory + "output/");
f = new File(rootDirectory + "output/de/test/subpackage1/ToImport2.class");
assertTrue(f.exists());
compiler = new JavaTXCompiler(new File(rootDirectory+"subpackage2/ToImport3.jav"));
compiler.typeInference();
compiler.generateBytecode(rootDirectory + "output/");
f = new File(rootDirectory + "output/de/test/subpackage2/ToImport3.class");
assertTrue(f.exists());
}
@Test
public void testSetPackageNameInBytecodeAndOutputFolder() throws IOException, ClassNotFoundException {
JavaTXCompiler compiler = new JavaTXCompiler(
Lists.newArrayList(new File(rootDirectory+"ImportTest.jav")),
Lists.newArrayList(new URL("file://"+rootDirectory+"output/")));
compiler.typeInference();
File f = new File(rootDirectory + "output/de/test/ImportTest.class");
if(f.exists() && !f.isDirectory()) {
f.delete();
}
compiler.generateBytecode(rootDirectory + "output/");
f = new File(rootDirectory + "output/de/test/ImportTest.class");
assertTrue(f.exists());
}
@Test
public void testSetPackageNameInBytecodeAndStandardOutputFolder() throws IOException, ClassNotFoundException {
JavaTXCompiler compiler = new JavaTXCompiler(
Lists.newArrayList(new File(rootDirectory+"ImportTest.jav")),
Lists.newArrayList(new URL("file://"+rootDirectory+"output/")));
compiler.typeInference();
File f = new File(rootDirectory + "ImportTest.class");
if(f.exists() && !f.isDirectory()) {
f.delete();
}
compiler.generateBytecode(null);
f = new File(rootDirectory + "ImportTest.class");
assertTrue(f.exists());
}
@Test
public void testImportTwoClasses() throws IOException, ClassNotFoundException {
JavaTXCompiler compiler = new JavaTXCompiler(
Lists.newArrayList(new File(rootDirectory+"ImportTest2.jav")),
Lists.newArrayList(new URL("file://"+rootDirectory+"output/")));
compiler.typeInference();
File f = new File(rootDirectory + "ImportTest2.class");
if(f.exists() && !f.isDirectory()) {
f.delete();
}
compiler.generateBytecode(null);
f = new File(rootDirectory + "ImportTest2.class");
assertTrue(f.exists());
}
}

View File

@ -12,7 +12,7 @@ public class ParsePackageName {
public static final String rootDirectory = System.getProperty("user.dir")+"/src/test/resources/javFiles/packageTest/de/test/";
@Test
public void parsePackage() throws IOException, ClassNotFoundException {
JavaTXCompiler compiler = new JavaTXCompiler(new File(rootDirectory+"packageNameTest.jav"));
JavaTXCompiler compiler = new JavaTXCompiler(new File(rootDirectory+"TestClass.jav"));
for(File f : compiler.sourceFiles.keySet()){
SourceFile sf = compiler.sourceFiles.get(f);
assert sf.getPkgName().equals("de.test");

View File

@ -28,6 +28,10 @@ public class JavaTXCompilerTest {
execute(new File(rootDirectory+"fc.jav"));
}
@Test
public void importTest() throws IOException, ClassNotFoundException {
execute(new File(rootDirectory+"Import.jav"));
}
@Test
public void lambda() throws IOException, ClassNotFoundException {
execute(new File(rootDirectory+"Lambda.jav"));
}

View File

@ -2,7 +2,7 @@ import java.util.Vector;
class Import {
void methode(){
Vector v;
var v;
v.add(v);
}
}

View File

@ -0,0 +1,9 @@
package de.test;
import de.test.ToImport;
class ImportTest{
void methode(){
new ToImport();
}
}

View File

@ -0,0 +1,11 @@
package de.test;
import de.test.subpackage1.ToImport2;
import de.test.subpackage2.ToImport3;
class ImportTest2{
void methode(){
new ToImport2().m1();
new ToImport3().m2();
}
}

View File

@ -0,0 +1,3 @@
package de.test;
public class TestClass{}

View File

@ -0,0 +1,4 @@
package de.test.subpackage1;
class Test1{
}

View File

@ -0,0 +1,5 @@
package de.test.subpackage1;
class ToImport2{
void m1(){}
}

View File

@ -0,0 +1,4 @@
package de.test.subpackage2;
class Test2{
}

View File

@ -0,0 +1,5 @@
package de.test.subpackage2;
class ToImport3{
void m2(){}
}

View File

@ -15,10 +15,13 @@
* Java kompiliert in den gleichen Ordner wie die source file
* Java erhält eine liste von Source Files, die es kompilieren soll
* anhand von deren Standort lassen sich relativ die Position von kompilierten Class-files bestimmen
* macht der javac Compiler allerdings nicht
* er nimmt sein Running Directory als classPath standardmäßig, falls keiner angegeben
* javac geht davon aus, dass CP richtig gesetzt wird, oder javac im Source-Root ausgeführt wird
* -cp path or -classpath path
Specifies where to find user class files, and (optionally) annotation processors and source files. This class path overrides the user class path in the CLASSPATH environment variable. If neither CLASSPATH, -cp nor -classpath is specified, then the
user class path is the current directory. See Setting the Class Path.
user class path is the current directory. See Setting the Class Path.
* Compiler kontrolliert nicht, ob package im korrekten Ordner ist
* auch keine Warnung
@ -28,3 +31,33 @@
* Optional
* damit lässt sich ein andere ort zur Ausgabe der Class-files bestimmen
# Tasks
## Class files in richtigen Ordner legen ##
* Wenn Pfad übergeben, dann in Pfad + packageName
* Ohne Pfad, direkt neben die Source File legen
* wenn Source File nicht in richtigem Ordner -> Warnung ausgeben
### Stand ###
* Beinahe abgeschlossen
* TODO: Tests schreiben
## Class files einlesen
* Wenn Classpath übergeben
* Suchen in Classpath + packageName
* Müsste eigentlich UTLClassLoader automatisch machen (also packageNamen anfügen)
* Wenn nichts übergeben
* dann currentDirectory + packageName
* Für die Tests muss korrekter Classpath gesetzt werden
### Stand ###
TODO:
* es muss überall wo im Quellcode ClassLoader.getSystemClassLoader() oder ein andere Classloader verwendet wird,
* dieser ausgetauscht werden mit dem classLaoder, welcher bei der Instanzierung von JavaTXCompiler erstellt wird
## Class files mit packageNamen versehen
* In die Class file muss noch der korrekte name geschrieben werden
* kann möglicherweise ASM
* Kann ASM mittesl `cw.visit("name/mit/slashe/Getrennt"..`
* wurde bereits erledigt -> TODO: Testen
## Console Interface um Parameter -classpath und -output directory erweitern