X-Git-Url: http://www2.svjatoslav.eu/gitweb/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Feu%2Fsvjatoslav%2Finspector%2Fjava%2Fstructure%2FClassDescriptor.java;h=b480f0b424548bd04f891059412246a17b9051d5;hb=fb953cf51bdc2bb20ba37c29ea22e26f283aa48e;hp=41f92f3f69c0ea3dbee5dc9bb35e7ffcfc5748ec;hpb=b7e94db7e5f68ae3e6130584d1ec3b4dd9677394;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 41f92f3..b480f0b --- a/src/main/java/eu/svjatoslav/inspector/java/structure/ClassDescriptor.java +++ b/src/main/java/eu/svjatoslav/inspector/java/structure/ClassDescriptor.java @@ -1,395 +1,518 @@ /* * JavaInspect - Utility to visualize java software - * Copyright (C) 2013, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu - * + * Copyright (C) 2013-2019, 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; import java.lang.reflect.Field; import java.lang.reflect.Method; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; /** * Describes single class instance */ -public class ClassDescriptor implements GraphElement { +public class ClassDescriptor implements GraphElement, Comparable { + + private static final int MAX_REFERECNES_COUNT = 10; + final List interfaces = new ArrayList<>(); + private final Map nameToFieldMap = new TreeMap<>(); + private final SortedSet methods = new TreeSet<>(); + private final ClassGraph classGraph; + boolean isEnum; + boolean isInterface; + boolean isArray; + ClassDescriptor superClass; + private String fullyQualifiedName; + /** + * Incoming arrows will have this color. + */ + private String distinctiveReferenceColor; + private String interfaceColor; + private String superClassColor; + private boolean isShown = true; + /** + * Amount of field and method references pointing to this class. + */ + 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; + + private ClassDescriptor arrayComponent; + + public ClassDescriptor(final ClassGraph classGraph) { + this.classGraph = classGraph; + } + + protected void analyzeClass(final Class clazz) { + + fullyQualifiedName = clazz.getName(); + + isArray = clazz.isArray(); + + if (isArray) { + final Class componentType = clazz.getComponentType(); + arrayComponent = getClassGraph().getOrCreateClassDescriptor( + componentType); + } + + // System.out.println("class: " + fullyQualifiedName); + + isEnum = clazz.isEnum(); + + isInterface = clazz.isInterface(); + + if (!isVisible()) + return; + + try { + indexFields(clazz.getDeclaredFields()); + indexFields(clazz.getFields()); + } catch (NoClassDefFoundError error) { + // TODO: better logging of this error + System.out.println(error.toString()); + } + + indexMethods(clazz); + + for (final Class interfaceClass : clazz.getInterfaces()) { + final ClassDescriptor interfaceClassDescriptor = getClassGraph() + .getOrCreateClassDescriptor(interfaceClass); + interfaceClassDescriptor.registerImplementation(); + interfaces.add(interfaceClassDescriptor); + } + + superClass = getClassGraph().getOrCreateClassDescriptor( + clazz.getSuperclass()); + if (superClass != null) + superClass.registerExtension(); + + } + + protected boolean areReferencesShown() { + return referencesCount <= MAX_REFERECNES_COUNT; + } + + private void enlistFieldReferences(final StringBuffer result) { + if (nameToFieldMap.isEmpty()) + return; + + result.append("\n"); + result.append(" // field references to other classes\n"); + nameToFieldMap.forEach((fieldName, field) -> result.append(field.getDot())); + } + + private void enlistFields(final StringBuffer result) { + if (nameToFieldMap.isEmpty()) + return; + + result.append("\n"); + result.append(" // fields:\n"); + + // enlist fields + for (final Map.Entry entry : nameToFieldMap + .entrySet()) + result.append(entry.getValue().getEmbeddedDot()); + } + + private void enlistImplementedInterfaces(final StringBuffer result) { + if (interfaces.isEmpty()) + return; - private static final int MAX_REFERECNES_COUNT = 10; + result.append("\n"); + result.append(" // interfaces implemented by class: " + + fullyQualifiedName + "\n"); - public final String fullyQualifiedName; + for (final ClassDescriptor interfaceDescriptor : interfaces) { + if (!interfaceDescriptor.isVisible()) + continue; - Map nameToFieldMap = new HashMap(); + if (!interfaceDescriptor.areReferencesShown()) + continue; - public List methods = new ArrayList(); + result.append(" " + interfaceDescriptor.getGraphId() + " -> " + + getGraphId() + "[style=\"dotted\", color=\"" + + interfaceDescriptor.getInterfaceColor() + + "\", penwidth=10, dir=\"forward\"];\n"); + } + } - /** - * Incoming arrows will have this color. - */ - private String distinctiveReferenceColor; + private void enlistMethodReferences(final StringBuffer result) { + if (methods.isEmpty()) + return; + + result.append("\n"); + result.append(" // method references to other classes\n"); + for (final MethodDescriptor methodDescriptor : methods) + result.append(methodDescriptor.getDot()); + } + + private void enlistMethods(final StringBuffer result) { + if (methods.isEmpty()) + return; + + result.append("\n"); + result.append(" // methods:\n"); + + // enlist methods + for (final MethodDescriptor methodDescriptor : methods) + result.append(methodDescriptor.getEmbeddedDot()); + } + + 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"); - private String interfaceColor; + result.append(" " + superClass.getGraphId() + " -> " + getGraphId() + + "[ color=\"" + superClass.getSuperClassColor() + + "\", penwidth=10, dir=\"forward\"];\n"); + } - private String superClassColor; + private void generateDotHeader(final StringBuffer result) { + result.append("\n"); + result.append("// Class: " + fullyQualifiedName + "\n"); - boolean isEnum; + result.append(" " + getGraphId() + "[label=<\n"); - boolean isInterface; + result.append("\n"); + result.append(" // class descriptor header\n"); + result.append(" \n"); + } - private final ClassGraph dump; + private String getBackgroundColor() { + String bgColor = ""; - List interfaces = new ArrayList(); + if (isEnum) + bgColor = "bgcolor=\"navajowhite2\""; - ClassDescriptor superClass; + if (isInterface) + bgColor = "bgcolor=\"darkslategray1\""; - /** - * Amount of field and method references pointing to this class. - */ - private int referenceCount = 0; + return bgColor; + } - public ClassDescriptor(final Class clazz, - final ClassGraph dump) { - this.dump = dump; + private String getBorderWidth() { - fullyQualifiedName = clazz.getName(); - dump.nameToClassMap.put(fullyQualifiedName, this); + if (!areReferencesShown()) + return "4"; + return "1"; + } - isArray = clazz.isArray(); + protected ClassGraph getClassGraph() { + return classGraph; + } - if (isArray) { - final Class componentType = clazz.getComponentType(); - dump.addClass(componentType); - } + protected String getClassName(final boolean differentiateArray) { + // this is needed for nested classes + final String actualClassName = fullyQualifiedName.replace('$', '.'); - // System.out.println("class: " + fullyQualifiedName); + 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); + } - isEnum = clazz.isEnum(); + if (differentiateArray) + if (isArray) + result += " []"; - isInterface = clazz.isInterface(); + // this is needed for nested classes + // result = result.replace('$', '.'); + return result; + } - if (!isVisible()) - return; + protected String getColor() { + if (distinctiveReferenceColor == null) + distinctiveReferenceColor = Utils.getNextDarkColor(); - indexFields(clazz.getDeclaredFields()); - indexFields(clazz.getFields()); + return distinctiveReferenceColor; + } - indexMethods(clazz); + @Override + public String getDot() { + if (!isVisible()) + return ""; - for (final Class interfaceClass : clazz.getInterfaces()) - interfaces.add(dump.addClass(interfaceClass)); + if (isArray) + return ""; - superClass = dump.addClass(clazz.getSuperclass()); - } + final StringBuffer result = new StringBuffer(); - public boolean areReferencesShown() { - return referenceCount <= MAX_REFERECNES_COUNT; - } + generateDotHeader(result); - public void enlistFieldReferences(final StringBuffer result) { - if (nameToFieldMap.isEmpty()) - return; + enlistFields(result); - result.append("\n"); - result.append(" // field references to other classes\n"); - for (final Map.Entry entry : nameToFieldMap - .entrySet()) - result.append(entry.getValue().getDot()); - } + enlistMethods(result); - public void enlistFields(final StringBuffer result) { - if (nameToFieldMap.isEmpty()) - return; + result.append("
" + + "" + getPackageName() + + "
"); - boolean isArray; + final String parentClassesName = getParentClassesName(); + if (parentClassesName.length() > 0) + result.append("" + parentClassesName + + "
\n"); - private boolean isShown = true; + result.append("" + getClassName(false) + + "" + "
>, shape=\"none\"];\n"); - result.append("\n"); - result.append(" // fields:\n"); + enlistFieldReferences(result); - // enlist fields - for (final Map.Entry entry : nameToFieldMap - .entrySet()) - result.append(entry.getValue().getEmbeddedDot()); - } + enlistMethodReferences(result); - public void enlistImplementedInterfaces(final StringBuffer result) { - if (interfaces.isEmpty()) - return; + enlistImplementedInterfaces(result); - result.append("\n"); - result.append(" // interfaces implemented by class: " - + fullyQualifiedName + "\n"); + enlistSuperClass(result); - for (final ClassDescriptor interfaceDescriptor : interfaces) { - if (!interfaceDescriptor.isVisible()) - continue; + return result.toString(); + } - result.append(" " + interfaceDescriptor.getGraphId() + " -> " - + getGraphId() + "[style=\"dotted, tapered\", color=\"" - + interfaceDescriptor.getInterfaceColor() - + "\", penwidth=20, dir=\"forward\"];\n"); - } - } + @Override + public String getEmbeddedDot() { + return null; + } - public void enlistMethodReferences(final StringBuffer result) { - if (methods.isEmpty()) - return; + /** + * Returns field with given name (case is ignored). Or null if + * field is not found. + * + * @param fieldToSearch field name (case is ignored) + * @return field matching given name + */ + protected FieldDescriptor getFieldIgnoreCase(final String fieldToSearch) { - result.append("\n"); - result.append(" // method references to other classes\n"); - for (final MethodDescriptor methodDescriptor : methods) - result.append(methodDescriptor.getDot()); - } + for (final String fieldName : nameToFieldMap.keySet()) + if (fieldToSearch.equalsIgnoreCase(fieldName)) + return nameToFieldMap.get(fieldName); - public void enlistMethods(final StringBuffer result) { - if (methods.isEmpty()) - return; + return null; + } - result.append("\n"); - result.append(" // methods:\n"); + protected String getFullyQualifiedName() { + return fullyQualifiedName; + } - // enlist methods - for (final MethodDescriptor methodDescriptor : methods) - result.append(methodDescriptor.getEmbeddedDot()); - } + @Override + public String getGraphId() { - public void enlistSuperClass(final StringBuffer result) { - if (superClass == null) - return; + return "class_" + + fullyQualifiedName + .replace('.', '_') + .replace(";", "") + .replace("[[", "") + .replace("[L", "") + .replace("[[L", "") // array of arrays + .replace("[[[L", "") // array of arrays of arrays + .replace('$', '_'); + } - if (!superClass.isVisible()) - return; + private String getInterfaceColor() { + if (interfaceColor == null) + interfaceColor = Utils.getNextDarkColor(); - result.append("\n"); - result.append(" // super class for: " + fullyQualifiedName + "\n"); + return interfaceColor; + } - result.append(" " + superClass.getGraphId() + " -> " + getGraphId() - + "[style=\"tapered\", color=\"" - + superClass.getSuperClassColor() - + "\", penwidth=10, dir=\"forward\"];\n"); - } + private FieldDescriptor getOrCreateFieldDescriptor(final Field field) { - public void generateDotHeader(final StringBuffer result) { - result.append("\n"); - result.append("// Class: " + fullyQualifiedName + "\n"); + final String fieldName = field.getName(); - result.append(" " + getGraphId() + "[label=<\n"); + if (nameToFieldMap.containsKey(fieldName)) + return nameToFieldMap.get(fieldName); - result.append("\n"); - result.append(" // class descriptor header\n"); - result.append(" \n"); - } + return newFieldDescriptor; + } - public List getAllFields() { - final List result = new ArrayList(); + private int getOutgoingReferencesCount() { + int result = 0; - for (final Map.Entry entry : nameToFieldMap - .entrySet()) - result.add(entry.getValue()); + // count method references + for (final MethodDescriptor methodDescriptor : methods) + result += methodDescriptor.getOutsideVisibleReferencesCount(); - return result; - } + // count field references + for (final FieldDescriptor fieldDescriptor : nameToFieldMap.values()) + result += fieldDescriptor.getOutsideVisibleReferencesCount(); - public String getBackgroundColor() { - String bgColor = ""; + // count implemented interfaces + for (final ClassDescriptor classDescriptor : interfaces) + if (classDescriptor.isVisible()) + result++; - if (isEnum) - bgColor = "bgcolor=\"navajowhite2\""; + // count superclass + if (superClass != null) + if (superClass.isVisible()) + result++; - if (isInterface) - bgColor = "bgcolor=\"darkslategray1\""; + return result; + } - return bgColor; - } + private String getPackageName() { - public String getBorderWidth() { + final int i = fullyQualifiedName.lastIndexOf('.'); - if (!areReferencesShown()) - return "4"; - return "1"; - } + if (i == -1) + return ""; - public String getClassName(final boolean differentiateArray) { - // this is needed for nested classes - final String actualClassName = fullyQualifiedName.replace('$', '.'); + return fullyQualifiedName.substring(0, i).replace("[L", ""); + } - final int i = actualClassName.lastIndexOf('.'); + private String getParentClassesName() { + int i = fullyQualifiedName.lastIndexOf('.'); + final String fullClassName = fullyQualifiedName.substring(i + 1); - String result = actualClassName.substring(i + 1); + i = fullClassName.lastIndexOf('$'); + if (i == -1) + return ""; + final String parentClassesName = fullClassName.substring(0, i); + return parentClassesName.replace('$', '.'); + } - if (isArray) - result = result.substring(0, result.length() - 1); + private String getSuperClassColor() { + if (superClassColor == null) + superClassColor = Utils.getNextLightColor(); - if (differentiateArray) - if (isArray) - result += " []"; + return superClassColor; + } - // this is needed for nested classes - // result = result.replace('$', '.'); - return result; - } + /** + * Checks if class has field with given name (case is ignored). Returns + * true if such field is found. + * + * @param fieldToSearch field to search for (case is ignored) + * @return true if field is found. + */ + protected boolean hasFieldIgnoreCase(final String fieldToSearch) { - public String getColor() { - if (getDistinctiveColor() == null) - setDistinctiveColor(Utils.getNextDarkColor()); + for (final String fieldName : nameToFieldMap.keySet()) + if (fieldToSearch.equalsIgnoreCase(fieldName)) + return true; - return getDistinctiveColor(); - } + return false; + } - public String getDistinctiveColor() { - return distinctiveReferenceColor; - } + private void hide() { + isShown = false; + } - @Override - public String getDot() { - if (!isVisible()) - return ""; + protected void hideClassIfNoReferences() { + if (!isVisible()) + return; - if (isArray) - return ""; + final int totalReferencesCount = getOutgoingReferencesCount() + + referencesCount + extensionsCount + implementationsCount; - final StringBuffer result = new StringBuffer(); + if (totalReferencesCount == 0) { + hide(); + return; + } - generateDotHeader(result); + } - enlistFields(result); + private void indexFields(final Field[] fields) { + for (final Field field : fields) + getOrCreateFieldDescriptor(field); + } - enlistMethods(result); + private void indexMethods(final Class clazz) { + for (final Method method : clazz.getMethods()) { + final MethodDescriptor methodDescriptor = new MethodDescriptor( + this, method.getName()); - result.append("
" - + "" + getPackageName() - + "
"); + final FieldDescriptor newFieldDescriptor = new FieldDescriptor(this); + nameToFieldMap.put(fieldName, newFieldDescriptor); - final String parentClassesName = getParentClassesName(); - if (parentClassesName.length() > 0) - result.append("" + parentClassesName - + "
\n"); + newFieldDescriptor.analyzeField(field); - result.append("" + getClassName(false) - + "" + "
>, shape=\"none\"];\n"); + methods.add(methodDescriptor); - enlistFieldReferences(result); + methodDescriptor.analyze(method); + } - enlistMethodReferences(result); + } - enlistImplementedInterfaces(result); + @Override + public boolean isVisible() { - enlistSuperClass(result); + if (Utils.isSystemDataType(fullyQualifiedName)) + return false; - return result.toString(); - } + if (Utils.isSystemPackage(fullyQualifiedName)) + return false; - @Override - public String getEmbeddedDot() { - return null; - } + if (!getClassGraph().isClassShown(fullyQualifiedName)) + return false; - @Override - public String getGraphId() { - final String result = "class_" - + fullyQualifiedName.replace('.', '_').replace(";", "") - .replace("[L", "").replace('$', '_'); - return result; - } + 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; - public String getInterfaceColor() { - if (interfaceColor == null) - interfaceColor = Utils.getNextLightColor(); + return isShown; + } - return interfaceColor; - } + /** + * Register event when another class is extending this one. + */ + protected void registerExtension() { + extensionsCount++; + } - public String getPackageName() { + protected void registerImplementation() { + implementationsCount++; + } - final int i = fullyQualifiedName.lastIndexOf('.'); + protected void registerReference() { + referencesCount++; + } - if (i == -1) - return ""; + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof ClassDescriptor)) return false; - return fullyQualifiedName.substring(0, i).replace("[L", ""); - } + ClassDescriptor that = (ClassDescriptor) o; - public String getParentClassesName() { - int i = fullyQualifiedName.lastIndexOf('.'); - final String fullClassName = fullyQualifiedName.substring(i + 1); + return getFullyQualifiedName().equals(that.getFullyQualifiedName()); - i = fullClassName.lastIndexOf('$'); - if (i == -1) - return ""; - final String parentClassesName = fullClassName.substring(0, i); - return parentClassesName.replace('$', '.'); - } + } - // 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; - // } + @Override + public int hashCode() { + return getFullyQualifiedName().hashCode(); + } - public String getSuperClassColor() { - if (superClassColor == null) - superClassColor = Utils.getNextLightColor(); - - return superClassColor; - } - - public void hide() { - isShown = false; - } - - public void indexFields(final Field[] fields) { - for (final Field field : fields) { - if (nameToFieldMap.containsKey(field.getName())) - continue; - - final FieldDescriptor fieldDescriptor = new FieldDescriptor(field, - this, dump); - - } - } - - private void indexMethods(final Class clazz) { - final Method[] methods = clazz.getMethods(); - - for (final Method method : methods) - new MethodDescriptor(method, this, dump); - - } - - @Override - public boolean isVisible() { - - if (Utils.isSystemDataType(fullyQualifiedName)) - return false; - - if (Utils.isSystemPackage(fullyQualifiedName)) - return false; - - return isShown; - } - - public void registerReference() { - referenceCount++; - } - - public void setDistinctiveColor(final String distinctiveColor) { - distinctiveReferenceColor = distinctiveColor; - } + @Override + public int compareTo(ClassDescriptor o) { + return fullyQualifiedName.compareTo(o.fullyQualifiedName); + } }