X-Git-Url: http://www2.svjatoslav.eu/gitweb/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Feu%2Fsvjatoslav%2Finspector%2Fjava%2Fstructure%2FClassDescriptor.java;h=9f657d1d76e7d2173945d4dda3e8aefb2c43fab0;hb=ffba53aac03e4d107545116ddcfff0c965bd5970;hp=a424897ea65d788e385b9883efcf0b7538e168db;hpb=d140a5c445361927abe4fe3a90e6ce95cd6530c3;p=javainspect.git diff --git a/src/main/java/eu/svjatoslav/inspector/java/structure/ClassDescriptor.java b/src/main/java/eu/svjatoslav/inspector/java/structure/ClassDescriptor.java old mode 100644 new mode 100755 index a424897..9f657d1 --- a/src/main/java/eu/svjatoslav/inspector/java/structure/ClassDescriptor.java +++ b/src/main/java/eu/svjatoslav/inspector/java/structure/ClassDescriptor.java @@ -1,10 +1,10 @@ /* * JavaInspect - Utility to visualize java software - * Copyright (C) 2013, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu - * + * Copyright (C) 2013-2015, 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; @@ -25,11 +25,11 @@ public class ClassDescriptor implements GraphElement { private static final int MAX_REFERECNES_COUNT = 10; - public final String fullyQualifiedName; + private String fullyQualifiedName; - Map nameToFieldMap = new TreeMap(); + private final Map nameToFieldMap = new TreeMap(); - public SortedSet methods = new TreeSet(); + private final SortedSet methods = new TreeSet(); /** * Incoming arrows will have this color. @@ -65,18 +65,22 @@ public class ClassDescriptor implements GraphElement { // counts amount of times this class is extended private int extensionsCount = 0; - public ClassDescriptor(final Class clazz, - final ClassGraph dump) { - classGraph = dump; + private ClassDescriptor arrayComponent; + + public ClassDescriptor(final ClassGraph classGraph) { + this.classGraph = classGraph; + } + + protected void analyzeClass(final Class clazz) { fullyQualifiedName = clazz.getName(); - dump.nameToClassMap.put(fullyQualifiedName, this); isArray = clazz.isArray(); if (isArray) { final Class componentType = clazz.getComponentType(); - dump.addClass(componentType); + arrayComponent = getClassGraph().getOrCreateClassDescriptor( + componentType); } // System.out.println("class: " + fullyQualifiedName); @@ -94,23 +98,24 @@ public class ClassDescriptor implements GraphElement { indexMethods(clazz); for (final Class interfaceClass : clazz.getInterfaces()) { - final ClassDescriptor classDescriptor = dump - .addClass(interfaceClass); - classDescriptor.registerImplementation(); - interfaces.add(classDescriptor); + final ClassDescriptor interfaceClassDescriptor = getClassGraph() + .getOrCreateClassDescriptor(interfaceClass); + interfaceClassDescriptor.registerImplementation(); + interfaces.add(interfaceClassDescriptor); } - superClass = dump.addClass(clazz.getSuperclass()); + superClass = getClassGraph().getOrCreateClassDescriptor( + clazz.getSuperclass()); if (superClass != null) superClass.registerExtension(); - } + }; - public boolean areReferencesShown() { + protected boolean areReferencesShown() { return referencesCount <= MAX_REFERECNES_COUNT; } - public void enlistFieldReferences(final StringBuffer result) { + private void enlistFieldReferences(final StringBuffer result) { if (nameToFieldMap.isEmpty()) return; @@ -121,7 +126,7 @@ public class ClassDescriptor implements GraphElement { result.append(entry.getValue().getDot()); } - public void enlistFields(final StringBuffer result) { + private void enlistFields(final StringBuffer result) { if (nameToFieldMap.isEmpty()) return; @@ -134,7 +139,7 @@ public class ClassDescriptor implements GraphElement { result.append(entry.getValue().getEmbeddedDot()); } - public void enlistImplementedInterfaces(final StringBuffer result) { + private void enlistImplementedInterfaces(final StringBuffer result) { if (interfaces.isEmpty()) return; @@ -146,14 +151,17 @@ public class ClassDescriptor implements GraphElement { if (!interfaceDescriptor.isVisible()) continue; + if (!interfaceDescriptor.areReferencesShown()) + continue; + result.append(" " + interfaceDescriptor.getGraphId() + " -> " - + getGraphId() + "[style=\"dotted, tapered\", color=\"" + + getGraphId() + "[style=\"dotted\", color=\"" + interfaceDescriptor.getInterfaceColor() - + "\", penwidth=20, dir=\"forward\"];\n"); + + "\", penwidth=10, dir=\"forward\"];\n"); } } - public void enlistMethodReferences(final StringBuffer result) { + private void enlistMethodReferences(final StringBuffer result) { if (methods.isEmpty()) return; @@ -163,7 +171,7 @@ public class ClassDescriptor implements GraphElement { result.append(methodDescriptor.getDot()); } - public void enlistMethods(final StringBuffer result) { + private void enlistMethods(final StringBuffer result) { if (methods.isEmpty()) return; @@ -175,23 +183,25 @@ public class ClassDescriptor implements GraphElement { result.append(methodDescriptor.getEmbeddedDot()); } - public void enlistSuperClass(final StringBuffer result) { + private void enlistSuperClass(final StringBuffer result) { if (superClass == null) return; if (!superClass.isVisible()) return; + if (!superClass.areReferencesShown()) + return; + result.append("\n"); result.append(" // super class for: " + fullyQualifiedName + "\n"); result.append(" " + superClass.getGraphId() + " -> " + getGraphId() - + "[style=\"tapered\", color=\"" - + superClass.getSuperClassColor() + + "[ color=\"" + superClass.getSuperClassColor() + "\", penwidth=10, dir=\"forward\"];\n"); } - public void generateDotHeader(final StringBuffer result) { + private void generateDotHeader(final StringBuffer result) { result.append("\n"); result.append("// Class: " + fullyQualifiedName + "\n"); @@ -214,17 +224,7 @@ public class ClassDescriptor implements GraphElement { + "" + "\n"); } - public List getAllFields() { - final List result = new ArrayList(); - - for (final Map.Entry entry : nameToFieldMap - .entrySet()) - result.add(entry.getValue()); - - return result; - } - - public String getBackgroundColor() { + private String getBackgroundColor() { String bgColor = ""; if (isEnum) @@ -236,23 +236,33 @@ public class ClassDescriptor implements GraphElement { return bgColor; } - public String getBorderWidth() { + private String getBorderWidth() { if (!areReferencesShown()) return "4"; return "1"; } - public String getClassName(final boolean differentiateArray) { + protected ClassGraph getClassGraph() { + return classGraph; + } + + protected String getClassName(final boolean differentiateArray) { // this is needed for nested classes final String actualClassName = fullyQualifiedName.replace('$', '.'); - final int i = actualClassName.lastIndexOf('.'); - - String result = actualClassName.substring(i + 1); - - if (isArray) - result = result.substring(0, result.length() - 1); + String result; + if (isArray) { + // for arrays use array component instead of array class name + result = arrayComponent.fullyQualifiedName; + if (result.contains(".")) { + final int i = result.lastIndexOf('.'); + result = result.substring(i + 1); + } + } else { + final int i = actualClassName.lastIndexOf('.'); + result = actualClassName.substring(i + 1); + } if (differentiateArray) if (isArray) @@ -263,14 +273,10 @@ public class ClassDescriptor implements GraphElement { return result; } - public String getColor() { - if (getDistinctiveColor() == null) - setDistinctiveColor(Utils.getNextDarkColor()); - - return getDistinctiveColor(); - } + protected String getColor() { + if (distinctiveReferenceColor == null) + distinctiveReferenceColor = Utils.getNextDarkColor(); - public String getDistinctiveColor() { return distinctiveReferenceColor; } @@ -308,6 +314,23 @@ public class ClassDescriptor implements GraphElement { return null; } + /** + * Returns field with given name (case is ignored). Or null if + * field is not found. + */ + protected FieldDescriptor getFieldIgnoreCase(final String fieldToSearch) { + + for (final String fieldName : nameToFieldMap.keySet()) + if (fieldToSearch.equalsIgnoreCase(fieldName)) + return nameToFieldMap.get(fieldName); + + return null; + } + + protected String getFullyQualifiedName() { + return fullyQualifiedName; + } + @Override public String getGraphId() { final String result = "class_" @@ -316,13 +339,28 @@ public class ClassDescriptor implements GraphElement { return result; } - public String getInterfaceColor() { + private String getInterfaceColor() { if (interfaceColor == null) - interfaceColor = Utils.getNextLightColor(); + interfaceColor = Utils.getNextDarkColor(); return interfaceColor; } + private FieldDescriptor getOrCreateFieldDescriptor(final Field field) { + + final String fieldName = field.getName(); + + if (nameToFieldMap.containsKey(fieldName)) + return nameToFieldMap.get(fieldName); + + final FieldDescriptor newFieldDescriptor = new FieldDescriptor(this); + nameToFieldMap.put(fieldName, newFieldDescriptor); + + newFieldDescriptor.analyzeField(field); + + return newFieldDescriptor; + } + private int getOutgoingReferencesCount() { int result = 0; @@ -347,7 +385,7 @@ public class ClassDescriptor implements GraphElement { return result; } - public String getPackageName() { + private String getPackageName() { final int i = fullyQualifiedName.lastIndexOf('.'); @@ -357,21 +395,7 @@ public class ClassDescriptor implements GraphElement { return fullyQualifiedName.substring(0, i).replace("[L", ""); } - // public String getReadableName() { - // - // // do not print full class name for well known system classes - // final String packageName = getPackageName(); - // - // if (packageName.equals("java.util")) - // return getClassName(); - // - // if (packageName.equals("java.lang")) - // return getClassName(); - // - // return fullyQualifiedName; - // } - - public String getParentClassesName() { + private String getParentClassesName() { int i = fullyQualifiedName.lastIndexOf('.'); final String fullClassName = fullyQualifiedName.substring(i + 1); @@ -382,18 +406,31 @@ public class ClassDescriptor implements GraphElement { return parentClassesName.replace('$', '.'); } - public String getSuperClassColor() { + private String getSuperClassColor() { if (superClassColor == null) superClassColor = Utils.getNextLightColor(); return superClassColor; } - public void hide() { + /** + * Checks if class has field with given name (case is ignored). Returns + * true if such field is found. + */ + protected boolean hasFieldIgnoreCase(final String fieldToSearch) { + + for (final String fieldName : nameToFieldMap.keySet()) + if (fieldToSearch.equalsIgnoreCase(fieldName)) + return true; + + return false; + } + + private void hide() { isShown = false; } - public void hideClassIfNoReferences() { + protected void hideClassIfNoReferences() { if (!isVisible()) return; @@ -408,22 +445,20 @@ public class ClassDescriptor implements GraphElement { return; } - public void indexFields(final Field[] fields) { - for (final Field field : fields) { - if (nameToFieldMap.containsKey(field.getName())) - continue; - - final FieldDescriptor fieldDescriptor = new FieldDescriptor(field, - this, classGraph); - - } + private void indexFields(final Field[] fields) { + for (final Field field : fields) + getOrCreateFieldDescriptor(field); } private void indexMethods(final Class clazz) { - final Method[] methods = clazz.getMethods(); + for (final Method method : clazz.getMethods()) { + final MethodDescriptor methodDescriptor = new MethodDescriptor( + this, method.getName()); + + methods.add(methodDescriptor); - for (final Method method : methods) - new MethodDescriptor(method, this, classGraph); + methodDescriptor.analyze(method); + } } @@ -436,28 +471,33 @@ public class ClassDescriptor implements GraphElement { if (Utils.isSystemPackage(fullyQualifiedName)) return false; - if (!classGraph.getFilter().isClassShown(fullyQualifiedName)) + if (!getClassGraph().isClassShown(fullyQualifiedName)) return false; + if (isArray) + if (arrayComponent != null) + if (Utils.isSystemDataType(arrayComponent.fullyQualifiedName)) + // Do not show references to primitive data types in arrays. + // That is: there is no point to show reference to byte when + // we have class with byte array field. + return false; + return isShown; } /** * Register event when another class is extending this one. */ - public void registerExtension() { + protected void registerExtension() { extensionsCount++; } - public void registerImplementation() { + protected void registerImplementation() { implementationsCount++; } - public void registerReference() { + protected void registerReference() { referencesCount++; } - public void setDistinctiveColor(final String distinctiveColor) { - distinctiveReferenceColor = distinctiveColor; - } }