Skip to content

Commit

Permalink
Cleaning up JSS code
Browse files Browse the repository at this point in the history
  • Loading branch information
matozoid committed Feb 3, 2018
1 parent 9f20df2 commit b54617e
Show file tree
Hide file tree
Showing 85 changed files with 811 additions and 952 deletions.
Expand Up @@ -431,7 +431,7 @@ public <N extends Node> List<N> getChildNodesByType(Class<N> clazz) {
} }


/** /**
* @deprecated use find(Class) * @deprecated use findAll(Class)
*/ */
@Deprecated @Deprecated
public <N extends Node> List<N> getNodesByType(Class<N> clazz) { public <N extends Node> List<N> getNodesByType(Class<N> clazz) {
Expand Down
Expand Up @@ -103,11 +103,17 @@ public Optional<ReferenceType> getSuperType() {
return Optional.ofNullable(superType); return Optional.ofNullable(superType);
} }


/**
* @deprecated use getExtendedType instead.
*/
@Deprecated @Deprecated
public Optional<ReferenceType> getExtendedTypes() { public Optional<ReferenceType> getExtendedTypes() {
return getExtendedType(); return getExtendedType();
} }


/**
* @deprecated use getSuperType instead.
*/
@Deprecated @Deprecated
public Optional<ReferenceType> getSuperTypes() { public Optional<ReferenceType> getSuperTypes() {
return getSuperType(); return getSuperType();
Expand Down
Expand Up @@ -8,21 +8,17 @@
import com.github.javaparser.ast.expr.NameExpr; import com.github.javaparser.ast.expr.NameExpr;
import com.github.javaparser.ast.expr.ThisExpr; import com.github.javaparser.ast.expr.ThisExpr;
import com.github.javaparser.ast.stmt.ExplicitConstructorInvocationStmt; import com.github.javaparser.ast.stmt.ExplicitConstructorInvocationStmt;
import com.github.javaparser.ast.type.ArrayType;
import com.github.javaparser.ast.type.Type; import com.github.javaparser.ast.type.Type;
import com.github.javaparser.resolution.SymbolResolver; import com.github.javaparser.resolution.SymbolResolver;
import com.github.javaparser.resolution.UnsolvedSymbolException; import com.github.javaparser.resolution.UnsolvedSymbolException;
import com.github.javaparser.resolution.declarations.*; import com.github.javaparser.resolution.declarations.*;
import com.github.javaparser.resolution.types.ResolvedType; import com.github.javaparser.resolution.types.ResolvedType;
import com.github.javaparser.symbolsolver.javaparser.Navigator;
import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade; import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade;
import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFactory; import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFactory;
import com.github.javaparser.symbolsolver.javaparsermodel.declarations.*; import com.github.javaparser.symbolsolver.javaparsermodel.declarations.*;
import com.github.javaparser.symbolsolver.model.resolution.SymbolReference; import com.github.javaparser.symbolsolver.model.resolution.SymbolReference;
import com.github.javaparser.symbolsolver.model.resolution.TypeSolver; import com.github.javaparser.symbolsolver.model.resolution.TypeSolver;


import java.util.Optional;

/** /**
* This implementation of the SymbolResolver wraps the functionalities of the library to make them easily usable * This implementation of the SymbolResolver wraps the functionalities of the library to make them easily usable
* from JavaParser nodes. * from JavaParser nodes.
Expand Down Expand Up @@ -66,7 +62,7 @@ public <T> T resolveDeclaration(Node node, Class<T> resultClass) {
} }
} }
if (node instanceof EnumConstantDeclaration) { if (node instanceof EnumConstantDeclaration) {
ResolvedEnumDeclaration enumDeclaration = Navigator.findAncestor(node, EnumDeclaration.class).get().resolve().asEnum(); ResolvedEnumDeclaration enumDeclaration = node.findParent(EnumDeclaration.class).get().resolve().asEnum();
ResolvedEnumConstantDeclaration resolved = enumDeclaration.getEnumConstants().stream().filter(c -> ((JavaParserEnumConstantDeclaration)c).getWrappedNode() == node).findFirst().get(); ResolvedEnumConstantDeclaration resolved = enumDeclaration.getEnumConstants().stream().filter(c -> ((JavaParserEnumConstantDeclaration)c).getWrappedNode() == node).findFirst().get();
if (resultClass.isInstance(resolved)) { if (resultClass.isInstance(resolved)) {
return resultClass.cast(resolved); return resultClass.cast(resolved);
Expand All @@ -88,7 +84,7 @@ public <T> T resolveDeclaration(Node node, Class<T> resultClass) {
} }
} }
if (node instanceof AnnotationMemberDeclaration) { if (node instanceof AnnotationMemberDeclaration) {
ResolvedAnnotationDeclaration annotationDeclaration = Navigator.findAncestor(node, AnnotationDeclaration.class).get().resolve(); ResolvedAnnotationDeclaration annotationDeclaration = node.findParent(AnnotationDeclaration.class).get().resolve();
ResolvedAnnotationMemberDeclaration resolved = annotationDeclaration.getAnnotationMembers().stream().filter(c -> ((JavaParserAnnotationMemberDeclaration)c).getWrappedNode() == node).findFirst().get(); ResolvedAnnotationMemberDeclaration resolved = annotationDeclaration.getAnnotationMembers().stream().filter(c -> ((JavaParserAnnotationMemberDeclaration)c).getWrappedNode() == node).findFirst().get();
if (resultClass.isInstance(resolved)) { if (resultClass.isInstance(resolved)) {
return resultClass.cast(resolved); return resultClass.cast(resolved);
Expand Down Expand Up @@ -153,7 +149,7 @@ public <T> T resolveDeclaration(Node node, Class<T> resultClass) {
if (node instanceof Parameter) { if (node instanceof Parameter) {
if (ResolvedParameterDeclaration.class.equals(resultClass)) { if (ResolvedParameterDeclaration.class.equals(resultClass)) {
Parameter parameter = (Parameter)node; Parameter parameter = (Parameter)node;
CallableDeclaration callableDeclaration = Navigator.findAncestor(node, CallableDeclaration.class).get(); CallableDeclaration callableDeclaration = node.findParent(CallableDeclaration.class).get();
ResolvedMethodLikeDeclaration resolvedMethodLikeDeclaration; ResolvedMethodLikeDeclaration resolvedMethodLikeDeclaration;
if (callableDeclaration.isConstructorDeclaration()) { if (callableDeclaration.isConstructorDeclaration()) {
resolvedMethodLikeDeclaration = callableDeclaration.asConstructorDeclaration().resolve(); resolvedMethodLikeDeclaration = callableDeclaration.asConstructorDeclaration().resolve();
Expand Down
Expand Up @@ -43,6 +43,7 @@
import java.util.List; import java.util.List;


import static com.github.javaparser.symbolsolver.javaparser.Navigator.getParentNode; import static com.github.javaparser.symbolsolver.javaparser.Navigator.getParentNode;
import static com.github.javaparser.symbolsolver.javaparser.Navigator.requireParentNode;


/** /**
* It prints information extracted from a source file. It is mainly intended as an example usage of JavaSymbolSolver. * It prints information extracted from a source file. It is mainly intended as an example usage of JavaSymbolSolver.
Expand Down Expand Up @@ -114,11 +115,11 @@ private void solve(Node node) {
if (node instanceof ClassOrInterfaceDeclaration) { if (node instanceof ClassOrInterfaceDeclaration) {
solveTypeDecl((ClassOrInterfaceDeclaration) node); solveTypeDecl((ClassOrInterfaceDeclaration) node);
} else if (node instanceof Expression) { } else if (node instanceof Expression) {
if ((getParentNode(node) instanceof ImportDeclaration) || (getParentNode(node) instanceof Expression) if ((requireParentNode(node) instanceof ImportDeclaration) || (requireParentNode(node) instanceof Expression)
|| (getParentNode(node) instanceof MethodDeclaration) || (requireParentNode(node) instanceof MethodDeclaration)
|| (getParentNode(node) instanceof PackageDeclaration)) { || (requireParentNode(node) instanceof PackageDeclaration)) {
// skip // skip
} else if ((getParentNode(node) instanceof Statement) || (getParentNode(node) instanceof VariableDeclarator)) { } else if ((requireParentNode(node) instanceof Statement) || (requireParentNode(node) instanceof VariableDeclarator)) {
try { try {
ResolvedType ref = JavaParserFacade.get(typeSolver).getType(node); ResolvedType ref = JavaParserFacade.get(typeSolver).getType(node);
out.println(" Line " + node.getRange().get().begin.line + ") " + node + " ==> " + ref.describe()); out.println(" Line " + node.getRange().get().begin.line + ") " + node + " ==> " + ref.describe());
Expand Down
Expand Up @@ -25,7 +25,6 @@
import com.github.javaparser.ast.stmt.ReturnStmt; import com.github.javaparser.ast.stmt.ReturnStmt;
import com.github.javaparser.ast.stmt.SwitchStmt; import com.github.javaparser.ast.stmt.SwitchStmt;


import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;


Expand All @@ -40,17 +39,16 @@ private Navigator() {
// prevent instantiation // prevent instantiation
} }


/**
* @deprecated use Node.getParentNode
*/
@Deprecated
public static Node getParentNode(Node node) { public static Node getParentNode(Node node) {
Node parent = node.getParentNode().orElse(null); return node.getParentNode().orElse(null);
return parent;
} }


public static Node requireParentNode(Node node) { public static Node requireParentNode(Node node) {
Node parent = getParentNode(node); return node.getParentNode().orElseThrow(() -> new IllegalStateException("Parent not found, the node does not appear to be inserted in a correct AST"));
if (parent == null) {
throw new IllegalStateException("Parent not found, the node does not appear to be inserted in a correct AST");
}
return parent;
} }


public static Optional<TypeDeclaration<?>> findType(CompilationUnit cu, String qualifiedName) { public static Optional<TypeDeclaration<?>> findType(CompilationUnit cu, String qualifiedName) {
Expand Down Expand Up @@ -109,7 +107,7 @@ public static MethodDeclaration demandMethod(TypeDeclaration<?> cd, String name)
for (BodyDeclaration<?> bd : cd.getMembers()) { for (BodyDeclaration<?> bd : cd.getMembers()) {
if (bd instanceof MethodDeclaration) { if (bd instanceof MethodDeclaration) {
MethodDeclaration md = (MethodDeclaration) bd; MethodDeclaration md = (MethodDeclaration) bd;
if (md.getName().getId().equals(name)) { if (md.getNameAsString().equals(name)) {
if (found != null) { if (found != null) {
throw new IllegalStateException("Ambiguous getName"); throw new IllegalStateException("Ambiguous getName");
} }
Expand All @@ -118,7 +116,7 @@ public static MethodDeclaration demandMethod(TypeDeclaration<?> cd, String name)
} }
} }
if (found == null) { if (found == null) {
throw new IllegalStateException("No method with given name"); throw new IllegalStateException("No method called " + name);
} }
return found; return found;
} }
Expand All @@ -137,126 +135,57 @@ public static VariableDeclarator demandField(ClassOrInterfaceDeclaration cd, Str
throw new IllegalStateException("No field with given name"); throw new IllegalStateException("No field with given name");
} }


public static NameExpr findNameExpression(Node node, String name) { public static Optional<NameExpr> findNameExpression(Node node, String name) {
if (node instanceof NameExpr) { return node.findFirst(NameExpr.class, n -> n.getNameAsString().equals(name));
NameExpr nameExpr = (NameExpr) node;
if (nameExpr.getName() != null && nameExpr.getName().getId().equals(name)) {
return nameExpr;
}
}
for (Node child : node.getChildNodes()) {
NameExpr res = findNameExpression(child, name);
if (res != null) {
return res;
}
}
return null;
} }


public static SimpleName findSimpleName(Node node, String name) { public static Optional<SimpleName> findSimpleName(Node node, String name) {
if (node instanceof SimpleName) { return node.findFirst(SimpleName.class, n -> n.asString().equals(name));
SimpleName nameExpr = (SimpleName) node;
if (nameExpr.getId() != null && nameExpr.getId().equals(name)) {
return nameExpr;
}
}
for (Node child : node.getChildNodes()) {
SimpleName res = findSimpleName(child, name);
if (res != null) {
return res;
}
}
return null;
} }


public static MethodCallExpr findMethodCall(Node node, String methodName) {
if (node instanceof MethodCallExpr) { public static Optional<MethodCallExpr> findMethodCall(Node node, String methodName) {
MethodCallExpr methodCallExpr = (MethodCallExpr) node; return node.findFirst(MethodCallExpr.class, n -> n.getNameAsString().equals(methodName));
if (methodCallExpr.getName().getId().equals(methodName)) {
return methodCallExpr;
}
}
for (Node child : node.getChildNodes()) {
MethodCallExpr res = findMethodCall(child, methodName);
if (res != null) {
return res;
}
}
return null;
} }


public static VariableDeclarator demandVariableDeclaration(Node node, String name) { public static Optional<VariableDeclarator> demandVariableDeclaration(Node node, String name) {
if (node instanceof VariableDeclarator) { return node.findFirst(VariableDeclarator.class, n -> n.getNameAsString().equals(name));
VariableDeclarator variableDeclarator = (VariableDeclarator) node;
if (variableDeclarator.getName().getId().equals(name)) {
return variableDeclarator;
}
}
for (Node child : node.getChildNodes()) {
VariableDeclarator res = demandVariableDeclaration(child, name);
if (res != null) {
return res;
}
}
return null;
} }


public static ClassOrInterfaceDeclaration demandClassOrInterface(CompilationUnit compilationUnit, String qualifiedName) { public static ClassOrInterfaceDeclaration demandClassOrInterface(CompilationUnit compilationUnit, String qualifiedName) {
Optional<TypeDeclaration<?>> res = findType(compilationUnit, qualifiedName); return findType(compilationUnit, qualifiedName)
if (!res.isPresent()) { .map(res -> res.toClassOrInterfaceDeclaration().orElseThrow(() -> new IllegalStateException("Type is not a class or an interface, it is " + res.getClass().getCanonicalName())))
throw new IllegalStateException("No type named '" + qualifiedName + "'found"); .orElseThrow(() -> new IllegalStateException("No type named '" + qualifiedName + "'found"));
}
if (!(res.get() instanceof ClassOrInterfaceDeclaration)) {
throw new IllegalStateException("Type is not a class or an interface, it is " + res.get().getClass().getCanonicalName());
}
ClassOrInterfaceDeclaration cd = (ClassOrInterfaceDeclaration) res.get();
return cd;
} }


// TODO should be demand or requireSwitch
public static SwitchStmt findSwitch(Node node) { public static SwitchStmt findSwitch(Node node) {
SwitchStmt res = findSwitchHelper(node); return findSwitchHelper(node).orElseThrow(IllegalArgumentException::new);
if (res == null) {
throw new IllegalArgumentException();
} else {
return res;
}
} }


/** public static <N extends Node> N findNodeOfGivenClass(Node node, Class<N> clazz) {
* @deprecated use Node.findFirst instead return node.findFirst(clazz).orElseThrow(IllegalArgumentException::new);
*/
@Deprecated
public static <N> N findNodeOfGivenClass(Node node, Class<N> clazz) {
N res = findNodeOfGivenClassHelper(node, clazz);
if (res == null) {
throw new IllegalArgumentException();
} else {
return res;
}
} }


/** /**
* @deprecated use Node.findAll instead * @deprecated use Node.findAll instead
*/ */
@Deprecated @Deprecated
public static <N> List<N> findAllNodesOfGivenClass(Node node, Class<N> clazz) { public static <N extends Node> List<N> findAllNodesOfGivenClass(Node node, Class<N> clazz) {
List<N> res = new LinkedList<>(); return node.findAll(clazz);
findAllNodesOfGivenClassHelper(node, clazz, res);
return res;
} }


// TODO should be demand or require...
public static ReturnStmt findReturnStmt(MethodDeclaration method) { public static ReturnStmt findReturnStmt(MethodDeclaration method) {
return findNodeOfGivenClass(method, ReturnStmt.class); return findNodeOfGivenClass(method, ReturnStmt.class);
} }


/**
* @deprecated use Node.findParent instead
*/
@Deprecated
public static <N extends Node> Optional<N> findAncestor(Node node, Class<N> clazz) { public static <N extends Node> Optional<N> findAncestor(Node node, Class<N> clazz) {
if (!node.getParentNode().isPresent()) { return node.findParent(clazz);
return Optional.empty();
} else if (clazz.isInstance(node.getParentNode().get())) {
return Optional.of(clazz.cast(node.getParentNode().get()));
} else {
return findAncestor(node.getParentNode().get(), clazz);
}
} }


/// ///
Expand All @@ -274,38 +203,17 @@ private static String getInnerTypeName(String qualifiedName) {
return ""; return "";
} }


private static SwitchStmt findSwitchHelper(Node node) { private static Optional<SwitchStmt> findSwitchHelper(Node node) {
// TODO can be replaced by findFirst with the correct algorithm.
if (node instanceof SwitchStmt) { if (node instanceof SwitchStmt) {
return (SwitchStmt) node; return Optional.of((SwitchStmt) node);
} }
for (Node child : node.getChildNodes()) { for (Node child : node.getChildNodes()) {
SwitchStmt resChild = findSwitchHelper(child); Optional<SwitchStmt> resChild = findSwitchHelper(child);
if (resChild != null) { if (resChild.isPresent()) {
return resChild; return resChild;
} }
} }
return null; return Optional.empty();
}

private static <N> N findNodeOfGivenClassHelper(Node node, Class<N> clazz) {
if (clazz.isInstance(node)) {
return clazz.cast(node);
}
for (Node child : node.getChildNodes()) {
N resChild = findNodeOfGivenClassHelper(child, clazz);
if (resChild != null) {
return resChild;
}
}
return null;
}

private static <N> void findAllNodesOfGivenClassHelper(Node node, Class<N> clazz, List<N> collector) {
if (clazz.isInstance(node)) {
collector.add(clazz.cast(node));
}
for (Node child : node.getChildNodes()) {
findAllNodesOfGivenClassHelper(child, clazz, collector);
}
} }
} }

0 comments on commit b54617e

Please sign in to comment.