/*
* 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;
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.SortedSet;
+import java.util.TreeMap;
+import java.util.TreeSet;
/**
* Describes single class instance
private static final int MAX_REFERECNES_COUNT = 10;
- public final String fullyQualifiedName;
+ private String fullyQualifiedName;
- Map<String, FieldDescriptor> nameToFieldMap = new HashMap<String, FieldDescriptor>();
+ private final Map<String, FieldDescriptor> nameToFieldMap = new TreeMap<String, FieldDescriptor>();
- public List<MethodDescriptor> methods = new ArrayList<MethodDescriptor>();
+ private final SortedSet<MethodDescriptor> methods = new TreeSet<MethodDescriptor>();
/**
* Incoming arrows will have this color.
private boolean isShown = true;
- private final ClassGraph dump;
+ private final ClassGraph classGraph;
List<ClassDescriptor> interfaces = new ArrayList<ClassDescriptor>();
/**
* Amount of field and method references pointing to this class.
*/
- private int referenceCount = 0;
+ private int referencesCount = 0;
- public ClassDescriptor(final Class<? extends Object> clazz,
- final ClassGraph dump) {
- this.dump = dump;
+ // 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<? extends Object> 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);
indexMethods(clazz);
- for (final Class interfaceClass : clazz.getInterfaces())
- interfaces.add(dump.addClass(interfaceClass));
+ for (final Class interfaceClass : clazz.getInterfaces()) {
+ 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() {
- return referenceCount <= MAX_REFERECNES_COUNT;
+ protected boolean areReferencesShown() {
+ return referencesCount <= MAX_REFERECNES_COUNT;
}
- public void enlistFieldReferences(final StringBuffer result) {
+ private void enlistFieldReferences(final StringBuffer result) {
if (nameToFieldMap.isEmpty())
return;
result.append(entry.getValue().getDot());
}
- public void enlistFields(final StringBuffer result) {
+ private void enlistFields(final StringBuffer result) {
if (nameToFieldMap.isEmpty())
return;
result.append(entry.getValue().getEmbeddedDot());
}
- public void enlistImplementedInterfaces(final StringBuffer result) {
+ private void enlistImplementedInterfaces(final StringBuffer result) {
if (interfaces.isEmpty())
return;
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;
result.append(methodDescriptor.getDot());
}
- public void enlistMethods(final StringBuffer result) {
+ private void enlistMethods(final StringBuffer result) {
if (methods.isEmpty())
return;
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");
result.append(" // class descriptor header\n");
result.append(" <TR><TD colspan=\"2\" PORT=\"f0\">"
+ "<FONT POINT-SIZE=\"8.0\" >" + getPackageName()
- + "</FONT><br/>" + "<FONT POINT-SIZE=\"25.0\"><B>"
- + getClassName(false) + "</B></FONT>" + "</TD></TR>\n");
- }
+ + "</FONT><br/>");
- public List<FieldDescriptor> getAllFields() {
- final List<FieldDescriptor> result = new ArrayList<FieldDescriptor>();
-
- for (final Map.Entry<String, FieldDescriptor> entry : nameToFieldMap
- .entrySet())
- result.add(entry.getValue());
+ final String parentClassesName = getParentClassesName();
+ if (parentClassesName.length() > 0)
+ result.append("<FONT POINT-SIZE=\"12.0\"><B>" + parentClassesName
+ + "</B></FONT><br/>\n");
- return result;
+ result.append("<FONT POINT-SIZE=\"25.0\"><B>" + getClassName(false)
+ + "</B></FONT>" + "</TD></TR>\n");
}
- public String getBackgroundColor() {
+ private String getBackgroundColor() {
String bgColor = "";
if (isEnum)
return bgColor;
}
- public String getBorderWidth() {
+ private String getBorderWidth() {
if (!areReferencesShown())
return "4";
return "1";
}
- public String getClassName(final boolean differentiateArray) {
-
- final int i = fullyQualifiedName.lastIndexOf('.');
+ protected ClassGraph getClassGraph() {
+ return classGraph;
+ }
- String result = fullyQualifiedName.substring(i + 1);
+ protected String getClassName(final boolean differentiateArray) {
+ // this is needed for nested classes
+ final String actualClassName = fullyQualifiedName.replace('$', '.');
- 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)
result += " []";
+ // this is needed for nested classes
+ // result = result.replace('$', '.');
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;
}
return null;
}
+ /**
+ * Returns field with given name (case is ignored). Or <code>null</code> if
+ * field is not found.
+ *
+ * @param fieldToSearch
+ * field name (case is ignored)
+ * @return field matching given name
+ */
+ 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_"
+ fullyQualifiedName.replace('.', '_').replace(";", "")
- .replace("[L", "");
+ .replace("[L", "").replace('$', '_');
return result;
}
- public String getInterfaceColor() {
+ private String getInterfaceColor() {
if (interfaceColor == null)
- interfaceColor = Utils.getNextLightColor();
+ interfaceColor = Utils.getNextDarkColor();
return interfaceColor;
}
- public String getPackageName() {
+ 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;
+
+ // count method references
+ for (final MethodDescriptor methodDescriptor : methods)
+ result += methodDescriptor.getOutsideVisibleReferencesCount();
+
+ // count field references
+ for (final FieldDescriptor fieldDescriptor : nameToFieldMap.values())
+ result += fieldDescriptor.getOutsideVisibleReferencesCount();
+
+ // count implemented interfaces
+ for (final ClassDescriptor classDescriptor : interfaces)
+ if (classDescriptor.isVisible())
+ result++;
+
+ // count superclass
+ if (superClass != null)
+ if (superClass.isVisible())
+ result++;
+
+ return result;
+ }
+
+ private String getPackageName() {
final int i = fullyQualifiedName.lastIndexOf('.');
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 getSuperClassColor() {
+ private String getParentClassesName() {
+ int i = fullyQualifiedName.lastIndexOf('.');
+ final String fullClassName = fullyQualifiedName.substring(i + 1);
+
+ i = fullClassName.lastIndexOf('$');
+ if (i == -1)
+ return "";
+ final String parentClassesName = fullClassName.substring(0, i);
+ return parentClassesName.replace('$', '.');
+ }
+
+ 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
+ * <code>true</code> if such field is found.
+ *
+ * @param fieldToSearch
+ * field to search for (case is ignored)
+ *
+ * @return <code>true</code> if 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 indexFields(final Field[] fields) {
- for (final Field field : fields) {
- if (nameToFieldMap.containsKey(field.getName()))
- continue;
+ protected void hideClassIfNoReferences() {
+ if (!isVisible())
+ return;
- final FieldDescriptor fieldDescriptor = new FieldDescriptor(field,
- this, dump);
+ final int totalReferencesCount = getOutgoingReferencesCount()
+ + referencesCount + extensionsCount + implementationsCount;
+ if (totalReferencesCount == 0) {
+ hide();
+ return;
}
+
+ return;
+ }
+
+ private void indexFields(final Field[] fields) {
+ for (final Field field : fields)
+ getOrCreateFieldDescriptor(field);
}
private void indexMethods(final Class<? extends Object> clazz) {
- final Method[] methods = clazz.getMethods();
+ for (final Method method : clazz.getMethods()) {
+ final MethodDescriptor methodDescriptor = new MethodDescriptor(
+ this, method.getName());
- for (final Method method : methods)
- new MethodDescriptor(method, this, dump);
+ methods.add(methodDescriptor);
+
+ methodDescriptor.analyze(method);
+ }
}
if (Utils.isSystemPackage(fullyQualifiedName))
return false;
+ 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;
}
- public void registerReference() {
- referenceCount++;
+ /**
+ * Register event when another class is extending this one.
+ */
+ protected void registerExtension() {
+ extensionsCount++;
+ }
+
+ protected void registerImplementation() {
+ implementationsCount++;
}
- public void setDistinctiveColor(final String distinctiveColor) {
- distinctiveReferenceColor = distinctiveColor;
+ protected void registerReference() {
+ referencesCount++;
}
+
}