216267469b10c2469c081f2fd3a2b77dd2a6517a
[javainspect.git] / src / main / java / eu / svjatoslav / inspector / java / structure / FieldDescriptor.java
1 /*
2  * JavaInspect - Utility to visualize java software
3  * Copyright (C) 2013-2014, 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 3 of the GNU Lesser General Public License
7  * or later as published by the Free Software Foundation.
8  */
9
10 package eu.svjatoslav.inspector.java.structure;
11
12 import java.lang.reflect.Field;
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 FieldDescriptor implements GraphElement {
19
20         /**
21          * This class corresponds to single field within a java class.
22          */
23
24         public String name;
25         private ClassDescriptor type;
26         private ClassDescriptor parentClass;
27         List<ClassDescriptor> typeArguments = new ArrayList<ClassDescriptor>();
28
29         public FieldDescriptor(final Field field, final ClassDescriptor parent,
30                         final ClassGraph dump) {
31
32                 parentClass = parent;
33
34                 if (!field.getDeclaringClass().getName()
35                                 .equals(parent.fullyQualifiedName))
36                         // if field is inherited, do not index it
37                         return;
38
39                 // if (field.getType().isArray())
40                 // System.out.println("field name: " + field.getName());
41
42                 parent.nameToFieldMap.put(field.getName(), this);
43
44                 name = field.getName();
45                 type = dump.addClass(field.getType());
46                 type.registerReference();
47
48                 final Type genericType = field.getGenericType();
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 genericTypeDescriptor = dump
55                                                         .addClass(cl);
56                                         genericTypeDescriptor.registerReference();
57                                         typeArguments.add(genericTypeDescriptor);
58                                 }
59
60                 }
61         }
62
63         @Override
64         public String getDot() {
65
66                 if (!isVisible())
67                         return "";
68
69                 final StringBuffer result = new StringBuffer();
70
71                 // describe associated types
72                 for (final ClassDescriptor classDescriptor : typeArguments)
73                         if (classDescriptor.isVisible())
74                                 if (classDescriptor.areReferencesShown())
75                                         result.append("    " + getGraphId() + " -> "
76                                                         + classDescriptor.getGraphId() + "[label=\"" + name
77                                                         + "\", color=\"" + classDescriptor.getColor()
78                                                         + "\", style=\"bold\"];\n");
79
80                 if (!type.isVisible())
81                         return result.toString();
82
83                 // main type
84                 boolean showLink = type.areReferencesShown();
85
86                 if (type == parentClass)
87                         showLink = false;
88
89                 if (parentClass.isEnum)
90                         showLink = false;
91
92                 if (showLink)
93                         result.append("    " + getGraphId() + " -> " + type.getGraphId()
94                                         + "[label=\"" + name + "\"," + " color=\""
95                                         + type.getColor() + "\", style=\"bold\"];\n");
96
97                 return result.toString();
98         }
99
100         @Override
101         public String getEmbeddedDot() {
102
103                 if (!isVisible())
104                         return "";
105
106                 final StringBuffer result = new StringBuffer();
107
108                 result.append("        // " + name + "\n");
109                 if (parentClass.isEnum && (type == parentClass)) {
110                         result.append("        <TR><TD colspan=\"2\" PORT=\"" + name);
111                         result.append("\" ALIGN=\"left\"><FONT POINT-SIZE=\"11.0\">");
112                         result.append(name + "</FONT></TD></TR>\n");
113                 } else {
114                         result.append("        <TR><td ALIGN=\"right\">");
115                         result.append("<FONT POINT-SIZE=\"8.0\">");
116                         result.append(type.getClassName(true) + "</FONT>");
117                         result.append("</td><TD PORT=\"" + name);
118                         result.append("\" ALIGN=\"left\"><FONT POINT-SIZE=\"11.0\">");
119                         result.append(name + "</FONT></TD></TR>\n");
120                 }
121                 return result.toString();
122         }
123
124         @Override
125         public String getGraphId() {
126                 return parentClass.getGraphId() + ":" + name;
127         }
128
129         public int getOutsideVisibleReferencesCount() {
130
131                 if (!isVisible())
132                         return 0;
133
134                 if (type != null)
135                         if (type.isVisible())
136                                 return 1;
137
138                 return 0;
139         }
140
141         public ClassDescriptor getType() {
142                 return type;
143         }
144
145         @Override
146         public boolean isVisible() {
147                 if (name.contains("$"))
148                         return false;
149
150                 if (name.equals("serialVersionUID"))
151                         return false;
152
153                 return true;
154         }
155
156 }