Updated copyright info
[javainspect.git] / src / main / java / eu / svjatoslav / inspector / java / structure / FieldDescriptor.java
index d0a607b..254b627 100755 (executable)
@@ -1,6 +1,6 @@
 /*
  * JavaInspect - Utility to visualize java software
- * Copyright (C) 2013-2015, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
+ * Copyright (C) 2013-2020, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of version 3 of the GNU Lesser General Public License
@@ -15,146 +15,146 @@ import java.lang.reflect.Type;
 import java.util.ArrayList;
 import java.util.List;
 
-public class FieldDescriptor implements GraphElement {
-
-       /**
-        * This class corresponds to single field within a java class.
-        */
-
-       public String name;
-       private ClassDescriptor type;
-       private final ClassDescriptor parentClass;
-       List<ClassDescriptor> typeArguments = new ArrayList<ClassDescriptor>();
-
-       public boolean isInherited;
-
-       public FieldDescriptor(final ClassDescriptor parent) {
-               parentClass = parent;
-       }
-
-       public void analyzeField(final Field field) {
-
-               if (!field.getDeclaringClass().getName()
-                               .equals(parentClass.classFullyQualifiedName))
-                       isInherited = true;
-
-               // if (field.getType().isArray())
-               // System.out.println("field name: " + field.getName());
-
-               name = field.getName();
-               type = parentClass.getClassGraph().getOrCreateClassDescriptor(
-                               field.getType());
-               type.registerReference();
-
-               final Type genericType = field.getGenericType();
-               if (genericType instanceof ParameterizedType) {
-                       final ParameterizedType pt = (ParameterizedType) genericType;
-                       for (final Type t : pt.getActualTypeArguments())
-                               if (t instanceof Class) {
-                                       final Class cl = (Class) t;
-                                       final ClassDescriptor genericTypeDescriptor = parentClass
-                                                       .getClassGraph().getOrCreateClassDescriptor(cl);
-                                       genericTypeDescriptor.registerReference();
-                                       typeArguments.add(genericTypeDescriptor);
-                               }
-
-               }
-       }
-
-       @Override
-       public String getDot() {
-
-               if (!isVisible())
-                       return "";
-
-               final StringBuffer result = new StringBuffer();
-
-               // describe associated types
-               for (final ClassDescriptor classDescriptor : typeArguments)
-                       if (classDescriptor.isVisible())
-                               if (classDescriptor.areReferencesShown())
-                                       result.append("    " + getGraphId() + " -> "
-                                                       + classDescriptor.getGraphId() + "[label=\"" + name
-                                                       + "\", color=\"" + classDescriptor.getColor()
-                                                       + "\", style=\"bold\"];\n");
-
-               if (!type.isVisible())
-                       return result.toString();
-
-               // main type
-               boolean showLink = type.areReferencesShown();
-
-               if (type == parentClass)
-                       showLink = false;
-
-               if (parentClass.isEnum)
-                       showLink = false;
-
-               if (showLink)
-                       result.append("    " + getGraphId() + " -> " + type.getGraphId()
-                                       + "[label=\"" + name + "\"," + " color=\""
-                                       + type.getColor() + "\", style=\"bold\"];\n");
-
-               return result.toString();
-       }
-
-       @Override
-       public String getEmbeddedDot() {
+/**
+ * This class corresponds to single field within a java class.
+ */
 
-               if (!isVisible())
-                       return "";
+public class FieldDescriptor implements GraphElement {
 
-               final StringBuffer result = new StringBuffer();
+    private final ClassDescriptor parentClassDescriptor;
+    private final List<ClassDescriptor> typeArguments = new ArrayList<>();
+    private String name;
+    private ClassDescriptor type;
+    private boolean isInherited;
+
+    public FieldDescriptor(final ClassDescriptor parent) {
+        parentClassDescriptor = parent;
+    }
+
+    public void analyzeField(final Field field) {
+
+        if (!field.getDeclaringClass().getName()
+                .equals(parentClassDescriptor.getFullyQualifiedName()))
+            isInherited = true;
+
+        name = field.getName();
+        type = parentClassDescriptor.getClassGraph().getOrCreateClassDescriptor(
+                field.getType());
+        type.registerReference();
+
+        final Type fieldGenericType = field.getGenericType();
+        if (fieldGenericType instanceof ParameterizedType) {
+            final ParameterizedType fieldParameterizedGenericType = (ParameterizedType) fieldGenericType;
+            for (final Type type : fieldParameterizedGenericType.getActualTypeArguments())
+                if (type instanceof Class) {
+                    final Class aClass = (Class) type;
+                    final ClassDescriptor genericTypeDescriptor = parentClassDescriptor
+                            .getClassGraph().getOrCreateClassDescriptor(aClass);
+                    genericTypeDescriptor.registerReference();
+                    typeArguments.add(genericTypeDescriptor);
+                }
+
+        }
+    }
+
+    @Override
+    public String getDot() {
+
+        if (!isVisible())
+            return "";
+
+        final StringBuilder result = new StringBuilder();
+
+        // describe associated types
+        for (final ClassDescriptor classDescriptor : typeArguments)
+            if (classDescriptor.isVisible())
+                if (classDescriptor.areReferencesShown())
+                    result.append("    " + getGraphId() + " -> "
+                            + classDescriptor.getGraphId() + "[label=\"" + name
+                            + "\", color=\"" + classDescriptor.getColor()
+                            + "\", style=\"bold\"];\n");
+
+        if (type == null) return result.toString();
+        if (!type.isVisible())
+            return result.toString();
+
+        // main type
+        boolean showLink = type.areReferencesShown();
+
+        if (type == parentClassDescriptor)
+            showLink = false;
+
+        if (parentClassDescriptor.isEnum)
+            showLink = false;
+
+        if (showLink)
+            result.append("    " + getGraphId() + " -> " + type.getGraphId()
+                    + "[label=\"" + name + "\"," + " color=\""
+                    + type.getColor() + "\", style=\"bold\"];\n");
+
+        return result.toString();
+    }
+
+    @Override
+    public String getEmbeddedDot() {
+
+        if (!isVisible())
+            return "";
+
+        final StringBuilder result = new StringBuilder();
+
+        result.append("        // " + name + "\n");
+        if (parentClassDescriptor.isEnum && (type == parentClassDescriptor)) {
+            result.append("        <TR><TD colspan=\"2\" PORT=\"" + name);
+            result.append("\" ALIGN=\"left\"><FONT POINT-SIZE=\"11.0\">");
+            result.append(name + "</FONT></TD></TR>\n");
+        } else {
+            result.append("        <TR><td ALIGN=\"right\">");
+            result.append("<FONT POINT-SIZE=\"8.0\">");
+            result.append(describeType() + "</FONT>");
+            result.append("</td><TD PORT=\"" + name);
+            result.append("\" ALIGN=\"left\"><FONT POINT-SIZE=\"11.0\">");
+            result.append(name + "</FONT></TD></TR>\n");
+        }
+        return result.toString();
+    }
+
+    private String describeType() {
+        if (type == null) return "-null-";
+        return type.getClassName(true);
+    }
+
+    @Override
+    public String getGraphId() {
+        return parentClassDescriptor.getGraphId() + ":" + name;
+    }
+
+    protected int getOutsideVisibleReferencesCount() {
+
+        if (!isVisible())
+            return 0;
+
+        if (type != null)
+            if (type.isVisible())
+                return 1;
+
+        return 0;
+    }
+
+    protected ClassDescriptor getType() {
+        return type;
+    }
+
+    @Override
+    public boolean isVisible() {
+        if (isInherited)
+            return false;
 
-               result.append("        // " + name + "\n");
-               if (parentClass.isEnum && (type == parentClass)) {
-                       result.append("        <TR><TD colspan=\"2\" PORT=\"" + name);
-                       result.append("\" ALIGN=\"left\"><FONT POINT-SIZE=\"11.0\">");
-                       result.append(name + "</FONT></TD></TR>\n");
-               } else {
-                       result.append("        <TR><td ALIGN=\"right\">");
-                       result.append("<FONT POINT-SIZE=\"8.0\">");
-                       result.append(type.getClassName(true) + "</FONT>");
-                       result.append("</td><TD PORT=\"" + name);
-                       result.append("\" ALIGN=\"left\"><FONT POINT-SIZE=\"11.0\">");
-                       result.append(name + "</FONT></TD></TR>\n");
-               }
-               return result.toString();
-       }
-
-       @Override
-       public String getGraphId() {
-               return parentClass.getGraphId() + ":" + name;
-       }
-
-       public int getOutsideVisibleReferencesCount() {
-
-               if (!isVisible())
-                       return 0;
+        if (name.contains("$"))
+            return false;
 
-               if (type != null)
-                       if (type.isVisible())
-                               return 1;
-
-               return 0;
-       }
-
-       public ClassDescriptor getType() {
-               return type;
-       }
-
-       @Override
-       public boolean isVisible() {
-               if (isInherited)
-                       return false;
-
-               if (name.contains("$"))
-                       return false;
-
-               if (name.equals("serialVersionUID"))
-                       return false;
+        return !name.equals("serialVersionUID");
 
-               return true;
-       }
+    }
 
-}
\ No newline at end of file
+}