From: Svjatoslav Agejenko Date: Wed, 3 Jul 2013 18:18:15 +0000 (+0300) Subject: refactored packages X-Git-Tag: javainspect-1.1~17 X-Git-Url: http://www2.svjatoslav.eu/gitweb/?a=commitdiff_plain;h=d717e90f4c46e26f9f54ba5638aade6688527bf4;p=javainspect.git refactored packages --- diff --git a/src/main/java/eu/svjatoslav/inspector/java/structure/ClassDescriptor.java b/src/main/java/eu/svjatoslav/inspector/java/structure/ClassDescriptor.java new file mode 100644 index 0000000..c8a2773 --- /dev/null +++ b/src/main/java/eu/svjatoslav/inspector/java/structure/ClassDescriptor.java @@ -0,0 +1,366 @@ +/* + * 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 nameToFieldMap = new HashMap(); + + public List methods = new ArrayList(); + + /** + * 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 interfaces = new ArrayList(); + + ClassDescriptor superClass; + + /** + * Amount of field and method references pointing to this class. + */ + private int referenceCount = 0; + + public ClassDescriptor(final Class 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 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 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=<\n"); + + result.append("\n"); + result.append(" // class descriptor header\n"); + result.append(" \n"); + } + + public List getAllFields() { + final List result = new ArrayList(); + + for (final Map.Entry 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("
" + + "" + getPackageName() + + "
" + "" + + getClassName(false) + "" + "
>, 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 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; + } +} diff --git a/src/main/java/eu/svjatoslav/inspector/java/structure/ClassGraph.java b/src/main/java/eu/svjatoslav/inspector/java/structure/ClassGraph.java new file mode 100644 index 0000000..bda0882 --- /dev/null +++ b/src/main/java/eu/svjatoslav/inspector/java/structure/ClassGraph.java @@ -0,0 +1,110 @@ +/* + * 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 nameToClassMap = new HashMap(); + + public ClassGraph() { + } + + public ClassGraph(final Class clazz) { + addClass(clazz); + } + + public ClassGraph(final Object root) { + addClass(root.getClass()); + } + + public ClassDescriptor addClass(final Class 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 entry : nameToClassMap + .entrySet()) + result.append(entry.getValue().getDot()); + + result.append("}\n"); + + final String resultStr = result.toString(); + return resultStr; + } + +} diff --git a/src/main/java/eu/svjatoslav/inspector/java/structure/FieldDescriptor.java b/src/main/java/eu/svjatoslav/inspector/java/structure/FieldDescriptor.java new file mode 100644 index 0000000..2cbb47c --- /dev/null +++ b/src/main/java/eu/svjatoslav/inspector/java/structure/FieldDescriptor.java @@ -0,0 +1,136 @@ +/* + * 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 typeArguments = new ArrayList(); + + 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(" "); + result.append(name + "\n"); + } else { + result.append(" "); + result.append(""); + result.append(type.getClassName(true) + ""); + result.append(""); + result.append(name + "\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 diff --git a/src/main/java/eu/svjatoslav/inspector/java/structure/GraphElement.java b/src/main/java/eu/svjatoslav/inspector/java/structure/GraphElement.java new file mode 100644 index 0000000..c8bab85 --- /dev/null +++ b/src/main/java/eu/svjatoslav/inspector/java/structure/GraphElement.java @@ -0,0 +1,22 @@ +/* + * 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(); + +} diff --git a/src/main/java/eu/svjatoslav/inspector/java/structure/MethodDescriptor.java b/src/main/java/eu/svjatoslav/inspector/java/structure/MethodDescriptor.java new file mode 100644 index 0000000..35e49d5 --- /dev/null +++ b/src/main/java/eu/svjatoslav/inspector/java/structure/MethodDescriptor.java @@ -0,0 +1,140 @@ +/* + * 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 typeArguments = new ArrayList(); + + 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(" " + + "" + returnType.getClassName(true) + + "" + "" + + getMethodLabel() + "\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; + } + +} diff --git a/src/main/java/eu/svjatoslav/inspector/java/structure/Utils.java b/src/main/java/eu/svjatoslav/inspector/java/structure/Utils.java new file mode 100644 index 0000000..c28d68b --- /dev/null +++ b/src/main/java/eu/svjatoslav/inspector/java/structure/Utils.java @@ -0,0 +1,144 @@ +/* + * 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 systemDataTypes = new ArrayList(); + + private static final List systemMethods = new ArrayList(); + + private static final List systemPackages = new ArrayList(); + + private static final List darkColors = new ArrayList(); + + private static final List lightColors = new ArrayList(); + + private static final List enumMethods = new ArrayList(); + + 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; + } + +} diff --git a/src/main/java/eu/svjatoslav/inspector/java/structure/example/RenderExampleProject.java b/src/main/java/eu/svjatoslav/inspector/java/structure/example/RenderExampleProject.java new file mode 100644 index 0000000..028c775 --- /dev/null +++ b/src/main/java/eu/svjatoslav/inspector/java/structure/example/RenderExampleProject.java @@ -0,0 +1,26 @@ +/* + * 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"); + + } + +} diff --git a/src/main/java/eu/svjatoslav/inspector/java/structure/example/RenderJavaInspect.java b/src/main/java/eu/svjatoslav/inspector/java/structure/example/RenderJavaInspect.java new file mode 100644 index 0000000..f993e22 --- /dev/null +++ b/src/main/java/eu/svjatoslav/inspector/java/structure/example/RenderJavaInspect.java @@ -0,0 +1,34 @@ +/* + * 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); + } +} diff --git a/src/main/java/eu/svjatoslav/inspector/java/structure/example/structure/ObjectReturnedByMethod.java b/src/main/java/eu/svjatoslav/inspector/java/structure/example/structure/ObjectReturnedByMethod.java new file mode 100644 index 0000000..3e507bb --- /dev/null +++ b/src/main/java/eu/svjatoslav/inspector/java/structure/example/structure/ObjectReturnedByMethod.java @@ -0,0 +1,5 @@ +package eu.svjatoslav.inspector.java.structure.example.structure; + +public class ObjectReturnedByMethod { + +} diff --git a/src/main/java/eu/svjatoslav/inspector/java/structure/example/structure/ObjectVisibleAsClassField.java b/src/main/java/eu/svjatoslav/inspector/java/structure/example/structure/ObjectVisibleAsClassField.java new file mode 100644 index 0000000..c69a881 --- /dev/null +++ b/src/main/java/eu/svjatoslav/inspector/java/structure/example/structure/ObjectVisibleAsClassField.java @@ -0,0 +1,5 @@ +package eu.svjatoslav.inspector.java.structure.example.structure; + +public class ObjectVisibleAsClassField { + +} diff --git a/src/main/java/eu/svjatoslav/inspector/java/structure/example/structure/SampleClass.java b/src/main/java/eu/svjatoslav/inspector/java/structure/example/structure/SampleClass.java new file mode 100644 index 0000000..3e496c4 --- /dev/null +++ b/src/main/java/eu/svjatoslav/inspector/java/structure/example/structure/SampleClass.java @@ -0,0 +1,11 @@ +package eu.svjatoslav.inspector.java.structure.example.structure; + +public class SampleClass extends SampleSuperClass { + + ObjectVisibleAsClassField sampleClassField; + + public ObjectReturnedByMethod sampleMethod() { + return new ObjectReturnedByMethod(); + } + +} diff --git a/src/main/java/eu/svjatoslav/inspector/java/structure/example/structure/SampleEnum.java b/src/main/java/eu/svjatoslav/inspector/java/structure/example/structure/SampleEnum.java new file mode 100644 index 0000000..dfb6ea2 --- /dev/null +++ b/src/main/java/eu/svjatoslav/inspector/java/structure/example/structure/SampleEnum.java @@ -0,0 +1,7 @@ +package eu.svjatoslav.inspector.java.structure.example.structure; + +public enum SampleEnum { + + ONE, TWO, THREE, FOUR + +} diff --git a/src/main/java/eu/svjatoslav/inspector/java/structure/example/structure/SampleInterface.java b/src/main/java/eu/svjatoslav/inspector/java/structure/example/structure/SampleInterface.java new file mode 100644 index 0000000..136bfde --- /dev/null +++ b/src/main/java/eu/svjatoslav/inspector/java/structure/example/structure/SampleInterface.java @@ -0,0 +1,5 @@ +package eu.svjatoslav.inspector.java.structure.example.structure; + +public interface SampleInterface { + public SampleEnum getSomeValue(); +} diff --git a/src/main/java/eu/svjatoslav/inspector/java/structure/example/structure/SampleSuperClass.java b/src/main/java/eu/svjatoslav/inspector/java/structure/example/structure/SampleSuperClass.java new file mode 100644 index 0000000..96edce2 --- /dev/null +++ b/src/main/java/eu/svjatoslav/inspector/java/structure/example/structure/SampleSuperClass.java @@ -0,0 +1,10 @@ +package eu.svjatoslav.inspector.java.structure.example.structure; + +public class SampleSuperClass implements SampleInterface { + + @Override + public SampleEnum getSomeValue() { + return SampleEnum.ONE; + } + +} diff --git a/src/main/java/eu/svjatoslav/inspector/xml/xsd/Main.java b/src/main/java/eu/svjatoslav/inspector/xml/xsd/Main.java new file mode 100644 index 0000000..9ecb76e --- /dev/null +++ b/src/main/java/eu/svjatoslav/inspector/xml/xsd/Main.java @@ -0,0 +1,9 @@ +package eu.svjatoslav.inspector.xml.xsd; + +public class Main { + + public static void main(final String[] args) { + + } + +} diff --git a/src/main/java/eu/svjatoslav/javainspect/example/RenderExampleProject.java b/src/main/java/eu/svjatoslav/javainspect/example/RenderExampleProject.java deleted file mode 100644 index 0c7d28a..0000000 --- a/src/main/java/eu/svjatoslav/javainspect/example/RenderExampleProject.java +++ /dev/null @@ -1,26 +0,0 @@ -/* - * 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"); - - } - -} diff --git a/src/main/java/eu/svjatoslav/javainspect/example/RenderJavaInspect.java b/src/main/java/eu/svjatoslav/javainspect/example/RenderJavaInspect.java deleted file mode 100644 index d79e23e..0000000 --- a/src/main/java/eu/svjatoslav/javainspect/example/RenderJavaInspect.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * 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); - } -} diff --git a/src/main/java/eu/svjatoslav/javainspect/example/structure/ObjectReturnedByMethod.java b/src/main/java/eu/svjatoslav/javainspect/example/structure/ObjectReturnedByMethod.java deleted file mode 100644 index 42e3186..0000000 --- a/src/main/java/eu/svjatoslav/javainspect/example/structure/ObjectReturnedByMethod.java +++ /dev/null @@ -1,5 +0,0 @@ -package eu.svjatoslav.javainspect.example.structure; - -public class ObjectReturnedByMethod { - -} diff --git a/src/main/java/eu/svjatoslav/javainspect/example/structure/ObjectVisibleAsClassField.java b/src/main/java/eu/svjatoslav/javainspect/example/structure/ObjectVisibleAsClassField.java deleted file mode 100644 index 76b34d2..0000000 --- a/src/main/java/eu/svjatoslav/javainspect/example/structure/ObjectVisibleAsClassField.java +++ /dev/null @@ -1,5 +0,0 @@ -package eu.svjatoslav.javainspect.example.structure; - -public class ObjectVisibleAsClassField { - -} diff --git a/src/main/java/eu/svjatoslav/javainspect/example/structure/SampleClass.java b/src/main/java/eu/svjatoslav/javainspect/example/structure/SampleClass.java deleted file mode 100644 index 244af16..0000000 --- a/src/main/java/eu/svjatoslav/javainspect/example/structure/SampleClass.java +++ /dev/null @@ -1,11 +0,0 @@ -package eu.svjatoslav.javainspect.example.structure; - -public class SampleClass extends SampleSuperClass { - - ObjectVisibleAsClassField sampleClassField; - - public ObjectReturnedByMethod sampleMethod() { - return new ObjectReturnedByMethod(); - } - -} diff --git a/src/main/java/eu/svjatoslav/javainspect/example/structure/SampleEnum.java b/src/main/java/eu/svjatoslav/javainspect/example/structure/SampleEnum.java deleted file mode 100644 index 3335289..0000000 --- a/src/main/java/eu/svjatoslav/javainspect/example/structure/SampleEnum.java +++ /dev/null @@ -1,7 +0,0 @@ -package eu.svjatoslav.javainspect.example.structure; - -public enum SampleEnum { - - ONE, TWO, THREE, FOUR - -} diff --git a/src/main/java/eu/svjatoslav/javainspect/example/structure/SampleInterface.java b/src/main/java/eu/svjatoslav/javainspect/example/structure/SampleInterface.java deleted file mode 100644 index f88cf16..0000000 --- a/src/main/java/eu/svjatoslav/javainspect/example/structure/SampleInterface.java +++ /dev/null @@ -1,5 +0,0 @@ -package eu.svjatoslav.javainspect.example.structure; - -public interface SampleInterface { - public SampleEnum getSomeValue(); -} diff --git a/src/main/java/eu/svjatoslav/javainspect/example/structure/SampleSuperClass.java b/src/main/java/eu/svjatoslav/javainspect/example/structure/SampleSuperClass.java deleted file mode 100644 index 837e35e..0000000 --- a/src/main/java/eu/svjatoslav/javainspect/example/structure/SampleSuperClass.java +++ /dev/null @@ -1,10 +0,0 @@ -package eu.svjatoslav.javainspect.example.structure; - -public class SampleSuperClass implements SampleInterface { - - @Override - public SampleEnum getSomeValue() { - return SampleEnum.ONE; - } - -} diff --git a/src/main/java/eu/svjatoslav/javainspect/structure/ClassDescriptor.java b/src/main/java/eu/svjatoslav/javainspect/structure/ClassDescriptor.java deleted file mode 100644 index f1d6a51..0000000 --- a/src/main/java/eu/svjatoslav/javainspect/structure/ClassDescriptor.java +++ /dev/null @@ -1,366 +0,0 @@ -/* - * 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 nameToFieldMap = new HashMap(); - - public List methods = new ArrayList(); - - /** - * 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 interfaces = new ArrayList(); - - ClassDescriptor superClass; - - /** - * Amount of field and method references pointing to this class. - */ - private int referenceCount = 0; - - public ClassDescriptor(final Class 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 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 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=<\n"); - - result.append("\n"); - result.append(" // class descriptor header\n"); - result.append(" \n"); - } - - public List getAllFields() { - final List result = new ArrayList(); - - for (final Map.Entry 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("
" - + "" + getPackageName() - + "
" + "" - + getClassName(false) + "" + "
>, 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 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; - } -} diff --git a/src/main/java/eu/svjatoslav/javainspect/structure/ClassGraph.java b/src/main/java/eu/svjatoslav/javainspect/structure/ClassGraph.java deleted file mode 100644 index 180b2c7..0000000 --- a/src/main/java/eu/svjatoslav/javainspect/structure/ClassGraph.java +++ /dev/null @@ -1,110 +0,0 @@ -/* - * 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 nameToClassMap = new HashMap(); - - public ClassGraph() { - } - - public ClassGraph(final Class clazz) { - addClass(clazz); - } - - public ClassGraph(final Object root) { - addClass(root.getClass()); - } - - public ClassDescriptor addClass(final Class 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 entry : nameToClassMap - .entrySet()) - result.append(entry.getValue().getDot()); - - result.append("}\n"); - - final String resultStr = result.toString(); - return resultStr; - } - -} diff --git a/src/main/java/eu/svjatoslav/javainspect/structure/FieldDescriptor.java b/src/main/java/eu/svjatoslav/javainspect/structure/FieldDescriptor.java deleted file mode 100644 index a91da4d..0000000 --- a/src/main/java/eu/svjatoslav/javainspect/structure/FieldDescriptor.java +++ /dev/null @@ -1,136 +0,0 @@ -/* - * 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 typeArguments = new ArrayList(); - - 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(" "); - result.append(name + "\n"); - } else { - result.append(" "); - result.append(""); - result.append(type.getClassName(true) + ""); - result.append(""); - result.append(name + "\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 diff --git a/src/main/java/eu/svjatoslav/javainspect/structure/GraphElement.java b/src/main/java/eu/svjatoslav/javainspect/structure/GraphElement.java deleted file mode 100644 index 7aed6fe..0000000 --- a/src/main/java/eu/svjatoslav/javainspect/structure/GraphElement.java +++ /dev/null @@ -1,22 +0,0 @@ -/* - * 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(); - -} diff --git a/src/main/java/eu/svjatoslav/javainspect/structure/MethodDescriptor.java b/src/main/java/eu/svjatoslav/javainspect/structure/MethodDescriptor.java deleted file mode 100644 index c541b03..0000000 --- a/src/main/java/eu/svjatoslav/javainspect/structure/MethodDescriptor.java +++ /dev/null @@ -1,140 +0,0 @@ -/* - * 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 typeArguments = new ArrayList(); - - 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(" " - + "" + returnType.getClassName(true) - + "" + "" - + getMethodLabel() + "\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; - } - -} diff --git a/src/main/java/eu/svjatoslav/javainspect/structure/Utils.java b/src/main/java/eu/svjatoslav/javainspect/structure/Utils.java deleted file mode 100644 index 25caa1d..0000000 --- a/src/main/java/eu/svjatoslav/javainspect/structure/Utils.java +++ /dev/null @@ -1,144 +0,0 @@ -/* - * 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 systemDataTypes = new ArrayList(); - - private static final List systemMethods = new ArrayList(); - - private static final List systemPackages = new ArrayList(); - - private static final List darkColors = new ArrayList(); - - private static final List lightColors = new ArrayList(); - - private static final List enumMethods = new ArrayList(); - - 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; - } - -}