build: switch to Maven configuration for JavaIndexer

This commit is contained in:
malte_langkabel
2017-07-27 13:16:57 +02:00
parent 053ca3d8d1
commit 2e117c4b6d
51 changed files with 133 additions and 93 deletions
@@ -0,0 +1,195 @@
package com.sourcetrail;
import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.lang.String;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.PackageDeclaration;
import com.github.javaparser.JavaParser;
import com.github.javaparser.ParseProblemException;
import com.github.javaparser.Problem;
import com.github.javaparser.Range;
import com.github.javaparser.symbolsolver.javaparser.Navigator;
import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade;
import com.github.javaparser.symbolsolver.model.resolution.TypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.JarTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
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)
{
AstVisitorClient astVisitorClient = new JavaIndexerAstVisitorClient(address);
astVisitorClient.logInfo( "indexing source file: " + filePath);
try
{
CombinedTypeSolver combinedTypeSolver = new CombinedTypeSolver();
combinedTypeSolver.add(new ReflectionTypeSolver());
for (String path: classPath.split("\\;"))
{
if (typeSolvers.containsKey(path))
{
combinedTypeSolver.add(typeSolvers.get(path));
}
else
{
TypeSolver typeSolver = null;
if (path.endsWith(".jar"))
{
try
{
typeSolver = new JarTypeSolver(path);
}
catch (IOException e)
{
System.out.println("unable to add jar file: " + path);
}
}
else if (!path.isEmpty())
{
typeSolver = new JavaParserTypeSolver(new File(path));
}
if (typeSolver != null)
{
typeSolvers.put(path, typeSolver);
combinedTypeSolver.add(typeSolver);
}
}
}
CompilationUnit cu = JavaParser.parse(new StringReader(fileContent));
AstVisitor astVisitor = (
verbose == 1 ?
new VerboseAstVisitor(astVisitorClient, filePath, new FileContent(fileContent), combinedTypeSolver) :
new AstVisitor(astVisitorClient, filePath, new FileContent(fileContent), combinedTypeSolver)
);
cu.accept(astVisitor, null);
}
catch (ParseProblemException e)
{
for (Problem problem: e.getProblems())
{
String message = problem.toString();
if (message.startsWith("(line "))
{
int startLine = Integer.parseInt(message.substring(
message.indexOf("line ") + ("line ").length(),
message.indexOf(",")
));
int startColumn = Integer.parseInt(message.substring(
message.indexOf("col ") + ("col ").length(),
message.indexOf(")")
));
astVisitorClient.recordError(
"Encountered unexpected token.", true, true,
Range.range(startLine, startColumn, startLine, startColumn)
);
}
else
{
Optional<Range> range = problem.getRange();
if (range.isPresent())
{
astVisitorClient.recordError(
problem.toString(), true, true,
range.get()
);
}
}
}
}
}
public static String getPackageName(String fileContent)
{
String packageName = "";
try
{
CompilationUnit cu = JavaParser.parse(new StringReader(fileContent));
PackageDeclaration pd = Navigator.findNodeOfGivenClass(cu, PackageDeclaration.class);
if (pd != null)
{
packageName = JavaparserDeclNameResolver.getQualifiedName(pd.getName()).toString();
}
}
catch (ParseProblemException e)
{
// do nothing
}
catch (IllegalArgumentException e)
{
// do nothing
}
return packageName;
}
public static void clearCaches()
{
typeSolvers.clear();
JavaParserFacade.clearInstances();
Runtime.getRuntime().gc();
}
// the following methods are defined in the native c++ code
static public native void logInfo(int address, String info);
static public native void logWarning(int address, String warning);
static public native void logError(int address, String error);
static public native void recordSymbol(
int address, String symbolName, int symbolType,
int access, int definitionKind
);
static public native void recordSymbolWithLocation(
int address, String symbolName, int symbolType,
int beginLine, int beginColumn, int endLine, int endColumn,
int access, int definitionKind
);
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 public native void recordReference(
int address, int referenceKind, String referencedName, String contextName, int beginLine, int beginColumn, int endLine, int endColumn
);
static public native void recordLocalSymbol(
int address, String symbolName, int beginLine, int beginColumn, int endLine, int endColumn
);
static public native void recordComment(
int address, int beginLine, int beginColumn, int endLine, int endColumn
);
static public native void recordError(
int address, String message, int fatal, int indexed, int beginLine, int beginColumn, int endLine, int endColumn
);
}