From: Svjatoslav Agejenko Date: Wed, 27 Nov 2013 20:19:01 +0000 (+0200) Subject: hide "is" getter fields for booleans of the same name X-Git-Tag: javainspect-1.3~4 X-Git-Url: http://www2.svjatoslav.eu/gitweb/?a=commitdiff_plain;h=ff4200b065c2eb7c8448fa060e7610ab57f6aec1;p=javainspect.git hide "is" getter fields for booleans of the same name --- 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 a424897..e1e45af 100644 --- a/src/main/java/eu/svjatoslav/inspector/java/structure/ClassDescriptor.java +++ b/src/main/java/eu/svjatoslav/inspector/java/structure/ClassDescriptor.java @@ -308,6 +308,19 @@ public class ClassDescriptor implements GraphElement { return null; } + /** + * Returns field with given name (case is ignored). Or null 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 + * true 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; } 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 5b141e3..db5a112 100644 --- a/src/main/java/eu/svjatoslav/inspector/java/structure/FieldDescriptor.java +++ b/src/main/java/eu/svjatoslav/inspector/java/structure/FieldDescriptor.java @@ -23,13 +23,13 @@ public class FieldDescriptor implements GraphElement { public String name; private ClassDescriptor type; - private ClassDescriptor parent; + private ClassDescriptor parentClass; List typeArguments = new ArrayList(); 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(" "); result.append(name + "\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 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 5e03b6b..89a492e 100644 --- a/src/main/java/eu/svjatoslav/inspector/java/structure/MethodDescriptor.java +++ b/src/main/java/eu/svjatoslav/inspector/java/structure/MethodDescriptor.java @@ -16,7 +16,7 @@ import java.util.ArrayList; import java.util.List; public class MethodDescriptor implements GraphElement, -Comparable { + Comparable { /** * This class corresponds to single method within a java class. @@ -24,14 +24,14 @@ Comparable { public String name; public ClassDescriptor returnType; - private final ClassDescriptor parent; + private final ClassDescriptor parentClass; - List typeArguments = new ArrayList(); + List argumentTypes = new ArrayList(); public MethodDescriptor(final Method method, final ClassDescriptor parent, final ClassGraph dump) { - this.parent = parent; + parentClass = parent; name = method.getName(); @@ -53,12 +53,22 @@ Comparable { 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 { 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 { @Override public String getGraphId() { - return parent.getGraphId() + ":" + name; + return parentClass.getGraphId() + ":" + name; } public String getMethodLabel() { @@ -122,7 +132,7 @@ Comparable { if (returnType.isVisible()) result++; - for (final ClassDescriptor classDescriptor : typeArguments) + for (final ClassDescriptor classDescriptor : argumentTypes) if (classDescriptor.isVisible()) result++; @@ -132,36 +142,30 @@ Comparable { @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()); } } diff --git a/src/main/java/eu/svjatoslav/inspector/java/structure/Utils.java b/src/main/java/eu/svjatoslav/inspector/java/structure/Utils.java index 5feae60..428afda 100644 --- a/src/main/java/eu/svjatoslav/inspector/java/structure/Utils.java +++ b/src/main/java/eu/svjatoslav/inspector/java/structure/Utils.java @@ -16,7 +16,7 @@ public class Utils { private static final List systemDataTypes = new ArrayList(); - private static final List systemMethods = new ArrayList(); + private static final List commonObjectMethods = new ArrayList(); private static final List systemPackages = new ArrayList(); @@ -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 index 0000000..e21bcc8 --- /dev/null +++ b/src/test/java/eu/svjatoslav/inspector/java/structure/example/RenderDemoClasses.java @@ -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 index 028c775..0000000 --- a/src/test/java/eu/svjatoslav/inspector/java/structure/example/RenderExampleProject.java +++ /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"); - - } - -}