possibility to hide orphaned classes from graph
authorSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Sun, 14 Jul 2013 09:55:21 +0000 (12:55 +0300)
committerSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Sun, 14 Jul 2013 09:55:21 +0000 (12:55 +0300)
src/main/java/eu/svjatoslav/inspector/java/structure/ClassDescriptor.java
src/main/java/eu/svjatoslav/inspector/java/structure/ClassGraph.java
src/main/java/eu/svjatoslav/inspector/java/structure/FieldDescriptor.java
src/main/java/eu/svjatoslav/inspector/java/structure/MethodDescriptor.java

index fbd491b..55ef66c 100644 (file)
@@ -55,7 +55,7 @@ public class ClassDescriptor implements GraphElement {
        /**
         * Amount of field and method references pointing to this class.
         */
-       private int referenceCount = 0;
+       private int incomingReferencesCount = 0;
 
        public ClassDescriptor(final Class<? extends Object> clazz,
                        final ClassGraph dump) {
@@ -92,7 +92,7 @@ public class ClassDescriptor implements GraphElement {
        }
 
        public boolean areReferencesShown() {
-               return referenceCount <= MAX_REFERECNES_COUNT;
+               return incomingReferencesCount <= MAX_REFERECNES_COUNT;
        }
 
        public void enlistFieldReferences(final StringBuffer result) {
@@ -354,6 +354,31 @@ public class ClassDescriptor implements GraphElement {
                isShown = false;
        }
 
+       public boolean hideClassIfNoReferences() {
+               if (!isVisible())
+                       return false;
+
+               int outgoingVisibleReferencesCount = 0;
+
+               for (final MethodDescriptor methodDescriptor : methods)
+                       outgoingVisibleReferencesCount += methodDescriptor
+                                       .getOutsideVisibleReferencesCount();
+
+               for (final FieldDescriptor fieldDescriptor : nameToFieldMap.values())
+                       outgoingVisibleReferencesCount += fieldDescriptor
+                                       .getOutsideVisibleReferencesCount();
+
+               final int totalReferencesCount = outgoingVisibleReferencesCount
+                               + incomingReferencesCount;
+
+               if (totalReferencesCount == 0) {
+                       hide();
+                       return true;
+               }
+
+               return false;
+       }
+
        public void indexFields(final Field[] fields) {
                for (final Field field : fields) {
                        if (nameToFieldMap.containsKey(field.getName()))
@@ -389,7 +414,7 @@ public class ClassDescriptor implements GraphElement {
        }
 
        public void registerReference() {
-               referenceCount++;
+               incomingReferencesCount++;
        }
 
        public void setDistinctiveColor(final String distinctiveColor) {
index 4c1dcbc..6155876 100644 (file)
@@ -21,6 +21,15 @@ import eu.svjatoslav.inspector.java.methods.ProjectScanner;
 
 public class ClassGraph {
 
+       public static void render(final String graphName, final Class... classes) {
+               final ClassGraph classGraph = new ClassGraph();
+
+               for (final Class clazz : classes)
+                       classGraph.addClass(clazz);
+
+               classGraph.generateGraph(graphName);
+       }
+
        /**
         * Maps class fully qualified names to class descriptors.
         */
@@ -124,4 +133,11 @@ public class ClassGraph {
                return resultStr;
        }
 
+       public void hideClassesWithoutReferences() {
+
+               for (final ClassDescriptor classDescriptor : nameToClassMap.values())
+                       classDescriptor.hideClassIfNoReferences();
+
+       }
+
 }
index 2cbb47c..e4611aa 100644 (file)
@@ -122,6 +122,17 @@ public class FieldDescriptor implements GraphElement {
                return parent.getGraphId() + ":" + name;
        }
 
+       public int getOutsideVisibleReferencesCount() {
+
+               if (!isVisible())
+                       return 0;
+
+               if (type.isVisible())
+                       return 1;
+
+               return 0;
+       }
+
        @Override
        public boolean isVisible() {
                if (name.contains("$"))
@@ -132,5 +143,4 @@ public class FieldDescriptor implements GraphElement {
 
                return true;
        }
-
 }
\ No newline at end of file
index 35e49d5..c3aeaf9 100644 (file)
@@ -52,7 +52,6 @@ public class MethodDescriptor implements GraphElement {
                                }
 
                }
-
        }
 
        @Override
@@ -112,6 +111,19 @@ public class MethodDescriptor implements GraphElement {
                return name;
        }
 
+       public int getOutsideVisibleReferencesCount() {
+               int result = 0;
+
+               if (returnType.isVisible())
+                       result++;
+
+               for (final ClassDescriptor classDescriptor : typeArguments)
+                       if (classDescriptor.isVisible())
+                               result++;
+
+               return result;
+       }
+
        @Override
        public boolean isVisible() {