2 * JavaInspect - Utility to visualize java software
3 * Copyright (C) 2013-2015, 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;
18 public class FieldDescriptor implements GraphElement {
21 * This class corresponds to single field within a java class.
25 private ClassDescriptor type;
26 private final ClassDescriptor parentClass;
27 List<ClassDescriptor> typeArguments = new ArrayList<ClassDescriptor>();
29 public boolean isInherited;
31 public FieldDescriptor(final ClassDescriptor parent) {
35 public void analyzeField(final Field field) {
37 if (!field.getDeclaringClass().getName()
38 .equals(parentClass.getFullyQualifiedName()))
41 // if (field.getType().isArray())
42 // System.out.println("field name: " + field.getName());
44 name = field.getName();
45 type = parentClass.getClassGraph().getOrCreateClassDescriptor(
47 type.registerReference();
49 final Type genericType = field.getGenericType();
50 if (genericType instanceof ParameterizedType) {
51 final ParameterizedType pt = (ParameterizedType) genericType;
52 for (final Type t : pt.getActualTypeArguments())
53 if (t instanceof Class) {
54 final Class cl = (Class) t;
55 final ClassDescriptor genericTypeDescriptor = parentClass
56 .getClassGraph().getOrCreateClassDescriptor(cl);
57 genericTypeDescriptor.registerReference();
58 typeArguments.add(genericTypeDescriptor);
65 public String getDot() {
70 final StringBuffer result = new StringBuffer();
72 // describe associated types
73 for (final ClassDescriptor classDescriptor : typeArguments)
74 if (classDescriptor.isVisible())
75 if (classDescriptor.areReferencesShown())
76 result.append(" " + getGraphId() + " -> "
77 + classDescriptor.getGraphId() + "[label=\"" + name
78 + "\", color=\"" + classDescriptor.getColor()
79 + "\", style=\"bold\"];\n");
81 if (!type.isVisible())
82 return result.toString();
85 boolean showLink = type.areReferencesShown();
87 if (type == parentClass)
90 if (parentClass.isEnum)
94 result.append(" " + getGraphId() + " -> " + type.getGraphId()
95 + "[label=\"" + name + "\"," + " color=\""
96 + type.getColor() + "\", style=\"bold\"];\n");
98 return result.toString();
102 public String getEmbeddedDot() {
107 final StringBuffer result = new StringBuffer();
109 result.append(" // " + name + "\n");
110 if (parentClass.isEnum && (type == parentClass)) {
111 result.append(" <TR><TD colspan=\"2\" PORT=\"" + name);
112 result.append("\" ALIGN=\"left\"><FONT POINT-SIZE=\"11.0\">");
113 result.append(name + "</FONT></TD></TR>\n");
115 result.append(" <TR><td ALIGN=\"right\">");
116 result.append("<FONT POINT-SIZE=\"8.0\">");
117 result.append(type.getClassName(true) + "</FONT>");
118 result.append("</td><TD PORT=\"" + name);
119 result.append("\" ALIGN=\"left\"><FONT POINT-SIZE=\"11.0\">");
120 result.append(name + "</FONT></TD></TR>\n");
122 return result.toString();
126 public String getGraphId() {
127 return parentClass.getGraphId() + ":" + name;
130 public int getOutsideVisibleReferencesCount() {
136 if (type.isVisible())
142 public ClassDescriptor getType() {
147 public boolean isVisible() {
151 if (name.contains("$"))
154 if (name.equals("serialVersionUID"))