2 * JavaInspect - Utility to visualize java software
3 * Copyright (C) 2013-2018, 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 final ClassDescriptor parentClass;
26 private final List<ClassDescriptor> argumentTypes = new ArrayList<>();
27 private ClassDescriptor returnType;
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 boolean equals(Object o) {
63 if (this == o) return true;
64 if (!(o instanceof MethodDescriptor)) return false;
66 MethodDescriptor that = (MethodDescriptor) o;
68 if (methodName != null ? !methodName.equals(that.methodName) : that.methodName != null) return false;
69 if (parentClass != null ? !parentClass.equals(that.parentClass) : that.parentClass != null) return false;
70 return argumentTypes != null ? argumentTypes.equals(that.argumentTypes) : that.argumentTypes == null;
76 public int hashCode() {
77 int result = methodName != null ? methodName.hashCode() : 0;
78 result = 31 * result + (parentClass != null ? parentClass.hashCode() : 0);
79 result = 31 * result + (argumentTypes != null ? argumentTypes.hashCode() : 0);
84 public String getDot() {
89 final StringBuilder result = new StringBuilder();
91 // describe associated types
92 for (final ClassDescriptor classDescriptor : argumentTypes)
93 if (classDescriptor.isVisible())
94 if (classDescriptor.areReferencesShown())
95 result.append(" " + getGraphId() + " -> "
96 + classDescriptor.getGraphId() + "[label=\""
97 + methodName + "\", color=\""
98 + classDescriptor.getColor()
99 + "\", style=\"dotted, bold\"];\n");
101 if (returnType == null) return result.toString();
102 if (!returnType.isVisible()) return result.toString();
105 if (returnType.areReferencesShown())
106 result.append(" " + getGraphId() + " -> "
107 + returnType.getGraphId() + "[label=\"" + methodName
108 + "\"," + " color=\"" + returnType.getColor()
109 + "\", style=\"dotted, bold\"];\n");
111 return result.toString();
115 public String getEmbeddedDot() {
119 final StringBuilder result = new StringBuilder();
121 result.append(" // " + methodName + "\n");
123 result.append(" <TR><td ALIGN=\"right\">"
124 + "<FONT POINT-SIZE=\"8.0\">" + describeReturnType()
125 + "</FONT>" + "</td><TD PORT=\"" + getMethodLabel()
126 + "\" ALIGN=\"left\"><FONT COLOR =\"red\" POINT-SIZE=\"11.0\">"
127 + getMethodLabel() + "</FONT></TD></TR>\n");
129 return result.toString();
132 private String describeReturnType() {
133 if (returnType == null) return "-null-";
135 return returnType.getClassName(true);
139 public String getGraphId() {
140 return parentClass.getGraphId() + ":" + methodName;
143 private String getMethodLabel() {
147 protected int getOutsideVisibleReferencesCount() {
150 if (returnType != null)
151 if (returnType.isVisible())
154 for (final ClassDescriptor classDescriptor : argumentTypes)
155 if (classDescriptor.isVisible())
162 public boolean isVisible() {
164 // hide inherited methods
168 // hide common object methods
169 if (Utils.isCommonObjectMethod(methodName))
172 // hide common Enumeration methods
173 if (parentClass.isEnum && Utils.isEnumMethod(methodName))
176 // hide get/set methods for the field of the same name
177 if (methodName.startsWith("get") || methodName.startsWith("set"))
178 if (parentClass.hasFieldIgnoreCase(methodName.substring(3)))
181 // hide is methods for the boolean field of the same name
182 if (methodName.startsWith("is")) {
183 final FieldDescriptor field = parentClass
184 .getFieldIgnoreCase(methodName.substring(2));
186 if ("boolean".equals(field.getType().getFullyQualifiedName()))
195 public int compareTo(MethodDescriptor that) {
196 if (this == that) return 0;
198 int comparisonResult = methodName.compareTo(that.methodName);
199 if (comparisonResult != 0) return comparisonResult;
201 return parentClass.compareTo(that.parentClass);