logic: indexer fixes for java imports
* fixed solving import for statically imported member types * enabled recording of fatal errors when an importing a type * added AstVisitorClient for improved abstraction and debugging purposes
This commit is contained in:
+92
-109
@@ -3,6 +3,7 @@ package com.sourcetrail;
|
||||
import java.lang.String;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
|
||||
@@ -45,24 +46,23 @@ import com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParse
|
||||
import com.github.javaparser.symbolsolver.model.declarations.MethodAmbiguityException;
|
||||
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;
|
||||
import com.github.javaparser.symbolsolver.model.resolution.TypeSolver;
|
||||
import com.github.javaparser.symbolsolver.model.resolution.UnsolvedSymbolException;
|
||||
import com.github.javaparser.symbolsolver.model.typesystem.*;
|
||||
import com.github.javaparser.symbolsolver.resolution.MethodResolutionLogic;
|
||||
|
||||
public class JavaAstVisitor extends JavaAstVisitorAdapter
|
||||
public class AstVisitor extends AstVisitorAdapter
|
||||
{
|
||||
protected int m_callbackId = -1;
|
||||
protected AstVisitorClient m_client = null;
|
||||
private String m_filePath;
|
||||
private FileContent m_fileContent;
|
||||
private TypeSolver m_typeSolver;
|
||||
private List<DeclContext> m_context = new ArrayList<DeclContext>();
|
||||
|
||||
public JavaAstVisitor(int callbackId, String filePath, FileContent fileContent, TypeSolver typeSolver)
|
||||
public AstVisitor(AstVisitorClient client, String filePath, FileContent fileContent, TypeSolver typeSolver)
|
||||
{
|
||||
m_callbackId = callbackId;
|
||||
m_client = client;
|
||||
m_filePath = filePath;
|
||||
m_fileContent = fileContent;
|
||||
m_typeSolver = typeSolver;
|
||||
@@ -76,9 +76,7 @@ public class JavaAstVisitor extends JavaAstVisitorAdapter
|
||||
{
|
||||
Name name = n.getName();
|
||||
|
||||
|
||||
JavaIndexer.recordSymbolWithLocationAndScope(
|
||||
m_callbackId,
|
||||
m_client.recordSymbolWithLocationAndScope(
|
||||
JavaparserDeclNameResolver.getQualifiedName(name).toSerializedNameHierarchy(),
|
||||
SymbolKind.PACKAGE,
|
||||
name.getRange(),
|
||||
@@ -90,8 +88,7 @@ public class JavaAstVisitor extends JavaAstVisitorAdapter
|
||||
while (name.getQualifier().isPresent())
|
||||
{
|
||||
name = name.getQualifier().get();
|
||||
JavaIndexer.recordSymbol(
|
||||
m_callbackId,
|
||||
m_client.recordSymbol(
|
||||
JavaparserDeclNameResolver.getQualifiedName(name).toSerializedNameHierarchy(),
|
||||
SymbolKind.PACKAGE,
|
||||
AccessKind.NONE,
|
||||
@@ -108,8 +105,8 @@ public class JavaAstVisitor extends JavaAstVisitorAdapter
|
||||
|
||||
String qualifiedName = JavaparserDeclNameResolver.getQualifiedDeclName(n, m_typeSolver).toSerializedNameHierarchy();
|
||||
|
||||
JavaIndexer.recordSymbolWithLocationAndScope(
|
||||
m_callbackId, qualifiedName, (n.isInterface() ? SymbolKind.INTERFACE : SymbolKind.CLASS),
|
||||
m_client.recordSymbolWithLocationAndScope(
|
||||
qualifiedName, (n.isInterface() ? SymbolKind.INTERFACE : SymbolKind.CLASS),
|
||||
name.getRange(),
|
||||
n.getRange(),
|
||||
AccessKind.fromAccessSpecifier(Modifier.getAccessSpecifier(n.getModifiers())),
|
||||
@@ -156,8 +153,8 @@ public class JavaAstVisitor extends JavaAstVisitorAdapter
|
||||
));
|
||||
}
|
||||
|
||||
JavaIndexer.recordSymbolWithLocation(
|
||||
m_callbackId, qualifiedName, SymbolKind.TYPE_PARAMETER,
|
||||
m_client.recordSymbolWithLocation(
|
||||
qualifiedName, SymbolKind.TYPE_PARAMETER,
|
||||
range,
|
||||
AccessKind.TYPE_PARAMETER,
|
||||
DefinitionKind.EXPLICIT
|
||||
@@ -176,8 +173,8 @@ public class JavaAstVisitor extends JavaAstVisitorAdapter
|
||||
|
||||
String qualifiedName = JavaparserDeclNameResolver.getQualifiedDeclName(n, m_typeSolver).toSerializedNameHierarchy();
|
||||
|
||||
JavaIndexer.recordSymbolWithLocationAndScope(
|
||||
m_callbackId, qualifiedName, SymbolKind.ENUM,
|
||||
m_client.recordSymbolWithLocationAndScope(
|
||||
qualifiedName, SymbolKind.ENUM,
|
||||
name.getRange(),
|
||||
n.getRange(),
|
||||
AccessKind.fromAccessSpecifier(Modifier.getAccessSpecifier(n.getModifiers())),
|
||||
@@ -201,8 +198,8 @@ public class JavaAstVisitor extends JavaAstVisitorAdapter
|
||||
{
|
||||
String qualifiedName = JavaparserDeclNameResolver.getQualifiedDeclName(n, m_typeSolver).toSerializedNameHierarchy();
|
||||
|
||||
JavaIndexer.recordSymbolWithLocation(
|
||||
m_callbackId, qualifiedName, SymbolKind.ENUM_CONSTANT,
|
||||
m_client.recordSymbolWithLocation(
|
||||
qualifiedName, SymbolKind.ENUM_CONSTANT,
|
||||
n.getRange(),
|
||||
AccessKind.NONE,
|
||||
DefinitionKind.EXPLICIT
|
||||
@@ -221,8 +218,8 @@ public class JavaAstVisitor extends JavaAstVisitorAdapter
|
||||
|
||||
String qualifiedName = JavaparserDeclNameResolver.getQualifiedDeclName(n, m_typeSolver).toSerializedNameHierarchy();
|
||||
|
||||
JavaIndexer.recordSymbolWithLocationAndScope(
|
||||
m_callbackId, qualifiedName, SymbolKind.METHOD,
|
||||
m_client.recordSymbolWithLocationAndScope(
|
||||
qualifiedName, SymbolKind.METHOD,
|
||||
name.getRange(),
|
||||
n.getRange(),
|
||||
AccessKind.fromAccessSpecifier(Modifier.getAccessSpecifier(n.getModifiers())),
|
||||
@@ -242,8 +239,8 @@ public class JavaAstVisitor extends JavaAstVisitorAdapter
|
||||
|
||||
String qualifiedName = JavaparserDeclNameResolver.getQualifiedDeclName(n, m_typeSolver).toSerializedNameHierarchy();
|
||||
|
||||
JavaIndexer.recordSymbolWithLocationAndScope(
|
||||
m_callbackId, qualifiedName, SymbolKind.METHOD,
|
||||
m_client.recordSymbolWithLocationAndScope(
|
||||
qualifiedName, SymbolKind.METHOD,
|
||||
name.getRange(),
|
||||
n.getRange(),
|
||||
AccessKind.fromAccessSpecifier(Modifier.getAccessSpecifier(n.getModifiers())),
|
||||
@@ -257,8 +254,8 @@ public class JavaAstVisitor extends JavaAstVisitorAdapter
|
||||
{
|
||||
String overriddenName = JavaparserDeclNameResolver.getQualifiedDeclName(((JavaParserMethodDeclaration)overridden).getWrappedNode(), m_typeSolver).toSerializedNameHierarchy();
|
||||
|
||||
JavaIndexer.recordReference(
|
||||
m_callbackId, ReferenceKind.OVERRIDE, overriddenName, qualifiedName,
|
||||
m_client.recordReference(
|
||||
ReferenceKind.OVERRIDE, overriddenName, qualifiedName,
|
||||
name.getRange()
|
||||
);
|
||||
}
|
||||
@@ -338,8 +335,8 @@ public class JavaAstVisitor extends JavaAstVisitorAdapter
|
||||
String qualifiedName = JavaparserDeclNameResolver.getQualifiedDeclName(declarator, m_typeSolver).toSerializedNameHierarchy();
|
||||
SimpleName name = declarator.getName();
|
||||
|
||||
JavaIndexer.recordSymbolWithLocation(
|
||||
m_callbackId, qualifiedName, SymbolKind.FIELD,
|
||||
m_client.recordSymbolWithLocation(
|
||||
qualifiedName, SymbolKind.FIELD,
|
||||
name.getRange(),
|
||||
AccessKind.fromAccessSpecifier(Modifier.getAccessSpecifier(n.getModifiers())),
|
||||
DefinitionKind.EXPLICIT
|
||||
@@ -362,10 +359,7 @@ public class JavaAstVisitor extends JavaAstVisitorAdapter
|
||||
{
|
||||
String qualifiedName = m_filePath + "<" + name.getBegin().get().line + ":" + name.getBegin().get().column + ">";
|
||||
|
||||
JavaIndexer.recordLocalSymbol(
|
||||
m_callbackId, qualifiedName,
|
||||
name.getRange()
|
||||
);
|
||||
m_client.recordLocalSymbol(qualifiedName, name.getRange());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -381,10 +375,7 @@ public class JavaAstVisitor extends JavaAstVisitorAdapter
|
||||
{
|
||||
String qualifiedName = m_filePath + "<" + name.getBegin().get().line + ":" + name.getBegin().get().column + ">";
|
||||
|
||||
JavaIndexer.recordLocalSymbol(
|
||||
m_callbackId, qualifiedName,
|
||||
name.getRange()
|
||||
);
|
||||
m_client.recordLocalSymbol(qualifiedName, name.getRange());
|
||||
}
|
||||
|
||||
// don't change the context here.
|
||||
@@ -398,13 +389,14 @@ public class JavaAstVisitor extends JavaAstVisitorAdapter
|
||||
public void visit(final ImportDeclaration n, final Void v)
|
||||
{
|
||||
Name name = n.getName();
|
||||
if (n.isAsterisk() || !n.isStatic())
|
||||
|
||||
if (n.isAsterisk())
|
||||
{
|
||||
String importedName = JavaparserDeclNameResolver.getQualifiedName(name).toSerializedNameHierarchy();
|
||||
for (DeclContext context: m_context)
|
||||
{
|
||||
JavaIndexer.recordReference(
|
||||
m_callbackId, ReferenceKind.IMPORT,
|
||||
m_client.recordReference(
|
||||
ReferenceKind.IMPORT,
|
||||
importedName, context.getName(),
|
||||
name.getRange()
|
||||
);
|
||||
@@ -413,43 +405,47 @@ public class JavaAstVisitor extends JavaAstVisitorAdapter
|
||||
SymbolReference<ReferenceTypeDeclaration> symbolReference = m_typeSolver.tryToSolveType(name.asString());
|
||||
if (!symbolReference.isSolved())
|
||||
{
|
||||
JavaIndexer.recordError(
|
||||
m_callbackId, "Import not found.", true, true,
|
||||
n.getRange()
|
||||
);
|
||||
m_client.recordError("Import not found.", true, true, n.getRange());
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
String typeName = name.getQualifier().get().asString();
|
||||
String memberName = name.getIdentifier();
|
||||
|
||||
List<JavaDeclName> importedDeclNames = new ArrayList<>();
|
||||
|
||||
ReferenceTypeDeclaration solvedDecl = m_typeSolver.solveType(typeName);
|
||||
|
||||
for (com.github.javaparser.symbolsolver.model.declarations.MethodDeclaration methodDecl: solvedDecl.getDeclaredMethods()) // look for method
|
||||
SymbolReference<ReferenceTypeDeclaration> solvedTypeDeclatarion = m_typeSolver.tryToSolveType(name.asString());
|
||||
if (solvedTypeDeclatarion.isSolved())
|
||||
{
|
||||
if (methodDecl.getName().equals(memberName))
|
||||
importedDeclNames.add(JavaSymbolSolverDeclNameResolver.getQualifiedDeclName(solvedTypeDeclatarion.getCorrespondingDeclaration(), m_typeSolver));
|
||||
}
|
||||
else
|
||||
{
|
||||
String typeName = name.getQualifier().get().asString();
|
||||
String memberName = name.getIdentifier();
|
||||
|
||||
ReferenceTypeDeclaration solvedDecl = m_typeSolver.solveType(typeName);
|
||||
|
||||
for (com.github.javaparser.symbolsolver.model.declarations.MethodDeclaration methodDecl: solvedDecl.getDeclaredMethods()) // look for method
|
||||
{
|
||||
importedDeclNames.add(
|
||||
JavaSymbolSolverDeclNameResolver.getQualifiedDeclName(methodDecl, m_typeSolver
|
||||
));
|
||||
if (methodDecl.getName().equals(memberName))
|
||||
{
|
||||
importedDeclNames.add(
|
||||
JavaSymbolSolverDeclNameResolver.getQualifiedDeclName(methodDecl, m_typeSolver
|
||||
));
|
||||
}
|
||||
}
|
||||
if (importedDeclNames.isEmpty() && solvedDecl.hasField(memberName)) // look for field
|
||||
{
|
||||
JavaDeclName importedTypeDeclName = JavaSymbolSolverDeclNameResolver.getQualifiedDeclName(solvedDecl, m_typeSolver);
|
||||
if (importedTypeDeclName != null)
|
||||
{
|
||||
JavaDeclName importedDeclName = new JavaDeclName(memberName);
|
||||
importedDeclName.setParent(importedTypeDeclName);
|
||||
importedDeclNames.add(importedDeclName);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (importedDeclNames.isEmpty() && solvedDecl.hasField(memberName)) // look for field
|
||||
{
|
||||
JavaDeclName importedTypeDeclName = JavaSymbolSolverDeclNameResolver.getQualifiedDeclName(solvedDecl, m_typeSolver);
|
||||
if (importedTypeDeclName != null)
|
||||
{
|
||||
JavaDeclName importedDeclName = new JavaDeclName(memberName);
|
||||
importedDeclName.setParent(importedTypeDeclName);
|
||||
importedDeclNames.add(importedDeclName);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (!importedDeclNames.isEmpty())
|
||||
{
|
||||
@@ -458,8 +454,8 @@ public class JavaAstVisitor extends JavaAstVisitorAdapter
|
||||
String nameHierarchy = importedDeclName.toSerializedNameHierarchy();
|
||||
for (DeclContext context: m_context)
|
||||
{
|
||||
JavaIndexer.recordReference(
|
||||
m_callbackId, ReferenceKind.IMPORT,
|
||||
m_client.recordReference(
|
||||
ReferenceKind.IMPORT,
|
||||
nameHierarchy, context.getName(),
|
||||
n.getRange()
|
||||
);
|
||||
@@ -468,10 +464,7 @@ public class JavaAstVisitor extends JavaAstVisitorAdapter
|
||||
}
|
||||
else
|
||||
{
|
||||
JavaIndexer.recordError(
|
||||
m_callbackId, "Import not found.", true, true,
|
||||
n.getRange()
|
||||
);
|
||||
m_client.recordError("Import not found.", true, true, n.getRange());
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
@@ -516,9 +509,8 @@ public class JavaAstVisitor extends JavaAstVisitorAdapter
|
||||
}
|
||||
}
|
||||
|
||||
JavaIndexer.recordReference(
|
||||
m_callbackId, getTypeReferenceKind(), referencedName, context.getName(),
|
||||
range
|
||||
m_client.recordReference(
|
||||
getTypeReferenceKind(), referencedName, context.getName(), range
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -535,16 +527,16 @@ public class JavaAstVisitor extends JavaAstVisitorAdapter
|
||||
{
|
||||
String referencedName = JavaparserTypeNameResolver.getQualifiedTypeName(n, m_typeSolver).toSerializedNameHierarchy();
|
||||
|
||||
JavaIndexer.recordSymbol(
|
||||
m_callbackId, referencedName, SymbolKind.BUILTIN_TYPE,
|
||||
m_client.recordSymbol(
|
||||
referencedName, SymbolKind.BUILTIN_TYPE,
|
||||
AccessKind.NONE,
|
||||
DefinitionKind.EXPLICIT
|
||||
);
|
||||
|
||||
for (DeclContext context: m_context)
|
||||
{
|
||||
JavaIndexer.recordReference(
|
||||
m_callbackId, getTypeReferenceKind(), referencedName, context.getName(),
|
||||
m_client.recordReference(
|
||||
getTypeReferenceKind(), referencedName, context.getName(),
|
||||
n.getRange()
|
||||
);
|
||||
}
|
||||
@@ -563,17 +555,14 @@ public class JavaAstVisitor extends JavaAstVisitorAdapter
|
||||
{
|
||||
String referencedName = JavaparserTypeNameResolver.getQualifiedTypeName(n, m_typeSolver).toSerializedNameHierarchy();
|
||||
|
||||
JavaIndexer.recordSymbol(
|
||||
m_callbackId, referencedName, SymbolKind.BUILTIN_TYPE,
|
||||
AccessKind.NONE,
|
||||
DefinitionKind.EXPLICIT
|
||||
m_client.recordSymbol(
|
||||
referencedName, SymbolKind.BUILTIN_TYPE, AccessKind.NONE, DefinitionKind.EXPLICIT
|
||||
);
|
||||
|
||||
for (DeclContext context: m_context)
|
||||
{
|
||||
JavaIndexer.recordReference(
|
||||
m_callbackId, getTypeReferenceKind(), referencedName, context.getName(),
|
||||
n.getRange()
|
||||
m_client.recordReference(
|
||||
getTypeReferenceKind(), referencedName, context.getName(), n.getRange()
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -611,8 +600,8 @@ public class JavaAstVisitor extends JavaAstVisitorAdapter
|
||||
String qualifiedName = JavaparserDeclNameResolver.getQualifiedDeclName(var, m_typeSolver).toSerializedNameHierarchy();
|
||||
for (DeclContext context: m_context)
|
||||
{
|
||||
JavaIndexer.recordReference(
|
||||
m_callbackId, ReferenceKind.USAGE, qualifiedName, context.getName(),
|
||||
m_client.recordReference(
|
||||
ReferenceKind.USAGE, qualifiedName, context.getName(),
|
||||
fieldName.getRange()
|
||||
);
|
||||
}
|
||||
@@ -676,8 +665,8 @@ public class JavaAstVisitor extends JavaAstVisitorAdapter
|
||||
String qualifiedName = JavaparserDeclNameResolver.getQualifiedDeclName(var, m_typeSolver).toSerializedNameHierarchy();
|
||||
for (DeclContext context: m_context)
|
||||
{
|
||||
JavaIndexer.recordReference(
|
||||
m_callbackId, ReferenceKind.USAGE, qualifiedName, context.getName(),
|
||||
m_client.recordReference(
|
||||
ReferenceKind.USAGE, qualifiedName, context.getName(),
|
||||
e.getName().getRange()
|
||||
);
|
||||
}
|
||||
@@ -691,8 +680,8 @@ public class JavaAstVisitor extends JavaAstVisitorAdapter
|
||||
{
|
||||
String qualifiedName = m_filePath + "<" + name.getBegin().get().line + ":" + name.getBegin().get().column + ">";
|
||||
|
||||
JavaIndexer.recordLocalSymbol(
|
||||
m_callbackId, qualifiedName,
|
||||
m_client.recordLocalSymbol(
|
||||
qualifiedName,
|
||||
e.getRange()
|
||||
);
|
||||
}
|
||||
@@ -705,8 +694,8 @@ public class JavaAstVisitor extends JavaAstVisitorAdapter
|
||||
|
||||
for (DeclContext context: m_context)
|
||||
{
|
||||
JavaIndexer.recordReference(
|
||||
m_callbackId, ReferenceKind.USAGE, qualifiedName, context.getName(),
|
||||
m_client.recordReference(
|
||||
ReferenceKind.USAGE, qualifiedName, context.getName(),
|
||||
e.getRange()
|
||||
);
|
||||
}
|
||||
@@ -718,8 +707,8 @@ public class JavaAstVisitor extends JavaAstVisitorAdapter
|
||||
{
|
||||
String qualifiedName = m_filePath + "<" + name.getBegin().get().line + ":" + name.getBegin().get().column + ">";
|
||||
|
||||
JavaIndexer.recordLocalSymbol(
|
||||
m_callbackId, qualifiedName,
|
||||
m_client.recordLocalSymbol(
|
||||
qualifiedName,
|
||||
e.getRange()
|
||||
);
|
||||
}
|
||||
@@ -769,8 +758,8 @@ public class JavaAstVisitor extends JavaAstVisitorAdapter
|
||||
SimpleName name = n.getName();
|
||||
for (DeclContext context: m_context)
|
||||
{
|
||||
JavaIndexer.recordReference(
|
||||
m_callbackId, ReferenceKind.CALL, qualifiedName, context.getName(),
|
||||
m_client.recordReference(
|
||||
ReferenceKind.CALL, qualifiedName, context.getName(),
|
||||
name.getRange()
|
||||
);
|
||||
}
|
||||
@@ -801,9 +790,9 @@ public class JavaAstVisitor extends JavaAstVisitorAdapter
|
||||
beginColumn = n.getBegin().get().column + "";
|
||||
}
|
||||
|
||||
JavaIndexer.logError(m_callbackId, e + " at " + m_filePath + "<"+ beginLine + ", " + beginColumn + ">");
|
||||
JavaIndexer.recordSymbolWithLocation(
|
||||
m_callbackId, ".\tmunsolved-symbol\ts\tp", SymbolKind.TYPE_MAX,
|
||||
m_client.logError(e + " at " + m_filePath + "<"+ beginLine + ", " + beginColumn + ">");
|
||||
m_client.recordSymbolWithLocation(
|
||||
".\tmunsolved-symbol\ts\tp", SymbolKind.TYPE_MAX,
|
||||
n.getRange(),
|
||||
AccessKind.DEFAULT,
|
||||
DefinitionKind.EXPLICIT
|
||||
@@ -850,8 +839,8 @@ public class JavaAstVisitor extends JavaAstVisitorAdapter
|
||||
ClassOrInterfaceType type = n.getType();
|
||||
for (DeclContext context: m_context)
|
||||
{
|
||||
JavaIndexer.recordReference(
|
||||
m_callbackId, ReferenceKind.CALL, qualifiedName, context.getName(),
|
||||
m_client.recordReference(
|
||||
ReferenceKind.CALL, qualifiedName, context.getName(),
|
||||
type.getRange()
|
||||
);
|
||||
}
|
||||
@@ -957,25 +946,19 @@ public class JavaAstVisitor extends JavaAstVisitorAdapter
|
||||
private void recordScope(Range range)
|
||||
{
|
||||
String qualifiedName = m_filePath + "<" + range.begin.line + ":" + range.begin.column + ">";
|
||||
JavaIndexer.recordLocalSymbol(m_callbackId, qualifiedName, range.begin.line, range.begin.column, range.begin.line, range.begin.column);
|
||||
JavaIndexer.recordLocalSymbol(m_callbackId, qualifiedName, range.end.line, range.end.column, range.end.line, range.end.column);
|
||||
m_client.recordLocalSymbol(qualifiedName, Range.range(range.begin, range.begin));
|
||||
m_client.recordLocalSymbol(qualifiedName, Range.range(range.end, range.end));
|
||||
}
|
||||
|
||||
@Override public void visit(final LineComment n, final Void v)
|
||||
{
|
||||
JavaIndexer.recordComment(
|
||||
m_callbackId,
|
||||
n.getRange()
|
||||
);
|
||||
m_client.recordComment(n.getRange());
|
||||
super.visit(n, v);
|
||||
}
|
||||
|
||||
@Override public void visit(final BlockComment n, final Void v)
|
||||
{
|
||||
JavaIndexer.recordComment(
|
||||
m_callbackId,
|
||||
n.getRange()
|
||||
);
|
||||
m_client.recordComment(n.getRange());
|
||||
super.visit(n, v);
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -13,7 +13,7 @@ import com.github.javaparser.ast.type.*;
|
||||
import java.util.Optional;
|
||||
import java.util.Stack;
|
||||
|
||||
public abstract class JavaAstVisitorAdapter extends VoidVisitorAdapter<Void>
|
||||
public abstract class AstVisitorAdapter extends VoidVisitorAdapter<Void>
|
||||
{
|
||||
private Stack<ReferenceKind> m_typeRefKind = new Stack<ReferenceKind>();
|
||||
|
||||
@@ -0,0 +1,117 @@
|
||||
package com.sourcetrail;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import com.github.javaparser.Range;
|
||||
|
||||
public abstract class AstVisitorClient
|
||||
{
|
||||
public void recordSymbolWithLocation(
|
||||
String symbolName, SymbolKind symbolType,
|
||||
Optional<Range> range,
|
||||
AccessKind access, DefinitionKind definitionKind
|
||||
)
|
||||
{
|
||||
recordSymbolWithLocation(
|
||||
symbolName, symbolType,
|
||||
range.orElse(Range.range(0, 0, 0, 0)),
|
||||
access, definitionKind
|
||||
);
|
||||
}
|
||||
|
||||
public void recordSymbolWithLocationAndScope(
|
||||
String symbolName, SymbolKind symbolType,
|
||||
Optional<Range> range,
|
||||
Optional<Range> scopeRange,
|
||||
AccessKind access, DefinitionKind definitionKind
|
||||
)
|
||||
{
|
||||
recordSymbolWithLocationAndScope(
|
||||
symbolName, symbolType,
|
||||
range.orElse(Range.range(0, 0, 0, 0)),
|
||||
scopeRange.orElse(Range.range(0, 0, 0, 0)),
|
||||
access, definitionKind
|
||||
);
|
||||
}
|
||||
|
||||
public void recordReference(
|
||||
ReferenceKind referenceKind, String referencedName, String contextName,
|
||||
Optional<Range> range
|
||||
)
|
||||
{
|
||||
recordReference(
|
||||
referenceKind, referencedName, contextName,
|
||||
range.orElse(Range.range(0, 0, 0, 0))
|
||||
);
|
||||
}
|
||||
|
||||
public void recordLocalSymbol(
|
||||
String symbolName,
|
||||
Optional<Range> range
|
||||
)
|
||||
{
|
||||
recordLocalSymbol(symbolName, range.orElse(Range.range(0, 0, 0, 0)));
|
||||
}
|
||||
|
||||
public void recordComment(
|
||||
Optional<Range> range
|
||||
)
|
||||
{
|
||||
recordComment(range.orElse(Range.range(0, 0, 0, 0)));
|
||||
}
|
||||
|
||||
public void recordError(
|
||||
String message, boolean fatal, boolean indexed,
|
||||
Optional<Range> range
|
||||
)
|
||||
{
|
||||
recordError(
|
||||
message, fatal, indexed,
|
||||
range.orElse(Range.range(0, 0, 0, 0))
|
||||
);
|
||||
}
|
||||
|
||||
public abstract void logInfo(String info);
|
||||
|
||||
public abstract void logWarning(String warning);
|
||||
|
||||
public abstract void logError(String error);
|
||||
|
||||
public abstract void recordSymbol(
|
||||
String symbolName, SymbolKind symbolType,
|
||||
AccessKind access, DefinitionKind definitionKind
|
||||
);
|
||||
|
||||
public abstract void recordSymbolWithLocation(
|
||||
String symbolName, SymbolKind symbolType,
|
||||
Range range,
|
||||
AccessKind access, DefinitionKind definitionKind
|
||||
);
|
||||
|
||||
public abstract void recordSymbolWithLocationAndScope(
|
||||
String symbolName, SymbolKind symbolType,
|
||||
Range range,
|
||||
Range scopeRange,
|
||||
AccessKind access, DefinitionKind definitionKind
|
||||
);
|
||||
|
||||
public abstract void recordReference(
|
||||
ReferenceKind referenceKind, String referencedName, String contextName,
|
||||
Range range
|
||||
);
|
||||
|
||||
public abstract void recordLocalSymbol(
|
||||
String symbolName,
|
||||
Range range
|
||||
);
|
||||
|
||||
public abstract void recordComment(
|
||||
Range range
|
||||
);
|
||||
|
||||
public abstract void recordError(
|
||||
String message, boolean fatal, boolean indexed,
|
||||
Range range
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,61 +0,0 @@
|
||||
package com.sourcetrail;
|
||||
|
||||
import javax.management.MBeanServer;
|
||||
import java.lang.management.ManagementFactory;
|
||||
import com.sun.management.HotSpotDiagnosticMXBean;
|
||||
|
||||
public class HeapDumper {
|
||||
// This is the name of the HotSpot Diagnostic MBean
|
||||
private static final String HOTSPOT_BEAN_NAME =
|
||||
"com.sun.management:type=HotSpotDiagnostic";
|
||||
|
||||
// field to store the hotspot diagnostic MBean
|
||||
private static volatile HotSpotDiagnosticMXBean hotspotMBean;
|
||||
|
||||
/*\*
|
||||
\* Call this method from your application whenever you
|
||||
\* want to dump the heap snapshot into a file.
|
||||
\*
|
||||
\* @param fileName name of the heap dump file
|
||||
\* @param live flag that tells whether to dump
|
||||
\* only the live objects
|
||||
\*/
|
||||
static void dumpHeap(String fileName, boolean live) {
|
||||
// initialize hotspot diagnostic MBean
|
||||
initHotspotMBean();
|
||||
try {
|
||||
hotspotMBean.dumpHeap(fileName, live);
|
||||
} catch (RuntimeException re) {
|
||||
throw re;
|
||||
} catch (Exception exp) {
|
||||
throw new RuntimeException(exp);
|
||||
}
|
||||
}
|
||||
|
||||
// initialize the hotspot diagnostic MBean field
|
||||
private static void initHotspotMBean() {
|
||||
if (hotspotMBean == null) {
|
||||
synchronized (HeapDumper.class) {
|
||||
if (hotspotMBean == null) {
|
||||
hotspotMBean = getHotspotMBean();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// get the hotspot diagnostic MBean from the
|
||||
// platform MBean server
|
||||
private static HotSpotDiagnosticMXBean getHotspotMBean() {
|
||||
try {
|
||||
MBeanServer server = ManagementFactory.getPlatformMBeanServer();
|
||||
HotSpotDiagnosticMXBean bean =
|
||||
ManagementFactory.newPlatformMXBeanProxy(server,
|
||||
HOTSPOT_BEAN_NAME, HotSpotDiagnosticMXBean.class);
|
||||
return bean;
|
||||
} catch (RuntimeException re) {
|
||||
throw re;
|
||||
} catch (Exception exp) {
|
||||
throw new RuntimeException(exp);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -24,12 +24,13 @@ import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeS
|
||||
|
||||
public class JavaIndexer
|
||||
{
|
||||
|
||||
private static Map<String, TypeSolver> typeSolvers = new HashMap<>();
|
||||
|
||||
public static void processFile(int address, String filePath, String fileContent, String classPath, int verbose)
|
||||
{
|
||||
logInfo(address, "indexing source file: " + filePath);
|
||||
AstVisitorClient astVisitorClient = new JavaIndexerAstVisitorClient(address);
|
||||
|
||||
astVisitorClient.logInfo( "indexing source file: " + filePath);
|
||||
|
||||
try
|
||||
{
|
||||
@@ -72,10 +73,10 @@ public class JavaIndexer
|
||||
|
||||
CompilationUnit cu = JavaParser.parse(new StringReader(fileContent));
|
||||
|
||||
JavaAstVisitor astVisitor = (
|
||||
AstVisitor astVisitor = (
|
||||
verbose == 1 ?
|
||||
new JavaVerboseAstVisitor(address, filePath, new FileContent(fileContent), combinedTypeSolver) :
|
||||
new JavaAstVisitor(address, filePath, new FileContent(fileContent), combinedTypeSolver)
|
||||
new VerboseAstVisitor(astVisitorClient, filePath, new FileContent(fileContent), combinedTypeSolver) :
|
||||
new AstVisitor(astVisitorClient, filePath, new FileContent(fileContent), combinedTypeSolver)
|
||||
);
|
||||
|
||||
cu.accept(astVisitor, null);
|
||||
@@ -97,8 +98,8 @@ public class JavaIndexer
|
||||
message.indexOf(")")
|
||||
));
|
||||
|
||||
recordError(
|
||||
address, "Encountered unexpected token.", true, true,
|
||||
astVisitorClient.recordError(
|
||||
"Encountered unexpected token.", true, true,
|
||||
Range.range(startLine, startColumn, startLine, startColumn)
|
||||
);
|
||||
}
|
||||
@@ -107,8 +108,8 @@ public class JavaIndexer
|
||||
Optional<Range> range = problem.getRange();
|
||||
if (range.isPresent())
|
||||
{
|
||||
recordError(
|
||||
address, problem.toString(), true, true,
|
||||
astVisitorClient.recordError(
|
||||
problem.toString(), true, true,
|
||||
range.get()
|
||||
);
|
||||
}
|
||||
@@ -148,156 +149,7 @@ public class JavaIndexer
|
||||
Runtime.getRuntime().gc();
|
||||
}
|
||||
|
||||
// helpers
|
||||
|
||||
static public void recordSymbol(
|
||||
int address, String symbolName, SymbolKind symbolType,
|
||||
AccessKind access, DefinitionKind definitionKind
|
||||
)
|
||||
{
|
||||
recordSymbol(
|
||||
address, symbolName, symbolType.getValue(),
|
||||
access.getValue(), definitionKind.getValue()
|
||||
);
|
||||
}
|
||||
|
||||
static public void recordSymbolWithLocation(
|
||||
int address, String symbolName, SymbolKind symbolType,
|
||||
Optional<Range> range,
|
||||
AccessKind access, DefinitionKind definitionKind
|
||||
)
|
||||
{
|
||||
recordSymbolWithLocation(
|
||||
address, symbolName, symbolType,
|
||||
range.orElse(Range.range(0, 0, 0, 0)),
|
||||
access, definitionKind
|
||||
);
|
||||
}
|
||||
|
||||
static public void recordSymbolWithLocation(
|
||||
int address, String symbolName, SymbolKind symbolType,
|
||||
Range range,
|
||||
AccessKind access, DefinitionKind definitionKind
|
||||
)
|
||||
{
|
||||
recordSymbolWithLocation(
|
||||
address, symbolName, symbolType.getValue(),
|
||||
range.begin.line, range.begin.column, range.end.line, range.end.column,
|
||||
access.getValue(), definitionKind.getValue()
|
||||
);
|
||||
}
|
||||
|
||||
static public void recordSymbolWithLocationAndScope(
|
||||
int address, String symbolName, SymbolKind symbolType,
|
||||
Optional<Range> range,
|
||||
Optional<Range> scopeRange,
|
||||
AccessKind access, DefinitionKind definitionKind
|
||||
)
|
||||
{
|
||||
recordSymbolWithLocationAndScope(
|
||||
address, symbolName, symbolType,
|
||||
range.orElse(Range.range(0, 0, 0, 0)),
|
||||
scopeRange.orElse(Range.range(0, 0, 0, 0)),
|
||||
access, definitionKind
|
||||
);
|
||||
}
|
||||
|
||||
static public void recordSymbolWithLocationAndScope(
|
||||
int address, String symbolName, SymbolKind symbolType,
|
||||
Range range,
|
||||
Range scopeRange,
|
||||
AccessKind access, DefinitionKind definitionKind
|
||||
)
|
||||
{
|
||||
recordSymbolWithLocationAndScope(
|
||||
address, symbolName, symbolType.getValue(),
|
||||
range.begin.line, range.begin.column, range.end.line, range.end.column,
|
||||
scopeRange.begin.line, scopeRange.begin.column, scopeRange.end.line, scopeRange.end.column,
|
||||
access.getValue(), definitionKind.getValue()
|
||||
);
|
||||
}
|
||||
|
||||
static public void recordReference(
|
||||
int address, ReferenceKind referenceKind, String referencedName, String contextName,
|
||||
Optional<Range> range
|
||||
)
|
||||
{
|
||||
recordReference(
|
||||
address, referenceKind, referencedName, contextName,
|
||||
range.orElse(Range.range(0, 0, 0, 0))
|
||||
);
|
||||
}
|
||||
|
||||
static public void recordReference(
|
||||
int address, ReferenceKind referenceKind, String referencedName, String contextName,
|
||||
Range range
|
||||
)
|
||||
{
|
||||
recordReference(
|
||||
address, referenceKind.getValue(), referencedName, contextName,
|
||||
range.begin.line, range.begin.column, range.end.line, range.end.column
|
||||
);
|
||||
}
|
||||
|
||||
static public void recordLocalSymbol(
|
||||
int address, String symbolName,
|
||||
Optional<Range> range
|
||||
)
|
||||
{
|
||||
recordLocalSymbol(address, symbolName, range.orElse(Range.range(0, 0, 0, 0)));
|
||||
}
|
||||
|
||||
static public void recordLocalSymbol(
|
||||
int address, String symbolName,
|
||||
Range range
|
||||
)
|
||||
{
|
||||
recordLocalSymbol(
|
||||
address, symbolName,
|
||||
range.begin.line, range.begin.column, range.end.line, range.end.column
|
||||
);
|
||||
}
|
||||
|
||||
static public void recordComment(
|
||||
int address,
|
||||
Optional<Range> range
|
||||
)
|
||||
{
|
||||
recordComment(address, range.orElse(Range.range(0, 0, 0, 0)));
|
||||
}
|
||||
|
||||
static public void recordComment(
|
||||
int address,
|
||||
Range range
|
||||
)
|
||||
{
|
||||
recordComment(
|
||||
address,
|
||||
range.begin.line, range.begin.column, range.end.line, range.end.column
|
||||
);
|
||||
}
|
||||
|
||||
static public void recordError(
|
||||
int address, String message, boolean fatal, boolean indexed,
|
||||
Optional<Range> range
|
||||
)
|
||||
{
|
||||
recordError(
|
||||
address, message, fatal, indexed,
|
||||
range.orElse(Range.range(0, 0, 0, 0))
|
||||
);
|
||||
}
|
||||
|
||||
static public void recordError(
|
||||
int address, String message, boolean fatal, boolean indexed,
|
||||
Range range
|
||||
)
|
||||
{
|
||||
recordError(
|
||||
address, message, (fatal ? 1 : 0), (indexed ? 1 : 0),
|
||||
range.begin.line, range.begin.column, range.end.line, range.end.column
|
||||
);
|
||||
}
|
||||
|
||||
// the following methods are defined in the native c++ code
|
||||
|
||||
@@ -307,25 +159,25 @@ public class JavaIndexer
|
||||
|
||||
static public native void logError(int address, String error);
|
||||
|
||||
static private native void recordSymbol(
|
||||
static public native void recordSymbol(
|
||||
int address, String symbolName, int symbolType,
|
||||
int access, int definitionKind
|
||||
);
|
||||
|
||||
static private native void recordSymbolWithLocation(
|
||||
static public native void recordSymbolWithLocation(
|
||||
int address, String symbolName, int symbolType,
|
||||
int beginLine, int beginColumn, int endLine, int endColumn,
|
||||
int access, int definitionKind
|
||||
);
|
||||
|
||||
static private native void recordSymbolWithLocationAndScope(
|
||||
static public native void recordSymbolWithLocationAndScope(
|
||||
int address, String symbolName, int symbolType,
|
||||
int beginLine, int beginColumn, int endLine, int endColumn,
|
||||
int scopeBeginLine, int scopeBeginColumn, int scopeEndLine, int scopeEndColumn,
|
||||
int access, int definitionKind
|
||||
);
|
||||
|
||||
static private native void recordReference(
|
||||
static public native void recordReference(
|
||||
int address, int referenceKind, String referencedName, String contextName, int beginLine, int beginColumn, int endLine, int endColumn
|
||||
);
|
||||
|
||||
@@ -337,7 +189,7 @@ public class JavaIndexer
|
||||
int address, int beginLine, int beginColumn, int endLine, int endColumn
|
||||
);
|
||||
|
||||
static private native void recordError(
|
||||
static public native void recordError(
|
||||
int address, String message, int fatal, int indexed, int beginLine, int beginColumn, int endLine, int endColumn
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
package com.sourcetrail;
|
||||
|
||||
import com.github.javaparser.Range;
|
||||
|
||||
public class JavaIndexerAstVisitorClient extends AstVisitorClient
|
||||
{
|
||||
private int m_address;
|
||||
|
||||
public JavaIndexerAstVisitorClient(int address)
|
||||
{
|
||||
m_address = address;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void logInfo(String info)
|
||||
{
|
||||
JavaIndexer.logInfo(m_address, info);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void logWarning(String warning)
|
||||
{
|
||||
JavaIndexer.logWarning(m_address, warning);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void logError(String error)
|
||||
{
|
||||
JavaIndexer.logError(m_address, error);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void recordSymbol(
|
||||
String symbolName, SymbolKind symbolType,
|
||||
AccessKind access, DefinitionKind definitionKind)
|
||||
{
|
||||
JavaIndexer.recordSymbol(
|
||||
m_address, symbolName, symbolType.getValue(),
|
||||
access.getValue(), definitionKind.getValue()
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void recordSymbolWithLocation(
|
||||
String symbolName, SymbolKind symbolType, Range range,
|
||||
AccessKind access, DefinitionKind definitionKind)
|
||||
{
|
||||
JavaIndexer.recordSymbolWithLocation(
|
||||
m_address, symbolName, symbolType.getValue(),
|
||||
range.begin.line, range.begin.column, range.end.line, range.end.column,
|
||||
access.getValue(), definitionKind.getValue()
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void recordSymbolWithLocationAndScope(
|
||||
String symbolName, SymbolKind symbolType, Range range,
|
||||
Range scopeRange, AccessKind access, DefinitionKind definitionKind)
|
||||
{
|
||||
JavaIndexer.recordSymbolWithLocationAndScope(
|
||||
m_address, symbolName, symbolType.getValue(),
|
||||
range.begin.line, range.begin.column, range.end.line, range.end.column,
|
||||
scopeRange.begin.line, scopeRange.begin.column, scopeRange.end.line, scopeRange.end.column,
|
||||
access.getValue(), definitionKind.getValue()
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void recordReference(
|
||||
ReferenceKind referenceKind, String referencedName,
|
||||
String contextName, Range range)
|
||||
{
|
||||
JavaIndexer.recordReference(
|
||||
m_address, referenceKind.getValue(), referencedName, contextName,
|
||||
range.begin.line, range.begin.column, range.end.line, range.end.column
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void recordLocalSymbol(String symbolName, Range range)
|
||||
{
|
||||
JavaIndexer.recordLocalSymbol(
|
||||
m_address, symbolName,
|
||||
range.begin.line, range.begin.column, range.end.line, range.end.column
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void recordComment(Range range)
|
||||
{
|
||||
JavaIndexer.recordComment(
|
||||
m_address,
|
||||
range.begin.line, range.begin.column, range.end.line, range.end.column
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void recordError(String message, boolean fatal, boolean indexed, Range range)
|
||||
{
|
||||
JavaIndexer.recordError(
|
||||
m_address, message, (fatal ? 1 : 0), (indexed ? 1 : 0),
|
||||
range.begin.line, range.begin.column, range.end.line, range.end.column
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
+4
-4
@@ -9,10 +9,10 @@ import com.github.javaparser.ast.stmt.*;
|
||||
import com.github.javaparser.ast.type.*;
|
||||
import com.github.javaparser.symbolsolver.model.resolution.TypeSolver;
|
||||
|
||||
public class JavaVerboseAstVisitor extends JavaAstVisitor{
|
||||
public class VerboseAstVisitor extends AstVisitor{
|
||||
|
||||
public JavaVerboseAstVisitor(int callbackId, String filePath, FileContent fileContent, TypeSolver typeSolver) {
|
||||
super(callbackId, filePath, fileContent, typeSolver);
|
||||
public VerboseAstVisitor(AstVisitorClient client, String filePath, FileContent fileContent, TypeSolver typeSolver) {
|
||||
super(client, filePath, fileContent, typeSolver);
|
||||
}
|
||||
|
||||
int indent = 0;
|
||||
@@ -51,7 +51,7 @@ public class JavaVerboseAstVisitor extends JavaAstVisitor{
|
||||
line += " line: " + n.getBegin().get().line;
|
||||
}
|
||||
|
||||
JavaIndexer.logInfo(m_callbackId, line);
|
||||
m_client.logInfo(line);
|
||||
}
|
||||
|
||||
//- Compilation Unit ----------------------------------
|
||||
Reference in New Issue
Block a user