--- /dev/null
+/*
+ * JavaInspect - Utility to visualize java software
+ * Copyright (C) 2013, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of version 2 of the GNU General Public License
+ * as published by the Free Software Foundation.
+ */
+
+package eu.svjatoslav.inspector.java.structure;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Describes single class instance
+ */
+public class ClassDescriptor implements GraphElement {
+
+ private static final int MAX_REFERECNES_COUNT = 10;
+
+ public final String fullyQualifiedName;
+
+ Map<String, FieldDescriptor> nameToFieldMap = new HashMap<String, FieldDescriptor>();
+
+ public List<MethodDescriptor> methods = new ArrayList<MethodDescriptor>();
+
+ /**
+ * Incoming arrows will have this color.
+ */
+ private String distinctiveReferenceColor;
+
+ private String interfaceColor;
+
+ private String superClassColor;
+
+ boolean isEnum;
+
+ boolean isInterface;
+
+ boolean isArray;
+
+ private final ClassGraph dump;
+
+ List<ClassDescriptor> interfaces = new ArrayList<ClassDescriptor>();
+
+ ClassDescriptor superClass;
+
+ /**
+ * Amount of field and method references pointing to this class.
+ */
+ private int referenceCount = 0;
+
+ public ClassDescriptor(final Class<? extends Object> clazz, final ClassGraph dump) {
+ this.dump = dump;
+
+ fullyQualifiedName = clazz.getName();
+ dump.nameToClassMap.put(fullyQualifiedName, this);
+
+ isArray = clazz.isArray();
+
+ if (isArray) {
+ final Class<?> componentType = clazz.getComponentType();
+ dump.addClass(componentType);
+ }
+
+ // System.out.println("class: " + fullyQualifiedName);
+
+ isEnum = clazz.isEnum();
+
+ isInterface = clazz.isInterface();
+
+ if (!isVisible())
+ return;
+
+ indexFields(clazz.getDeclaredFields());
+ indexFields(clazz.getFields());
+
+ indexMethods(clazz);
+
+ for (final Class interfaceClass : clazz.getInterfaces())
+ interfaces.add(dump.addClass(interfaceClass));
+
+ superClass = dump.addClass(clazz.getSuperclass());
+ }
+
+ public boolean areReferencesShown() {
+ return referenceCount <= MAX_REFERECNES_COUNT;
+ }
+
+ public void enlistFieldReferences(final StringBuffer result) {
+ if (nameToFieldMap.isEmpty())
+ return;
+
+ result.append("\n");
+ result.append(" // field references to other classes\n");
+ for (final Map.Entry<String, FieldDescriptor> entry : nameToFieldMap
+ .entrySet())
+ result.append(entry.getValue().getDot());
+ }
+
+ public void enlistFields(final StringBuffer result) {
+ if (nameToFieldMap.isEmpty())
+ return;
+
+ result.append("\n");
+ result.append(" // fields:\n");
+
+ // enlist fields
+ for (final Map.Entry<String, FieldDescriptor> entry : nameToFieldMap
+ .entrySet())
+ result.append(entry.getValue().getEmbeddedDot());
+ }
+
+ public void enlistImplementedInterfaces(final StringBuffer result) {
+ if (interfaces.isEmpty())
+ return;
+
+ result.append("\n");
+ result.append(" // interfaces implemented by class: "
+ + fullyQualifiedName + "\n");
+
+ for (final ClassDescriptor interfaceDescriptor : interfaces) {
+ if (!interfaceDescriptor.isVisible())
+ continue;
+
+ result.append(" " + interfaceDescriptor.getGraphId() + " -> "
+ + getGraphId() + "[style=\"dotted, tapered\", color=\""
+ + interfaceDescriptor.getInterfaceColor()
+ + "\", penwidth=20, dir=\"forward\"];\n");
+ }
+ }
+
+ public void enlistMethodReferences(final StringBuffer result) {
+ if (methods.isEmpty())
+ return;
+
+ result.append("\n");
+ result.append(" // method references to other classes\n");
+ for (final MethodDescriptor methodDescriptor : methods)
+ result.append(methodDescriptor.getDot());
+ }
+
+ public void enlistMethods(final StringBuffer result) {
+ if (methods.isEmpty())
+ return;
+
+ result.append("\n");
+ result.append(" // methods:\n");
+
+ // enlist methods
+ for (final MethodDescriptor methodDescriptor : methods)
+ result.append(methodDescriptor.getEmbeddedDot());
+ }
+
+ public void enlistSuperClass(final StringBuffer result) {
+ if (superClass == null)
+ return;
+
+ if (!superClass.isVisible())
+ return;
+
+ result.append("\n");
+ result.append(" // super class for: " + fullyQualifiedName + "\n");
+
+ result.append(" " + superClass.getGraphId() + " -> " + getGraphId()
+ + "[style=\"tapered\", color=\""
+ + superClass.getSuperClassColor()
+ + "\", penwidth=10, dir=\"forward\"];\n");
+ }
+
+ public void generateDotHeader(final StringBuffer result) {
+ result.append("\n");
+ result.append("// Class: " + fullyQualifiedName + "\n");
+
+ result.append(" " + getGraphId() + "[label=<<TABLE "
+ + getBackgroundColor() + " BORDER=\"" + getBorderWidth()
+ + "\" CELLBORDER=\"1\" CELLSPACING=\"0\">\n");
+
+ result.append("\n");
+ result.append(" // class descriptor header\n");
+ result.append(" <TR><TD colspan=\"2\" PORT=\"f0\">"
+ + "<FONT POINT-SIZE=\"8.0\" >" + getPackageName()
+ + "</FONT><br/>" + "<FONT POINT-SIZE=\"25.0\"><B>"
+ + getClassName(false) + "</B></FONT>" + "</TD></TR>\n");
+ }
+
+ public List<FieldDescriptor> getAllFields() {
+ final List<FieldDescriptor> result = new ArrayList<FieldDescriptor>();
+
+ for (final Map.Entry<String, FieldDescriptor> entry : nameToFieldMap
+ .entrySet())
+ result.add(entry.getValue());
+
+ return result;
+ }
+
+ public String getBackgroundColor() {
+ String bgColor = "";
+
+ if (isEnum)
+ bgColor = "bgcolor=\"navajowhite2\"";
+
+ if (isInterface)
+ bgColor = "bgcolor=\"darkslategray1\"";
+
+ return bgColor;
+ }
+
+ public String getBorderWidth() {
+
+ if (!areReferencesShown())
+ return "4";
+ return "1";
+ }
+
+ public String getClassName(final boolean differentiateArray) {
+
+ final int i = fullyQualifiedName.lastIndexOf('.');
+
+ String result = fullyQualifiedName.substring(i + 1);
+
+ if (isArray)
+ result = result.substring(0, result.length() - 1);
+
+ if (differentiateArray)
+ if (isArray)
+ result += " []";
+
+ return result;
+ }
+
+ public String getColor() {
+ if (getDistinctiveColor() == null)
+ setDistinctiveColor(Utils.getNextDarkColor());
+
+ return getDistinctiveColor();
+ }
+
+ public String getDistinctiveColor() {
+ return distinctiveReferenceColor;
+ }
+
+ @Override
+ public String getDot() {
+ if (!isVisible())
+ return "";
+
+ if (isArray)
+ return "";
+
+ final StringBuffer result = new StringBuffer();
+
+ generateDotHeader(result);
+
+ enlistFields(result);
+
+ enlistMethods(result);
+
+ result.append(" </TABLE>>, shape=\"none\"];\n");
+
+ enlistFieldReferences(result);
+
+ enlistMethodReferences(result);
+
+ enlistImplementedInterfaces(result);
+
+ enlistSuperClass(result);
+
+ return result.toString();
+ }
+
+ @Override
+ public String getEmbeddedDot() {
+ return null;
+ }
+
+ @Override
+ public String getGraphId() {
+ final String result = "class_"
+ + fullyQualifiedName.replace('.', '_').replace(";", "")
+ .replace("[L", "");
+ return result;
+ }
+
+ public String getInterfaceColor() {
+ if (interfaceColor == null)
+ interfaceColor = Utils.getNextLightColor();
+
+ return interfaceColor;
+ }
+
+ public String getPackageName() {
+
+ final int i = fullyQualifiedName.lastIndexOf('.');
+
+ if (i == -1)
+ return "";
+
+ return fullyQualifiedName.substring(0, i).replace("[L", "");
+ }
+
+ // public String getReadableName() {
+ //
+ // // do not print full class name for well known system classes
+ // final String packageName = getPackageName();
+ //
+ // if (packageName.equals("java.util"))
+ // return getClassName();
+ //
+ // if (packageName.equals("java.lang"))
+ // return getClassName();
+ //
+ // return fullyQualifiedName;
+ // }
+
+ public String getSuperClassColor() {
+ if (superClassColor == null)
+ superClassColor = Utils.getNextLightColor();
+
+ return superClassColor;
+ }
+
+ public void indexFields(final Field[] fields) {
+ for (final Field field : fields) {
+ if (nameToFieldMap.containsKey(field.getName()))
+ continue;
+
+ final FieldDescriptor fieldDescriptor = new FieldDescriptor(field,
+ this, dump);
+
+ }
+ }
+
+ private void indexMethods(final Class<? extends Object> clazz) {
+ final Method[] methods = clazz.getMethods();
+
+ for (final Method method : methods)
+ new MethodDescriptor(method, this, dump);
+
+ }
+
+ @Override
+ public boolean isVisible() {
+
+ if (Utils.isSystemDataType(fullyQualifiedName))
+ return false;
+
+ if (Utils.isSystemPackage(fullyQualifiedName))
+ return false;
+
+ return true;
+ }
+
+ public void registerReference() {
+ referenceCount++;
+ }
+
+ public void setDistinctiveColor(final String distinctiveColor) {
+ distinctiveReferenceColor = distinctiveColor;
+ }
+}
--- /dev/null
+/*
+ * JavaInspect - Utility to visualize java software
+ * Copyright (C) 2013, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of version 2 of the GNU General Public License
+ * as published by the Free Software Foundation.
+ */
+
+package eu.svjatoslav.inspector.java.structure;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.util.HashMap;
+import java.util.Map;
+
+import eu.svjatoslav.commons.file.CommonPathResolver;
+
+public class ClassGraph {
+
+ /**
+ * Maps class fully qualified names to class descriptors.
+ */
+ Map<String, ClassDescriptor> nameToClassMap = new HashMap<String, ClassDescriptor>();
+
+ public ClassGraph() {
+ }
+
+ public ClassGraph(final Class<? extends Object> clazz) {
+ addClass(clazz);
+ }
+
+ public ClassGraph(final Object root) {
+ addClass(root.getClass());
+ }
+
+ public ClassDescriptor addClass(final Class<? extends Object> clazz) {
+
+ if (clazz == null)
+ return null;
+
+ final String className = clazz.getName();
+
+ if (nameToClassMap.containsKey(className))
+ return nameToClassMap.get(className);
+
+ return new ClassDescriptor(clazz, this);
+ }
+
+ public ClassDescriptor addObject(final Object object) {
+ return addClass(object.getClass());
+ }
+
+ public void generateGraph(final String graphName) {
+ generateGraph(graphName, false);
+ }
+
+ public void generateGraph(final String graphName, final boolean keepDotFile) {
+
+ final String desktopPath = CommonPathResolver.getDesktopDirectory()
+ .getAbsolutePath() + "/";
+
+ final String dotFilePath = desktopPath + graphName + ".dot";
+ final String imageFilePath = desktopPath + graphName + ".png";
+
+ System.out.println("Dot file path:" + dotFilePath);
+
+ try {
+ // write DOT file to disk
+ final PrintWriter out = new PrintWriter(dotFilePath);
+ out.write(getDot());
+ out.close();
+
+ // execute GraphViz to visualize graph
+ try {
+ Runtime.getRuntime()
+ .exec(new String[] { "dot", "-Tpng", dotFilePath, "-o",
+ imageFilePath }).waitFor();
+ } catch (final InterruptedException e) {
+ } finally {
+ }
+
+ if (!keepDotFile) {
+ // delete dot file
+ final File dotFile = new File(dotFilePath);
+ dotFile.delete();
+ }
+ } catch (final IOException e) {
+ System.err.println(e);
+ }
+ }
+
+ private String getDot() {
+ final StringBuffer result = new StringBuffer();
+
+ result.append("digraph Java {\n");
+ result.append("graph [rankdir=LR, overlap = false, concentrate=true];\n");
+
+ for (final Map.Entry<String, ClassDescriptor> entry : nameToClassMap
+ .entrySet())
+ result.append(entry.getValue().getDot());
+
+ result.append("}\n");
+
+ final String resultStr = result.toString();
+ return resultStr;
+ }
+
+}
--- /dev/null
+/*
+ * JavaInspect - Utility to visualize java software
+ * Copyright (C) 2013, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of version 2 of the GNU General Public License
+ * as published by the Free Software Foundation.
+ */
+
+package eu.svjatoslav.inspector.java.structure;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.ParameterizedType;
+import java.lang.reflect.Type;
+import java.util.ArrayList;
+import java.util.List;
+
+public class FieldDescriptor implements GraphElement {
+
+ public String name;
+ public ClassDescriptor type;
+ private ClassDescriptor parent;
+ List<ClassDescriptor> typeArguments = new ArrayList<ClassDescriptor>();
+
+ public FieldDescriptor(final Field field, final ClassDescriptor parent,
+ final ClassGraph dump) {
+
+ this.parent = parent;
+
+ if (!field.getDeclaringClass().getName()
+ .equals(parent.fullyQualifiedName))
+ // if field is inherited, do not index it
+ return;
+
+ // if (field.getType().isArray())
+ // System.out.println("field name: " + field.getName());
+
+ parent.nameToFieldMap.put(field.getName(), this);
+
+ name = field.getName();
+ type = dump.addClass(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 = dump
+ .addClass(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 == parent)
+ showLink = false;
+
+ if (parent.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 StringBuffer result = new StringBuffer();
+
+ result.append(" // " + name + "\n");
+ if (parent.isEnum && (type == parent)) {
+ 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 parent.getGraphId() + ":" + name;
+ }
+
+ @Override
+ public boolean isVisible() {
+ if (name.contains("$"))
+ return false;
+
+ if (name.equals("serialVersionUID"))
+ return false;
+
+ return true;
+ }
+
+}
\ No newline at end of file
--- /dev/null
+/*
+ * JavaInspect - Utility to visualize java software
+ * Copyright (C) 2013, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of version 2 of the GNU General Public License
+ * as published by the Free Software Foundation.
+ */
+
+package eu.svjatoslav.inspector.java.structure;
+
+public interface GraphElement {
+
+ public String getDot();
+
+ public String getEmbeddedDot();
+
+ public String getGraphId();
+
+ public boolean isVisible();
+
+}
--- /dev/null
+/*
+ * JavaInspect - Utility to visualize java software
+ * Copyright (C) 2013, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of version 2 of the GNU General Public License
+ * as published by the Free Software Foundation.
+ */
+
+package eu.svjatoslav.inspector.java.structure;
+
+import java.lang.reflect.Method;
+import java.lang.reflect.ParameterizedType;
+import java.lang.reflect.Type;
+import java.util.ArrayList;
+import java.util.List;
+
+public class MethodDescriptor implements GraphElement {
+
+ public String name;
+ public ClassDescriptor returnType;
+ private final ClassDescriptor parent;
+
+ List<ClassDescriptor> typeArguments = new ArrayList<ClassDescriptor>();
+
+ public MethodDescriptor(final Method method, final ClassDescriptor parent,
+ final ClassGraph dump) {
+
+ this.parent = parent;
+
+ name = method.getName();
+
+ if (!method.getDeclaringClass().getName()
+ .equals(parent.fullyQualifiedName))
+ // do not index inherited methods
+ return;
+
+ parent.methods.add(this);
+
+ returnType = dump.addClass(method.getReturnType());
+ returnType.registerReference();
+
+ final Type genericType = method.getGenericReturnType();
+ 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 classDescriptor = dump.addClass(cl);
+ classDescriptor.registerReference();
+ typeArguments.add(classDescriptor);
+ }
+
+ }
+
+ }
+
+ @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=\"dotted, bold\"];\n");
+
+ if (!returnType.isVisible())
+ return result.toString();
+
+ // main type
+ if (returnType.areReferencesShown())
+ result.append(" " + getGraphId() + " -> "
+ + returnType.getGraphId() + "[label=\"" + name + "\","
+ + " color=\"" + returnType.getColor()
+ + "\", style=\"dotted, bold\"];\n");
+
+ return result.toString();
+ }
+
+ @Override
+ public String getEmbeddedDot() {
+ if (!isVisible())
+ return "";
+
+ final StringBuffer result = new StringBuffer();
+
+ result.append(" // " + name + "\n");
+
+ result.append(" <TR><td ALIGN=\"right\">"
+ + "<FONT POINT-SIZE=\"8.0\">" + returnType.getClassName(true)
+ + "</FONT>" + "</td><TD PORT=\"" + getMethodLabel()
+ + "\" ALIGN=\"left\"><FONT COLOR =\"red\" POINT-SIZE=\"11.0\">"
+ + getMethodLabel() + "</FONT></TD></TR>\n");
+
+ return result.toString();
+ }
+
+ @Override
+ public String getGraphId() {
+ return parent.getGraphId() + ":" + name;
+ }
+
+ public String getMethodLabel() {
+ return name;
+ }
+
+ @Override
+ public boolean isVisible() {
+
+ if (Utils.isSystemMethod(name))
+ return false;
+
+ if (parent.isEnum && Utils.isEnumMethod(name))
+ return false;
+
+ if (!(name.startsWith("get") || name.startsWith("set")))
+ return true;
+
+ final String upprCaseName = name.substring(3).toUpperCase();
+
+ for (String parentField : parent.nameToFieldMap.keySet()) {
+ parentField = parentField.toUpperCase();
+
+ if (upprCaseName.equals(parentField))
+ return false;
+
+ }
+
+ return true;
+ }
+
+}
--- /dev/null
+/*
+ * JavaInspect - Utility to visualize java software
+ * Copyright (C) 2013, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of version 2 of the GNU General Public License
+ * as published by the Free Software Foundation.
+ */
+
+package eu.svjatoslav.inspector.java.structure;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class Utils {
+
+ private static final List<String> systemDataTypes = new ArrayList<String>();
+
+ private static final List<String> systemMethods = new ArrayList<String>();
+
+ private static final List<String> systemPackages = new ArrayList<String>();
+
+ private static final List<String> darkColors = new ArrayList<String>();
+
+ private static final List<String> lightColors = new ArrayList<String>();
+
+ private static final List<String> enumMethods = new ArrayList<String>();
+
+ public static int lastChosenDarkColor = -1;
+
+ public static int lastChosenLightColor = -1;
+
+ static {
+ initEnumMethods();
+ initSystemDataTypes();
+ initDarkColors();
+ initLightColors();
+ initSystemMethods();
+ initSystemPackages();
+ }
+
+ /**
+ * retrieves colors from predefined palette
+ */
+ public static String getNextDarkColor() {
+ lastChosenDarkColor++;
+ if (lastChosenDarkColor >= darkColors.size())
+ lastChosenDarkColor = 0;
+
+ return darkColors.get(lastChosenDarkColor);
+ }
+
+ /**
+ * retrieves colors from predefined palette
+ */
+ public static String getNextLightColor() {
+ lastChosenLightColor++;
+ if (lastChosenLightColor >= lightColors.size())
+ lastChosenLightColor = 0;
+
+ return lightColors.get(lastChosenLightColor);
+ }
+
+ public static void initDarkColors() {
+ darkColors.add("antiquewhite4");
+ darkColors.add("blueviolet");
+ darkColors.add("brown4");
+ darkColors.add("chartreuse4");
+ darkColors.add("cyan4");
+ darkColors.add("deeppink1");
+ darkColors.add("deepskyblue3");
+ darkColors.add("firebrick1");
+ darkColors.add("goldenrod3");
+ darkColors.add("gray0");
+ }
+
+ public static void initEnumMethods() {
+ enumMethods.add("values");
+ enumMethods.add("valueOf");
+ enumMethods.add("name");
+ enumMethods.add("compareTo");
+ enumMethods.add("valueOf");
+ enumMethods.add("getDeclaringClass");
+ enumMethods.add("ordinal");
+ }
+
+ public static void initLightColors() {
+ lightColors.add("olivedrab2");
+ lightColors.add("peachpuff2");
+ lightColors.add("seagreen1");
+ lightColors.add("yellow");
+ lightColors.add("violet");
+ }
+
+ public static void initSystemDataTypes() {
+ systemDataTypes.add("void");
+ systemDataTypes.add("int");
+ systemDataTypes.add("long");
+ systemDataTypes.add("float");
+ systemDataTypes.add("double");
+ systemDataTypes.add("boolean");
+ systemDataTypes.add("char");
+ systemDataTypes.add("short");
+ systemDataTypes.add("byte");
+ }
+
+ public static void initSystemMethods() {
+ systemMethods.add("wait");
+ systemMethods.add("equals");
+ systemMethods.add("toString");
+ systemMethods.add("hashCode");
+ systemMethods.add("notify");
+ systemMethods.add("notifyAll");
+ systemMethods.add("getClass");
+ }
+
+ public static void initSystemPackages() {
+ systemPackages.add("java.");
+ systemPackages.add("javax.");
+ systemPackages.add("sun.");
+ }
+
+ public static boolean isEnumMethod(final String name) {
+ return enumMethods.contains(name);
+ }
+
+ public static boolean isSystemDataType(final String name) {
+ return systemDataTypes.contains(name);
+ }
+
+ public static boolean isSystemMethod(final String name) {
+ return systemMethods.contains(name);
+ }
+
+ public static boolean isSystemPackage(final String name) {
+
+ for (final String packagePrefix : systemPackages)
+ if (name.startsWith(packagePrefix))
+ return true;
+
+ return false;
+ }
+
+}
--- /dev/null
+/*
+ * JavaInspect - Utility to visualize java software
+ * Copyright (C) 2013, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of version 2 of the GNU General Public License
+ * as published by the Free Software Foundation.
+ */
+
+package eu.svjatoslav.inspector.java.structure.example;
+
+import eu.svjatoslav.inspector.java.structure.ClassGraph;
+import eu.svjatoslav.inspector.java.structure.example.structure.SampleClass;
+
+public class RenderExampleProject {
+
+ public static void main(final String[] args) {
+ final ClassGraph graph = new ClassGraph();
+
+ graph.addClass(SampleClass.class);
+
+ graph.generateGraph("example");
+
+ }
+
+}
--- /dev/null
+/*
+ * JavaInspect - Utility to visualize java software
+ * Copyright (C) 2013, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of version 2 of the GNU General Public License
+ * as published by the Free Software Foundation.
+ */
+
+package eu.svjatoslav.inspector.java.structure.example;
+
+import java.io.FileNotFoundException;
+
+import eu.svjatoslav.inspector.java.structure.ClassGraph;
+import eu.svjatoslav.inspector.java.structure.Utils;
+
+public class RenderJavaInspect {
+
+ public static void main(final String[] args) throws FileNotFoundException {
+
+ // Create graph
+ final ClassGraph graph = new ClassGraph();
+
+ // Add some object to the graph.
+ graph.addObject(graph);
+
+ // Add some class to the graph.
+ graph.addClass(Utils.class);
+
+ // Produce bitmap image titled "JavaInspect.png" to the user Desktop
+ // directory.
+ graph.generateGraph("JavaInspect", true);
+ }
+}
--- /dev/null
+package eu.svjatoslav.inspector.java.structure.example.structure;
+
+public class ObjectReturnedByMethod {
+
+}
--- /dev/null
+package eu.svjatoslav.inspector.java.structure.example.structure;
+
+public class ObjectVisibleAsClassField {
+
+}
--- /dev/null
+package eu.svjatoslav.inspector.java.structure.example.structure;
+
+public class SampleClass extends SampleSuperClass {
+
+ ObjectVisibleAsClassField sampleClassField;
+
+ public ObjectReturnedByMethod sampleMethod() {
+ return new ObjectReturnedByMethod();
+ }
+
+}
--- /dev/null
+package eu.svjatoslav.inspector.java.structure.example.structure;
+
+public enum SampleEnum {
+
+ ONE, TWO, THREE, FOUR
+
+}
--- /dev/null
+package eu.svjatoslav.inspector.java.structure.example.structure;
+
+public interface SampleInterface {
+ public SampleEnum getSomeValue();
+}
--- /dev/null
+package eu.svjatoslav.inspector.java.structure.example.structure;
+
+public class SampleSuperClass implements SampleInterface {
+
+ @Override
+ public SampleEnum getSomeValue() {
+ return SampleEnum.ONE;
+ }
+
+}
--- /dev/null
+package eu.svjatoslav.inspector.xml.xsd;
+
+public class Main {
+
+ public static void main(final String[] args) {
+
+ }
+
+}
+++ /dev/null
-/*
- * JavaInspect - Utility to visualize java software
- * Copyright (C) 2013, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of version 2 of the GNU General Public License
- * as published by the Free Software Foundation.
- */
-
-package eu.svjatoslav.javainspect.example;
-
-import eu.svjatoslav.javainspect.example.structure.SampleClass;
-import eu.svjatoslav.javainspect.structure.ClassGraph;
-
-public class RenderExampleProject {
-
- public static void main(final String[] args) {
- final ClassGraph graph = new ClassGraph();
-
- graph.addClass(SampleClass.class);
-
- graph.generateGraph("example");
-
- }
-
-}
+++ /dev/null
-/*
- * JavaInspect - Utility to visualize java software
- * Copyright (C) 2013, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of version 2 of the GNU General Public License
- * as published by the Free Software Foundation.
- */
-
-package eu.svjatoslav.javainspect.example;
-
-import java.io.FileNotFoundException;
-
-import eu.svjatoslav.javainspect.structure.ClassGraph;
-import eu.svjatoslav.javainspect.structure.Utils;
-
-public class RenderJavaInspect {
-
- public static void main(final String[] args) throws FileNotFoundException {
-
- // Create graph
- final ClassGraph graph = new ClassGraph();
-
- // Add some object to the graph.
- graph.addObject(graph);
-
- // Add some class to the graph.
- graph.addClass(Utils.class);
-
- // Produce bitmap image titled "JavaInspect.png" to the user Desktop
- // directory.
- graph.generateGraph("JavaInspect", true);
- }
-}
+++ /dev/null
-package eu.svjatoslav.javainspect.example.structure;
-
-public class ObjectReturnedByMethod {
-
-}
+++ /dev/null
-package eu.svjatoslav.javainspect.example.structure;
-
-public class ObjectVisibleAsClassField {
-
-}
+++ /dev/null
-package eu.svjatoslav.javainspect.example.structure;
-
-public class SampleClass extends SampleSuperClass {
-
- ObjectVisibleAsClassField sampleClassField;
-
- public ObjectReturnedByMethod sampleMethod() {
- return new ObjectReturnedByMethod();
- }
-
-}
+++ /dev/null
-package eu.svjatoslav.javainspect.example.structure;
-
-public enum SampleEnum {
-
- ONE, TWO, THREE, FOUR
-
-}
+++ /dev/null
-package eu.svjatoslav.javainspect.example.structure;
-
-public interface SampleInterface {
- public SampleEnum getSomeValue();
-}
+++ /dev/null
-package eu.svjatoslav.javainspect.example.structure;
-
-public class SampleSuperClass implements SampleInterface {
-
- @Override
- public SampleEnum getSomeValue() {
- return SampleEnum.ONE;
- }
-
-}
+++ /dev/null
-/*
- * JavaInspect - Utility to visualize java software
- * Copyright (C) 2013, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of version 2 of the GNU General Public License
- * as published by the Free Software Foundation.
- */
-
-package eu.svjatoslav.javainspect.structure;
-
-import java.lang.reflect.Field;
-import java.lang.reflect.Method;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
-/**
- * Describes single class instance
- */
-public class ClassDescriptor implements GraphElement {
-
- private static final int MAX_REFERECNES_COUNT = 10;
-
- public final String fullyQualifiedName;
-
- Map<String, FieldDescriptor> nameToFieldMap = new HashMap<String, FieldDescriptor>();
-
- public List<MethodDescriptor> methods = new ArrayList<MethodDescriptor>();
-
- /**
- * Incoming arrows will have this color.
- */
- private String distinctiveReferenceColor;
-
- private String interfaceColor;
-
- private String superClassColor;
-
- boolean isEnum;
-
- boolean isInterface;
-
- boolean isArray;
-
- private final ClassGraph dump;
-
- List<ClassDescriptor> interfaces = new ArrayList<ClassDescriptor>();
-
- ClassDescriptor superClass;
-
- /**
- * Amount of field and method references pointing to this class.
- */
- private int referenceCount = 0;
-
- public ClassDescriptor(final Class<? extends Object> clazz, final ClassGraph dump) {
- this.dump = dump;
-
- fullyQualifiedName = clazz.getName();
- dump.nameToClassMap.put(fullyQualifiedName, this);
-
- isArray = clazz.isArray();
-
- if (isArray) {
- final Class<?> componentType = clazz.getComponentType();
- dump.addClass(componentType);
- }
-
- // System.out.println("class: " + fullyQualifiedName);
-
- isEnum = clazz.isEnum();
-
- isInterface = clazz.isInterface();
-
- if (!isVisible())
- return;
-
- indexFields(clazz.getDeclaredFields());
- indexFields(clazz.getFields());
-
- indexMethods(clazz);
-
- for (final Class interfaceClass : clazz.getInterfaces())
- interfaces.add(dump.addClass(interfaceClass));
-
- superClass = dump.addClass(clazz.getSuperclass());
- }
-
- public boolean areReferencesShown() {
- return referenceCount <= MAX_REFERECNES_COUNT;
- }
-
- public void enlistFieldReferences(final StringBuffer result) {
- if (nameToFieldMap.isEmpty())
- return;
-
- result.append("\n");
- result.append(" // field references to other classes\n");
- for (final Map.Entry<String, FieldDescriptor> entry : nameToFieldMap
- .entrySet())
- result.append(entry.getValue().getDot());
- }
-
- public void enlistFields(final StringBuffer result) {
- if (nameToFieldMap.isEmpty())
- return;
-
- result.append("\n");
- result.append(" // fields:\n");
-
- // enlist fields
- for (final Map.Entry<String, FieldDescriptor> entry : nameToFieldMap
- .entrySet())
- result.append(entry.getValue().getEmbeddedDot());
- }
-
- public void enlistImplementedInterfaces(final StringBuffer result) {
- if (interfaces.isEmpty())
- return;
-
- result.append("\n");
- result.append(" // interfaces implemented by class: "
- + fullyQualifiedName + "\n");
-
- for (final ClassDescriptor interfaceDescriptor : interfaces) {
- if (!interfaceDescriptor.isVisible())
- continue;
-
- result.append(" " + interfaceDescriptor.getGraphId() + " -> "
- + getGraphId() + "[style=\"dotted, tapered\", color=\""
- + interfaceDescriptor.getInterfaceColor()
- + "\", penwidth=20, dir=\"forward\"];\n");
- }
- }
-
- public void enlistMethodReferences(final StringBuffer result) {
- if (methods.isEmpty())
- return;
-
- result.append("\n");
- result.append(" // method references to other classes\n");
- for (final MethodDescriptor methodDescriptor : methods)
- result.append(methodDescriptor.getDot());
- }
-
- public void enlistMethods(final StringBuffer result) {
- if (methods.isEmpty())
- return;
-
- result.append("\n");
- result.append(" // methods:\n");
-
- // enlist methods
- for (final MethodDescriptor methodDescriptor : methods)
- result.append(methodDescriptor.getEmbeddedDot());
- }
-
- public void enlistSuperClass(final StringBuffer result) {
- if (superClass == null)
- return;
-
- if (!superClass.isVisible())
- return;
-
- result.append("\n");
- result.append(" // super class for: " + fullyQualifiedName + "\n");
-
- result.append(" " + superClass.getGraphId() + " -> " + getGraphId()
- + "[style=\"tapered\", color=\""
- + superClass.getSuperClassColor()
- + "\", penwidth=10, dir=\"forward\"];\n");
- }
-
- public void generateDotHeader(final StringBuffer result) {
- result.append("\n");
- result.append("// Class: " + fullyQualifiedName + "\n");
-
- result.append(" " + getGraphId() + "[label=<<TABLE "
- + getBackgroundColor() + " BORDER=\"" + getBorderWidth()
- + "\" CELLBORDER=\"1\" CELLSPACING=\"0\">\n");
-
- result.append("\n");
- result.append(" // class descriptor header\n");
- result.append(" <TR><TD colspan=\"2\" PORT=\"f0\">"
- + "<FONT POINT-SIZE=\"8.0\" >" + getPackageName()
- + "</FONT><br/>" + "<FONT POINT-SIZE=\"25.0\"><B>"
- + getClassName(false) + "</B></FONT>" + "</TD></TR>\n");
- }
-
- public List<FieldDescriptor> getAllFields() {
- final List<FieldDescriptor> result = new ArrayList<FieldDescriptor>();
-
- for (final Map.Entry<String, FieldDescriptor> entry : nameToFieldMap
- .entrySet())
- result.add(entry.getValue());
-
- return result;
- }
-
- public String getBackgroundColor() {
- String bgColor = "";
-
- if (isEnum)
- bgColor = "bgcolor=\"navajowhite2\"";
-
- if (isInterface)
- bgColor = "bgcolor=\"darkslategray1\"";
-
- return bgColor;
- }
-
- public String getBorderWidth() {
-
- if (!areReferencesShown())
- return "4";
- return "1";
- }
-
- public String getClassName(final boolean differentiateArray) {
-
- final int i = fullyQualifiedName.lastIndexOf('.');
-
- String result = fullyQualifiedName.substring(i + 1);
-
- if (isArray)
- result = result.substring(0, result.length() - 1);
-
- if (differentiateArray)
- if (isArray)
- result += " []";
-
- return result;
- }
-
- public String getColor() {
- if (getDistinctiveColor() == null)
- setDistinctiveColor(Utils.getNextDarkColor());
-
- return getDistinctiveColor();
- }
-
- public String getDistinctiveColor() {
- return distinctiveReferenceColor;
- }
-
- @Override
- public String getDot() {
- if (!isVisible())
- return "";
-
- if (isArray)
- return "";
-
- final StringBuffer result = new StringBuffer();
-
- generateDotHeader(result);
-
- enlistFields(result);
-
- enlistMethods(result);
-
- result.append(" </TABLE>>, shape=\"none\"];\n");
-
- enlistFieldReferences(result);
-
- enlistMethodReferences(result);
-
- enlistImplementedInterfaces(result);
-
- enlistSuperClass(result);
-
- return result.toString();
- }
-
- @Override
- public String getEmbeddedDot() {
- return null;
- }
-
- @Override
- public String getGraphId() {
- final String result = "class_"
- + fullyQualifiedName.replace('.', '_').replace(";", "")
- .replace("[L", "");
- return result;
- }
-
- public String getInterfaceColor() {
- if (interfaceColor == null)
- interfaceColor = Utils.getNextLightColor();
-
- return interfaceColor;
- }
-
- public String getPackageName() {
-
- final int i = fullyQualifiedName.lastIndexOf('.');
-
- if (i == -1)
- return "";
-
- return fullyQualifiedName.substring(0, i).replace("[L", "");
- }
-
- // public String getReadableName() {
- //
- // // do not print full class name for well known system classes
- // final String packageName = getPackageName();
- //
- // if (packageName.equals("java.util"))
- // return getClassName();
- //
- // if (packageName.equals("java.lang"))
- // return getClassName();
- //
- // return fullyQualifiedName;
- // }
-
- public String getSuperClassColor() {
- if (superClassColor == null)
- superClassColor = Utils.getNextLightColor();
-
- return superClassColor;
- }
-
- public void indexFields(final Field[] fields) {
- for (final Field field : fields) {
- if (nameToFieldMap.containsKey(field.getName()))
- continue;
-
- final FieldDescriptor fieldDescriptor = new FieldDescriptor(field,
- this, dump);
-
- }
- }
-
- private void indexMethods(final Class<? extends Object> clazz) {
- final Method[] methods = clazz.getMethods();
-
- for (final Method method : methods)
- new MethodDescriptor(method, this, dump);
-
- }
-
- @Override
- public boolean isVisible() {
-
- if (Utils.isSystemDataType(fullyQualifiedName))
- return false;
-
- if (Utils.isSystemPackage(fullyQualifiedName))
- return false;
-
- return true;
- }
-
- public void registerReference() {
- referenceCount++;
- }
-
- public void setDistinctiveColor(final String distinctiveColor) {
- distinctiveReferenceColor = distinctiveColor;
- }
-}
+++ /dev/null
-/*
- * JavaInspect - Utility to visualize java software
- * Copyright (C) 2013, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of version 2 of the GNU General Public License
- * as published by the Free Software Foundation.
- */
-
-package eu.svjatoslav.javainspect.structure;
-
-import java.io.File;
-import java.io.IOException;
-import java.io.PrintWriter;
-import java.util.HashMap;
-import java.util.Map;
-
-import eu.svjatoslav.commons.file.CommonPathResolver;
-
-public class ClassGraph {
-
- /**
- * Maps class fully qualified names to class descriptors.
- */
- Map<String, ClassDescriptor> nameToClassMap = new HashMap<String, ClassDescriptor>();
-
- public ClassGraph() {
- }
-
- public ClassGraph(final Class<? extends Object> clazz) {
- addClass(clazz);
- }
-
- public ClassGraph(final Object root) {
- addClass(root.getClass());
- }
-
- public ClassDescriptor addClass(final Class<? extends Object> clazz) {
-
- if (clazz == null)
- return null;
-
- final String className = clazz.getName();
-
- if (nameToClassMap.containsKey(className))
- return nameToClassMap.get(className);
-
- return new ClassDescriptor(clazz, this);
- }
-
- public ClassDescriptor addObject(final Object object) {
- return addClass(object.getClass());
- }
-
- public void generateGraph(final String graphName) {
- generateGraph(graphName, false);
- }
-
- public void generateGraph(final String graphName, final boolean keepDotFile) {
-
- final String desktopPath = CommonPathResolver.getDesktopDirectory()
- .getAbsolutePath() + "/";
-
- final String dotFilePath = desktopPath + graphName + ".dot";
- final String imageFilePath = desktopPath + graphName + ".png";
-
- System.out.println("Dot file path:" + dotFilePath);
-
- try {
- // write DOT file to disk
- final PrintWriter out = new PrintWriter(dotFilePath);
- out.write(getDot());
- out.close();
-
- // execute GraphViz to visualize graph
- try {
- Runtime.getRuntime()
- .exec(new String[] { "dot", "-Tpng", dotFilePath, "-o",
- imageFilePath }).waitFor();
- } catch (final InterruptedException e) {
- } finally {
- }
-
- if (!keepDotFile) {
- // delete dot file
- final File dotFile = new File(dotFilePath);
- dotFile.delete();
- }
- } catch (final IOException e) {
- System.err.println(e);
- }
- }
-
- private String getDot() {
- final StringBuffer result = new StringBuffer();
-
- result.append("digraph Java {\n");
- result.append("graph [rankdir=LR, overlap = false, concentrate=true];\n");
-
- for (final Map.Entry<String, ClassDescriptor> entry : nameToClassMap
- .entrySet())
- result.append(entry.getValue().getDot());
-
- result.append("}\n");
-
- final String resultStr = result.toString();
- return resultStr;
- }
-
-}
+++ /dev/null
-/*
- * JavaInspect - Utility to visualize java software
- * Copyright (C) 2013, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of version 2 of the GNU General Public License
- * as published by the Free Software Foundation.
- */
-
-package eu.svjatoslav.javainspect.structure;
-
-import java.lang.reflect.Field;
-import java.lang.reflect.ParameterizedType;
-import java.lang.reflect.Type;
-import java.util.ArrayList;
-import java.util.List;
-
-public class FieldDescriptor implements GraphElement {
-
- public String name;
- public ClassDescriptor type;
- private ClassDescriptor parent;
- List<ClassDescriptor> typeArguments = new ArrayList<ClassDescriptor>();
-
- public FieldDescriptor(final Field field, final ClassDescriptor parent,
- final ClassGraph dump) {
-
- this.parent = parent;
-
- if (!field.getDeclaringClass().getName()
- .equals(parent.fullyQualifiedName))
- // if field is inherited, do not index it
- return;
-
- // if (field.getType().isArray())
- // System.out.println("field name: " + field.getName());
-
- parent.nameToFieldMap.put(field.getName(), this);
-
- name = field.getName();
- type = dump.addClass(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 = dump
- .addClass(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 == parent)
- showLink = false;
-
- if (parent.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 StringBuffer result = new StringBuffer();
-
- result.append(" // " + name + "\n");
- if (parent.isEnum && (type == parent)) {
- 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 parent.getGraphId() + ":" + name;
- }
-
- @Override
- public boolean isVisible() {
- if (name.contains("$"))
- return false;
-
- if (name.equals("serialVersionUID"))
- return false;
-
- return true;
- }
-
-}
\ No newline at end of file
+++ /dev/null
-/*
- * JavaInspect - Utility to visualize java software
- * Copyright (C) 2013, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of version 2 of the GNU General Public License
- * as published by the Free Software Foundation.
- */
-
-package eu.svjatoslav.javainspect.structure;
-
-public interface GraphElement {
-
- public String getDot();
-
- public String getEmbeddedDot();
-
- public String getGraphId();
-
- public boolean isVisible();
-
-}
+++ /dev/null
-/*
- * JavaInspect - Utility to visualize java software
- * Copyright (C) 2013, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of version 2 of the GNU General Public License
- * as published by the Free Software Foundation.
- */
-
-package eu.svjatoslav.javainspect.structure;
-
-import java.lang.reflect.Method;
-import java.lang.reflect.ParameterizedType;
-import java.lang.reflect.Type;
-import java.util.ArrayList;
-import java.util.List;
-
-public class MethodDescriptor implements GraphElement {
-
- public String name;
- public ClassDescriptor returnType;
- private final ClassDescriptor parent;
-
- List<ClassDescriptor> typeArguments = new ArrayList<ClassDescriptor>();
-
- public MethodDescriptor(final Method method, final ClassDescriptor parent,
- final ClassGraph dump) {
-
- this.parent = parent;
-
- name = method.getName();
-
- if (!method.getDeclaringClass().getName()
- .equals(parent.fullyQualifiedName))
- // do not index inherited methods
- return;
-
- parent.methods.add(this);
-
- returnType = dump.addClass(method.getReturnType());
- returnType.registerReference();
-
- final Type genericType = method.getGenericReturnType();
- 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 classDescriptor = dump.addClass(cl);
- classDescriptor.registerReference();
- typeArguments.add(classDescriptor);
- }
-
- }
-
- }
-
- @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=\"dotted, bold\"];\n");
-
- if (!returnType.isVisible())
- return result.toString();
-
- // main type
- if (returnType.areReferencesShown())
- result.append(" " + getGraphId() + " -> "
- + returnType.getGraphId() + "[label=\"" + name + "\","
- + " color=\"" + returnType.getColor()
- + "\", style=\"dotted, bold\"];\n");
-
- return result.toString();
- }
-
- @Override
- public String getEmbeddedDot() {
- if (!isVisible())
- return "";
-
- final StringBuffer result = new StringBuffer();
-
- result.append(" // " + name + "\n");
-
- result.append(" <TR><td ALIGN=\"right\">"
- + "<FONT POINT-SIZE=\"8.0\">" + returnType.getClassName(true)
- + "</FONT>" + "</td><TD PORT=\"" + getMethodLabel()
- + "\" ALIGN=\"left\"><FONT COLOR =\"red\" POINT-SIZE=\"11.0\">"
- + getMethodLabel() + "</FONT></TD></TR>\n");
-
- return result.toString();
- }
-
- @Override
- public String getGraphId() {
- return parent.getGraphId() + ":" + name;
- }
-
- public String getMethodLabel() {
- return name;
- }
-
- @Override
- public boolean isVisible() {
-
- if (Utils.isSystemMethod(name))
- return false;
-
- if (parent.isEnum && Utils.isEnumMethod(name))
- return false;
-
- if (!(name.startsWith("get") || name.startsWith("set")))
- return true;
-
- final String upprCaseName = name.substring(3).toUpperCase();
-
- for (String parentField : parent.nameToFieldMap.keySet()) {
- parentField = parentField.toUpperCase();
-
- if (upprCaseName.equals(parentField))
- return false;
-
- }
-
- return true;
- }
-
-}
+++ /dev/null
-/*
- * JavaInspect - Utility to visualize java software
- * Copyright (C) 2013, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of version 2 of the GNU General Public License
- * as published by the Free Software Foundation.
- */
-
-package eu.svjatoslav.javainspect.structure;
-
-import java.util.ArrayList;
-import java.util.List;
-
-public class Utils {
-
- private static final List<String> systemDataTypes = new ArrayList<String>();
-
- private static final List<String> systemMethods = new ArrayList<String>();
-
- private static final List<String> systemPackages = new ArrayList<String>();
-
- private static final List<String> darkColors = new ArrayList<String>();
-
- private static final List<String> lightColors = new ArrayList<String>();
-
- private static final List<String> enumMethods = new ArrayList<String>();
-
- public static int lastChosenDarkColor = -1;
-
- public static int lastChosenLightColor = -1;
-
- static {
- initEnumMethods();
- initSystemDataTypes();
- initDarkColors();
- initLightColors();
- initSystemMethods();
- initSystemPackages();
- }
-
- /**
- * retrieves colors from predefined palette
- */
- public static String getNextDarkColor() {
- lastChosenDarkColor++;
- if (lastChosenDarkColor >= darkColors.size())
- lastChosenDarkColor = 0;
-
- return darkColors.get(lastChosenDarkColor);
- }
-
- /**
- * retrieves colors from predefined palette
- */
- public static String getNextLightColor() {
- lastChosenLightColor++;
- if (lastChosenLightColor >= lightColors.size())
- lastChosenLightColor = 0;
-
- return lightColors.get(lastChosenLightColor);
- }
-
- public static void initDarkColors() {
- darkColors.add("antiquewhite4");
- darkColors.add("blueviolet");
- darkColors.add("brown4");
- darkColors.add("chartreuse4");
- darkColors.add("cyan4");
- darkColors.add("deeppink1");
- darkColors.add("deepskyblue3");
- darkColors.add("firebrick1");
- darkColors.add("goldenrod3");
- darkColors.add("gray0");
- }
-
- public static void initEnumMethods() {
- enumMethods.add("values");
- enumMethods.add("valueOf");
- enumMethods.add("name");
- enumMethods.add("compareTo");
- enumMethods.add("valueOf");
- enumMethods.add("getDeclaringClass");
- enumMethods.add("ordinal");
- }
-
- public static void initLightColors() {
- lightColors.add("olivedrab2");
- lightColors.add("peachpuff2");
- lightColors.add("seagreen1");
- lightColors.add("yellow");
- lightColors.add("violet");
- }
-
- public static void initSystemDataTypes() {
- systemDataTypes.add("void");
- systemDataTypes.add("int");
- systemDataTypes.add("long");
- systemDataTypes.add("float");
- systemDataTypes.add("double");
- systemDataTypes.add("boolean");
- systemDataTypes.add("char");
- systemDataTypes.add("short");
- systemDataTypes.add("byte");
- }
-
- public static void initSystemMethods() {
- systemMethods.add("wait");
- systemMethods.add("equals");
- systemMethods.add("toString");
- systemMethods.add("hashCode");
- systemMethods.add("notify");
- systemMethods.add("notifyAll");
- systemMethods.add("getClass");
- }
-
- public static void initSystemPackages() {
- systemPackages.add("java.");
- systemPackages.add("javax.");
- systemPackages.add("sun.");
- }
-
- public static boolean isEnumMethod(final String name) {
- return enumMethods.contains(name);
- }
-
- public static boolean isSystemDataType(final String name) {
- return systemDataTypes.contains(name);
- }
-
- public static boolean isSystemMethod(final String name) {
- return systemMethods.contains(name);
- }
-
- public static boolean isSystemPackage(final String name) {
-
- for (final String packagePrefix : systemPackages)
- if (name.startsWith(packagePrefix))
- return true;
-
- return false;
- }
-
-}