improved project documentation
[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 Comparable<MethodDescriptor> {
20
21         /**
22          * This class corresponds to single method within a java class.
23          */
24
25         public String name;
26         public ClassDescriptor returnType;
27         private final ClassDescriptor parent;
28
29         List<ClassDescriptor> typeArguments = new ArrayList<ClassDescriptor>();
30
31         public MethodDescriptor(final Method method, final ClassDescriptor parent,
32                         final ClassGraph dump) {
33
34                 this.parent = parent;
35
36                 name = method.getName();
37
38                 if (!method.getDeclaringClass().getName()
39                                 .equals(parent.fullyQualifiedName))
40                         // do not index inherited methods
41                         return;
42
43                 parent.methods.add(this);
44
45                 returnType = dump.addClass(method.getReturnType());
46                 returnType.registerReference();
47
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);
57                                 }
58
59                 }
60         }
61
62         @Override
63         public String getDot() {
64
65                 if (!isVisible())
66                         return "";
67
68                 final StringBuffer result = new StringBuffer();
69
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");
78
79                 if (!returnType.isVisible())
80                         return result.toString();
81
82                 // main type
83                 if (returnType.areReferencesShown())
84                         result.append("    " + getGraphId() + " -> "
85                                         + returnType.getGraphId() + "[label=\"" + name + "\","
86                                         + " color=\"" + returnType.getColor()
87                                         + "\", style=\"dotted, bold\"];\n");
88
89                 return result.toString();
90         }
91
92         @Override
93         public String getEmbeddedDot() {
94                 if (!isVisible())
95                         return "";
96
97                 final StringBuffer result = new StringBuffer();
98
99                 result.append("        // " + name + "\n");
100
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");
106
107                 return result.toString();
108         }
109
110         @Override
111         public String getGraphId() {
112                 return parent.getGraphId() + ":" + name;
113         }
114
115         public String getMethodLabel() {
116                 return name;
117         }
118
119         public int getOutsideVisibleReferencesCount() {
120                 int result = 0;
121
122                 if (returnType.isVisible())
123                         result++;
124
125                 for (final ClassDescriptor classDescriptor : typeArguments)
126                         if (classDescriptor.isVisible())
127                                 result++;
128
129                 return result;
130         }
131
132         @Override
133         public boolean isVisible() {
134
135                 if (Utils.isSystemMethod(name))
136                         return false;
137
138                 if (parent.isEnum && Utils.isEnumMethod(name))
139                         return false;
140
141                 if (!(name.startsWith("get") || name.startsWith("set")))
142                         return true;
143
144                 final String upprCaseName = name.substring(3).toUpperCase();
145
146                 for (String parentField : parent.nameToFieldMap.keySet()) {
147                         parentField = parentField.toUpperCase();
148
149                         if (upprCaseName.equals(parentField))
150                                 return false;
151
152                 }
153
154                 return true;
155         }
156
157         @Override
158         public int compareTo(MethodDescriptor o) {
159
160                 int nameComparisonResult = name.compareTo(o.name);
161                 if (nameComparisonResult != 0)
162                         return nameComparisonResult;
163
164                 return toString().compareTo(o.toString());
165         }
166
167 }