Better variable names.
[javainspect.git] / src / main / java / eu / svjatoslav / inspector / java / structure / FieldDescriptor.java
1 /*
2  * JavaInspect - Utility to visualize java software
3  * Copyright (C) 2013-2017, 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 /**
19  * This class corresponds to single field within a java class.
20  */
21
22 public class FieldDescriptor implements GraphElement {
23
24     private final ClassDescriptor parentClassDescriptior;
25     private final List<ClassDescriptor> typeArguments = new ArrayList<ClassDescriptor>();
26     private String name;
27     private ClassDescriptor type;
28     private boolean isInherited;
29
30     public FieldDescriptor(final ClassDescriptor parent) {
31         parentClassDescriptior = parent;
32     }
33
34     public void analyzeField(final Field field) {
35
36         if (!field.getDeclaringClass().getName()
37                 .equals(parentClassDescriptior.getFullyQualifiedName()))
38             isInherited = true;
39
40         name = field.getName();
41         type = parentClassDescriptior.getClassGraph().getOrCreateClassDescriptor(
42                 field.getType());
43         type.registerReference();
44
45         final Type fieldGenericType = field.getGenericType();
46         if (fieldGenericType instanceof ParameterizedType) {
47             final ParameterizedType fieldParameterizedGenericType = (ParameterizedType) fieldGenericType;
48             for (final Type type : fieldParameterizedGenericType.getActualTypeArguments())
49                 if (type instanceof Class) {
50                     final Class aClass = (Class) type;
51                     final ClassDescriptor genericTypeDescriptor = parentClassDescriptior
52                             .getClassGraph().getOrCreateClassDescriptor(aClass);
53                     genericTypeDescriptor.registerReference();
54                     typeArguments.add(genericTypeDescriptor);
55                 }
56
57         }
58     }
59
60     @Override
61     public String getDot() {
62
63         if (!isVisible())
64             return "";
65
66         final StringBuffer result = new StringBuffer();
67
68         // describe associated types
69         for (final ClassDescriptor classDescriptor : typeArguments)
70             if (classDescriptor.isVisible())
71                 if (classDescriptor.areReferencesShown())
72                     result.append("    " + getGraphId() + " -> "
73                             + classDescriptor.getGraphId() + "[label=\"" + name
74                             + "\", color=\"" + classDescriptor.getColor()
75                             + "\", style=\"bold\"];\n");
76
77         if (!type.isVisible())
78             return result.toString();
79
80         // main type
81         boolean showLink = type.areReferencesShown();
82
83         if (type == parentClassDescriptior)
84             showLink = false;
85
86         if (parentClassDescriptior.isEnum)
87             showLink = false;
88
89         if (showLink)
90             result.append("    " + getGraphId() + " -> " + type.getGraphId()
91                     + "[label=\"" + name + "\"," + " color=\""
92                     + type.getColor() + "\", style=\"bold\"];\n");
93
94         return result.toString();
95     }
96
97     @Override
98     public String getEmbeddedDot() {
99
100         if (!isVisible())
101             return "";
102
103         final StringBuffer result = new StringBuffer();
104
105         result.append("        // " + name + "\n");
106         if (parentClassDescriptior.isEnum && (type == parentClassDescriptior)) {
107             result.append("        <TR><TD colspan=\"2\" PORT=\"" + name);
108             result.append("\" ALIGN=\"left\"><FONT POINT-SIZE=\"11.0\">");
109             result.append(name + "</FONT></TD></TR>\n");
110         } else {
111             result.append("        <TR><td ALIGN=\"right\">");
112             result.append("<FONT POINT-SIZE=\"8.0\">");
113             result.append(type.getClassName(true) + "</FONT>");
114             result.append("</td><TD PORT=\"" + name);
115             result.append("\" ALIGN=\"left\"><FONT POINT-SIZE=\"11.0\">");
116             result.append(name + "</FONT></TD></TR>\n");
117         }
118         return result.toString();
119     }
120
121     @Override
122     public String getGraphId() {
123         return parentClassDescriptior.getGraphId() + ":" + name;
124     }
125
126     protected int getOutsideVisibleReferencesCount() {
127
128         if (!isVisible())
129             return 0;
130
131         if (type != null)
132             if (type.isVisible())
133                 return 1;
134
135         return 0;
136     }
137
138     protected ClassDescriptor getType() {
139         return type;
140     }
141
142     @Override
143     public boolean isVisible() {
144         if (isInherited)
145             return false;
146
147         if (name.contains("$"))
148             return false;
149
150         return !name.equals("serialVersionUID");
151
152     }
153
154 }