2cbb47c36f90421951b7796a30f212b0bfd4cb1d
[javainspect.git] / src / main / java / eu / svjatoslav / inspector / java / structure / FieldDescriptor.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.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         public String name;
21         public ClassDescriptor type;
22         private ClassDescriptor parent;
23         List<ClassDescriptor> typeArguments = new ArrayList<ClassDescriptor>();
24
25         public FieldDescriptor(final Field field, final ClassDescriptor parent,
26                         final ClassGraph dump) {
27
28                 this.parent = parent;
29
30                 if (!field.getDeclaringClass().getName()
31                                 .equals(parent.fullyQualifiedName))
32                         // if field is inherited, do not index it
33                         return;
34
35                 // if (field.getType().isArray())
36                 // System.out.println("field name: " + field.getName());
37
38                 parent.nameToFieldMap.put(field.getName(), this);
39
40                 name = field.getName();
41                 type = dump.addClass(field.getType());
42                 type.registerReference();
43
44                 final Type genericType = field.getGenericType();
45                 if (genericType instanceof ParameterizedType) {
46                         final ParameterizedType pt = (ParameterizedType) genericType;
47                         for (final Type t : pt.getActualTypeArguments())
48                                 if (t instanceof Class) {
49                                         final Class cl = (Class) t;
50                                         final ClassDescriptor genericTypeDescriptor = dump
51                                                         .addClass(cl);
52                                         genericTypeDescriptor.registerReference();
53                                         typeArguments.add(genericTypeDescriptor);
54                                 }
55
56                 }
57         }
58
59         @Override
60         public String getDot() {
61
62                 if (!isVisible())
63                         return "";
64
65                 final StringBuffer result = new StringBuffer();
66
67                 // describe associated types
68                 for (final ClassDescriptor classDescriptor : typeArguments)
69                         if (classDescriptor.isVisible())
70                                 if (classDescriptor.areReferencesShown())
71                                         result.append("    " + getGraphId() + " -> "
72                                                         + classDescriptor.getGraphId() + "[label=\"" + name
73                                                         + "\", color=\"" + classDescriptor.getColor()
74                                                         + "\", style=\"bold\"];\n");
75
76                 if (!type.isVisible())
77                         return result.toString();
78
79                 // main type
80                 boolean showLink = type.areReferencesShown();
81
82                 if (type == parent)
83                         showLink = false;
84
85                 if (parent.isEnum)
86                         showLink = false;
87
88                 if (showLink)
89                         result.append("    " + getGraphId() + " -> " + type.getGraphId()
90                                         + "[label=\"" + name + "\"," + " color=\""
91                                         + type.getColor() + "\", style=\"bold\"];\n");
92
93                 return result.toString();
94         }
95
96         @Override
97         public String getEmbeddedDot() {
98
99                 if (!isVisible())
100                         return "";
101
102                 final StringBuffer result = new StringBuffer();
103
104                 result.append("        // " + name + "\n");
105                 if (parent.isEnum && (type == parent)) {
106                         result.append("        <TR><TD colspan=\"2\" PORT=\"" + name);
107                         result.append("\" ALIGN=\"left\"><FONT POINT-SIZE=\"11.0\">");
108                         result.append(name + "</FONT></TD></TR>\n");
109                 } else {
110                         result.append("        <TR><td ALIGN=\"right\">");
111                         result.append("<FONT POINT-SIZE=\"8.0\">");
112                         result.append(type.getClassName(true) + "</FONT>");
113                         result.append("</td><TD PORT=\"" + name);
114                         result.append("\" ALIGN=\"left\"><FONT POINT-SIZE=\"11.0\">");
115                         result.append(name + "</FONT></TD></TR>\n");
116                 }
117                 return result.toString();
118         }
119
120         @Override
121         public String getGraphId() {
122                 return parent.getGraphId() + ":" + name;
123         }
124
125         @Override
126         public boolean isVisible() {
127                 if (name.contains("$"))
128                         return false;
129
130                 if (name.equals("serialVersionUID"))
131                         return false;
132
133                 return true;
134         }
135
136 }