From 2e9a71a18d972e082ee6c41708cf03e7806487bc Mon Sep 17 00:00:00 2001 From: Jakob Herrmann Date: Wed, 22 Feb 2017 21:25:57 +0100 Subject: [PATCH] * Implemented recognition of SingleTypeImportDecl (tested). * InvalidClassNameException is thrown if class/package in ImportDecl is not known. --- .../parser/InvalidClassNameException.java | 4 +++ src/de/dhbwstuttgart/parser/RunParser.java | 9 +++++++ .../parser/SyntaxTreeGenerator.java | 27 ++++++++++++++++--- .../dhbwstuttgart/syntaxtree/SourceFile.java | 5 ++++ 4 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 src/de/dhbwstuttgart/parser/InvalidClassNameException.java diff --git a/src/de/dhbwstuttgart/parser/InvalidClassNameException.java b/src/de/dhbwstuttgart/parser/InvalidClassNameException.java new file mode 100644 index 00000000..97115416 --- /dev/null +++ b/src/de/dhbwstuttgart/parser/InvalidClassNameException.java @@ -0,0 +1,4 @@ +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 index f9456a85..68d717a4 100644 --- a/src/de/dhbwstuttgart/parser/RunParser.java +++ b/src/de/dhbwstuttgart/parser/RunParser.java @@ -1,6 +1,7 @@ package de.dhbwstuttgart.parser; 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; @@ -29,9 +30,14 @@ public class RunParser{ 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.KlassenVektor){ for(Modifier mod : c.getModifiers().getModifierList()){ @@ -51,6 +57,9 @@ public class RunParser{ 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.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator.java index 6e23c95d..a2376916 100644 --- a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator.java +++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator.java @@ -18,6 +18,8 @@ import java.lang.ClassNotFoundException; import org.antlr.v4.runtime.Token; import org.antlr.v4.runtime.tree.TerminalNode; +import org.antlr.v4.runtime.RecognitionException; + import sun.reflect.generics.reflectiveObjects.NotImplementedException; public class SyntaxTreeGenerator{ @@ -107,7 +109,7 @@ public class SyntaxTreeGenerator{ return ret; } - public void setImports(Java8Parser.CompilationUnitContext ctx){ + public void setImports(Java8Parser.CompilationUnitContext ctx) throws InvalidClassNameException{ List newImports = new ArrayList(); for(Java8Parser.ImportDeclarationContext importDeclCtx : ctx.importDeclaration()){ if(importDeclCtx.singleTypeImportDeclaration() != null){ @@ -126,8 +128,17 @@ public class SyntaxTreeGenerator{ this.imports.addAll(newImports); } - private JavaClassName convertSingleTypeImportDeclaration(Java8Parser.SingleTypeImportDeclarationContext ctx){ - return null; + private JavaClassName convertSingleTypeImportDeclaration(Java8Parser.SingleTypeImportDeclarationContext ctx) throws InvalidClassNameException{ + String typeName = convertTypeName(ctx.typeName()); + String packageName = getPackageFromClass(typeName); + List classes = PackageCrawler.getClassNames(packageName); + JavaClassName ret = new JavaClassName(typeName); + if(classes.contains(ret)){ + return ret; + } + else{ + throw new InvalidClassNameException(); + } } private JavaClassName convertTypeImportOnDemandDeclaration(Java8Parser.TypeImportOnDemandDeclarationContext ctx){ @@ -142,6 +153,16 @@ public class SyntaxTreeGenerator{ return null; } + private String getPackageFromClass(String cls){ + String ret = ""; + String[] parts = cls.split("\\."); + for(int i = 0; i < parts.length - 1; i++){ + ret = ret + "." + parts[i]; + } + ret = ret.substring(1); + return ret; + } + public SourceFile convert(Java8Parser.CompilationUnitContext ctx){ List classes = new ArrayList<>(); this.getNames(ctx); diff --git a/src/de/dhbwstuttgart/syntaxtree/SourceFile.java b/src/de/dhbwstuttgart/syntaxtree/SourceFile.java index 57d9658f..6bb304e0 100755 --- a/src/de/dhbwstuttgart/syntaxtree/SourceFile.java +++ b/src/de/dhbwstuttgart/syntaxtree/SourceFile.java @@ -40,4 +40,9 @@ public class SourceFile extends SyntaxTreeNode{ public String getPkgName(){ return this.pkgName; } + + // Get imports (to test implementation) + public List getImports(){ + return this.imports; + } }