Skip to content

Commit

Permalink
Make the JavaParserTypeSolver store the path in the compilation units…
Browse files Browse the repository at this point in the history
… it creates
  • Loading branch information
matozoid committed May 14, 2018
1 parent 2816c25 commit 218b720
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 25 deletions.
Expand Up @@ -329,7 +329,7 @@ private static <T extends Node> T simplifiedParse(ParseStart<T> context, Provide
if (result.isSuccessful()) {
return result.getResult().get();
}
throw new ParseProblemException(result.getProblems());
throw new ParseProblemException(result);
}

/**
Expand Down
Expand Up @@ -35,14 +35,16 @@ public class ParseProblemException extends RuntimeException {
* The problems that were encountered during parsing
*/
private final List<Problem> problems;
private final ParseResult<?> result;

public ParseProblemException(List<Problem> problems) {
super(createMessage(assertNotNull(problems)));
this.problems = problems;
public ParseProblemException(ParseResult<?> result) {
super(createMessage(assertNotNull(result.getProblems())));
this.problems = result.getProblems();
this.result = result;
}

public ParseProblemException(Throwable throwable) {
this(singletonList(new Problem(throwable.getMessage(), null, throwable)));
this(new ParseResult<>(null, singletonList(new Problem(throwable.getMessage(), null, throwable)), null, null));
}

private static String createMessage(List<Problem> problems) {
Expand All @@ -56,4 +58,8 @@ private static String createMessage(List<Problem> problems) {
public List<Problem> getProblems() {
return problems;
}

public ParseResult<?> getResult() {
return result;
}
}
Expand Up @@ -253,7 +253,7 @@ public CompilationUnit parse(String startPackage, String filename) {
if (result.isSuccessful()) {
return result.getResult().get();
}
throw new ParseProblemException(result.getProblems());
throw new ParseProblemException(result);
} catch (IOException e) {
throw new ParseProblemException(e);
}
Expand Down
Expand Up @@ -310,11 +310,12 @@ public Optional<ResolvedReferenceTypeDeclaration> containerType() {

private ResolvedReferenceType toReferenceType(ClassOrInterfaceType classOrInterfaceType) {
SymbolReference<? extends ResolvedTypeDeclaration> ref = null;
if (classOrInterfaceType.toString().indexOf('.') > -1) {
ref = typeSolver.tryToSolveType(classOrInterfaceType.toString());
String typeName = classOrInterfaceType.getNameAsString();
if (typeName.indexOf('.') > -1) {
ref = typeSolver.tryToSolveType(typeName);
}
if (ref == null || !ref.isSolved()) {
ref = solveType(classOrInterfaceType.toString(), typeSolver);
ref = solveType(typeName, typeSolver);
}
if (!ref.isSolved()) {
ref = solveType(classOrInterfaceType.getName().getId(), typeSolver);
Expand Down
Expand Up @@ -16,8 +16,7 @@

package com.github.javaparser.symbolsolver.resolution.typesolvers;

import com.github.javaparser.JavaParser;
import com.github.javaparser.ParseProblemException;
import com.github.javaparser.*;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration;
import com.github.javaparser.symbolsolver.javaparser.Navigator;
Expand All @@ -36,6 +35,10 @@
import java.util.Optional;
import java.util.concurrent.ExecutionException;

import static com.github.javaparser.ParseStart.*;
import static com.github.javaparser.ParserConfiguration.LanguageLevel.*;
import static com.github.javaparser.Providers.provider;

/**
* Defines a directory containig source code that should be used for solving symbols.
* The directory must correspond to the root package of the files within.
Expand All @@ -45,6 +48,7 @@
public class JavaParserTypeSolver implements TypeSolver {

private final File srcDir;
private final JavaParser javaParser;

private TypeSolver parent;

Expand All @@ -65,6 +69,11 @@ public JavaParserTypeSolver(File srcDir) {
throw new IllegalStateException("SrcDir does not exist or is not a directory: " + srcDir.getAbsolutePath());
}
this.srcDir = srcDir;
javaParser = new JavaParser(
new ParserConfiguration()
.setLanguageLevel(BLEEDING_EDGE)
.setAttributeComments(false)
.setStoreTokens(false));
}

@Override
Expand All @@ -85,17 +94,21 @@ public void setParent(TypeSolver parent) {
this.parent = parent;
}


private Optional<CompilationUnit> parse(File srcFile) {
try {
return parsedFiles.get(srcFile.getAbsolutePath(), () -> {
try {
return Optional.of(JavaParser.parse(srcFile));
if (!srcFile.exists()) {
return Optional.empty();
}
return javaParser.parse(COMPILATION_UNIT, provider(srcFile))
.getResult()
.map(cu -> cu.setStorage(srcFile.toPath()));
} catch (FileNotFoundException e) {
Log.trace("File not found while type solving: " + srcFile.getAbsolutePath());
return Optional.empty();
throw new RuntimeException("Issue while parsing while type solving: " + srcFile.getAbsolutePath(), e);
} catch (ParseProblemException e) {
throw new RuntimeException("Issue while parsing " + srcFile.getAbsolutePath(), e);
Log.trace("Issue while parsing while type solving: " + srcFile.getAbsolutePath(), e);
return Optional.of((CompilationUnit) e.getResult().getResult().get());
}
});
} catch (ExecutionException e) {
Expand Down Expand Up @@ -143,25 +156,26 @@ private SymbolReference<ResolvedReferenceTypeDeclaration> tryToSolveTypeUncached
String[] nameElements = name.split("\\.");

for (int i = nameElements.length; i > 0; i--) {
String filePath = srcDir.getAbsolutePath();
StringBuilder filePath = new StringBuilder(srcDir.getAbsolutePath());
for (int j = 0; j < i; j++) {
filePath += "/" + nameElements[j];
filePath.append("/")
.append(nameElements[j]);
}
filePath += ".java";
filePath.append(".java");

String typeName = "";
StringBuilder typeName = new StringBuilder();
for (int j = i - 1; j < nameElements.length; j++) {
if (j != i - 1) {
typeName += ".";
typeName.append(".");
}
typeName += nameElements[j];
typeName.append(nameElements[j]);
}

File srcFile = new File(filePath);
File srcFile = new File(filePath.toString());
{
Optional<CompilationUnit> compilationUnit = parse(srcFile);
if (compilationUnit.isPresent()) {
Optional<com.github.javaparser.ast.body.TypeDeclaration<?>> astTypeDeclaration = Navigator.findType(compilationUnit.get(), typeName);
Optional<com.github.javaparser.ast.body.TypeDeclaration<?>> astTypeDeclaration = Navigator.findType(compilationUnit.get(), typeName.toString());
if (astTypeDeclaration.isPresent()) {
return SymbolReference.solved(JavaParserFacade.get(this).getTypeDeclaration(astTypeDeclaration.get()));
}
Expand All @@ -171,7 +185,7 @@ private SymbolReference<ResolvedReferenceTypeDeclaration> tryToSolveTypeUncached
{
List<CompilationUnit> compilationUnits = parseDirectory(srcFile.getParentFile());
for (CompilationUnit compilationUnit : compilationUnits) {
Optional<com.github.javaparser.ast.body.TypeDeclaration<?>> astTypeDeclaration = Navigator.findType(compilationUnit, typeName);
Optional<com.github.javaparser.ast.body.TypeDeclaration<?>> astTypeDeclaration = Navigator.findType(compilationUnit, typeName.toString());
if (astTypeDeclaration.isPresent()) {
return SymbolReference.solved(JavaParserFacade.get(this).getTypeDeclaration(astTypeDeclaration.get()));
}
Expand Down

0 comments on commit 218b720

Please sign in to comment.