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.javainspect.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 public ClassDescriptor type;
22 private ClassDescriptor parent;
23 List<ClassDescriptor> typeArguments = new ArrayList<ClassDescriptor>();
25 public FieldDescriptor(final Field field, final ClassDescriptor parent,
26 final ClassGraph dump) {
30 if (!field.getDeclaringClass().getName()
31 .equals(parent.fullyQualifiedName))
32 // if field is inherited, do not index it
35 // if (field.getType().isArray())
36 // System.out.println("field name: " + field.getName());
38 parent.nameToFieldMap.put(field.getName(), this);
40 name = field.getName();
41 type = dump.addClass(field.getType());
42 type.registerReference();
44 final Type genericType = field.getGenericType();
45 if (genericType instanceof ParameterizedType) {
46 final ParameterizedType pt = (ParameterizedType) genericType;
47 for (final Type t : pt.getActualTypeArguments())
48 if (t instanceof Class) {
49 final Class cl = (Class) t;
50 final ClassDescriptor genericTypeDescriptor = dump
52 genericTypeDescriptor.registerReference();
53 typeArguments.add(genericTypeDescriptor);
60 public String getDot() {
65 final StringBuffer result = new StringBuffer();
67 // describe associated types
68 for (final ClassDescriptor classDescriptor : typeArguments)
69 if (classDescriptor.isVisible())
70 if (classDescriptor.areReferencesShown())
71 result.append(" " + getGraphId() + " -> "
72 + classDescriptor.getGraphId() + "[label=\"" + name
73 + "\", color=\"" + classDescriptor.getColor()
74 + "\", style=\"bold\"];\n");
76 if (!type.isVisible())
77 return result.toString();
80 boolean showLink = type.areReferencesShown();
89 result.append(" " + getGraphId() + " -> " + type.getGraphId()
90 + "[label=\"" + name + "\"," + " color=\""
91 + type.getColor() + "\", style=\"bold\"];\n");
93 return result.toString();
97 public String getEmbeddedDot() {
102 final StringBuffer result = new StringBuffer();
104 result.append(" // " + name + "\n");
105 if (parent.isEnum && (type == parent)) {
106 result.append(" <TR><TD colspan=\"2\" PORT=\"" + name);
107 result.append("\" ALIGN=\"left\"><FONT POINT-SIZE=\"11.0\">");
108 result.append(name + "</FONT></TD></TR>\n");
110 result.append(" <TR><td ALIGN=\"right\">");
111 result.append("<FONT POINT-SIZE=\"8.0\">");
112 result.append(type.getClassName(true) + "</FONT>");
113 result.append("</td><TD PORT=\"" + name);
114 result.append("\" ALIGN=\"left\"><FONT POINT-SIZE=\"11.0\">");
115 result.append(name + "</FONT></TD></TR>\n");
117 return result.toString();
121 public String getGraphId() {
122 return parent.getGraphId() + ":" + name;
126 public boolean isVisible() {
127 if (name.contains("$"))
130 if (name.equals("serialVersionUID"))