Changed license to LGPLv3.
[javainspect.git] / src / main / java / eu / svjatoslav / inspector / java / structure / ClassDescriptor.java
old mode 100644 (file)
new mode 100755 (executable)
index eae630b..2247a77
@@ -1,10 +1,10 @@
 /*
  * JavaInspect - Utility to visualize java software
- * Copyright (C) 2013, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
+ * Copyright (C) 2013-2014, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
  * 
  * This program is free software; you can redistribute it and/or
- * modify it under the terms of version 2 of the GNU General Public License
- * as published by the Free Software Foundation.
+ * modify it under the terms of version 3 of the GNU Lesser General Public License
+ * or later as published by the Free Software Foundation.
  */
 
 package eu.svjatoslav.inspector.java.structure;
@@ -57,8 +57,12 @@ public class ClassDescriptor implements GraphElement {
        /**
         * Amount of field and method references pointing to this class.
         */
-       private int incomingReferencesCount = 0;
+       private int referencesCount = 0;
 
+       // for interface, counts amount of found implementations
+       private int implementationsCount = 0;
+
+       // counts amount of times this class is extended
        private int extensionsCount = 0;
 
        public ClassDescriptor(final Class<? extends Object> clazz,
@@ -92,7 +96,7 @@ public class ClassDescriptor implements GraphElement {
                for (final Class interfaceClass : clazz.getInterfaces()) {
                        final ClassDescriptor classDescriptor = dump
                                        .addClass(interfaceClass);
-                       classDescriptor.registerExtension();
+                       classDescriptor.registerImplementation();
                        interfaces.add(classDescriptor);
                }
 
@@ -103,7 +107,7 @@ public class ClassDescriptor implements GraphElement {
        }
 
        public boolean areReferencesShown() {
-               return incomingReferencesCount <= MAX_REFERECNES_COUNT;
+               return referencesCount <= MAX_REFERECNES_COUNT;
        }
 
        public void enlistFieldReferences(final StringBuffer result) {
@@ -304,6 +308,19 @@ public class ClassDescriptor implements GraphElement {
                return null;
        }
 
+       /**
+        * Returns field with given name (case is ignored). Or <code>null</code> if
+        * field is not found.
+        */
+       public FieldDescriptor getFieldIgnoreCase(final String fieldToSearch) {
+
+               for (final String fieldName : nameToFieldMap.keySet())
+                       if (fieldToSearch.equalsIgnoreCase(fieldName))
+                               return nameToFieldMap.get(fieldName);
+
+               return null;
+       }
+
        @Override
        public String getGraphId() {
                final String result = "class_"
@@ -343,16 +360,6 @@ public class ClassDescriptor implements GraphElement {
                return result;
        }
 
-       public String getPackageName() {
-
-               final int i = fullyQualifiedName.lastIndexOf('.');
-
-               if (i == -1)
-                       return "";
-
-               return fullyQualifiedName.substring(0, i).replace("[L", "");
-       }
-
        // public String getReadableName() {
        //
        // // do not print full class name for well known system classes
@@ -367,6 +374,16 @@ public class ClassDescriptor implements GraphElement {
        // return fullyQualifiedName;
        // }
 
+       public String getPackageName() {
+
+               final int i = fullyQualifiedName.lastIndexOf('.');
+
+               if (i == -1)
+                       return "";
+
+               return fullyQualifiedName.substring(0, i).replace("[L", "");
+       }
+
        public String getParentClassesName() {
                int i = fullyQualifiedName.lastIndexOf('.');
                final String fullClassName = fullyQualifiedName.substring(i + 1);
@@ -385,6 +402,19 @@ public class ClassDescriptor implements GraphElement {
                return superClassColor;
        }
 
+       /**
+        * Checks if class has field with given name (case is ignored). Returns
+        * <code>true</code> if such field is found.
+        */
+       public boolean hasFieldIgnoreCase(final String fieldToSearch) {
+
+               for (final String fieldName : nameToFieldMap.keySet())
+                       if (fieldToSearch.equalsIgnoreCase(fieldName))
+                               return true;
+
+               return false;
+       }
+
        public void hide() {
                isShown = false;
        }
@@ -394,7 +424,7 @@ public class ClassDescriptor implements GraphElement {
                        return;
 
                final int totalReferencesCount = getOutgoingReferencesCount()
-                               + incomingReferencesCount + extensionsCount;
+                               + referencesCount + extensionsCount + implementationsCount;
 
                if (totalReferencesCount == 0) {
                        hide();
@@ -439,15 +469,18 @@ public class ClassDescriptor implements GraphElement {
        }
 
        /**
-        * Register event when another class is extending this one, or is
-        * implementing interface declared by this class.
+        * Register event when another class is extending this one.
         */
        public void registerExtension() {
                extensionsCount++;
        }
 
+       public void registerImplementation() {
+               implementationsCount++;
+       }
+
        public void registerReference() {
-               incomingReferencesCount++;
+               referencesCount++;
        }
 
        public void setDistinctiveColor(final String distinctiveColor) {