From dad08cfa4639175b21abd4f300d7ebc95052ffd8 Mon Sep 17 00:00:00 2001 From: Jakob Herrmann Date: Thu, 6 Apr 2017 16:16:47 +0200 Subject: [PATCH 1/5] PackageCrawler latest version --- src/de/dhbwstuttgart/parser/PackageCrawler.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/de/dhbwstuttgart/parser/PackageCrawler.java b/src/de/dhbwstuttgart/parser/PackageCrawler.java index 18bcd444..7028c8e7 100644 --- a/src/de/dhbwstuttgart/parser/PackageCrawler.java +++ b/src/de/dhbwstuttgart/parser/PackageCrawler.java @@ -12,7 +12,7 @@ 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) @@ -20,10 +20,11 @@ import java.util.Set; */ public class PackageCrawler { - public static Set> getClassesInPackage(String packageName) { + public static Set> getClassesInPackage(String packageName){ List classLoadersList = new LinkedList(); - classLoadersList.add(ClasspathHelper.contextClassLoader()); + 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()) From e1af4e1f6589fb8edba0c3f3dba1e7cdb512c2dc Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Thu, 6 Apr 2017 16:22:36 +0200 Subject: [PATCH 2/5] =?UTF-8?q?Tests=20=C3=A4ndern?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/parser/GeneralParserTest.java | 1 - test/parser/RunParserTest.java | 5 ++++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/test/parser/GeneralParserTest.java b/test/parser/GeneralParserTest.java index 9cb20ac6..d74681f5 100644 --- a/test/parser/GeneralParserTest.java +++ b/test/parser/GeneralParserTest.java @@ -29,7 +29,6 @@ public class GeneralParserTest{ filenames.add("ImportTest.jav"); filenames.add("CastTest.jav"); filenames.add("StatementsTest.jav"); - filenames.add("Methods.jav"); filenames.add("ImportTestGeneric.jav"); filenames.add("CastTest.jav"); //filenames.add("BoundedParameter.jav"); diff --git a/test/parser/RunParserTest.java b/test/parser/RunParserTest.java index dd8db38e..5effe244 100644 --- a/test/parser/RunParserTest.java +++ b/test/parser/RunParserTest.java @@ -1,8 +1,11 @@ package parser; +import de.dhbwstuttgart.parser.JavaTXParser; import de.dhbwstuttgart.parser.RunParser; import org.junit.Test; +import java.io.File; + import static org.junit.Assert.*; public class RunParserTest { @@ -13,6 +16,6 @@ public class RunParserTest { public void testMain() throws Exception { String[] args = new String[1]; args[0] = rootDirectory+"ImportTest2.jav"; - RunParser.main(args); + new JavaTXParser().parse(new File(args[0])); } } \ No newline at end of file From 18965e0edae325f2e8cee153f1db4231a3203157 Mon Sep 17 00:00:00 2001 From: Jakob Herrmann Date: Thu, 13 Apr 2017 15:00:13 +0200 Subject: [PATCH 3/5] Simple bash script to compile packages needed for parser. --- src/build_compiler.sh | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100755 src/build_compiler.sh diff --git a/src/build_compiler.sh b/src/build_compiler.sh new file mode 100755 index 00000000..6ccc2dc4 --- /dev/null +++ b/src/build_compiler.sh @@ -0,0 +1,24 @@ +#!/bin/bash +# A basic script to compile the necessary packages and their subpackages to work with the parser. +# Messages are logged to stderr. +>&2 echo "Building de.dhbwstuttgart.typecheck..." +javac -d ../bin ./de/dhbwstuttgart/typecheck/*.java +>&2 echo "Building de.dhbwstuttgart.syntaxtree..." +javac -d ../bin ./de/dhbwstuttgart/syntaxtree/*.java +>&2 echo "Building de.dhbwstuttgart.syntaxtree.factory..." +javac -d ../bin ./de/dhbwstuttgart/syntaxtree/factory/*.java +>&2 echo "Building de.dhbwstuttgart.syntaxtree.operator..." +javac -d ../bin ./de/dhbwstuttgart/syntaxtree/operator/*.java +>&2 echo "Building de.dhbwstuttgart.syntaxtree.statement..." +javac -d ../bin ./de/dhbwstuttgart/syntaxtree/statement/*.java +>&2 echo "Building de.dhbwstuttgart.syntaxtree.statement.literal..." +javac -d ../bin ./de/dhbwstuttgart/syntaxtree/statement/literal/*.java +>&2 echo "Building de.dhbwstuttgart.syntaxtree.type..." +javac -d ../bin ./de/dhbwstuttgart/syntaxtree/type/*.java +>&2 echo "Building de.dhbwstuttgart.parser..." +javac -d ../bin ./de/dhbwstuttgart/parser/*.java +>&2 echo "Building de.dhbwstuttgart.parser.SyntaxTreeGenerator..." +javac -d ../bin ./de/dhbwstuttgart/parser/SyntaxTreeGenerator/*.java +>&2 echo "Building de.dhbwstuttgart.parser.antlr..." +javac -d ../bin ./de/dhbwstuttgart/parser/antlr/*.java +echo "Done. Now its your turn to debug:)." From 5cc418371d8835b4eb22efe5866550c2b1a346b7 Mon Sep 17 00:00:00 2001 From: Jakob Herrmann Date: Thu, 20 Apr 2017 13:47:06 +0200 Subject: [PATCH 4/5] Main class for JavaTXParser. --- src/de/dhbwstuttgart/parser/RunTXParser.java | 57 ++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 src/de/dhbwstuttgart/parser/RunTXParser.java diff --git a/src/de/dhbwstuttgart/parser/RunTXParser.java b/src/de/dhbwstuttgart/parser/RunTXParser.java new file mode 100644 index 00000000..8d4c6d9e --- /dev/null +++ b/src/de/dhbwstuttgart/parser/RunTXParser.java @@ -0,0 +1,57 @@ +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(); + } + } +} From f05222fb563282945ba395047f54f6b3059b10c5 Mon Sep 17 00:00:00 2001 From: Jakob Herrmann Date: Thu, 20 Apr 2017 17:15:52 +0200 Subject: [PATCH 5/5] Some cleanup to be able to recompile, i.e: * Remove handling of superclasses which needs further debugging * add missing exceptions * remove ClassFinder as it is not used and needs debugging --- .../parser/ClassNotFoundException.java | 4 ++ .../parser/InvalidClassNameException.java | 4 -- src/de/dhbwstuttgart/parser/RunParser.java | 68 ------------------- .../SyntaxTreeGenerator.java | 18 ++--- 4 files changed, 11 insertions(+), 83 deletions(-) create mode 100644 src/de/dhbwstuttgart/parser/ClassNotFoundException.java delete mode 100644 src/de/dhbwstuttgart/parser/InvalidClassNameException.java delete mode 100644 src/de/dhbwstuttgart/parser/RunParser.java diff --git a/src/de/dhbwstuttgart/parser/ClassNotFoundException.java b/src/de/dhbwstuttgart/parser/ClassNotFoundException.java new file mode 100644 index 00000000..08609467 --- /dev/null +++ b/src/de/dhbwstuttgart/parser/ClassNotFoundException.java @@ -0,0 +1,4 @@ +package de.dhbwstuttgart.parser; +public class ClassNotFoundException extends Exception{ + +} diff --git a/src/de/dhbwstuttgart/parser/InvalidClassNameException.java b/src/de/dhbwstuttgart/parser/InvalidClassNameException.java deleted file mode 100644 index 97115416..00000000 --- a/src/de/dhbwstuttgart/parser/InvalidClassNameException.java +++ /dev/null @@ -1,4 +0,0 @@ -package de.dhbwstuttgart.parser; -public class InvalidClassNameException extends Exception{ - -} diff --git a/src/de/dhbwstuttgart/parser/RunParser.java b/src/de/dhbwstuttgart/parser/RunParser.java deleted file mode 100644 index 4480eb48..00000000 --- a/src/de/dhbwstuttgart/parser/RunParser.java +++ /dev/null @@ -1,68 +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 RunParser{ - public static void main(String[] args){ - try{ - File file = new File(args[0]); - Scanner sc = new Scanner(file); - String inputString = sc.nextLine(); - while(sc.hasNextLine()) inputString = inputString + sc.nextLine() + "\n"; - InputStream stream = new ByteArrayInputStream(inputString.getBytes(StandardCharsets.UTF_8)); - ANTLRInputStream input = new ANTLRInputStream(stream); - Java8Lexer lexer = new Java8Lexer(input); - CommonTokenStream tokens = new CommonTokenStream(lexer); - Java8Parser parser = new Java8Parser(tokens); - Java8Parser.CompilationUnitContext tree = parser.compilationUnit(); - SyntaxTreeGenerator generator = new SyntaxTreeGenerator(new JavaClassRegistry(new ArrayList<>())); - generator.setImports(tree); - SourceFile f = generator.convert((Java8Parser.CompilationUnitContext) tree); - 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(FileNotFoundException e){ - System.out.println("File not found."); - } - catch(InvalidClassNameException e){ - e.printStackTrace(); - } - catch(IOException e){ - System.out.println("An exception occured which is on our TODO list."); - e.printStackTrace(); - } - } -} diff --git a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java index 68b67fe5..c6f8bd54 100644 --- a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java +++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java @@ -1,7 +1,7 @@ package de.dhbwstuttgart.parser.SyntaxTreeGenerator; import de.dhbwstuttgart.exceptions.NotImplementedException; -import de.dhbwstuttgart.parser.InvalidClassNameException; +import de.dhbwstuttgart.parser.ClassNotFoundException; import de.dhbwstuttgart.parser.NullToken; import de.dhbwstuttgart.parser.PackageCrawler; import de.dhbwstuttgart.parser.antlr.Java8Parser; @@ -109,7 +109,7 @@ public class SyntaxTreeGenerator{ return ret; } - public void setImports(Java8Parser.CompilationUnitContext ctx) throws InvalidClassNameException { + public void setImports(Java8Parser.CompilationUnitContext ctx) throws ClassNotFoundException { List newImports = new ArrayList(); for(Java8Parser.ImportDeclarationContext importDeclCtx : ctx.importDeclaration()){ if(importDeclCtx.singleTypeImportDeclaration() != null){ @@ -128,7 +128,7 @@ public class SyntaxTreeGenerator{ this.imports.addAll(newImports); } - private JavaClassName convertSingleTypeImportDeclaration(Java8Parser.SingleTypeImportDeclarationContext ctx) throws InvalidClassNameException{ + private JavaClassName convertSingleTypeImportDeclaration(Java8Parser.SingleTypeImportDeclarationContext ctx) throws ClassNotFoundException{ String typeName = convertTypeName(ctx.typeName()); String packageName = getPackageFromClass(typeName); List classes = PackageCrawler.getClassNames(packageName); @@ -138,7 +138,7 @@ public class SyntaxTreeGenerator{ return ret; } else{ - throw new InvalidClassNameException(); + throw new ClassNotFoundException(); } } @@ -164,9 +164,10 @@ public class SyntaxTreeGenerator{ return ret; } - public SourceFile convert(Java8Parser.CompilationUnitContext ctx){ + public SourceFile convert(Java8Parser.CompilationUnitContext ctx) throws ClassNotFoundException{ List classes = new ArrayList<>(); this.getNames(ctx); + this.setImports(ctx); for(Java8Parser.TypeDeclarationContext typeDecl : ctx.typeDeclaration()){ ClassOrInterface newClass; if(typeDecl.classDeclaration() != null){ @@ -205,12 +206,7 @@ public class SyntaxTreeGenerator{ List methods = convertMethods(ctx.classBody(), name); Token offset = ctx.getStart(); - RefType superClass ; - if(ctx.superclass() != null){ - superClass = convert(ctx.superclass()); - }else{ - superClass = new ASTFactory(reg).createObjectClass().getType(); - } + RefType superClass = null; Boolean isInterface = false; List implementedInterfaces = null; return new ClassOrInterface(modifiers, name, fielddecl, methods, genericClassParameters, superClass, isInterface, implementedInterfaces, offset);