2 * JavaInspect - Utility to visualize java software
3 * Copyright (C) 2013-2017, 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 parentClassDescriptior;
25 private final List<ClassDescriptor> typeArguments = new ArrayList<ClassDescriptor>();
27 private ClassDescriptor type;
28 private boolean isInherited;
30 public FieldDescriptor(final ClassDescriptor parent) {
31 parentClassDescriptior = parent;
34 public void analyzeField(final Field field) {
36 if (!field.getDeclaringClass().getName()
37 .equals(parentClassDescriptior.getFullyQualifiedName()))
40 name = field.getName();
41 type = parentClassDescriptior.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 = parentClassDescriptior
52 .getClassGraph().getOrCreateClassDescriptor(aClass);
53 genericTypeDescriptor.registerReference();
54 typeArguments.add(genericTypeDescriptor);
61 public String getDot() {
66 final StringBuffer result = new StringBuffer();
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.isVisible())
78 return result.toString();
81 boolean showLink = type.areReferencesShown();
83 if (type == parentClassDescriptior)
86 if (parentClassDescriptior.isEnum)
90 result.append(" " + getGraphId() + " -> " + type.getGraphId()
91 + "[label=\"" + name + "\"," + " color=\""
92 + type.getColor() + "\", style=\"bold\"];\n");
94 return result.toString();
98 public String getEmbeddedDot() {
103 final StringBuffer result = new StringBuffer();
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");
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");
118 return result.toString();
122 public String getGraphId() {
123 return parentClassDescriptior.getGraphId() + ":" + name;
126 protected int getOutsideVisibleReferencesCount() {
132 if (type.isVisible())
138 protected ClassDescriptor getType() {
143 public boolean isVisible() {
147 if (name.contains("$"))
150 return !name.equals("serialVersionUID");