ClassLoader austauschen gegen den Classloader, welcher in JavaCompilerCore erstellt wird

This commit is contained in:
JanUlrich 2019-12-22 14:27:54 +01:00
parent 3e67da6e34
commit 0378c17e7d
14 changed files with 44 additions and 41 deletions

View File

@ -270,7 +270,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.getSystemClassLoader());
if (!field.getParameterList().iterator().hasNext()
&& !(field.block.statements.get(field.block.statements.size() - 1) instanceof ReturnVoid)) {
mv.visitInsn(Opcodes.RETURN);
@ -351,7 +351,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.getSystemClassLoader());
mv.visitMaxs(0, 0);
mv.visitEnd();

View File

@ -77,11 +77,13 @@ public class BytecodeGenMethod implements StatementVisitor {
private int constructorPos = 0;
private ArrayList<RefTypeOrTPHOrWildcardOrGeneric> varsFunInterface = new ArrayList<>();;
private final ClassLoader classLoader;
// generate bytecode for constructor
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) {
SourceFile sf, String path, Block block, int constructorPos, ClassLoader classLoader) {
this.className = className;
this.superClass = superClass;
@ -99,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(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) {
HashMap<String, String> genericsAndBounds, boolean isInterface, HashMap<JavaClassName, byte[]> classFiles, SourceFile sf,String path, ClassLoader classLoader) {
this.className = className;
this.superClass = superClass;
@ -125,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(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) {
HashMap<String, String> genericsAndBounds, ClassLoader classLoader) {
this.className = className;
this.cw = cw;
this.resultSet = resultSet;
@ -145,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;
@ -637,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();
@ -757,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;

View File

@ -98,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);
@ -135,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);
}
@ -304,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);
@ -472,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);

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)) {

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

@ -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

@ -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

@ -59,3 +59,5 @@ TODO:
* 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