* Implemented recognition of SingleTypeImportDecl (tested).
* InvalidClassNameException is thrown if class/package in ImportDecl is not known.
This commit is contained in:
parent
d5ba34e620
commit
2e9a71a18d
@ -0,0 +1,4 @@
|
||||
package de.dhbwstuttgart.parser;
|
||||
public class InvalidClassNameException extends Exception{
|
||||
|
||||
}
|
@ -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();
|
||||
|
@ -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<JavaClassName> 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<JavaClassName> 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<ClassOrInterface> classes = new ArrayList<>();
|
||||
this.getNames(ctx);
|
||||
|
@ -40,4 +40,9 @@ public class SourceFile extends SyntaxTreeNode{
|
||||
public String getPkgName(){
|
||||
return this.pkgName;
|
||||
}
|
||||
|
||||
// Get imports (to test implementation)
|
||||
public List<JavaClassName> getImports(){
|
||||
return this.imports;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user