Skip to content

Commit

Permalink
cleaned code
Browse files Browse the repository at this point in the history
  • Loading branch information
mlangkabel committed Sep 22, 2016
1 parent 0778805 commit ac259cd
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 29 deletions.
Expand Up @@ -19,49 +19,60 @@ private Navigator() {
// prevent instantiation // prevent instantiation
} }


public static Optional<TypeDeclaration> findType(CompilationUnit cu, String name) { private static String getOuterTypeName(String qualifiedName) {
return qualifiedName.split("\\.", 2)[0];
}

private static String getInnerTypeName(String qualifiedName) {
if (qualifiedName.contains(".")) {
return qualifiedName.split("\\.", 2)[1];
}
return "";
}

public static Optional<TypeDeclaration> findType(CompilationUnit cu, String qualifiedName) {
if (cu.getTypes() == null) { if (cu.getTypes() == null) {
return Optional.empty(); return Optional.empty();
} }


String[] nameParts = name.split("\\.", 2); final String typeName = getOuterTypeName(qualifiedName);
final String typeName = nameParts[0];

Optional<TypeDeclaration> type = cu.getTypes().stream().filter((t) -> t.getName().equals(typeName)).findFirst(); Optional<TypeDeclaration> type = cu.getTypes().stream().filter((t) -> t.getName().equals(typeName)).findFirst();


if (nameParts.length > 1 && type.isPresent()) { final String innerTypeName = getInnerTypeName(qualifiedName);
return findType(type.get(), nameParts[1]); if (type.isPresent() && !innerTypeName.isEmpty()) {
return findType(type.get(), innerTypeName);
} }
return type; return type;
} }


public static Optional<TypeDeclaration> findType(TypeDeclaration td, String name) { public static Optional<TypeDeclaration> findType(TypeDeclaration td, String qualifiedName) {
String[] nameParts = name.split("\\.", 2); final String typeName = getOuterTypeName(qualifiedName);


Optional<TypeDeclaration> type = Optional.empty(); Optional<TypeDeclaration> type = Optional.empty();
for (Node n: td.getChildrenNodes()) { for (Node n: td.getChildrenNodes()) {
if (n instanceof TypeDeclaration && ((TypeDeclaration)n).getName().equals(nameParts[0])) { if (n instanceof TypeDeclaration && ((TypeDeclaration)n).getName().equals(typeName)) {
type = Optional.of((TypeDeclaration)n); type = Optional.of((TypeDeclaration)n);
break; break;
} }
} }
if (nameParts.length > 1 && type.isPresent()) { final String innerTypeName = getInnerTypeName(qualifiedName);
return findType(type.get(), nameParts[1]); if (type.isPresent() && !innerTypeName.isEmpty()) {
return findType(type.get(), innerTypeName);
} }
return type; return type;
} }




public static ClassOrInterfaceDeclaration demandClass(CompilationUnit cu, String name) { public static ClassOrInterfaceDeclaration demandClass(CompilationUnit cu, String qualifiedName) {
ClassOrInterfaceDeclaration cd = demandClassOrInterface(cu, name); ClassOrInterfaceDeclaration cd = demandClassOrInterface(cu, qualifiedName);
if (cd.isInterface()) { if (cd.isInterface()) {
throw new IllegalStateException("Type is not a class"); throw new IllegalStateException("Type is not a class");
} }
return cd; return cd;
} }


public static EnumDeclaration demandEnum(CompilationUnit cu, String name) { public static EnumDeclaration demandEnum(CompilationUnit cu, String qualifiedName) {
Optional<TypeDeclaration> res = findType(cu, name); Optional<TypeDeclaration> res = findType(cu, qualifiedName);
if (!res.isPresent()) { if (!res.isPresent()) {
throw new IllegalStateException("No type found"); throw new IllegalStateException("No type found");
} }
Expand Down Expand Up @@ -152,10 +163,10 @@ public static VariableDeclarator demandVariableDeclaration(Node node, String nam
return null; return null;
} }


public static ClassOrInterfaceDeclaration demandClassOrInterface(CompilationUnit compilationUnit, String name) { public static ClassOrInterfaceDeclaration demandClassOrInterface(CompilationUnit compilationUnit, String qualifiedName) {
Optional<TypeDeclaration> res = findType(compilationUnit, name); Optional<TypeDeclaration> res = findType(compilationUnit, qualifiedName);
if (!res.isPresent()) { if (!res.isPresent()) {
throw new IllegalStateException("No type named '" + name + "'found"); throw new IllegalStateException("No type named '" + qualifiedName + "'found");
} }
if (!(res.get() instanceof ClassOrInterfaceDeclaration)) { if (!(res.get() instanceof ClassOrInterfaceDeclaration)) {
throw new IllegalStateException("Type is not a class or an interface, it is " + res.get().getClass().getCanonicalName()); throw new IllegalStateException("Type is not a class or an interface, it is " + res.get().getClass().getCanonicalName());
Expand Down
Expand Up @@ -3,7 +3,6 @@
import com.github.javaparser.JavaParser; import com.github.javaparser.JavaParser;
import com.github.javaparser.ParseException; import com.github.javaparser.ParseException;
import com.github.javaparser.ast.CompilationUnit; import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.Node;


import me.tomassetti.symbolsolver.javaparser.Navigator; import me.tomassetti.symbolsolver.javaparser.Navigator;
import me.tomassetti.symbolsolver.model.declarations.TypeDeclaration; import me.tomassetti.symbolsolver.model.declarations.TypeDeclaration;
Expand Down Expand Up @@ -46,15 +45,6 @@ public void setParent(TypeSolver parent) {
this.parent = parent; this.parent = parent;
} }


private String simpleName(String name) {
int index = name.lastIndexOf('.');
if (index == -1) {
return name;
} else {
return name.substring(index + 1);
}
}

@Override @Override
public SymbolReference<TypeDeclaration> tryToSolveType(String name) { public SymbolReference<TypeDeclaration> tryToSolveType(String name) {
// TODO support enums // TODO support enums
Expand Down

0 comments on commit ac259cd

Please sign in to comment.