Skip to content

Commit

Permalink
creating ReferenceTypeDeclaration
Browse files Browse the repository at this point in the history
  • Loading branch information
ftomassetti committed Nov 5, 2016
1 parent f93a05f commit 6df1aa2
Show file tree
Hide file tree
Showing 62 changed files with 576 additions and 493 deletions.
Expand Up @@ -17,7 +17,7 @@
package com.github.javaparser.symbolsolver.core.resolution;

import com.github.javaparser.symbolsolver.model.declarations.MethodDeclaration;
import com.github.javaparser.symbolsolver.model.declarations.TypeDeclaration;
import com.github.javaparser.symbolsolver.model.declarations.ReferenceTypeDeclaration;
import com.github.javaparser.symbolsolver.model.declarations.ValueDeclaration;
import com.github.javaparser.symbolsolver.model.methods.MethodUsage;
import com.github.javaparser.symbolsolver.model.resolution.SymbolReference;
Expand All @@ -44,10 +44,10 @@ default Optional<Type> solveGenericType(String name, TypeSolver typeSolver) {
return Optional.empty();
}

default SymbolReference<TypeDeclaration> solveType(String name, TypeSolver typeSolver) {
default SymbolReference<ReferenceTypeDeclaration> solveType(String name, TypeSolver typeSolver) {
Context parent = getParent();
if (parent == null) {
return SymbolReference.unsolved(TypeDeclaration.class);
return SymbolReference.unsolved(ReferenceTypeDeclaration.class);
} else {
return getParent().solveType(name, typeSolver);
}
Expand Down
Expand Up @@ -26,7 +26,14 @@

import java.util.List;

public class ContextHelper {
/**
* @author Federico Tomassetti
*/
class ContextHelper {

private ContextHelper() {
// prevent instantiation
}

static MethodUsage resolveTypeVariables(Context context, MethodDeclaration methodDeclaration, List<Type> parameterTypes) {
if (methodDeclaration instanceof JavaParserMethodDeclaration) {
Expand Down
Expand Up @@ -31,17 +31,15 @@

/**
* This class can be used to easily retrieve nodes from a JavaParser AST.
*
* @author Federico Tomassetti
*/
public final class Navigator {

private Navigator() {
// prevent instantiation
}

private static String getOuterTypeName(String qualifiedName) {
return qualifiedName.split("\\.", 2)[0];
}

public static Node getParentNode(Node node) {
Node parent = node.getParentNode();
if (parent instanceof NodeList) {
Expand All @@ -51,13 +49,6 @@ public static Node getParentNode(Node node) {
}
}

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().isEmpty()) {
return Optional.empty();
Expand Down Expand Up @@ -90,7 +81,6 @@ public static Optional<TypeDeclaration<?>> findType(TypeDeclaration<?> td, Strin
return type;
}


public static ClassOrInterfaceDeclaration demandClass(CompilationUnit cu, String qualifiedName) {
ClassOrInterfaceDeclaration cd = demandClassOrInterface(cu, qualifiedName);
if (cd.isInterface()) {
Expand Down Expand Up @@ -212,6 +202,50 @@ public static SwitchStmt findSwitch(Node node) {
}
}

public static <N> N findNodeOfGivenClass(Node node, Class<N> clazz) {
N res = findNodeOfGivenClassHelper(node, clazz);
if (res == null) {
throw new IllegalArgumentException();
} else {
return res;
}
}

public static <N> List<N> findAllNodesOfGivenClass(Node node, Class<N> clazz) {
List<N> res = new LinkedList<>();
findAllNodesOfGivenClassHelper(node, clazz, res);
return res;
}

public static ReturnStmt findReturnStmt(MethodDeclaration method) {
return findNodeOfGivenClass(method, ReturnStmt.class);
}

public static <N extends Node> Optional<N> findAncestor(Node node, Class<N> clazz) {
if (node.getParentNode() == null) {
return Optional.empty();
} else if (clazz.isInstance(node.getParentNode())) {
return Optional.of(clazz.cast(node.getParentNode()));
} else {
return findAncestor(node.getParentNode(), clazz);
}
}

///
/// Private methods
///

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 "";
}

private static SwitchStmt findSwitchHelper(Node node) {
if (node instanceof SwitchStmt) {
return (SwitchStmt) node;
Expand All @@ -238,21 +272,6 @@ private static <N> N findNodeOfGivenClassHelper(Node node, Class<N> clazz) {
return null;
}

public static <N> N findNodeOfGivenClass(Node node, Class<N> clazz) {
N res = findNodeOfGivenClassHelper(node, clazz);
if (res == null) {
throw new IllegalArgumentException();
} else {
return res;
}
}

public static <N> List<N> findAllNodesOfGivenClass(Node node, Class<N> clazz) {
List<N> res = new LinkedList<>();
findAllNodesOfGivenClassHelper(node, clazz, res);
return res;
}

private static <N> void findAllNodesOfGivenClassHelper(Node node, Class<N> clazz, List<N> collector) {
if (clazz.isInstance(node)) {
collector.add(clazz.cast(node));
Expand All @@ -261,18 +280,4 @@ private static <N> void findAllNodesOfGivenClassHelper(Node node, Class<N> clazz
findAllNodesOfGivenClassHelper(child, clazz, collector);
}
}

public static ReturnStmt findReturnStmt(MethodDeclaration method) {
return findNodeOfGivenClass(method, ReturnStmt.class);
}

public static <N extends Node> Optional<N> findAncestor(Node node, Class<N> clazz) {
if (node.getParentNode() == null) {
return Optional.empty();
} else if (clazz.isInstance(node.getParentNode())) {
return Optional.of(clazz.cast(node.getParentNode()));
} else {
return findAncestor(node.getParentNode(), clazz);
}
}
}
Expand Up @@ -245,7 +245,7 @@ private MethodUsage toMethodUsage(MethodReferenceExpr methodReferenceExpr) {
throw new UnsupportedOperationException(typeExpr.getType().getClass().getCanonicalName());
}
ClassOrInterfaceType classOrInterfaceType = (ClassOrInterfaceType) typeExpr.getType();
SymbolReference<TypeDeclaration> typeDeclarationSymbolReference = JavaParserFactory.getContext(classOrInterfaceType, typeSolver).solveType(classOrInterfaceType.getName(), typeSolver);
SymbolReference<ReferenceTypeDeclaration> typeDeclarationSymbolReference = JavaParserFactory.getContext(classOrInterfaceType, typeSolver).solveType(classOrInterfaceType.getName(), typeSolver);
if (!typeDeclarationSymbolReference.isSolved()) {
throw new UnsupportedOperationException();
}
Expand Down Expand Up @@ -439,7 +439,7 @@ private Type getTypeConcrete(Node node, boolean solveLambdas) {
// We should understand if this is a static access
if (fieldAccessExpr.getScope() instanceof NameExpr) {
NameExpr staticValue = (NameExpr) fieldAccessExpr.getScope();
SymbolReference<TypeDeclaration> typeAccessedStatically = JavaParserFactory.getContext(fieldAccessExpr, typeSolver).solveType(staticValue.toString(), typeSolver);
SymbolReference<ReferenceTypeDeclaration> typeAccessedStatically = JavaParserFactory.getContext(fieldAccessExpr, typeSolver).solveType(staticValue.toString(), typeSolver);
if (typeAccessedStatically.isSolved()) {
// TODO here maybe we have to substitute type typeParametersValues
return typeAccessedStatically.getCorrespondingDeclaration().getField(fieldAccessExpr.getField()).getType();
Expand Down Expand Up @@ -591,11 +591,11 @@ public Type convertToUsage(com.github.javaparser.ast.type.Type type, Context con
if (type instanceof ClassOrInterfaceType) {
ClassOrInterfaceType classOrInterfaceType = (ClassOrInterfaceType) type;
String name = qName(classOrInterfaceType);
SymbolReference<TypeDeclaration> ref = context.solveType(name, typeSolver);
SymbolReference<ReferenceTypeDeclaration> ref = context.solveType(name, typeSolver);
if (!ref.isSolved()) {
throw new UnsolvedSymbolException(name);
}
TypeDeclaration typeDeclaration = ref.getCorrespondingDeclaration();
ReferenceTypeDeclaration typeDeclaration = ref.getCorrespondingDeclaration();
List<Type> typeParameters = Collections.emptyList();
if (classOrInterfaceType.getTypeArguments().isPresent()) {
typeParameters = classOrInterfaceType.getTypeArguments().get().stream().map((pt) -> convertToUsage(pt, context)).collect(Collectors.toList());
Expand Down Expand Up @@ -664,7 +664,7 @@ public MethodUsage solveMethodAsUsage(MethodCallExpr call) {
return methodUsage.get();
}

public TypeDeclaration getTypeDeclaration(ClassOrInterfaceDeclaration classOrInterfaceDeclaration) {
public ReferenceTypeDeclaration getTypeDeclaration(ClassOrInterfaceDeclaration classOrInterfaceDeclaration) {
if (classOrInterfaceDeclaration.isInterface()) {
return new JavaParserInterfaceDeclaration(classOrInterfaceDeclaration, typeSolver);
} else {
Expand All @@ -687,7 +687,7 @@ public Type getTypeOfThisIn(Node node) {
}
}

public TypeDeclaration getTypeDeclaration(com.github.javaparser.ast.body.TypeDeclaration typeDeclaration) {
public ReferenceTypeDeclaration getTypeDeclaration(com.github.javaparser.ast.body.TypeDeclaration typeDeclaration) {
if (typeDeclaration instanceof ClassOrInterfaceDeclaration) {
return getTypeDeclaration((ClassOrInterfaceDeclaration) typeDeclaration);
} else if (typeDeclaration instanceof EnumDeclaration) {
Expand Down
Expand Up @@ -40,13 +40,9 @@ public abstract class AbstractJavaParserContext<N extends Node> implements Conte
protected N wrappedNode;
protected TypeSolver typeSolver;

public AbstractJavaParserContext(N wrappedNode, TypeSolver typeSolver) {
if (wrappedNode == null) {
throw new NullPointerException();
}
this.wrappedNode = wrappedNode;
this.typeSolver = typeSolver;
}
///
/// Static methods
///

protected static final SymbolReference<ValueDeclaration> solveWith(SymbolDeclarator symbolDeclarator, String name) {
for (ValueDeclaration decl : symbolDeclarator.getSymbolDeclarations()) {
Expand All @@ -57,6 +53,22 @@ protected static final SymbolReference<ValueDeclaration> solveWith(SymbolDeclara
return SymbolReference.unsolved(ValueDeclaration.class);
}

///
/// Constructors
///

public AbstractJavaParserContext(N wrappedNode, TypeSolver typeSolver) {
if (wrappedNode == null) {
throw new NullPointerException();
}
this.wrappedNode = wrappedNode;
this.typeSolver = typeSolver;
}

///
/// Public methods
///

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down Expand Up @@ -84,15 +96,6 @@ public Optional<Type> solveGenericType(String name, TypeSolver typeSolver) {
}
}

protected Optional<Value> solveWithAsValue(SymbolDeclarator symbolDeclarator, String name, TypeSolver typeSolver) {
for (ValueDeclaration decl : symbolDeclarator.getSymbolDeclarations()) {
if (decl.getName().equals(name)) {
return Optional.of(Value.from(decl));
}
}
return Optional.empty();
}

@Override
public final Context getParent() {
if (getParentNode(wrappedNode) instanceof MethodCallExpr) {
Expand Down Expand Up @@ -120,4 +123,15 @@ public final Context getParent() {
return JavaParserFactory.getContext(notMethod, typeSolver);
}

///
/// Protected methods
///

protected Optional<Value> solveWithAsValue(SymbolDeclarator symbolDeclarator, String name, TypeSolver typeSolver) {
return symbolDeclarator.getSymbolDeclarations().stream()
.filter(d -> d.getName().equals(name))
.map(d -> Value.from(d))
.findFirst();
}

}

0 comments on commit 6df1aa2

Please sign in to comment.