From: Svjatoslav Agejenko Date: Sun, 14 Jul 2013 09:55:21 +0000 (+0300) Subject: possibility to hide orphaned classes from graph X-Git-Tag: javainspect-1.1~6 X-Git-Url: http://www2.svjatoslav.eu/gitweb/?a=commitdiff_plain;h=39a8ba91a8541b26180cc3c3dcb99f5ff295785d;p=javainspect.git possibility to hide orphaned classes from graph --- diff --git a/src/main/java/eu/svjatoslav/inspector/java/structure/ClassDescriptor.java b/src/main/java/eu/svjatoslav/inspector/java/structure/ClassDescriptor.java index fbd491b..55ef66c 100644 --- a/src/main/java/eu/svjatoslav/inspector/java/structure/ClassDescriptor.java +++ b/src/main/java/eu/svjatoslav/inspector/java/structure/ClassDescriptor.java @@ -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 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) { diff --git a/src/main/java/eu/svjatoslav/inspector/java/structure/ClassGraph.java b/src/main/java/eu/svjatoslav/inspector/java/structure/ClassGraph.java index 4c1dcbc..6155876 100644 --- a/src/main/java/eu/svjatoslav/inspector/java/structure/ClassGraph.java +++ b/src/main/java/eu/svjatoslav/inspector/java/structure/ClassGraph.java @@ -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(); + + } + } diff --git a/src/main/java/eu/svjatoslav/inspector/java/structure/FieldDescriptor.java b/src/main/java/eu/svjatoslav/inspector/java/structure/FieldDescriptor.java index 2cbb47c..e4611aa 100644 --- a/src/main/java/eu/svjatoslav/inspector/java/structure/FieldDescriptor.java +++ b/src/main/java/eu/svjatoslav/inspector/java/structure/FieldDescriptor.java @@ -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 diff --git a/src/main/java/eu/svjatoslav/inspector/java/structure/MethodDescriptor.java b/src/main/java/eu/svjatoslav/inspector/java/structure/MethodDescriptor.java index 35e49d5..c3aeaf9 100644 --- a/src/main/java/eu/svjatoslav/inspector/java/structure/MethodDescriptor.java +++ b/src/main/java/eu/svjatoslav/inspector/java/structure/MethodDescriptor.java @@ -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() {