From: Svjatoslav Agejenko Date: Thu, 21 Nov 2013 20:27:08 +0000 (+0200) Subject: count class extensions as references when calculating orphaned classes X-Git-Tag: javainspect-1.3~10 X-Git-Url: http://www2.svjatoslav.eu/gitweb/?a=commitdiff_plain;h=8285a2e198a01177b14e528f8e12de2f7d5273e9;p=javainspect.git count class extensions as references when calculating orphaned classes --- 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 c0be547..eae630b 100644 --- a/src/main/java/eu/svjatoslav/inspector/java/structure/ClassDescriptor.java +++ b/src/main/java/eu/svjatoslav/inspector/java/structure/ClassDescriptor.java @@ -59,6 +59,8 @@ public class ClassDescriptor implements GraphElement { */ private int incomingReferencesCount = 0; + private int extensionsCount = 0; + public ClassDescriptor(final Class clazz, final ClassGraph dump) { classGraph = dump; @@ -87,10 +89,17 @@ public class ClassDescriptor implements GraphElement { indexMethods(clazz); - for (final Class interfaceClass : clazz.getInterfaces()) - interfaces.add(dump.addClass(interfaceClass)); + for (final Class interfaceClass : clazz.getInterfaces()) { + final ClassDescriptor classDescriptor = dump + .addClass(interfaceClass); + classDescriptor.registerExtension(); + interfaces.add(classDescriptor); + } superClass = dump.addClass(clazz.getSuperclass()); + if (superClass != null) + superClass.registerExtension(); + } public boolean areReferencesShown() { @@ -299,7 +308,7 @@ public class ClassDescriptor implements GraphElement { public String getGraphId() { final String result = "class_" + fullyQualifiedName.replace('.', '_').replace(";", "") - .replace("[L", "").replace('$', '_'); + .replace("[L", "").replace('$', '_'); return result; } @@ -310,25 +319,38 @@ public class ClassDescriptor implements GraphElement { return interfaceColor; } - public String getPackageName() { + private int getOutgoingReferencesCount() { + int result = 0; - final int i = fullyQualifiedName.lastIndexOf('.'); + // count method references + for (final MethodDescriptor methodDescriptor : methods) + result += methodDescriptor.getOutsideVisibleReferencesCount(); - if (i == -1) - return ""; + // count field references + for (final FieldDescriptor fieldDescriptor : nameToFieldMap.values()) + result += fieldDescriptor.getOutsideVisibleReferencesCount(); - return fullyQualifiedName.substring(0, i).replace("[L", ""); + // count implemented interfaces + for (final ClassDescriptor classDescriptor : interfaces) + if (classDescriptor.isVisible()) + result++; + + // count superclass + if (superClass != null) + if (superClass.isVisible()) + result++; + + return result; } - public String getParentClassesName() { - int i = fullyQualifiedName.lastIndexOf('.'); - final String fullClassName = fullyQualifiedName.substring(i + 1); + public String getPackageName() { + + final int i = fullyQualifiedName.lastIndexOf('.'); - i = fullClassName.lastIndexOf('$'); if (i == -1) return ""; - final String parentClassesName = fullClassName.substring(0, i); - return parentClassesName.replace('$', '.'); + + return fullyQualifiedName.substring(0, i).replace("[L", ""); } // public String getReadableName() { @@ -345,6 +367,17 @@ public class ClassDescriptor implements GraphElement { // return fullyQualifiedName; // } + public String getParentClassesName() { + int i = fullyQualifiedName.lastIndexOf('.'); + final String fullClassName = fullyQualifiedName.substring(i + 1); + + i = fullClassName.lastIndexOf('$'); + if (i == -1) + return ""; + final String parentClassesName = fullClassName.substring(0, i); + return parentClassesName.replace('$', '.'); + } + public String getSuperClassColor() { if (superClassColor == null) superClassColor = Utils.getNextLightColor(); @@ -356,29 +389,19 @@ public class ClassDescriptor implements GraphElement { isShown = false; } - public boolean hideClassIfNoReferences() { + public void 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(); + return; - final int totalReferencesCount = outgoingVisibleReferencesCount - + incomingReferencesCount; + final int totalReferencesCount = getOutgoingReferencesCount() + + incomingReferencesCount + extensionsCount; if (totalReferencesCount == 0) { hide(); - return true; + return; } - return false; + return; } public void indexFields(final Field[] fields) { @@ -415,6 +438,14 @@ public class ClassDescriptor implements GraphElement { return isShown; } + /** + * Register event when another class is extending this one, or is + * implementing interface declared by this class. + */ + public void registerExtension() { + extensionsCount++; + } + public void registerReference() { incomingReferencesCount++; } 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 42ecfcf..5b141e3 100644 --- a/src/main/java/eu/svjatoslav/inspector/java/structure/FieldDescriptor.java +++ b/src/main/java/eu/svjatoslav/inspector/java/structure/FieldDescriptor.java @@ -22,7 +22,7 @@ public class FieldDescriptor implements GraphElement { */ public String name; - public ClassDescriptor type; + private ClassDescriptor type; private ClassDescriptor parent; List typeArguments = new ArrayList(); @@ -131,8 +131,9 @@ public class FieldDescriptor implements GraphElement { if (!isVisible()) return 0; - if (type.isVisible()) - return 1; + if (type != null) + if (type.isVisible()) + return 1; return 0; }