2 * JavaInspect - Utility to visualize java software
3 * Copyright (C) 2013-2018, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
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.
10 package eu.svjatoslav.inspector.java.structure;
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;
19 * This class corresponds to single field within a java class.
22 public class FieldDescriptor implements GraphElement {
24 private final ClassDescriptor parentClassDescriptor;
25 private final List<ClassDescriptor> typeArguments = new ArrayList<>();
27 private ClassDescriptor type;
28 private boolean isInherited;
30 public FieldDescriptor(final ClassDescriptor parent) {
31 parentClassDescriptor = parent;
34 public void analyzeField(final Field field) {
36 if (!field.getDeclaringClass().getName()
37 .equals(parentClassDescriptor.getFullyQualifiedName()))
40 name = field.getName();
41 type = parentClassDescriptor.getClassGraph().getOrCreateClassDescriptor(
43 type.registerReference();
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 = parentClassDescriptor
52 .getClassGraph().getOrCreateClassDescriptor(aClass);
53 genericTypeDescriptor.registerReference();
54 typeArguments.add(genericTypeDescriptor);
61 public String getDot() {
66 final StringBuilder result = new StringBuilder();
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");
77 if (type == null) return result.toString();
78 if (!type.isVisible())
79 return result.toString();
82 boolean showLink = type.areReferencesShown();
84 if (type == parentClassDescriptor)
87 if (parentClassDescriptor.isEnum)
91 result.append(" " + getGraphId() + " -> " + type.getGraphId()
92 + "[label=\"" + name + "\"," + " color=\""
93 + type.getColor() + "\", style=\"bold\"];\n");
95 return result.toString();
99 public String getEmbeddedDot() {
104 final StringBuilder result = new StringBuilder();
106 result.append(" // " + name + "\n");
107 if (parentClassDescriptor.isEnum && (type == parentClassDescriptor)) {
108 result.append(" <TR><TD colspan=\"2\" PORT=\"" + name);
109 result.append("\" ALIGN=\"left\"><FONT POINT-SIZE=\"11.0\">");
110 result.append(name + "</FONT></TD></TR>\n");
112 result.append(" <TR><td ALIGN=\"right\">");
113 result.append("<FONT POINT-SIZE=\"8.0\">");
114 result.append(describeType() + "</FONT>");
115 result.append("</td><TD PORT=\"" + name);
116 result.append("\" ALIGN=\"left\"><FONT POINT-SIZE=\"11.0\">");
117 result.append(name + "</FONT></TD></TR>\n");
119 return result.toString();
122 private String describeType() {
123 if (type == null) return "-null-";
124 return type.getClassName(true);
128 public String getGraphId() {
129 return parentClassDescriptor.getGraphId() + ":" + name;
132 protected int getOutsideVisibleReferencesCount() {
138 if (type.isVisible())
144 protected ClassDescriptor getType() {
149 public boolean isVisible() {
153 if (name.contains("$"))
156 return !name.equals("serialVersionUID");