2 * JavaInspect - Utility to visualize java software
3 * Copyright (C) 2013, 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 2 of the GNU General Public License
7 * 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 ClassDescriptor parentClass;
27 List<ClassDescriptor> typeArguments = new ArrayList<ClassDescriptor>();
29 public FieldDescriptor(final Field field, final ClassDescriptor parent,
30 final ClassGraph dump) {
34 if (!field.getDeclaringClass().getName()
35 .equals(parent.fullyQualifiedName))
36 // if field is inherited, do not index it
39 // if (field.getType().isArray())
40 // System.out.println("field name: " + field.getName());
42 parent.nameToFieldMap.put(field.getName(), this);
44 name = field.getName();
45 type = dump.addClass(field.getType());
46 type.registerReference();
48 final Type genericType = field.getGenericType();
49 if (genericType instanceof ParameterizedType) {
50 final ParameterizedType pt = (ParameterizedType) genericType;
51 for (final Type t : pt.getActualTypeArguments())
52 if (t instanceof Class) {
53 final Class cl = (Class) t;
54 final ClassDescriptor genericTypeDescriptor = dump
56 genericTypeDescriptor.registerReference();
57 typeArguments.add(genericTypeDescriptor);
64 public String getDot() {
69 final StringBuffer result = new StringBuffer();
71 // describe associated types
72 for (final ClassDescriptor classDescriptor : typeArguments)
73 if (classDescriptor.isVisible())
74 if (classDescriptor.areReferencesShown())
75 result.append(" " + getGraphId() + " -> "
76 + classDescriptor.getGraphId() + "[label=\"" + name
77 + "\", color=\"" + classDescriptor.getColor()
78 + "\", style=\"bold\"];\n");
80 if (!type.isVisible())
81 return result.toString();
84 boolean showLink = type.areReferencesShown();
86 if (type == parentClass)
89 if (parentClass.isEnum)
93 result.append(" " + getGraphId() + " -> " + type.getGraphId()
94 + "[label=\"" + name + "\"," + " color=\""
95 + type.getColor() + "\", style=\"bold\"];\n");
97 return result.toString();
101 public String getEmbeddedDot() {
106 final StringBuffer result = new StringBuffer();
108 result.append(" // " + name + "\n");
109 if (parentClass.isEnum && (type == parentClass)) {
110 result.append(" <TR><TD colspan=\"2\" PORT=\"" + name);
111 result.append("\" ALIGN=\"left\"><FONT POINT-SIZE=\"11.0\">");
112 result.append(name + "</FONT></TD></TR>\n");
114 result.append(" <TR><td ALIGN=\"right\">");
115 result.append("<FONT POINT-SIZE=\"8.0\">");
116 result.append(type.getClassName(true) + "</FONT>");
117 result.append("</td><TD PORT=\"" + name);
118 result.append("\" ALIGN=\"left\"><FONT POINT-SIZE=\"11.0\">");
119 result.append(name + "</FONT></TD></TR>\n");
121 return result.toString();
125 public String getGraphId() {
126 return parentClass.getGraphId() + ":" + name;
129 public int getOutsideVisibleReferencesCount() {
135 if (type.isVisible())
141 public ClassDescriptor getType() {
146 public boolean isVisible() {
147 if (name.contains("$"))
150 if (name.equals("serialVersionUID"))