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;
19 * This class corresponds to single method within a java class.
21 public class MethodDescriptor implements GraphElement,
22 Comparable<MethodDescriptor> {
24 private final String methodName;
25 private ClassDescriptor returnType;
26 private final ClassDescriptor parentClass;
27 private final List<ClassDescriptor> argumentTypes = new ArrayList<ClassDescriptor>();
28 private boolean isInherited;
30 public MethodDescriptor(final ClassDescriptor parent,
31 final String methodName) {
33 this.methodName = methodName;
36 public void analyze(final Method method) {
38 if (!method.getDeclaringClass().getName()
39 .equals(parentClass.getFullyQualifiedName()))
42 returnType = parentClass.getClassGraph().getOrCreateClassDescriptor(
43 method.getReturnType());
44 returnType.registerReference();
46 final Type genericType = method.getGenericReturnType();
47 if (genericType instanceof ParameterizedType) {
48 final ParameterizedType pt = (ParameterizedType) genericType;
49 for (final Type t : pt.getActualTypeArguments())
50 if (t instanceof Class) {
51 final Class cl = (Class) t;
52 final ClassDescriptor classDescriptor = parentClass
53 .getClassGraph().getOrCreateClassDescriptor(cl);
54 classDescriptor.registerReference();
55 argumentTypes.add(classDescriptor);
62 public int compareTo(final MethodDescriptor o) {
64 final int nameComparisonResult = methodName.compareTo(o.methodName);
65 if (nameComparisonResult != 0)
66 return nameComparisonResult;
68 return toString().compareTo(o.toString());
72 public String getDot() {
77 final StringBuffer result = new StringBuffer();
79 // describe associated types
80 for (final ClassDescriptor classDescriptor : argumentTypes)
81 if (classDescriptor.isVisible())
82 if (classDescriptor.areReferencesShown())
83 result.append(" " + getGraphId() + " -> "
84 + classDescriptor.getGraphId() + "[label=\""
85 + methodName + "\", color=\""
86 + 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 private String getMethodLabel() {
129 protected int getOutsideVisibleReferencesCount() {
132 if (returnType.isVisible())
135 for (final ClassDescriptor classDescriptor : argumentTypes)
136 if (classDescriptor.isVisible())
143 public boolean isVisible() {
145 // hide inherited methods
149 // hide common object methods
150 if (Utils.isCommonObjectMethod(methodName))
153 // hide common Enumeration methods
154 if (parentClass.isEnum && Utils.isEnumMethod(methodName))
157 // hide get/set methods for the field of the same name
158 if (methodName.startsWith("get") || methodName.startsWith("set"))
159 if (parentClass.hasFieldIgnoreCase(methodName.substring(3)))
162 // hide is methods for the boolean field of the same name
163 if (methodName.startsWith("is")) {
164 final FieldDescriptor field = parentClass
165 .getFieldIgnoreCase(methodName.substring(2));
167 if ("boolean".equals(field.getType().getFullyQualifiedName()))