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.javainspect.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,
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);
59 public String getDot() {
64 final StringBuffer result = new StringBuffer();
66 // describe associated types
67 for (final ClassDescriptor classDescriptor : typeArguments)
68 if (classDescriptor.isVisible())
69 if (classDescriptor.areReferencesShown())
70 result.append(" " + getGraphId() + " -> "
71 + classDescriptor.getGraphId() + "[label=\"" + name
72 + "\", color=\"" + classDescriptor.getColor()
73 + "\", style=\"dotted, bold\"];\n");
75 if (!returnType.isVisible())
76 return result.toString();
79 if (returnType.areReferencesShown())
80 result.append(" " + getGraphId() + " -> "
81 + returnType.getGraphId() + "[label=\"" + name + "\","
82 + " color=\"" + returnType.getColor()
83 + "\", style=\"dotted, bold\"];\n");
85 return result.toString();
89 public String getEmbeddedDot() {
93 final StringBuffer result = new StringBuffer();
95 result.append(" // " + name + "\n");
97 result.append(" <TR><td ALIGN=\"right\">"
98 + "<FONT POINT-SIZE=\"8.0\">" + returnType.getClassName(true)
99 + "</FONT>" + "</td><TD PORT=\"" + getMethodLabel()
100 + "\" ALIGN=\"left\"><FONT COLOR =\"red\" POINT-SIZE=\"11.0\">"
101 + getMethodLabel() + "</FONT></TD></TR>\n");
103 return result.toString();
107 public String getGraphId() {
108 return parent.getGraphId() + ":" + name;
111 public String getMethodLabel() {
116 public boolean isVisible() {
118 if (Utils.isSystemMethod(name))
121 if (parent.isEnum && Utils.isEnumMethod(name))
124 if (!(name.startsWith("get") || name.startsWith("set")))
127 final String upprCaseName = name.substring(3).toUpperCase();
129 for (String parentField : parent.nameToFieldMap.keySet()) {
130 parentField = parentField.toUpperCase();
132 if (upprCaseName.equals(parentField))