From bcee5e5209a667625338c0ea79f4f5be35ebe241 Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Thu, 22 Mar 2018 11:41:59 +0100 Subject: [PATCH] Fehler im Parsen von Package beheben --- .../SyntaxTreeGenerator/SyntaxTreeGenerator.java | 14 ++++++++++++-- test/javFiles/Package.jav | 5 +++++ test/typeinference/JavaTXCompilerTest.java | 4 ++++ 3 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 test/javFiles/Package.jav diff --git a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java index 726067132..b7f14f198 100644 --- a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java +++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java @@ -73,6 +73,7 @@ public class SyntaxTreeGenerator{ } public SourceFile convert(Java8Parser.CompilationUnitContext ctx, PackageCrawler packageCrawler) throws ClassNotFoundException{ + if(ctx.packageDeclaration()!=null)this.pkgName = convert(ctx.packageDeclaration()); List classes = new ArrayList<>(); Map imports = GatherNames.getImports(ctx, packageCrawler); this.imports = imports.keySet().stream().map(name -> reg.getName(name)).collect(Collectors.toSet()); @@ -89,6 +90,15 @@ public class SyntaxTreeGenerator{ return new SourceFile(this.pkgName, classes, this.imports); } + private String convert(Java8Parser.PackageDeclarationContext packageDeclarationContext) { + String ret = ""; + for(TerminalNode identifier : packageDeclarationContext.Identifier()){ + ret += identifier.getText()+"."; + } + ret = ret.substring(0, ret.length()-1); + return ret; + } + public Method convert(Java8Parser.MethodDeclarationContext methodDeclarationContext, JavaClassName parentClass, RefType superClass, GenericsRegistry generics) { Java8Parser.MethodHeaderContext header = methodDeclarationContext.methodHeader(); int modifiers = SyntaxTreeGenerator.convert(methodDeclarationContext.methodModifier()); @@ -165,8 +175,8 @@ public class SyntaxTreeGenerator{ } } String className = this.pkgName + (this.pkgName.length()>0?".":"") + ctx.Identifier().getText(); - JavaClassName name = reg.getName(className); - if(! name.toString().equals(className)){ + JavaClassName name = reg.getName(className); //Holt den Package Namen mit dazu + if(! name.toString().equals(className)){ //Kommt die Klasse schon in einem anderen Package vor? throw new TypeinferenceException("Name " + className + " bereits vorhanden in " + reg.getName(className).toString() ,ctx.getStart()); } diff --git a/test/javFiles/Package.jav b/test/javFiles/Package.jav new file mode 100644 index 000000000..bbc1e51da --- /dev/null +++ b/test/javFiles/Package.jav @@ -0,0 +1,5 @@ +package strucType.input; + +class Neu +{ +} \ No newline at end of file diff --git a/test/typeinference/JavaTXCompilerTest.java b/test/typeinference/JavaTXCompilerTest.java index 7c4c35825..ba004cb59 100644 --- a/test/typeinference/JavaTXCompilerTest.java +++ b/test/typeinference/JavaTXCompilerTest.java @@ -66,6 +66,10 @@ public class JavaTXCompilerTest { execute(new File(rootDirectory+"Matrix.jav")); } @Test + public void packageTests() throws IOException, ClassNotFoundException { + execute(new File(rootDirectory+"Package.jav")); + } + @Test public void vector() throws IOException, ClassNotFoundException { execute(new File(rootDirectory+"Vector.jav")); }