possibility to hide orphaned classes from graph
[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         @Override
58         public String getDot() {
59
60                 if (!isVisible())
61                         return "";
62
63                 final StringBuffer result = new StringBuffer();
64
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");
73
74                 if (!returnType.isVisible())
75                         return result.toString();
76
77                 // main type
78                 if (returnType.areReferencesShown())
79                         result.append("    " + getGraphId() + " -> "
80                                         + returnType.getGraphId() + "[label=\"" + name + "\","
81                                         + " color=\"" + returnType.getColor()
82                                         + "\", style=\"dotted, bold\"];\n");
83
84                 return result.toString();
85         }
86
87         @Override
88         public String getEmbeddedDot() {
89                 if (!isVisible())
90                         return "";
91
92                 final StringBuffer result = new StringBuffer();
93
94                 result.append("        // " + name + "\n");
95
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");
101
102                 return result.toString();
103         }
104
105         @Override
106         public String getGraphId() {
107                 return parent.getGraphId() + ":" + name;
108         }
109
110         public String getMethodLabel() {
111                 return name;
112         }
113
114         public int getOutsideVisibleReferencesCount() {
115                 int result = 0;
116
117                 if (returnType.isVisible())
118                         result++;
119
120                 for (final ClassDescriptor classDescriptor : typeArguments)
121                         if (classDescriptor.isVisible())
122                                 result++;
123
124                 return result;
125         }
126
127         @Override
128         public boolean isVisible() {
129
130                 if (Utils.isSystemMethod(name))
131                         return false;
132
133                 if (parent.isEnum && Utils.isEnumMethod(name))
134                         return false;
135
136                 if (!(name.startsWith("get") || name.startsWith("set")))
137                         return true;
138
139                 final String upprCaseName = name.substring(3).toUpperCase();
140
141                 for (String parentField : parent.nameToFieldMap.keySet()) {
142                         parentField = parentField.toUpperCase();
143
144                         if (upprCaseName.equals(parentField))
145                                 return false;
146
147                 }
148
149                 return true;
150         }
151
152 }