2 * JavaInspect - Utility to visualize java software
3 * Copyright (C) 2013-2015, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 3 of the GNU Lesser General Public License
7 * or later as published by the Free Software Foundation.
10 package eu.svjatoslav.inspector.java.structure;
12 import java.lang.reflect.Method;
13 import java.lang.reflect.ParameterizedType;
14 import java.lang.reflect.Type;
15 import java.util.ArrayList;
16 import java.util.List;
18 public class MethodDescriptor implements GraphElement,
19 Comparable<MethodDescriptor> {
22 * This class corresponds to single method within a java class.
25 public String methodName;
26 public ClassDescriptor returnType;
27 private final ClassDescriptor parentClass;
29 List<ClassDescriptor> argumentTypes = new ArrayList<ClassDescriptor>();
31 public MethodDescriptor(final Method method, final ClassDescriptor parent,
32 final ClassGraph dump) {
36 methodName = method.getName();
38 if (!method.getDeclaringClass().getName()
39 .equals(parent.classFullyQualifiedName))
40 // do not index inherited methods
43 parent.methods.add(this);
45 returnType = dump.addClass(method.getReturnType());
46 returnType.registerReference();
48 final Type genericType = method.getGenericReturnType();
49 if (genericType instanceof ParameterizedType) {
50 final ParameterizedType pt = (ParameterizedType) genericType;
51 for (final Type t : pt.getActualTypeArguments())
52 if (t instanceof Class) {
53 final Class cl = (Class) t;
54 final ClassDescriptor classDescriptor = dump.addClass(cl);
55 classDescriptor.registerReference();
56 argumentTypes.add(classDescriptor);
63 public int compareTo(final MethodDescriptor o) {
65 final int nameComparisonResult = methodName.compareTo(o.methodName);
66 if (nameComparisonResult != 0)
67 return nameComparisonResult;
69 return toString().compareTo(o.toString());
73 public String getDot() {
78 final StringBuffer result = new StringBuffer();
80 // describe associated types
81 for (final ClassDescriptor classDescriptor : argumentTypes)
82 if (classDescriptor.isVisible())
83 if (classDescriptor.areReferencesShown())
84 result.append(" " + getGraphId() + " -> "
85 + classDescriptor.getGraphId() + "[label=\"" + methodName
86 + "\", color=\"" + classDescriptor.getColor()
87 + "\", style=\"dotted, bold\"];\n");
89 if (!returnType.isVisible())
90 return result.toString();
93 if (returnType.areReferencesShown())
94 result.append(" " + getGraphId() + " -> "
95 + returnType.getGraphId() + "[label=\"" + methodName + "\","
96 + " color=\"" + returnType.getColor()
97 + "\", style=\"dotted, bold\"];\n");
99 return result.toString();
103 public String getEmbeddedDot() {
107 final StringBuffer result = new StringBuffer();
109 result.append(" // " + methodName + "\n");
111 result.append(" <TR><td ALIGN=\"right\">"
112 + "<FONT POINT-SIZE=\"8.0\">" + returnType.getClassName(true)
113 + "</FONT>" + "</td><TD PORT=\"" + getMethodLabel()
114 + "\" ALIGN=\"left\"><FONT COLOR =\"red\" POINT-SIZE=\"11.0\">"
115 + getMethodLabel() + "</FONT></TD></TR>\n");
117 return result.toString();
121 public String getGraphId() {
122 return parentClass.getGraphId() + ":" + methodName;
125 public String getMethodLabel() {
129 public int getOutsideVisibleReferencesCount() {
132 if (returnType.isVisible())
135 for (final ClassDescriptor classDescriptor : argumentTypes)
136 if (classDescriptor.isVisible())
143 public boolean isVisible() {
145 // hide common object methods
146 if (Utils.isCommonObjectMethod(methodName))
149 // hide common Enumeration methods
150 if (parentClass.isEnum && Utils.isEnumMethod(methodName))
153 // hide get/set methods for the field of the same name
154 if (methodName.startsWith("get") || methodName.startsWith("set"))
155 if (parentClass.hasFieldIgnoreCase(methodName.substring(3)))
158 // hide is methods for the boolean field of the same name
159 if (methodName.startsWith("is")) {
160 final FieldDescriptor field = parentClass.getFieldIgnoreCase(methodName
163 if ("boolean".equals(field.getType().classFullyQualifiedName))