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>();
33 public MethodDescriptor(final ClassDescriptor parent,
34 final String methodName) {
36 this.methodName = methodName;
39 public void analyze(final Method method) {
41 if (!method.getDeclaringClass().getName()
42 .equals(parentClass.classFullyQualifiedName))
45 returnType = parentClass.getClassGraph().getOrCreateClassDescriptor(
46 method.getReturnType());
47 returnType.registerReference();
49 final Type genericType = method.getGenericReturnType();
50 if (genericType instanceof ParameterizedType) {
51 final ParameterizedType pt = (ParameterizedType) genericType;
52 for (final Type t : pt.getActualTypeArguments())
53 if (t instanceof Class) {
54 final Class cl = (Class) t;
55 final ClassDescriptor classDescriptor = parentClass
56 .getClassGraph().getOrCreateClassDescriptor(cl);
57 classDescriptor.registerReference();
58 argumentTypes.add(classDescriptor);
65 public int compareTo(final MethodDescriptor o) {
67 final int nameComparisonResult = methodName.compareTo(o.methodName);
68 if (nameComparisonResult != 0)
69 return nameComparisonResult;
71 return toString().compareTo(o.toString());
75 public String getDot() {
80 final StringBuffer result = new StringBuffer();
82 // describe associated types
83 for (final ClassDescriptor classDescriptor : argumentTypes)
84 if (classDescriptor.isVisible())
85 if (classDescriptor.areReferencesShown())
86 result.append(" " + getGraphId() + " -> "
87 + classDescriptor.getGraphId() + "[label=\""
88 + methodName + "\", color=\""
89 + classDescriptor.getColor()
90 + "\", style=\"dotted, bold\"];\n");
92 if (!returnType.isVisible())
93 return result.toString();
96 if (returnType.areReferencesShown())
97 result.append(" " + getGraphId() + " -> "
98 + returnType.getGraphId() + "[label=\"" + methodName
99 + "\"," + " color=\"" + returnType.getColor()
100 + "\", style=\"dotted, bold\"];\n");
102 return result.toString();
106 public String getEmbeddedDot() {
110 final StringBuffer result = new StringBuffer();
112 result.append(" // " + methodName + "\n");
114 result.append(" <TR><td ALIGN=\"right\">"
115 + "<FONT POINT-SIZE=\"8.0\">" + returnType.getClassName(true)
116 + "</FONT>" + "</td><TD PORT=\"" + getMethodLabel()
117 + "\" ALIGN=\"left\"><FONT COLOR =\"red\" POINT-SIZE=\"11.0\">"
118 + getMethodLabel() + "</FONT></TD></TR>\n");
120 return result.toString();
124 public String getGraphId() {
125 return parentClass.getGraphId() + ":" + methodName;
128 public String getMethodLabel() {
132 public int getOutsideVisibleReferencesCount() {
135 if (returnType.isVisible())
138 for (final ClassDescriptor classDescriptor : argumentTypes)
139 if (classDescriptor.isVisible())
146 public boolean isVisible() {
148 // hide inherited methods
152 // hide common object methods
153 if (Utils.isCommonObjectMethod(methodName))
156 // hide common Enumeration methods
157 if (parentClass.isEnum && Utils.isEnumMethod(methodName))
160 // hide get/set methods for the field of the same name
161 if (methodName.startsWith("get") || methodName.startsWith("set"))
162 if (parentClass.hasFieldIgnoreCase(methodName.substring(3)))
165 // hide is methods for the boolean field of the same name
166 if (methodName.startsWith("is")) {
167 final FieldDescriptor field = parentClass
168 .getFieldIgnoreCase(methodName.substring(2));
170 if ("boolean".equals(field.getType().classFullyQualifiedName))