parse annotations
authorSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Sat, 13 Jul 2013 08:27:59 +0000 (11:27 +0300)
committerSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Sat, 13 Jul 2013 08:27:59 +0000 (11:27 +0300)
src/main/java/eu/svjatoslav/inspector/java/methods/Annotation.java [new file with mode: 0644]
src/main/java/eu/svjatoslav/inspector/java/methods/Clazz.java
src/main/java/eu/svjatoslav/inspector/java/methods/JavaFile.java
src/main/java/eu/svjatoslav/inspector/java/methods/ProjectScanner.java
src/main/java/eu/svjatoslav/inspector/java/structure/ClassGraph.java

diff --git a/src/main/java/eu/svjatoslav/inspector/java/methods/Annotation.java b/src/main/java/eu/svjatoslav/inspector/java/methods/Annotation.java
new file mode 100644 (file)
index 0000000..d262061
--- /dev/null
@@ -0,0 +1,34 @@
+package eu.svjatoslav.inspector.java.methods;
+
+import eu.svjatoslav.inspector.tokenizer.InvalidSyntaxException;
+import eu.svjatoslav.inspector.tokenizer.Tokenizer;
+import eu.svjatoslav.inspector.tokenizer.TokenizerMatch;
+
+public class Annotation {
+
+       private String name;
+
+       public Annotation(final Tokenizer tokenizer) throws InvalidSyntaxException {
+
+               name = tokenizer.getNextToken().token;
+
+               if (!tokenizer.probeNextToken("("))
+                       return;
+
+               int depth = 1;
+
+               while (true) {
+                       final TokenizerMatch token = tokenizer.getNextToken();
+
+                       if ("(".equals(token.token))
+                               depth++;
+                       if (")".equals(token.token))
+                               depth--;
+
+                       if (depth == 0)
+                               return;
+               }
+
+       }
+
+}
index 3c5333b..29c2899 100644 (file)
@@ -52,6 +52,10 @@ public class Clazz {
                }
        }
 
+       public String getFullName() {
+               return packageName + "." + className;
+       }
+
        public void parseClassBody(final Tokenizer tokenizer) {
                tokenizer.skipUntilDataEnd();
        }
index ea05f01..3dd5557 100644 (file)
@@ -22,6 +22,8 @@ public class JavaFile {
 
        StringBuffer contents = new StringBuffer();
 
+       public List<Clazz> classes = new ArrayList<Clazz>();
+
        public JavaFile(final File file) throws IOException, InvalidSyntaxException {
                this.file = file;
                parse();
@@ -49,6 +51,7 @@ public class JavaFile {
                tokenizer.addTerminator("<", false);
                tokenizer.addTerminator(">", false);
                tokenizer.addTerminator(",", false);
+               tokenizer.addTerminator("@", false);
 
                // comments
                tokenizer.addTerminator("//", "\n", true);
@@ -85,6 +88,11 @@ public class JavaFile {
                                continue;
                        }
 
+                       if ("@".equals(match.token)) {
+                               final Annotation annotation = new Annotation(tokenizer);
+                               continue;
+                       }
+
                        System.out.println("    " + modifiers.toString() + " "
                                        + match.token);
                        modifiers.reset();
@@ -99,7 +107,9 @@ public class JavaFile {
                final TokenizerMatch match = tokenizer.getNextToken();
                final Clazz clazz = new Clazz(packageName, match.token, tokenizer,
                                false);
-               System.out.println(clazz.toString());
+               // System.out.println(clazz.toString());
+               classes.add(clazz);
+
        }
 
        private void parseImport(final Tokenizer tokenizer)
@@ -125,7 +135,8 @@ public class JavaFile {
 
                final TokenizerMatch match = tokenizer.getNextToken();
                final Clazz clazz = new Clazz(packageName, match.token, tokenizer, true);
-               System.out.println(clazz.toString());
+               // System.out.println(clazz.toString());
+               classes.add(clazz);
        }
 
        private void parsePackage(final Tokenizer tokenizer)
index 3ed5a31..b37fa4f 100644 (file)
@@ -2,7 +2,9 @@ package eu.svjatoslav.inspector.java.methods;
 
 import java.io.File;
 import java.io.IOException;
+import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 
 import eu.svjatoslav.commons.file.FilePathParser;
@@ -13,12 +15,23 @@ public class ProjectScanner {
        private final File scanPath;
 
        Map<File, Project> projects = new HashMap<File, Project>();
+       public List<JavaFile> javaFiles = new ArrayList<JavaFile>();
 
        public ProjectScanner(final File projectPath) {
                scanPath = projectPath;
                parse();
        }
 
+       public List<Clazz> getAllClasses() {
+               final List<Clazz> result = new ArrayList<Clazz>();
+
+               for (final JavaFile file : javaFiles)
+                       for (final Clazz clazz : file.classes)
+                               result.add(clazz);
+
+               return result;
+       }
+
        public void parse() {
 
                if (!scanPath.exists())
@@ -51,6 +64,7 @@ public class ProjectScanner {
                if ("java".equalsIgnoreCase(fileExtension))
                        try {
                                final JavaFile javaFile = new JavaFile(file);
+                               javaFiles.add(javaFile);
                        } catch (final IOException e) {
                                System.out.println("Error parsing file: " + file.toString()
                                                + ": " + e.toString());
index b8f9c2c..4c1dcbc 100644 (file)
@@ -16,6 +16,8 @@ import java.util.HashMap;
 import java.util.Map;
 
 import eu.svjatoslav.commons.file.CommonPathResolver;
+import eu.svjatoslav.inspector.java.methods.Clazz;
+import eu.svjatoslav.inspector.java.methods.ProjectScanner;
 
 public class ClassGraph {
 
@@ -54,6 +56,19 @@ public class ClassGraph {
                return addClass(object.getClass());
        }
 
+       public void addProject(final String path) {
+               final ProjectScanner projectScanner = new ProjectScanner(new File(path));
+               for (final Clazz clazz : projectScanner.getAllClasses())
+                       try {
+                               System.out.println("Class full name: " + clazz.getFullName());
+                               final Class c = this.getClass().forName(clazz.getFullName());
+                               addClass(c);
+                       } catch (final Exception exception) {
+                               System.out.println("cannot add class: "
+                                               + exception.getMessage());
+                       }
+       }
+
        public void generateGraph(final String graphName) {
                generateGraph(graphName, false);
        }