From d6b9aa36eec691e17ad10eaa814308b50e44255a Mon Sep 17 00:00:00 2001 From: Svjatoslav Agejenko Date: Mon, 25 Dec 2017 13:41:27 +0200 Subject: [PATCH] Migrating to latest svjatoslav-commons. Adding sophisticated commandline arguments parsing. --- commandline launcher/install | 7 +-- commandline launcher/javainspect | 5 +-- javainspect.iml | 2 +- pom.xml | 2 +- .../java/CommandlineConfiguration.java | 41 ++++++++++++++++++ .../eu/svjatoslav/inspector/java/Main.java | 18 +++----- .../inspector/java/methods/Annotation.java | 2 +- .../java/methods/ClassReference.java | 6 +-- .../inspector/java/methods/Clazz.java | 2 +- .../inspector/java/methods/JavaFile.java | 43 ++++++++++--------- 10 files changed, 82 insertions(+), 46 deletions(-) create mode 100644 src/main/java/eu/svjatoslav/inspector/java/CommandlineConfiguration.java diff --git a/commandline launcher/install b/commandline launcher/install index f17fc78..b7c7b3e 100755 --- a/commandline launcher/install +++ b/commandline launcher/install @@ -1,12 +1,13 @@ #!/bin/bash +# +# This script installs JavaInspect into /opt/javainspect/ directory. +# It also enables javainspect command at the commandline. +# ( # go to project root directory cd .. - # This script installs JavaInspect into /opt/javainspect/ directory. - # It also enables javainspect command at the commandline. - # remove old installation (if any) sudo rm -rf /opt/javainspect/ diff --git a/commandline launcher/javainspect b/commandline launcher/javainspect index eb6110c..162f317 100755 --- a/commandline launcher/javainspect +++ b/commandline launcher/javainspect @@ -10,7 +10,4 @@ # -java \ - -cp .:./example/target/myproject-0.0.jar:./target/javainspect-1.6-SNAPSHOT.jar \ - eu.svjatoslav.inspector.java.Main \ - ./example/src/main/java/ com.myproject.* myproject +java -jar /opt/javainspect/javainspect.jar $@ diff --git a/javainspect.iml b/javainspect.iml index a81dd61..43da783 100644 --- a/javainspect.iml +++ b/javainspect.iml @@ -11,6 +11,6 @@ - + \ No newline at end of file diff --git a/pom.xml b/pom.xml index 2aa7657..6614ca1 100644 --- a/pom.xml +++ b/pom.xml @@ -108,7 +108,7 @@ eu.svjatoslav svjatoslavcommons - 1.6 + 1.7-SNAPSHOT diff --git a/src/main/java/eu/svjatoslav/inspector/java/CommandlineConfiguration.java b/src/main/java/eu/svjatoslav/inspector/java/CommandlineConfiguration.java new file mode 100644 index 0000000..4c2cbd7 --- /dev/null +++ b/src/main/java/eu/svjatoslav/inspector/java/CommandlineConfiguration.java @@ -0,0 +1,41 @@ +package eu.svjatoslav.inspector.java; + +import eu.svjatoslav.commons.commandline.parameterparser.Parser; +import eu.svjatoslav.commons.commandline.parameterparser.parameter.FileParameter; +import eu.svjatoslav.commons.commandline.parameterparser.parameter.StringParameter; +import eu.svjatoslav.commons.commandline.parameterparser.parameter.StringParameters; + +public class CommandlineConfiguration { + + public CommandlineConfiguration (String args[]){ + Parser parser = buildCommandlineParameterParser(); + if (!parser.parse(args)) { + parser.showHelp(); + } + } + + public Parser buildCommandlineParameterParser() { + Parser parser = new Parser(); + + parser.add( + new FileParameter("JAR file")) + .mustExist() + .addAliases("-j"); + + parser.add( + new StringParameter("graph name")) + .setMandatory() + .addAliases("-n"); + + parser.add( + new StringParameters("whitelist glob")) + .addAliases("-w"); + + parser.add( + new StringParameters("blacklist glob")) + .addAliases("-b"); + + return parser; + } + +} diff --git a/src/main/java/eu/svjatoslav/inspector/java/Main.java b/src/main/java/eu/svjatoslav/inspector/java/Main.java index f24eae3..eda82ee 100644 --- a/src/main/java/eu/svjatoslav/inspector/java/Main.java +++ b/src/main/java/eu/svjatoslav/inspector/java/Main.java @@ -10,21 +10,15 @@ import static java.lang.System.getProperty; */ public class Main { public static void main(String[] args) { - if (args.length == 0) { - System.err.println("usage: javainspect [PROJECT_DIR] [PACKAGE_GLOB] [GRAPH_NAME]"); - System.exit(1); - } - - String projectDir = args[0]; - String packageGlob = args[1]; - String graphName = args[2]; + CommandlineConfiguration commandlineConfiguration = new CommandlineConfiguration(args); + System.out.println("Commandline config validated"); ClassGraph cg = new ClassGraph(); cg.setTargetDirectoryPath(getProperty("user.dir") + separator); - cg.addProject(projectDir); - cg.whitelistClassGlob(packageGlob); - cg.setKeepDotFile(true); - cg.generateGraph(graphName); +// cg.addProject(projectDir); +// cg.whitelistClassGlob(packageGlob); +// cg.setKeepDotFile(true); +// cg.generateGraph(graphName); } } diff --git a/src/main/java/eu/svjatoslav/inspector/java/methods/Annotation.java b/src/main/java/eu/svjatoslav/inspector/java/methods/Annotation.java index 94b2232..94b320c 100755 --- a/src/main/java/eu/svjatoslav/inspector/java/methods/Annotation.java +++ b/src/main/java/eu/svjatoslav/inspector/java/methods/Annotation.java @@ -21,7 +21,7 @@ public class Annotation { name = tokenizer.getNextToken().token; - if (!tokenizer.probeNextToken("(")) + if (!tokenizer.consumeIfNextToken("(")) return; int depth = 1; diff --git a/src/main/java/eu/svjatoslav/inspector/java/methods/ClassReference.java b/src/main/java/eu/svjatoslav/inspector/java/methods/ClassReference.java index a403494..bac017d 100755 --- a/src/main/java/eu/svjatoslav/inspector/java/methods/ClassReference.java +++ b/src/main/java/eu/svjatoslav/inspector/java/methods/ClassReference.java @@ -25,18 +25,18 @@ public class ClassReference { throws InvalidSyntaxException { name = tokenizer.getNextToken().token; - if (!tokenizer.probeNextToken("<")) + if (!tokenizer.consumeIfNextToken("<")) return; while (true) { final ClassReference parameterType = new ClassReference(tokenizer); typeParameters.add(parameterType); - if (!tokenizer.probeNextToken(",")) + if (!tokenizer.consumeIfNextToken(",")) break; } - tokenizer.expectNextToken(">"); + tokenizer.expectAndConsumeNextToken(">"); } @Override diff --git a/src/main/java/eu/svjatoslav/inspector/java/methods/Clazz.java b/src/main/java/eu/svjatoslav/inspector/java/methods/Clazz.java index f6051c9..49f3d5e 100755 --- a/src/main/java/eu/svjatoslav/inspector/java/methods/Clazz.java +++ b/src/main/java/eu/svjatoslav/inspector/java/methods/Clazz.java @@ -45,7 +45,7 @@ public class Clazz { while (true) { implementedInterfaces.add(new ClassReference(tokenizer)); - if (tokenizer.probeNextToken(",")) + if (tokenizer.consumeIfNextToken(",")) continue; break; diff --git a/src/main/java/eu/svjatoslav/inspector/java/methods/JavaFile.java b/src/main/java/eu/svjatoslav/inspector/java/methods/JavaFile.java index df53ee8..9ec86c3 100755 --- a/src/main/java/eu/svjatoslav/inspector/java/methods/JavaFile.java +++ b/src/main/java/eu/svjatoslav/inspector/java/methods/JavaFile.java @@ -17,6 +17,9 @@ import java.io.*; import java.util.ArrayList; import java.util.List; +import static eu.svjatoslav.commons.string.tokenizer.Terminator.TerminationStrategy.DROP; +import static eu.svjatoslav.commons.string.tokenizer.Terminator.TerminationStrategy.PRESERVE; + public class JavaFile { public static final String UTF_8 = "UTF-8"; @@ -39,25 +42,25 @@ public class JavaFile { final Tokenizer tokenizer = new Tokenizer(contents.toString()); // empty space - tokenizer.addTerminator(" ", true); - tokenizer.addTerminator("\t", true); - tokenizer.addTerminator("\n", true); - - tokenizer.addTerminator(";", false); - tokenizer.addTerminator("{", false); - tokenizer.addTerminator("}", false); - tokenizer.addTerminator("(", false); - tokenizer.addTerminator(")", false); - tokenizer.addTerminator("[", false); - tokenizer.addTerminator("]", false); - tokenizer.addTerminator("<", false); - tokenizer.addTerminator(">", false); - tokenizer.addTerminator(",", false); - tokenizer.addTerminator("@", false); + tokenizer.addTerminator(" ", DROP); + tokenizer.addTerminator("\t", DROP); + tokenizer.addTerminator("\n", DROP); + + tokenizer.addTerminator(";", PRESERVE); + tokenizer.addTerminator("{", PRESERVE); + tokenizer.addTerminator("}", PRESERVE); + tokenizer.addTerminator("(", PRESERVE); + tokenizer.addTerminator(")", PRESERVE); + tokenizer.addTerminator("[", PRESERVE); + tokenizer.addTerminator("]", PRESERVE); + tokenizer.addTerminator("<", PRESERVE); + tokenizer.addTerminator(">", PRESERVE); + tokenizer.addTerminator(",", PRESERVE); + tokenizer.addTerminator("@", PRESERVE); // comments - tokenizer.addTerminator("//", "\n", true); - tokenizer.addTerminator("/*", "*/", true); + tokenizer.addTerminator("//", "\n", DROP); + tokenizer.addTerminator("/*", "*/", DROP); final Modifiers modifiers = new Modifiers(); @@ -129,7 +132,7 @@ public class JavaFile { imports.add(imp); - tokenizer.expectNextToken(";"); + tokenizer.expectAndConsumeNextToken(";"); } private void parseInterface(final Tokenizer tokenizer) @@ -148,7 +151,7 @@ public class JavaFile { packageName = match.token; - tokenizer.expectNextToken(";"); + tokenizer.expectAndConsumeNextToken(";"); } private void readFile() throws IOException { @@ -170,7 +173,7 @@ public class JavaFile { inputStream.close(); } - public void skipUntilSemicolon(final Tokenizer tokenizer) { + public void skipUntilSemicolon(final Tokenizer tokenizer) throws InvalidSyntaxException { while (true) { final TokenizerMatch token = tokenizer.getNextToken(); -- 2.20.1