hide "is" getter fields for booleans of the same name
authorSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Wed, 27 Nov 2013 20:19:01 +0000 (22:19 +0200)
committerSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Wed, 27 Nov 2013 20:19:01 +0000 (22:19 +0200)
src/main/java/eu/svjatoslav/inspector/java/structure/ClassDescriptor.java
src/main/java/eu/svjatoslav/inspector/java/structure/FieldDescriptor.java
src/main/java/eu/svjatoslav/inspector/java/structure/MethodDescriptor.java
src/main/java/eu/svjatoslav/inspector/java/structure/Utils.java
src/test/java/eu/svjatoslav/inspector/java/structure/example/RenderDemoClasses.java [new file with mode: 0644]
src/test/java/eu/svjatoslav/inspector/java/structure/example/RenderExampleProject.java [deleted file]

index a424897..e1e45af 100644 (file)
@@ -308,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_"
@@ -347,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
@@ -371,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);
@@ -389,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;
        }
index 5b141e3..db5a112 100644 (file)
@@ -23,13 +23,13 @@ public class FieldDescriptor implements GraphElement {
 
        public String name;
        private ClassDescriptor type;
-       private ClassDescriptor parent;
+       private ClassDescriptor parentClass;
        List<ClassDescriptor> typeArguments = new ArrayList<ClassDescriptor>();
 
        public FieldDescriptor(final Field field, final ClassDescriptor parent,
                        final ClassGraph dump) {
 
-               this.parent = parent;
+               parentClass = parent;
 
                if (!field.getDeclaringClass().getName()
                                .equals(parent.fullyQualifiedName))
@@ -83,10 +83,10 @@ public class FieldDescriptor implements GraphElement {
                // main type
                boolean showLink = type.areReferencesShown();
 
-               if (type == parent)
+               if (type == parentClass)
                        showLink = false;
 
-               if (parent.isEnum)
+               if (parentClass.isEnum)
                        showLink = false;
 
                if (showLink)
@@ -106,7 +106,7 @@ public class FieldDescriptor implements GraphElement {
                final StringBuffer result = new StringBuffer();
 
                result.append("        // " + name + "\n");
-               if (parent.isEnum && (type == parent)) {
+               if (parentClass.isEnum && (type == parentClass)) {
                        result.append("        <TR><TD colspan=\"2\" PORT=\"" + name);
                        result.append("\" ALIGN=\"left\"><FONT POINT-SIZE=\"11.0\">");
                        result.append(name + "</FONT></TD></TR>\n");
@@ -123,7 +123,7 @@ public class FieldDescriptor implements GraphElement {
 
        @Override
        public String getGraphId() {
-               return parent.getGraphId() + ":" + name;
+               return parentClass.getGraphId() + ":" + name;
        }
 
        public int getOutsideVisibleReferencesCount() {
@@ -138,6 +138,10 @@ public class FieldDescriptor implements GraphElement {
                return 0;
        }
 
+       public ClassDescriptor getType() {
+               return type;
+       }
+
        @Override
        public boolean isVisible() {
                if (name.contains("$"))
@@ -148,4 +152,5 @@ public class FieldDescriptor implements GraphElement {
 
                return true;
        }
+
 }
\ No newline at end of file
index 5e03b6b..89a492e 100644 (file)
@@ -16,7 +16,7 @@ import java.util.ArrayList;
 import java.util.List;
 
 public class MethodDescriptor implements GraphElement,
-Comparable<MethodDescriptor> {
+               Comparable<MethodDescriptor> {
 
        /**
         * This class corresponds to single method within a java class.
@@ -24,14 +24,14 @@ Comparable<MethodDescriptor> {
 
        public String name;
        public ClassDescriptor returnType;
-       private final ClassDescriptor parent;
+       private final ClassDescriptor parentClass;
 
-       List<ClassDescriptor> typeArguments = new ArrayList<ClassDescriptor>();
+       List<ClassDescriptor> argumentTypes = new ArrayList<ClassDescriptor>();
 
        public MethodDescriptor(final Method method, final ClassDescriptor parent,
                        final ClassGraph dump) {
 
-               this.parent = parent;
+               parentClass = parent;
 
                name = method.getName();
 
@@ -53,12 +53,22 @@ Comparable<MethodDescriptor> {
                                        final Class cl = (Class) t;
                                        final ClassDescriptor classDescriptor = dump.addClass(cl);
                                        classDescriptor.registerReference();
-                                       typeArguments.add(classDescriptor);
+                                       argumentTypes.add(classDescriptor);
                                }
 
                }
        }
 
+       @Override
+       public int compareTo(final MethodDescriptor o) {
+
+               final int nameComparisonResult = name.compareTo(o.name);
+               if (nameComparisonResult != 0)
+                       return nameComparisonResult;
+
+               return toString().compareTo(o.toString());
+       }
+
        @Override
        public String getDot() {
 
@@ -68,7 +78,7 @@ Comparable<MethodDescriptor> {
                final StringBuffer result = new StringBuffer();
 
                // describe associated types
-               for (final ClassDescriptor classDescriptor : typeArguments)
+               for (final ClassDescriptor classDescriptor : argumentTypes)
                        if (classDescriptor.isVisible())
                                if (classDescriptor.areReferencesShown())
                                        result.append("    " + getGraphId() + " -> "
@@ -109,7 +119,7 @@ Comparable<MethodDescriptor> {
 
        @Override
        public String getGraphId() {
-               return parent.getGraphId() + ":" + name;
+               return parentClass.getGraphId() + ":" + name;
        }
 
        public String getMethodLabel() {
@@ -122,7 +132,7 @@ Comparable<MethodDescriptor> {
                if (returnType.isVisible())
                        result++;
 
-               for (final ClassDescriptor classDescriptor : typeArguments)
+               for (final ClassDescriptor classDescriptor : argumentTypes)
                        if (classDescriptor.isVisible())
                                result++;
 
@@ -132,36 +142,30 @@ Comparable<MethodDescriptor> {
        @Override
        public boolean isVisible() {
 
-               if (Utils.isSystemMethod(name))
+               // hide common object methods
+               if (Utils.isCommonObjectMethod(name))
                        return false;
 
-               if (parent.isEnum && Utils.isEnumMethod(name))
+               // hide common Enumeration methods
+               if (parentClass.isEnum && Utils.isEnumMethod(name))
                        return false;
 
-               if (!(name.startsWith("get") || name.startsWith("set")))
-                       return true;
-
-               final String upprCaseName = name.substring(3).toUpperCase();
-
-               for (String parentField : parent.nameToFieldMap.keySet()) {
-                       parentField = parentField.toUpperCase();
-
-                       if (upprCaseName.equals(parentField))
+               // hide get/set methods for the field of the same name
+               if (name.startsWith("get") || name.startsWith("set"))
+                       if (parentClass.hasFieldIgnoreCase(name.substring(3)))
                                return false;
 
+               // hide is methods for the boolean field of the same name
+               if (name.startsWith("is")) {
+                       final FieldDescriptor field = parentClass.getFieldIgnoreCase(name
+                                       .substring(2));
+                       if (field != null)
+                               if ("boolean".equals(field.getType().fullyQualifiedName))
+                                       return false;
                }
 
                return true;
-       }
-
-       @Override
-       public int compareTo(MethodDescriptor o) {
 
-               int nameComparisonResult = name.compareTo(o.name);
-               if (nameComparisonResult != 0)
-                       return nameComparisonResult;
-
-               return toString().compareTo(o.toString());
        }
 
 }
index 5feae60..428afda 100644 (file)
@@ -16,7 +16,7 @@ public class Utils {
 
        private static final List<String> systemDataTypes = new ArrayList<String>();
 
-       private static final List<String> systemMethods = new ArrayList<String>();
+       private static final List<String> commonObjectMethods = new ArrayList<String>();
 
        private static final List<String> systemPackages = new ArrayList<String>();
 
@@ -35,7 +35,7 @@ public class Utils {
                initSystemDataTypes();
                initDarkColors();
                initLightColors();
-               initSystemMethods();
+               initCommonObjectMethods();
                initSystemPackages();
        }
 
@@ -105,14 +105,14 @@ public class Utils {
                systemDataTypes.add("byte");
        }
 
-       public static void initSystemMethods() {
-               systemMethods.add("wait");
-               systemMethods.add("equals");
-               systemMethods.add("toString");
-               systemMethods.add("hashCode");
-               systemMethods.add("notify");
-               systemMethods.add("notifyAll");
-               systemMethods.add("getClass");
+       public static void initCommonObjectMethods() {
+               commonObjectMethods.add("wait");
+               commonObjectMethods.add("equals");
+               commonObjectMethods.add("toString");
+               commonObjectMethods.add("hashCode");
+               commonObjectMethods.add("notify");
+               commonObjectMethods.add("notifyAll");
+               commonObjectMethods.add("getClass");
        }
 
        public static void initSystemPackages() {
@@ -129,8 +129,8 @@ public class Utils {
                return systemDataTypes.contains(name);
        }
 
-       public static boolean isSystemMethod(final String name) {
-               return systemMethods.contains(name);
+       public static boolean isCommonObjectMethod(final String name) {
+               return commonObjectMethods.contains(name);
        }
 
        public static boolean isSystemPackage(final String name) {
diff --git a/src/test/java/eu/svjatoslav/inspector/java/structure/example/RenderDemoClasses.java b/src/test/java/eu/svjatoslav/inspector/java/structure/example/RenderDemoClasses.java
new file mode 100644 (file)
index 0000000..e21bcc8
--- /dev/null
@@ -0,0 +1,16 @@
+package eu.svjatoslav.inspector.java.structure.example;
+
+import eu.svjatoslav.inspector.java.structure.ClassGraph;
+import eu.svjatoslav.inspector.java.structure.example.structure.SampleClass;
+
+public class RenderDemoClasses {
+
+       public static void main(final String[] args) {
+               final ClassGraph graph = new ClassGraph();
+
+               graph.addClass(SampleClass.class);
+
+               graph.generateGraph("example");
+       }
+
+}
diff --git a/src/test/java/eu/svjatoslav/inspector/java/structure/example/RenderExampleProject.java b/src/test/java/eu/svjatoslav/inspector/java/structure/example/RenderExampleProject.java
deleted file mode 100644 (file)
index 028c775..0000000
+++ /dev/null
@@ -1,26 +0,0 @@
-/*
- * JavaInspect - Utility to visualize java software
- * Copyright (C) 2013, 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.
- */
-
-package eu.svjatoslav.inspector.java.structure.example;
-
-import eu.svjatoslav.inspector.java.structure.ClassGraph;
-import eu.svjatoslav.inspector.java.structure.example.structure.SampleClass;
-
-public class RenderExampleProject {
-
-       public static void main(final String[] args) {
-               final ClassGraph graph = new ClassGraph();
-
-               graph.addClass(SampleClass.class);
-
-               graph.generateGraph("example");
-
-       }
-
-}