Reflections Library anfügen. PackageCrawler anfügen
This commit is contained in:
parent
d9b81224b6
commit
fa4a24c653
BIN
lib/annotations-2.0.1.jar
Normal file
BIN
lib/annotations-2.0.1.jar
Normal file
Binary file not shown.
BIN
lib/guava-15.0.jar
Normal file
BIN
lib/guava-15.0.jar
Normal file
Binary file not shown.
BIN
lib/javassist-3.19.0-GA.jar
Normal file
BIN
lib/javassist-3.19.0-GA.jar
Normal file
Binary file not shown.
BIN
lib/reflections-0.9.10-javadoc.jar
Normal file
BIN
lib/reflections-0.9.10-javadoc.jar
Normal file
Binary file not shown.
BIN
lib/reflections-0.9.10-sources.jar
Normal file
BIN
lib/reflections-0.9.10-sources.jar
Normal file
Binary file not shown.
BIN
lib/reflections-0.9.10.jar
Normal file
BIN
lib/reflections-0.9.10.jar
Normal file
Binary file not shown.
@ -7,7 +7,7 @@ import java.io.IOException;
|
||||
|
||||
public class JavaTXCompiler {
|
||||
|
||||
public void parse(File sourceFile) throws IOException {
|
||||
public void parse(File sourceFile) throws IOException, ClassNotFoundException {
|
||||
new JavaTXParser().parse(sourceFile);
|
||||
}
|
||||
|
||||
|
@ -14,7 +14,7 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class JavaTXParser {
|
||||
public SourceFile parse(File sourceFile) throws IOException {
|
||||
public SourceFile parse(File sourceFile) throws IOException, ClassNotFoundException {
|
||||
InputStream stream = new FileInputStream(sourceFile);
|
||||
ANTLRInputStream input = new ANTLRInputStream(stream);
|
||||
Java8Lexer lexer = new Java8Lexer(input);
|
||||
@ -27,17 +27,12 @@ public class JavaTXParser {
|
||||
return generator.convert(tree);
|
||||
}
|
||||
|
||||
private List<String> generateJavaLangNames(){
|
||||
private List<String> generateJavaLangNames() throws IOException, ClassNotFoundException {
|
||||
List<String> ret = new ArrayList<>();
|
||||
|
||||
//TODO: Eigentlich sind es noch viele mehr:
|
||||
ret.add("java.lang.String");
|
||||
ret.add("java.lang.Boolean");
|
||||
ret.add("java.lang.Integer");
|
||||
ret.add("java.lang.Double");
|
||||
ret.add("java.lang.Float");
|
||||
ret.add("java.lang.Long");
|
||||
ret.add("java.lang.Byte");
|
||||
for(Class cl : PackageCrawler.getClassesInPackage("java.lang")){
|
||||
ret.add(cl.getName());
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
35
src/de/dhbwstuttgart/parser/PackageCrawler.java
Normal file
35
src/de/dhbwstuttgart/parser/PackageCrawler.java
Normal file
@ -0,0 +1,35 @@
|
||||
package de.dhbwstuttgart.parser;
|
||||
|
||||
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.Set;
|
||||
|
||||
/**
|
||||
* 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(ClasspathHelper.contextClassLoader());
|
||||
classLoadersList.add(ClasspathHelper.staticClassLoader());
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
37
test/parser/FieldTest.java
Normal file
37
test/parser/FieldTest.java
Normal file
@ -0,0 +1,37 @@
|
||||
package parser;
|
||||
|
||||
import de.dhbwstuttgart.parser.JavaTXParser;
|
||||
import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
|
||||
import de.dhbwstuttgart.syntaxtree.Field;
|
||||
import de.dhbwstuttgart.syntaxtree.SourceFile;
|
||||
import de.dhbwstuttgart.syntaxtree.modifier.Modifier;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
public class FieldTest {
|
||||
private static final String rootDirectory = System.getProperty("user.dir")+"/test/parser/";
|
||||
|
||||
@Test
|
||||
public void test() throws IOException, ClassNotFoundException {
|
||||
|
||||
JavaTXParser parser = new JavaTXParser();
|
||||
SourceFile f = parser.parse(new File(rootDirectory + "FieldVarTest.jav"));
|
||||
|
||||
String pkgName = f.getPkgName();
|
||||
System.out.println("package: " + pkgName);
|
||||
System.out.println("classes:");
|
||||
for(ClassOrInterface c : f.KlassenVektor){
|
||||
for(Modifier mod : c.getModifiers().getModifierList()){
|
||||
System.out.println(mod.getClass().getName());
|
||||
}
|
||||
System.out.println(c.getClassName().toString());
|
||||
System.out.println("{");
|
||||
for(Field field : c.getFieldDecl()){
|
||||
System.out.println(field.getName());
|
||||
}
|
||||
System.out.println("}");
|
||||
}
|
||||
}
|
||||
}
|
@ -1,3 +1,5 @@
|
||||
package test;
|
||||
|
||||
class Test<Typ>{
|
||||
Typ a;
|
||||
}
|
@ -3,22 +3,13 @@ package parser;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import de.dhbwstuttgart.core.JavaTXCompiler;
|
||||
import de.dhbwstuttgart.parser.JavaTXParser;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import de.dhbwstuttgart.core.MyCompiler;
|
||||
import de.dhbwstuttgart.core.MyCompilerAPI;
|
||||
|
||||
/**
|
||||
* Dieser Test pr�ft nur, ob .java-Dateien fehlerfrei geparst werden.
|
||||
@ -36,6 +27,7 @@ public class GeneralParserTest{
|
||||
List<String> filenames = new ArrayList<String>();
|
||||
filenames.add("FieldInitializationTest.jav");
|
||||
filenames.add("ImportTest.jav");
|
||||
filenames.add("ImportTestGeneric.jav");
|
||||
//filenames.add("BoundedParameter.jav");
|
||||
//filenames.add("GenericFieldVarTest.jav");
|
||||
filenames.add("FieldVarTest.jav");
|
||||
|
5
test/parser/ImportTestGeneric.jav
Normal file
5
test/parser/ImportTestGeneric.jav
Normal file
@ -0,0 +1,5 @@
|
||||
import java.util.List;
|
||||
|
||||
class ImportTest{
|
||||
List<ImportTest> test;
|
||||
}
|
Loading…
Reference in New Issue
Block a user