35e49d5071ababb2e4b57701e2e030efa23aed7e
[javainspect.git] / src / main / java / eu / svjatoslav / inspector / java / structure / MethodDescriptor.java
1 /*
2  * JavaInspect - Utility to visualize java software
3  * Copyright (C) 2013, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
4  * 
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.
8  */
9
10 package eu.svjatoslav.inspector.java.structure;
11
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;
17
18 public class MethodDescriptor implements GraphElement {
19
20         public String name;
21         public ClassDescriptor returnType;
22         private final ClassDescriptor parent;
23
24         List<ClassDescriptor> typeArguments = new ArrayList<ClassDescriptor>();
25
26         public MethodDescriptor(final Method method, final ClassDescriptor parent,
27                         final ClassGraph dump) {
28
29                 this.parent = parent;
30
31                 name = method.getName();
32
33                 if (!method.getDeclaringClass().getName()
34                                 .equals(parent.fullyQualifiedName))
35                         // do not index inherited methods
36                         return;
37
38                 parent.methods.add(this);
39
40                 returnType = dump.addClass(method.getReturnType());
41                 returnType.registerReference();
42
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);
52                                 }
53
54                 }
55
56         }
57
58         @Override
59         public String getDot() {
60
61                 if (!isVisible())
62                         return "";
63
64                 final StringBuffer result = new StringBuffer();
65
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");
74
75                 if (!returnType.isVisible())
76                         return result.toString();
77
78                 // main type
79                 if (returnType.areReferencesShown())
80                         result.append("    " + getGraphId() + " -> "
81                                         + returnType.getGraphId() + "[label=\"" + name + "\","
82                                         + " color=\"" + returnType.getColor()
83                                         + "\", style=\"dotted, bold\"];\n");
84
85                 return result.toString();
86         }
87
88         @Override
89         public String getEmbeddedDot() {
90                 if (!isVisible())
91                         return "";
92
93                 final StringBuffer result = new StringBuffer();
94
95                 result.append("        // " + name + "\n");
96
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");
102
103                 return result.toString();
104         }
105
106         @Override
107         public String getGraphId() {
108                 return parent.getGraphId() + ":" + name;
109         }
110
111         public String getMethodLabel() {
112                 return name;
113         }
114
115         @Override
116         public boolean isVisible() {
117
118                 if (Utils.isSystemMethod(name))
119                         return false;
120
121                 if (parent.isEnum && Utils.isEnumMethod(name))
122                         return false;
123
124                 if (!(name.startsWith("get") || name.startsWith("set")))
125                         return true;
126
127                 final String upprCaseName = name.substring(3).toUpperCase();
128
129                 for (String parentField : parent.nameToFieldMap.keySet()) {
130                         parentField = parentField.toUpperCase();
131
132                         if (upprCaseName.equals(parentField))
133                                 return false;
134
135                 }
136
137                 return true;
138         }
139
140 }