PackageCrawler durch ClassLoader ersetzen

This commit is contained in:
Andreas Stadelmeier 2017-07-14 17:47:02 +02:00
parent 057df7c702
commit 841ac5fcbf
9 changed files with 50 additions and 141 deletions

View File

@ -4,19 +4,18 @@
<classpathentry kind="src" path="test"/> <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="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="lib" path="lib/bcel-6.1-SNAPSHOT.jar" sourcepath="lib/bcel-6.1-SNAPSHOT-sources.jar"/>
<classpathentry kind="lib" path="lib/guava-10.0.1.jar"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/> <classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
<classpathentry kind="lib" path="lib/antlr-complete.jar"/> <classpathentry kind="lib" path="lib/antlr-complete.jar"/>
<classpathentry kind="lib" path="lib/annotations-2.0.1.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/bcel-6.1-SNAPSHOT-sources.jar"/>
<classpathentry kind="lib" path="lib/cloning.jar"/> <classpathentry kind="lib" path="lib/cloning.jar"/>
<classpathentry kind="lib" path="lib/commons-bcel6-6.0-SNAPSHOT.jar"/> <classpathentry kind="lib" path="lib/commons-bcel6-6.0-SNAPSHOT.jar"/>
<classpathentry kind="lib" path="lib/guava-15.0.jar"/>
<classpathentry kind="lib" path="lib/javassist-3.19.0-GA.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/junit-4.0.jar"/>
<classpathentry kind="lib" path="lib/log4j-1.2.12.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-javadoc.jar"/>
<classpathentry kind="lib" path="lib/reflections-0.9.10-sources.jar"/> <classpathentry kind="lib" path="lib/reflections-0.9.10-sources.jar"/>
<classpathentry kind="lib" path="lib/reflections-0.9.10.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="output" path="bin"/> <classpathentry kind="output" path="bin"/>
</classpath> </classpath>

View File

@ -72,13 +72,13 @@ public class JavaTXCompiler {
return ret; return ret;
} }
public SourceFile parse(File sourceFile) throws IOException, ClassNotFoundException { public SourceFile parse(File sourceFile) throws IOException, java.lang.ClassNotFoundException {
SourceFile ret = new JavaTXParser().parse(sourceFile); SourceFile ret = new JavaTXParser().parse(sourceFile);
sourceFiles.add(ret); sourceFiles.add(ret);
return ret; return ret;
} }
public SourceFile parse(String source) throws IOException, ClassNotFoundException { public SourceFile parse(String source) throws IOException, java.lang.ClassNotFoundException {
SourceFile ret = new JavaTXParser().parse(source); SourceFile ret = new JavaTXParser().parse(source);
sourceFiles.add(ret); sourceFiles.add(ret);
return ret; return ret;

View File

@ -14,35 +14,25 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
public class JavaTXParser { public class JavaTXParser {
public SourceFile parse(InputStream source) throws IOException, ClassNotFoundException { public SourceFile parse(InputStream source) throws IOException, java.lang.ClassNotFoundException {
InputStream stream = source;//new FileInputStream(sourceFile); InputStream stream = source;//new FileInputStream(sourceFile);
ANTLRInputStream input = new ANTLRInputStream(stream); ANTLRInputStream input = new ANTLRInputStream(stream);
Java8Lexer lexer = new Java8Lexer(input); Java8Lexer lexer = new Java8Lexer(input);
CommonTokenStream tokens = new CommonTokenStream(lexer); CommonTokenStream tokens = new CommonTokenStream(lexer);
Java8Parser parser = new Java8Parser(tokens); Java8Parser parser = new Java8Parser(tokens);
Java8Parser.CompilationUnitContext tree = parser.compilationUnit(); Java8Parser.CompilationUnitContext tree = parser.compilationUnit();
SyntaxTreeGenerator generator = new SyntaxTreeGenerator(new JavaClassRegistry(generateJavaLangNames())); JavaClassRegistry reg = new JavaClassRegistry(new ArrayList<>());
reg.addPackage("java.lang");
SyntaxTreeGenerator generator = new SyntaxTreeGenerator(reg);
return generator.convert(tree); return generator.convert(tree);
} }
private List<String> generateJavaLangNames() throws IOException, ClassNotFoundException { public SourceFile parse(File file) throws IOException, java.lang.ClassNotFoundException {
List<String> ret = new ArrayList<>();
for(Class cl : PackageCrawler.getClassesInPackage("java.lang")){
ret.add(cl.getName());
}
//TODO: Wieso muss man das händisch anhängen?
ret.add("java.lang.Object");
return ret;
}
public SourceFile parse(File file) throws IOException, ClassNotFoundException {
return this.parse(new FileInputStream(file)); return this.parse(new FileInputStream(file));
} }
public SourceFile parse(String fileContent) throws IOException, ClassNotFoundException { public SourceFile parse(String fileContent) throws IOException, java.lang.ClassNotFoundException {
return this.parse(new ByteArrayInputStream(fileContent.getBytes(StandardCharsets.UTF_8))); return this.parse(new ByteArrayInputStream(fileContent.getBytes(StandardCharsets.UTF_8)));
} }
} }

View File

@ -1,48 +0,0 @@
package de.dhbwstuttgart.parser;
import de.dhbwstuttgart.typecheck.JavaClassName;
import org.reflections.Reflections;
import org.reflections.scanners.ResourcesScanner;
import org.reflections.scanners.SubTypesScanner;
import org.reflections.util.ClasspathHelper;
import org.reflections.util.ConfigurationBuilder;
import org.reflections.util.FilterBuilder;
import java.util.LinkedList;
import java.util.List;
import java.util.ArrayList;
import java.util.Set;
import java.lang.InterruptedException;
/**
* Hilft beim Durchsuchen von Packages
* Benutzt die Reflections-Library (https://github.com/ronmamo/reflections)
* Hilfe dazu: http://stackoverflow.com/a/9571146
*/
public class PackageCrawler {
public static Set<Class<?>> getClassesInPackage(String packageName){
List<ClassLoader> classLoadersList = new LinkedList<ClassLoader>();
classLoadersList.add(Thread.currentThread().getContextClassLoader());
classLoadersList.add(ClasspathHelper.staticClassLoader());
classLoadersList.add(Thread.currentThread().getContextClassLoader().getParent());
Reflections reflections = new Reflections(new ConfigurationBuilder()
.setScanners(new SubTypesScanner(false /* don't exclude Object.class */), new ResourcesScanner())
.setUrls(ClasspathHelper.forClassLoader(classLoadersList.toArray(new ClassLoader[0])))
.filterInputsBy(new FilterBuilder().include(FilterBuilder.prefix(packageName))));
Set<Class<?>> classes = reflections.getSubTypesOf(Object.class);
return classes;
}
// Returns a list of JavaClassNames.
public static List<JavaClassName> getClassNames(String packageName){
List<JavaClassName> nameList = new ArrayList();
Set<Class<?>> classes = getClassesInPackage(packageName);
for(Class c : classes){
nameList.add(new JavaClassName(c.getName()));
}
return nameList;
}
}

View File

@ -1,57 +0,0 @@
package de.dhbwstuttgart.parser;
import de.dhbwstuttgart.parser.SyntaxTreeGenerator.SyntaxTreeGenerator;
import de.dhbwstuttgart.parser.antlr.Java8Lexer;
import de.dhbwstuttgart.parser.antlr.Java8Parser;
import de.dhbwstuttgart.typecheck.JavaClassName;
import de.dhbwstuttgart.typecheck.JavaClassRegistry;
import org.antlr.v4.runtime.ANTLRInputStream;
import org.antlr.v4.runtime.CommonTokenStream;
import de.dhbwstuttgart.syntaxtree.*;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
public class RunTXParser{
public static void main(String[] args){
try{
JavaTXParser parser = new JavaTXParser();
SourceFile f = parser.parse(new File(args[0]));
String pkgName = f.getPkgName();
System.out.println("package: " + pkgName);
System.out.println("Imports:");
for(JavaClassName c : f.getImports()){
System.out.println(c.toString());
}
System.out.println("classes:");
for(ClassOrInterface c : f.getClasses()){
int mod = c.getModifiers();
System.out.println(Modifier.toString(mod));
System.out.println(c.getClassName().toString());
System.out.println("{");
for(Field field : c.getFieldDecl()){
System.out.println(field.getName());
}
System.out.println("}");
}
}
catch(java.util.NoSuchElementException e){
System.out.println("Error: Source seems to be empty.");
}
catch(ClassNotFoundException e){
System.out.println("Class not found.");
}
catch(FileNotFoundException e){
System.out.println("File not found.");
}
catch(IOException e){
System.out.println("An exception occured which is on our TODO list.");
e.printStackTrace();
}
}
}

View File

@ -1,9 +1,8 @@
package de.dhbwstuttgart.parser.SyntaxTreeGenerator; package de.dhbwstuttgart.parser.SyntaxTreeGenerator;
import de.dhbwstuttgart.exceptions.NotImplementedException; import de.dhbwstuttgart.exceptions.NotImplementedException;
import de.dhbwstuttgart.parser.ClassNotFoundException; import java.lang.ClassNotFoundException;
import de.dhbwstuttgart.parser.NullToken; import de.dhbwstuttgart.parser.NullToken;
import de.dhbwstuttgart.parser.PackageCrawler;
import de.dhbwstuttgart.parser.antlr.Java8Parser; import de.dhbwstuttgart.parser.antlr.Java8Parser;
import de.dhbwstuttgart.syntaxtree.*; import de.dhbwstuttgart.syntaxtree.*;
import de.dhbwstuttgart.syntaxtree.factory.ASTFactory; import de.dhbwstuttgart.syntaxtree.factory.ASTFactory;
@ -136,16 +135,10 @@ public class SyntaxTreeGenerator{
private JavaClassName convertSingleTypeImportDeclaration(Java8Parser.SingleTypeImportDeclarationContext ctx) throws ClassNotFoundException{ private JavaClassName convertSingleTypeImportDeclaration(Java8Parser.SingleTypeImportDeclarationContext ctx) throws ClassNotFoundException{
String typeName = convertTypeName(ctx.typeName()); String typeName = convertTypeName(ctx.typeName());
String packageName = getPackageFromClass(typeName); Thread.currentThread().getContextClassLoader().loadClass(typeName);
List<JavaClassName> classes = PackageCrawler.getClassNames(packageName); reg.add(typeName);
reg.add(typeName); JavaClassName ret = reg.getName(typeName);
JavaClassName ret = reg.getName(typeName); return ret;
if(classes.contains(ret)){
return ret;
}
else{
throw new ClassNotFoundException();
}
} }
private JavaClassName convertTypeImportOnDemandDeclaration(Java8Parser.TypeImportOnDemandDeclarationContext ctx){ private JavaClassName convertTypeImportOnDemandDeclaration(Java8Parser.TypeImportOnDemandDeclarationContext ctx){

View File

@ -34,6 +34,15 @@ public class JavaClassName {
} }
} }
/**
* Gibt von einem Klassennamen nur den Namen der Klasse zurück
* Beispiel:
* java.lang.Object wird zu: Object
*/
public static String stripClassName(String className) {
return new JavaClassName(className).name;
}
@Override @Override
public int hashCode() { public int hashCode() {
final int prime = 31; final int prime = 31;

View File

@ -7,7 +7,8 @@ import java.util.List;
* Speichert die Klassen im aktuellen Projektscope * Speichert die Klassen im aktuellen Projektscope
*/ */
public class JavaClassRegistry { public class JavaClassRegistry {
public List<JavaClassName> existingClasses = new ArrayList<>(); final List<JavaClassName> existingClasses = new ArrayList<>();
final List<String> importedPackages = new ArrayList<>();
public JavaClassRegistry(List<String> initialNames){ public JavaClassRegistry(List<String> initialNames){
for(String name : initialNames){ for(String name : initialNames){
@ -15,6 +16,15 @@ public class JavaClassRegistry {
} }
} }
/**
* Fügt ein gesamtes Package der ClassRegistry hinzu.
* Dies geschieht beispielsweise, wenn der Benutzer ein "import package.*;" statement verwendet
* @param packageName
*/
public void addPackage(String packageName) {
importedPackages.add(packageName);
}
public void add(String className){ public void add(String className){
existingClasses.add(new JavaClassName(className)); existingClasses.add(new JavaClassName(className));
} }
@ -23,6 +33,19 @@ public class JavaClassRegistry {
for(JavaClassName name : existingClasses){ for(JavaClassName name : existingClasses){
if(name.equals(new JavaClassName(className)))return name; if(name.equals(new JavaClassName(className)))return name;
} }
//Jetzt noch alle importierten Packages durchsuchen:
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!
}
}
throw new TypeNotPresentException(className, new Throwable()); throw new TypeNotPresentException(className, new Throwable());
} }

View File

@ -32,7 +32,7 @@ public class JavaTXCompilerTest extends JavaTXCompiler {
private static final List<File> filesToTest = new ArrayList<>(); private static final List<File> filesToTest = new ArrayList<>();
@Test @Test
public void test() throws IOException, ClassNotFoundException { public void test() throws IOException, java.lang.ClassNotFoundException {
//filesToTest.add(new File(rootDirectory+"Faculty.jav")); //filesToTest.add(new File(rootDirectory+"Faculty.jav"));
filesToTest.add(new File(rootDirectory+"mathStruc.jav")); filesToTest.add(new File(rootDirectory+"mathStruc.jav"));
//filesToTest.add(new File(rootDirectory+"Lambda.jav")); //filesToTest.add(new File(rootDirectory+"Lambda.jav"));