From e341d9075e3e98001a2b61906196be2656ded707 Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Thu, 5 Oct 2017 01:13:08 +0200 Subject: [PATCH] Alle Klassen aus Standard Library laden --- .idea/uiDesigner.xml | 124 ++++++++++++++++++ src/de/dhbwstuttgart/core/JavaTXCompiler.java | 4 +- .../environment/CompilationEnvironment.java | 17 ++- .../environment/PackageCrawler.java | 10 ++ .../syntaxtree/type/RefType.java | 7 - .../TypeInferenceBlockInformation.java | 3 +- .../assumptions/TypeInferenceInformation.java | 7 +- .../typeinference/typeAlgo/TYPE.java | 38 +++--- 8 files changed, 178 insertions(+), 32 deletions(-) create mode 100644 .idea/uiDesigner.xml diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 000000000..e96534fb2 --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/de/dhbwstuttgart/core/JavaTXCompiler.java b/src/de/dhbwstuttgart/core/JavaTXCompiler.java index 997239186..5e466eae4 100644 --- a/src/de/dhbwstuttgart/core/JavaTXCompiler.java +++ b/src/de/dhbwstuttgart/core/JavaTXCompiler.java @@ -40,12 +40,12 @@ public class JavaTXCompiler { } public List typeInference(){ - List allClasses = new ArrayList<>(); + List allClasses = environment.getAllAvailableClasses(); for(SourceFile sf : sourceFiles.values()){ allClasses.addAll(sf.getClasses()); } FiniteClosure finiteClosure = UnifyTypeFactory.generateFC(allClasses); - final ConstraintSet cons = new TYPE(sourceFiles.values()).getConstraints(); + final ConstraintSet cons = new TYPE(sourceFiles.values(), allClasses).getConstraints(); ConstraintSet unifyCons = UnifyTypeFactory.convert(cons); TypeUnify unify = new TypeUnify(); diff --git a/src/de/dhbwstuttgart/environment/CompilationEnvironment.java b/src/de/dhbwstuttgart/environment/CompilationEnvironment.java index 929b9117e..42671db79 100644 --- a/src/de/dhbwstuttgart/environment/CompilationEnvironment.java +++ b/src/de/dhbwstuttgart/environment/CompilationEnvironment.java @@ -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,7 +34,8 @@ import de.dhbwstuttgart.parser.scope.JavaClassRegistry; */ public class CompilationEnvironment { private final List librarys; - + private final List sourceFiles; + /** * Imitiert die Environment beim Aufruf des JavaCompilers auf einer Menge von java-Dateien * Die Environment enth�lt automatisch die Java Standard Library @@ -47,12 +51,21 @@ public class CompilationEnvironment { new DebugException("Fehler im Classpath auf diesem System"); } } + this.sourceFiles = sourceFiles; } public JavaClassRegistry getRegistry(File forSourceFile) throws ClassNotFoundException, IOException { - List allNames = new ArrayList<>(); + List allNames; CompilationUnitContext tree = JavaTXParser.parse(forSourceFile); allNames = GatherNames.getNames(tree, new PackageCrawler(librarys)); return new JavaClassRegistry(allNames); } + + public List getAllAvailableClasses() { + List ret = new ArrayList<>(); + for(Class c : new PackageCrawler(librarys).getAllAvailableClasses()){ + ret.add(ASTFactory.createClass(c)); + } + return ret; + } } diff --git a/src/de/dhbwstuttgart/environment/PackageCrawler.java b/src/de/dhbwstuttgart/environment/PackageCrawler.java index c1a4aad2c..48c16502e 100644 --- a/src/de/dhbwstuttgart/environment/PackageCrawler.java +++ b/src/de/dhbwstuttgart/environment/PackageCrawler.java @@ -53,6 +53,16 @@ public class PackageCrawler { return classes; } + + public Set> getAllAvailableClasses(){ + Reflections reflections = new Reflections(new ConfigurationBuilder() + .setScanners(new SubTypesScanner(false /* don't exclude Object.class */), new ResourcesScanner()) + .setUrls(urls)); + + Set> classes = reflections.getSubTypesOf(Object.class); + + return classes; + } public List getClassNames(String packageName){ List nameList = new ArrayList(); diff --git a/src/de/dhbwstuttgart/syntaxtree/type/RefType.java b/src/de/dhbwstuttgart/syntaxtree/type/RefType.java index 2951062be..b28761199 100644 --- a/src/de/dhbwstuttgart/syntaxtree/type/RefType.java +++ b/src/de/dhbwstuttgart/syntaxtree/type/RefType.java @@ -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 parameter; /** diff --git a/src/de/dhbwstuttgart/typeinference/assumptions/TypeInferenceBlockInformation.java b/src/de/dhbwstuttgart/typeinference/assumptions/TypeInferenceBlockInformation.java index 68a536742..df280d480 100644 --- a/src/de/dhbwstuttgart/typeinference/assumptions/TypeInferenceBlockInformation.java +++ b/src/de/dhbwstuttgart/typeinference/assumptions/TypeInferenceBlockInformation.java @@ -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 availableClasses, + public TypeInferenceBlockInformation(Collection availableClasses, ClassOrInterface currentClass, TypeScope methodContext) { super(availableClasses); this.methodContext = new TypeScopeContainer(currentClass, methodContext); diff --git a/src/de/dhbwstuttgart/typeinference/assumptions/TypeInferenceInformation.java b/src/de/dhbwstuttgart/typeinference/assumptions/TypeInferenceInformation.java index 017732a7c..90548e6d3 100644 --- a/src/de/dhbwstuttgart/typeinference/assumptions/TypeInferenceInformation.java +++ b/src/de/dhbwstuttgart/typeinference/assumptions/TypeInferenceInformation.java @@ -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 classes; + private Collection classes; - public TypeInferenceInformation(Set availableClasses){ + public TypeInferenceInformation(Collection availableClasses){ classes = availableClasses; } @@ -52,7 +53,7 @@ public class TypeInferenceInformation { return ret; } - public Set getAvailableClasses() { + public Collection getAvailableClasses() { return classes; } diff --git a/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPE.java b/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPE.java index 23d740a41..4a41176c5 100644 --- a/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPE.java +++ b/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPE.java @@ -13,18 +13,19 @@ import java.util.*; public class TYPE { private final Collection sfs; + private final TypeInferenceInformation typeInferenceInformation; - public TYPE(Collection sourceFiles){ + public TYPE(Collection sourceFiles, Collection 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 classes = new HashSet<>(); - private TypeInferenceInformation getTypeInferenceInformation() { - ClassLoader classLoader = ClassLoader.getSystemClassLoader(); - Set 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);