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 {
21 public ClassDescriptor returnType;
22 private final ClassDescriptor parent;
24 List<ClassDescriptor> typeArguments = new ArrayList<ClassDescriptor>();
26 public MethodDescriptor(final Method method, final ClassDescriptor parent,
27 final ClassGraph dump) {
31 name = method.getName();
33 if (!method.getDeclaringClass().getName()
34 .equals(parent.fullyQualifiedName))
35 // do not index inherited methods
38 parent.methods.add(this);
40 returnType = dump.addClass(method.getReturnType());
41 returnType.registerReference();
43 final Type genericType = method.getGenericReturnType();
44 if (genericType instanceof ParameterizedType) {
45 final ParameterizedType pt = (ParameterizedType) genericType;
46 for (final Type t : pt.getActualTypeArguments())
47 if (t instanceof Class) {
48 final Class cl = (Class) t;
49 final ClassDescriptor classDescriptor = dump.addClass(cl);
50 classDescriptor.registerReference();
51 typeArguments.add(classDescriptor);
58 public String getDot() {
63 final StringBuffer result = new StringBuffer();
65 // describe associated types
66 for (final ClassDescriptor classDescriptor : typeArguments)
67 if (classDescriptor.isVisible())
68 if (classDescriptor.areReferencesShown())
69 result.append(" " + getGraphId() + " -> "
70 + classDescriptor.getGraphId() + "[label=\"" + name
71 + "\", color=\"" + classDescriptor.getColor()
72 + "\", style=\"dotted, bold\"];\n");
74 if (!returnType.isVisible())
75 return result.toString();
78 if (returnType.areReferencesShown())
79 result.append(" " + getGraphId() + " -> "
80 + returnType.getGraphId() + "[label=\"" + name + "\","
81 + " color=\"" + returnType.getColor()
82 + "\", style=\"dotted, bold\"];\n");
84 return result.toString();
88 public String getEmbeddedDot() {
92 final StringBuffer result = new StringBuffer();
94 result.append(" // " + name + "\n");
96 result.append(" <TR><td ALIGN=\"right\">"
97 + "<FONT POINT-SIZE=\"8.0\">" + returnType.getClassName(true)
98 + "</FONT>" + "</td><TD PORT=\"" + getMethodLabel()
99 + "\" ALIGN=\"left\"><FONT COLOR =\"red\" POINT-SIZE=\"11.0\">"
100 + getMethodLabel() + "</FONT></TD></TR>\n");
102 return result.toString();
106 public String getGraphId() {
107 return parent.getGraphId() + ":" + name;
110 public String getMethodLabel() {
114 public int getOutsideVisibleReferencesCount() {
117 if (returnType.isVisible())
120 for (final ClassDescriptor classDescriptor : typeArguments)
121 if (classDescriptor.isVisible())
128 public boolean isVisible() {
130 if (Utils.isSystemMethod(name))
133 if (parent.isEnum && Utils.isEnumMethod(name))
136 if (!(name.startsWith("get") || name.startsWith("set")))
139 final String upprCaseName = name.substring(3).toUpperCase();
141 for (String parentField : parent.nameToFieldMap.keySet()) {
142 parentField = parentField.toUpperCase();
144 if (upprCaseName.equals(parentField))