Skip to content

Commit

Permalink
playing with loop detection
Browse files Browse the repository at this point in the history
  • Loading branch information
ftomassetti committed Mar 7, 2017
1 parent 42dbe7d commit eb15b0c
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 14 deletions.
Expand Up @@ -55,6 +55,7 @@
public class JavaParserFacade {

private static Logger logger = Logger.getLogger(JavaParserFacade.class.getCanonicalName());
private Map<ClassOrInterfaceDeclaration, ReferenceTypeDeclaration> declarationsCache = new WeakHashMap<>();

static {
logger.setLevel(Level.INFO);
Expand Down Expand Up @@ -487,6 +488,13 @@ public MethodUsage solveMethodAsUsage(MethodCallExpr call) {
}

public ReferenceTypeDeclaration getTypeDeclaration(ClassOrInterfaceDeclaration classOrInterfaceDeclaration) {
if (!declarationsCache.containsKey(classOrInterfaceDeclaration)) {
declarationsCache.put(classOrInterfaceDeclaration, getTypeDeclarationImpl(classOrInterfaceDeclaration));
}
return declarationsCache.get(classOrInterfaceDeclaration);
}

private ReferenceTypeDeclaration getTypeDeclarationImpl(ClassOrInterfaceDeclaration classOrInterfaceDeclaration) {
if (classOrInterfaceDeclaration.isInterface()) {
return new JavaParserInterfaceDeclaration(classOrInterfaceDeclaration, typeSolver);
} else {
Expand Down
Expand Up @@ -30,14 +30,26 @@
import com.github.javaparser.symbolsolver.model.resolution.TypeSolver;
import com.github.javaparser.symbolsolver.resolution.SymbolDeclarator;

import java.util.Map;
import java.util.WeakHashMap;

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

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

private static Map<Node, Context> cache = new WeakHashMap<>();

public static Context getContext(Node node, TypeSolver typeSolver) {
if (!cache.containsKey(node)) {
cache.put(node, getContextImpl(node, typeSolver));
}
return cache.get(node);
}

private static Context getContextImpl(Node node, TypeSolver typeSolver) {
if (node == null) {
return null;
} else if (node instanceof CompilationUnit) {
Expand Down
Expand Up @@ -17,6 +17,7 @@
package com.github.javaparser.symbolsolver.javaparsermodel.contexts;

import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade;
import com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserClassDeclaration;
import com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserInterfaceDeclaration;
import com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserTypeParameter;
Expand All @@ -26,6 +27,7 @@
import com.github.javaparser.symbolsolver.model.resolution.Value;
import com.github.javaparser.symbolsolver.model.typesystem.Type;
import com.github.javaparser.symbolsolver.model.typesystem.TypeVariable;
import com.github.javaparser.utils.Pair;

import java.util.List;
import java.util.Optional;
Expand Down Expand Up @@ -87,7 +89,10 @@ public Optional<Type> solveGenericType(String name, TypeSolver typeSolver) {

@Override
public SymbolReference<TypeDeclaration> solveType(String name, TypeSolver typeSolver) {
return javaParserTypeDeclarationAdapter.solveType(name, typeSolver);
System.out.println("<" + System.identityHashCode(this)+ ">solvingType " + name + " START");
SymbolReference<TypeDeclaration> res = javaParserTypeDeclarationAdapter.solveType(name, typeSolver);
System.out.println("<" + System.identityHashCode(this)+ ">solvingType " + name + " DONE");
return res;
}

@Override
Expand All @@ -104,10 +109,6 @@ public SymbolReference<ConstructorDeclaration> solveConstructor(List<Type> argum
///

private ReferenceTypeDeclaration getDeclaration() {
if (this.wrappedNode.isInterface()) {
return new JavaParserInterfaceDeclaration(this.wrappedNode, typeSolver);
} else {
return new JavaParserClassDeclaration(this.wrappedNode, typeSolver);
}
return JavaParserFacade.get(typeSolver).getTypeDeclaration(this.wrappedNode);
}
}
Expand Up @@ -17,6 +17,7 @@
import com.github.javaparser.symbolsolver.resolution.MethodResolutionLogic;

import java.util.List;
import java.util.Stack;
import java.util.stream.Collectors;

/**
Expand All @@ -28,6 +29,7 @@ public class JavaParserTypeDeclarationAdapter {
private TypeSolver typeSolver;
private Context context;
private ReferenceTypeDeclaration typeDeclaration;
private Stack<String> namesBeingSolved = new Stack<>();

public JavaParserTypeDeclarationAdapter(com.github.javaparser.ast.body.TypeDeclaration<?> wrappedNode, TypeSolver typeSolver,
ReferenceTypeDeclaration typeDeclaration,
Expand All @@ -39,6 +41,10 @@ public JavaParserTypeDeclarationAdapter(com.github.javaparser.ast.body.TypeDecla
}

public SymbolReference<TypeDeclaration> solveType(String name, TypeSolver typeSolver) {
boolean loop = namesBeingSolved.contains(name);

System.out.println("<"+System.identityHashCode(this)+">ADAPTER START ON "+name);
namesBeingSolved.add(name);
if (this.wrappedNode.getName().getId().equals(name)) {
return SymbolReference.solved(JavaParserFacade.get(typeSolver).getTypeDeclaration(wrappedNode));
}
Expand Down Expand Up @@ -68,14 +74,18 @@ public SymbolReference<TypeDeclaration> solveType(String name, TypeSolver typeSo
}

// Look into extended classes and implemented interfaces
for (ReferenceType ancestor : this.typeDeclaration.getAncestors()) {
for (TypeDeclaration internalTypeDeclaration : ancestor.getTypeDeclaration().internalTypes()) {
if (internalTypeDeclaration.getName().equals(name)) {
return SymbolReference.solved(internalTypeDeclaration);
if (!loop) {
for (ReferenceType ancestor : this.typeDeclaration.getAncestors()) {
for (TypeDeclaration internalTypeDeclaration : ancestor.getTypeDeclaration().internalTypes()) {
if (internalTypeDeclaration.getName().equals(name)) {
return SymbolReference.solved(internalTypeDeclaration);
}
}
}
}

System.out.println("<"+System.identityHashCode(this)+">ADAPTER START ON "+name+ " ABOUT TO END");
namesBeingSolved.remove(name);
return context.getParent().solveType(name, typeSolver);
}

Expand Down
Expand Up @@ -27,7 +27,6 @@
import com.github.javaparser.symbolsolver.logic.AbstractClassDeclaration;
import com.github.javaparser.symbolsolver.model.declarations.*;
import com.github.javaparser.symbolsolver.model.declarations.ConstructorDeclaration;
import com.github.javaparser.symbolsolver.model.declarations.EnumDeclaration;
import com.github.javaparser.symbolsolver.model.declarations.FieldDeclaration;
import com.github.javaparser.symbolsolver.model.declarations.MethodDeclaration;
import com.github.javaparser.symbolsolver.model.declarations.TypeDeclaration;
Expand All @@ -47,6 +46,8 @@
*/
public class JavaParserClassDeclaration extends AbstractClassDeclaration {

private static Map<String, TypeDeclaration> cache = new HashMap<>();

///
/// Fields
///
Expand Down Expand Up @@ -149,10 +150,14 @@ public String getName() {

@Override
public ReferenceType getSuperClass() {
System.out.println("<"+System.identityHashCode(this)+"> getSuperClass START");
if (wrappedNode.getExtendedTypes().isEmpty()) {
System.out.println("<"+System.identityHashCode(this)+"> getSuperClass DONE");
return object();
} else {
return toReferenceType(wrappedNode.getExtendedTypes().get(0));
ReferenceType res = toReferenceType(wrappedNode.getExtendedTypes().get(0));
System.out.println("<"+System.identityHashCode(this)+"> getSuperClass DONE");
return res;
}
}

Expand Down Expand Up @@ -250,6 +255,11 @@ public boolean isTypeParameter() {

@Deprecated
public SymbolReference<TypeDeclaration> solveType(String name, TypeSolver typeSolver) {
System.out.println("<"+System.identityHashCode(this)+">Solving type "+name+" from class declaration "+this.getName()+ " STARTING");
if (cache.containsKey(name)) {
System.out.println("<"+System.identityHashCode(this)+">Solving type "+name+" from class declaration "+this.getName()+ " DONE FROM CACHE");
return SymbolReference.solved(cache.get(name));
}
if (this.wrappedNode.getName().getId().equals(name)) {
return SymbolReference.solved(this);
}
Expand All @@ -263,11 +273,17 @@ public SymbolReference<TypeDeclaration> solveType(String name, TypeSolver typeSo
return new JavaParserClassDeclaration(this.wrappedNode, typeSolver).solveType(name.substring(prefix.length()), typeSolver);
}

return getContext().getParent().solveType(name, typeSolver);
SymbolReference<TypeDeclaration> res = getContext().getParent().solveType(name, typeSolver);
if (res.isSolved()) {
cache.put(name, res.getCorrespondingDeclaration());
}
System.out.println("<"+System.identityHashCode(this)+">Solving type "+name+" from class declaration "+this.getName()+ " DONE");
return res;
}

@Override
public List<ReferenceType> getAncestors() {
System.out.println("<"+System.identityHashCode(this)+"> getAncestors START");
List<ReferenceType> ancestors = new ArrayList<>();
ReferenceType superclass = getSuperClass();
if (superclass != null) {
Expand All @@ -279,6 +295,7 @@ public List<ReferenceType> getAncestors() {
ancestors.add(ancestor);
}
}
System.out.println("<"+System.identityHashCode(this)+"> getAncestors DONE");
return ancestors;
}

Expand Down Expand Up @@ -328,6 +345,7 @@ protected ReferenceType object() {
///

private ReferenceType toReferenceType(ClassOrInterfaceType classOrInterfaceType) {
System.out.println("<"+System.identityHashCode(this)+">Transforming "+classOrInterfaceType.getName()+ " to ReferenceType "+classOrInterfaceType + " " + System.identityHashCode(classOrInterfaceType) + " START");
String className = classOrInterfaceType.getName().getId();
if (classOrInterfaceType.getScope().isPresent()) {
// look for the qualified name (for example class of type Rectangle2D.Double)
Expand All @@ -350,6 +368,7 @@ private ReferenceType toReferenceType(ClassOrInterfaceType classOrInterfaceType)
List<Type> superClassTypeParameters = classOrInterfaceType.getTypeArguments().get()
.stream().map(ta -> JavaParserFacade.get(typeSolver).convert(ta, ta))
.collect(Collectors.toList());
System.out.println("<"+System.identityHashCode(this)+">Transforming "+classOrInterfaceType.getName()+ " to ReferenceType "+classOrInterfaceType + " " + System.identityHashCode(classOrInterfaceType) + " DONE");
return new ReferenceTypeImpl(ref.getCorrespondingDeclaration().asReferenceType(), superClassTypeParameters, typeSolver);
}

Expand Down
Expand Up @@ -8,7 +8,7 @@ class CA<T> implements I {

}

private class CB extends CA<java.util.List<Boolean>> {
class CB extends CA<java.util.List<Boolean>> {

String s;

Expand Down

0 comments on commit eb15b0c

Please sign in to comment.