2 * JavaInspect - Utility to visualize java software
3 * Copyright (C) 2013, 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 2 of the GNU General Public License
7 * 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.
26 public ClassDescriptor returnType;
27 private final ClassDescriptor parent;
29 List<ClassDescriptor> typeArguments = new ArrayList<ClassDescriptor>();
31 public MethodDescriptor(final Method method, final ClassDescriptor parent,
32 final ClassGraph dump) {
36 name = method.getName();
38 if (!method.getDeclaringClass().getName()
39 .equals(parent.fullyQualifiedName))
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 typeArguments.add(classDescriptor);
63 public String getDot() {
68 final StringBuffer result = new StringBuffer();
70 // describe associated types
71 for (final ClassDescriptor classDescriptor : typeArguments)
72 if (classDescriptor.isVisible())
73 if (classDescriptor.areReferencesShown())
74 result.append(" " + getGraphId() + " -> "
75 + classDescriptor.getGraphId() + "[label=\"" + name
76 + "\", color=\"" + classDescriptor.getColor()
77 + "\", style=\"dotted, bold\"];\n");
79 if (!returnType.isVisible())
80 return result.toString();
83 if (returnType.areReferencesShown())
84 result.append(" " + getGraphId() + " -> "
85 + returnType.getGraphId() + "[label=\"" + name + "\","
86 + " color=\"" + returnType.getColor()
87 + "\", style=\"dotted, bold\"];\n");
89 return result.toString();
93 public String getEmbeddedDot() {
97 final StringBuffer result = new StringBuffer();
99 result.append(" // " + name + "\n");
101 result.append(" <TR><td ALIGN=\"right\">"
102 + "<FONT POINT-SIZE=\"8.0\">" + returnType.getClassName(true)
103 + "</FONT>" + "</td><TD PORT=\"" + getMethodLabel()
104 + "\" ALIGN=\"left\"><FONT COLOR =\"red\" POINT-SIZE=\"11.0\">"
105 + getMethodLabel() + "</FONT></TD></TR>\n");
107 return result.toString();
111 public String getGraphId() {
112 return parent.getGraphId() + ":" + name;
115 public String getMethodLabel() {
119 public int getOutsideVisibleReferencesCount() {
122 if (returnType.isVisible())
125 for (final ClassDescriptor classDescriptor : typeArguments)
126 if (classDescriptor.isVisible())
133 public boolean isVisible() {
135 if (Utils.isSystemMethod(name))
138 if (parent.isEnum && Utils.isEnumMethod(name))
141 if (!(name.startsWith("get") || name.startsWith("set")))
144 final String upprCaseName = name.substring(3).toUpperCase();
146 for (String parentField : parent.nameToFieldMap.keySet()) {
147 parentField = parentField.toUpperCase();
149 if (upprCaseName.equals(parentField))
158 public int compareTo(MethodDescriptor o) {
160 int nameComparisonResult = name.compareTo(o.name);
161 if (nameComparisonResult != 0)
162 return nameComparisonResult;
164 return toString().compareTo(o.toString());