Migrating to latest svjatoslav-commons.
authorSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Mon, 25 Dec 2017 11:41:27 +0000 (13:41 +0200)
committerSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Mon, 25 Dec 2017 11:41:27 +0000 (13:41 +0200)
Adding sophisticated commandline arguments parsing.

commandline launcher/install
commandline launcher/javainspect
javainspect.iml
pom.xml
src/main/java/eu/svjatoslav/inspector/java/CommandlineConfiguration.java [new file with mode: 0644]
src/main/java/eu/svjatoslav/inspector/java/Main.java
src/main/java/eu/svjatoslav/inspector/java/methods/Annotation.java
src/main/java/eu/svjatoslav/inspector/java/methods/ClassReference.java
src/main/java/eu/svjatoslav/inspector/java/methods/Clazz.java
src/main/java/eu/svjatoslav/inspector/java/methods/JavaFile.java

index f17fc78..b7c7b3e 100755 (executable)
@@ -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/
 
index eb6110c..162f317 100755 (executable)
@@ -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 $@
index a81dd61..43da783 100644 (file)
@@ -11,6 +11,6 @@
     </content>
     <orderEntry type="inheritedJdk" />
     <orderEntry type="sourceFolder" forTests="false" />
-    <orderEntry type="library" name="Maven: eu.svjatoslav:svjatoslavcommons:1.6" level="project" />
+    <orderEntry type="library" name="Maven: eu.svjatoslav:svjatoslavcommons:1.7-SNAPSHOT" level="project" />
   </component>
 </module>
\ No newline at end of file
diff --git a/pom.xml b/pom.xml
index 2aa7657..6614ca1 100644 (file)
--- a/pom.xml
+++ b/pom.xml
         <dependency>
             <groupId>eu.svjatoslav</groupId>
             <artifactId>svjatoslavcommons</artifactId>
-            <version>1.6</version>
+            <version>1.7-SNAPSHOT</version>
         </dependency>
     </dependencies>
 
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 (file)
index 0000000..4c2cbd7
--- /dev/null
@@ -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;
+    }
+
+}
index f24eae3..eda82ee 100644 (file)
@@ -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);
        }
 }
index 94b2232..94b320c 100755 (executable)
@@ -21,7 +21,7 @@ public class Annotation {
 
         name = tokenizer.getNextToken().token;
 
-        if (!tokenizer.probeNextToken("("))
+        if (!tokenizer.consumeIfNextToken("("))
             return;
 
         int depth = 1;
index a403494..bac017d 100755 (executable)
@@ -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
index f6051c9..49f3d5e 100755 (executable)
@@ -45,7 +45,7 @@ public class Clazz {
                 while (true) {
                     implementedInterfaces.add(new ClassReference(tokenizer));
 
-                    if (tokenizer.probeNextToken(","))
+                    if (tokenizer.consumeIfNextToken(","))
                         continue;
 
                     break;
index df53ee8..9ec86c3 100755 (executable)
@@ -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();