Skip to content

Commit

Permalink
Housekeeping: removed '-content' flag and fixed some warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
Strum355 committed Sep 16, 2020
1 parent 199fa8c commit ae10916
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 56 deletions.
21 changes: 10 additions & 11 deletions src/main/java/lsifjava/ArgumentParser.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package lsifjava;

import java.io.IOException;
import java.nio.file.Paths;

import org.apache.commons.cli.*;

public class ArgumentParser {
public static final String VERSION = "0.1.0";
public static final String PROTOCOL_VERSION = "0.4.0";

public static Arguments parse(String[] args) throws IOException{
public static Arguments parse(String[] args) {
Options options = createOptions();
CommandLineParser parser = new DefaultParser();
HelpFormatter formatter = new HelpFormatter();
Expand All @@ -33,13 +34,16 @@ public static Arguments parse(String[] args) throws IOException{
System.exit(0);
}

String projectRoot = cmd.getOptionValue("projectRoot", ".");
boolean contents = cmd.hasOption("contents");
String outFile = cmd.getOptionValue("out");
String projectRoot;
try {
projectRoot = Paths.get(cmd.getOptionValue("projectRoot", ".")).toFile().getCanonicalPath();
} catch(IOException e) {
throw new RuntimeException(String.format("Directory %s could not be found", cmd.getOptionValue("projectRoot", ".")));
}
String outFile = cmd.getOptionValue("out", projectRoot + "/dump.lsif");
boolean verbosity = cmd.hasOption("verbose");


return new Arguments(projectRoot, contents, outFile, verbosity);
return new Arguments(projectRoot, outFile, verbosity);
}

private static Options createOptions() {
Expand All @@ -65,11 +69,6 @@ private static Options createOptions() {
"Specifies the project root. Defaults to the current working directory."
));

options.addOption(new Option(
"contents", false,
"File contents will be embedded into the dump."
));

options.addOption(new Option(
"out", true,
"The output file the dump is save to."
Expand Down
11 changes: 3 additions & 8 deletions src/main/java/lsifjava/Arguments.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
package lsifjava;

import java.io.IOException;
import java.nio.file.Paths;

public class Arguments {
public final String projectRoot;
public final boolean contents;
public final String outFile;
public final boolean verbose;

public Arguments(String projectRoot, boolean contents, String outFile, boolean verbose) throws IOException {
this.projectRoot = Paths.get(projectRoot).toFile().getCanonicalPath();
this.contents = contents;
this.outFile = outFile != null ? outFile : projectRoot + "/dump.lsif";
public Arguments(String projectRoot, String outFile, boolean verbose) {
this.projectRoot = projectRoot;
this.outFile = outFile;
this.verbose = verbose;
}
}
27 changes: 6 additions & 21 deletions src/main/java/lsifjava/DocumentIndexer.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,13 @@
import spoon.reflect.reference.CtTypeReference;
import spoon.reflect.visitor.CtScanner;

import java.io.IOException;
import java.lang.annotation.Annotation;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;

public class DocumentIndexer {
private String projectRoot;
private boolean verbose;
private boolean contents;
private String pathname;
private CtElement spoonElement;
private String projectId;
Expand All @@ -35,7 +31,6 @@ public class DocumentIndexer {
public DocumentIndexer(
String projectRoot,
boolean verbose,
boolean contents,
String pathname,
CtElement spoonElement,
String projectId,
Expand All @@ -44,7 +39,6 @@ public DocumentIndexer(
) {
this.projectRoot = projectRoot;
this.verbose = verbose;
this.contents = contents;
this.pathname = pathname;
this.spoonElement = spoonElement;
this.projectId = projectId;
Expand All @@ -70,15 +64,6 @@ public void preIndex() {
"uri", String.format("file://%s", Paths.get(pathname).toAbsolutePath().toString())
);

if (contents) {
try {
List<String> lines = Files.readAllLines(Paths.get(pathname), StandardCharsets.UTF_8);
args = Util.union(args, Util.mapOf("contents", String.join("\n", lines)));
} catch (IOException ex) {
throw new RuntimeException(String.format("Failed to read file %s", pathname));
}
}

this.documentId = emitter.emitVertex("document", args);
}

Expand Down Expand Up @@ -305,7 +290,7 @@ public <T> void visitCtFieldRead(CtFieldRead<T> el) {
return;
}
Range useRange = identifierRange(el, el.getVariable().getSimpleName());
CtField decl = el.getVariable().getDeclaration();
CtField<?> decl = el.getVariable().getDeclaration();
if (decl == null) {
return;
}
Expand All @@ -322,7 +307,7 @@ public <T> void visitCtTypeReference(CtTypeReference<T> el) {
if (el.getPosition() instanceof NoSourcePosition) {
return;
}
CtType decl = el.getDeclaration();
CtType<?> decl = el.getDeclaration();
if (decl == null) {
return;
}
Expand Down Expand Up @@ -363,7 +348,7 @@ public <T> void visitCtCatchVariableReference(CtCatchVariableReference<T> el) {
return;
}

CtCatchVariable decl = el.getDeclaration();
CtCatchVariable<?> decl = el.getDeclaration();
if (decl == null || decl.getPosition() instanceof NoSourcePosition) {
return;
}
Expand All @@ -382,7 +367,7 @@ public <T> void visitCtConstructorCall(CtConstructorCall<T> el) {
return;
}

CtExecutableReference exec = el.getExecutable();
CtExecutableReference<?> exec = el.getExecutable();
if (exec == null || exec.getDeclaration() == null || exec.getDeclaration().getPosition() instanceof NoSourcePosition) {
return;
}
Expand Down Expand Up @@ -455,7 +440,7 @@ private Range nameRange(Range a, String name) {
);
}

private String mkDoc(CtTypeReference t, String docComment) {
private String mkDoc(CtTypeReference<?> t, String docComment) {
return "```java\n" + t + "\n```" + (docComment.equals("") ? "" : "\n---\n" + docComment);
}

Expand All @@ -475,7 +460,7 @@ private Map<String, Object> createPosition(Position position) {
return Util.mapOf("line", position.line - 1, "character", position.column - 1);
}

private Range identifierRange(CtTargetedExpression a, String b) {
private Range identifierRange(CtTargetedExpression<?, ?> a, String b) {
// Ugh, we only want the range for the identifier, not the whole expression.
// This will probably break on fields/methods that are spread across lines.
// +1 for `.`, +1 because (I'm guessing) end is inclusive
Expand Down
1 change: 0 additions & 1 deletion src/main/java/lsifjava/Emitter.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.google.gson.Gson;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

public class Emitter {
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/lsifjava/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
public class Main {
public static void main(String[] args) throws IOException{
System.out.println("Running JVM " + System.getProperty("java.version"));

Arguments arguments = ArgumentParser.parse(args);
PrintWriter writer = createWriter(arguments);
Emitter emitter = new Emitter(writer);
Expand All @@ -18,7 +19,6 @@ public static void main(String[] args) throws IOException{
indexer.index();
} finally {
writer.flush();

writer.close();
}

Expand All @@ -34,7 +34,6 @@ private static PrintWriter createWriter(Arguments arguments) {
}

private static void displayStats(ProjectIndexer indexer, Emitter emitter, long start) {

System.out.printf(
"%d file(s), %d def(s), %d element(s)\n",
indexer.numFiles,
Expand Down
12 changes: 0 additions & 12 deletions src/main/java/lsifjava/ProjectIndexer.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package lsifjava;

import spoon.MavenLauncher;
import spoon.compiler.Environment;
import spoon.reflect.declaration.CtElement;
import spoon.support.StandardEnvironment;
import spoon.support.compiler.SpoonPom;

import java.io.IOException;
Expand All @@ -16,7 +14,6 @@
import java.util.*;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.google.common.collect.Sets;

Expand Down Expand Up @@ -109,7 +106,6 @@ public FileVisitResult visitFileFailed(Path arg0, IOException arg1) throws IOExc
indexers.put(pathname.getKey(), new DocumentIndexer(
arguments.projectRoot,
arguments.verbose,
arguments.contents,
pathname.getKey(),
pathname.getValue(),
projectId,
Expand Down Expand Up @@ -157,14 +153,6 @@ public FileVisitResult visitFileFailed(Path arg0, IOException arg1) throws IOExc
}
}

private Stream<Path> createFileStream() {
try {
return Files.walk(Paths.get(arguments.projectRoot));
} catch (IOException ex) {
throw new RuntimeException(String.format("Failed to walk files in %s", arguments.projectRoot));
}
}

private void printModule(SpoonPom pom) throws IOException {
System.out.println("Found module '" + pom.getName() + "' at " + Paths.get(pom.getPath()).getParent().toFile().getCanonicalPath().replaceFirst("^"+arguments.projectRoot, "."));
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/lsifjava/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ protected static Map<String, Object> union(Map<String, Object> h1, Map<String, O
}

protected static Map<String, Object> mapOf(Object... args) {
Map<String, Object> map = new HashMap();
Map<String, Object> map = new HashMap<>();
for (int i = 0; i < args.length - 1; i += 2) {
map.put(args[i].toString(), args[i + 1]);
}
Expand Down

0 comments on commit ae10916

Please sign in to comment.