Skip to content

Commit

Permalink
fix(java_indexer): use Path to resolve metadata (fixes #4135) (#4136)
Browse files Browse the repository at this point in the history
* fix(java_indexer): use Path to resolve metadata (fixes #4135)

* chore: apply suggestions
  • Loading branch information
shahms committed Oct 8, 2019
1 parent ae19c93 commit e03d472
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1370,10 +1370,7 @@ private <T extends JCTree> List<JavaNode> scanList(List<T> trees, TreeContext ow
private void loadAnnotationsFile(String path) {
URI uri = filePositions.getSourceFile().toUri();
try {
String fullPath = uri.resolve(path).getPath();
if (fullPath.startsWith("/")) {
fullPath = fullPath.substring(1);
}
String fullPath = resolveSourcePath(path);
FileObject file = Iterables.getOnlyElement(fileManager.getJavaFileObjects(fullPath), null);
if (file == null) {
logger.atWarning().log("Can't find metadata %s for %s at %s", path, uri, fullPath);
Expand Down Expand Up @@ -1424,6 +1421,22 @@ private void loadAnnotationsFromClassDecl(JCClassDecl decl) {
}
}

/** Resovles a string as a source-file relative path */
private String resolveSourcePath(String path) {
try {
return fileManager.asPath(filePositions.getSourceFile()).resolveSibling(path).toString();
} catch (UnsupportedOperationException
| IllegalArgumentException
| NullPointerException unused) {
URI uri = filePositions.getSourceFile().toUri();
String fullPath = uri.resolve(path).getPath();
if (fullPath.startsWith("/")) {
fullPath = fullPath.substring(1);
}
return fullPath;
}
}

private Type externalType(Symbol sym) {
return sym.externalType(Types.instance(javaContext));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import com.google.protobuf.DescriptorProtos.GeneratedCodeInfo;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.function.Function;
import org.checkerframework.checker.nullness.qual.Nullable;
Expand Down Expand Up @@ -81,10 +83,11 @@ public ProtobufMetadataLoader(
/** @return a function that looks up the VName for some filename in the given CompilationUnit. */
private static Function<String, VName> lookupVNameFromCompilationUnit(CompilationUnit unit) {
HashMap<String, VName> map = new HashMap<>();
Path root = Paths.get("/", unit.getWorkingDirectory());
for (CompilationUnit.FileInput input : unit.getRequiredInputList()) {
map.put(input.getInfo().getPath(), input.getVName());
map.put(root.resolve(input.getInfo().getPath()).toString(), input.getVName());
}
return map::get;
return p -> map.get(root.resolve(p).toString());
}

@Override
Expand Down

0 comments on commit e03d472

Please sign in to comment.