/*
* 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.util.ArrayList;
import java.util.List;
-public class MethodDescriptor implements GraphElement {
-
- public String name;
- public ClassDescriptor returnType;
- private final ClassDescriptor parent;
-
- List<ClassDescriptor> typeArguments = new ArrayList<ClassDescriptor>();
-
- public MethodDescriptor(final Method method, final ClassDescriptor parent,
- final ClassGraph dump) {
-
- this.parent = parent;
+/**
+ * This class corresponds to single method within a java class.
+ */
+public class MethodDescriptor implements GraphElement,
+ Comparable<MethodDescriptor> {
+
+ private final String methodName;
+ private ClassDescriptor returnType;
+ private final ClassDescriptor parentClass;
+ private final List<ClassDescriptor> argumentTypes = new ArrayList<ClassDescriptor>();
+ private boolean isInherited;
+
+ public MethodDescriptor(final ClassDescriptor parent,
+ final String methodName) {
+ parentClass = parent;
+ this.methodName = methodName;
+ }
- name = method.getName();
+ public void analyze(final Method method) {
if (!method.getDeclaringClass().getName()
- .equals(parent.fullyQualifiedName))
- // do not index inherited methods
- return;
+ .equals(parentClass.getFullyQualifiedName()))
+ isInherited = true;
- parent.methods.add(this);
-
- returnType = dump.addClass(method.getReturnType());
+ returnType = parentClass.getClassGraph().getOrCreateClassDescriptor(
+ method.getReturnType());
returnType.registerReference();
final Type genericType = method.getGenericReturnType();
for (final Type t : pt.getActualTypeArguments())
if (t instanceof Class) {
final Class cl = (Class) t;
- final ClassDescriptor classDescriptor = dump.addClass(cl);
+ final ClassDescriptor classDescriptor = parentClass
+ .getClassGraph().getOrCreateClassDescriptor(cl);
classDescriptor.registerReference();
- typeArguments.add(classDescriptor);
+ argumentTypes.add(classDescriptor);
}
}
}
+ @Override
+ public int compareTo(final MethodDescriptor o) {
+
+ final int nameComparisonResult = methodName.compareTo(o.methodName);
+ if (nameComparisonResult != 0)
+ return nameComparisonResult;
+
+ return toString().compareTo(o.toString());
+ }
+
@Override
public String getDot() {
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() + " -> "
- + classDescriptor.getGraphId() + "[label=\"" + name
- + "\", color=\"" + classDescriptor.getColor()
+ + classDescriptor.getGraphId() + "[label=\""
+ + methodName + "\", color=\""
+ + classDescriptor.getColor()
+ "\", style=\"dotted, bold\"];\n");
if (!returnType.isVisible())
// main type
if (returnType.areReferencesShown())
result.append(" " + getGraphId() + " -> "
- + returnType.getGraphId() + "[label=\"" + name + "\","
- + " color=\"" + returnType.getColor()
+ + returnType.getGraphId() + "[label=\"" + methodName
+ + "\"," + " color=\"" + returnType.getColor()
+ "\", style=\"dotted, bold\"];\n");
return result.toString();
final StringBuffer result = new StringBuffer();
- result.append(" // " + name + "\n");
+ result.append(" // " + methodName + "\n");
result.append(" <TR><td ALIGN=\"right\">"
+ "<FONT POINT-SIZE=\"8.0\">" + returnType.getClassName(true)
@Override
public String getGraphId() {
- return parent.getGraphId() + ":" + name;
+ return parentClass.getGraphId() + ":" + methodName;
}
- public String getMethodLabel() {
- return name;
+ private String getMethodLabel() {
+ return methodName;
}
- public int getOutsideVisibleReferencesCount() {
+ protected int getOutsideVisibleReferencesCount() {
int result = 0;
if (returnType.isVisible())
result++;
- for (final ClassDescriptor classDescriptor : typeArguments)
+ for (final ClassDescriptor classDescriptor : argumentTypes)
if (classDescriptor.isVisible())
result++;
@Override
public boolean isVisible() {
- if (Utils.isSystemMethod(name))
+ // hide inherited methods
+ if (isInherited)
return false;
- if (parent.isEnum && Utils.isEnumMethod(name))
+ // hide common object methods
+ if (Utils.isCommonObjectMethod(methodName))
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();
+ // hide common Enumeration methods
+ if (parentClass.isEnum && Utils.isEnumMethod(methodName))
+ return false;
- if (upprCaseName.equals(parentField))
+ // hide get/set methods for the field of the same name
+ if (methodName.startsWith("get") || methodName.startsWith("set"))
+ if (parentClass.hasFieldIgnoreCase(methodName.substring(3)))
return false;
+ // hide is methods for the boolean field of the same name
+ if (methodName.startsWith("is")) {
+ final FieldDescriptor field = parentClass
+ .getFieldIgnoreCase(methodName.substring(2));
+ if (field != null)
+ if ("boolean".equals(field.getType().getFullyQualifiedName()))
+ return false;
}
return true;
+
}
}