From: Svjatoslav Agejenko Date: Sat, 29 Jun 2013 07:40:41 +0000 (+0300) Subject: use better class name to avoid potetntial name collisions X-Git-Tag: javainspect-1.1~20 X-Git-Url: http://www2.svjatoslav.eu/gitweb/?a=commitdiff_plain;h=5420578cb5e2f66fd0ca780192808be376f9d5b9;p=javainspect.git use better class name to avoid potetntial name collisions --- diff --git a/src/main/java/eu/svjatoslav/javainspect/example/RenderExampleProject.java b/src/main/java/eu/svjatoslav/javainspect/example/RenderExampleProject.java index 0cea641..0c7d28a 100644 --- a/src/main/java/eu/svjatoslav/javainspect/example/RenderExampleProject.java +++ b/src/main/java/eu/svjatoslav/javainspect/example/RenderExampleProject.java @@ -10,12 +10,12 @@ package eu.svjatoslav.javainspect.example; import eu.svjatoslav.javainspect.example.structure.SampleClass; -import eu.svjatoslav.javainspect.structure.Graph; +import eu.svjatoslav.javainspect.structure.ClassGraph; public class RenderExampleProject { public static void main(final String[] args) { - final Graph graph = new Graph(); + final ClassGraph graph = new ClassGraph(); graph.addClass(SampleClass.class); diff --git a/src/main/java/eu/svjatoslav/javainspect/example/RenderJavaInspect.java b/src/main/java/eu/svjatoslav/javainspect/example/RenderJavaInspect.java index e749a1b..d79e23e 100644 --- a/src/main/java/eu/svjatoslav/javainspect/example/RenderJavaInspect.java +++ b/src/main/java/eu/svjatoslav/javainspect/example/RenderJavaInspect.java @@ -11,7 +11,7 @@ package eu.svjatoslav.javainspect.example; import java.io.FileNotFoundException; -import eu.svjatoslav.javainspect.structure.Graph; +import eu.svjatoslav.javainspect.structure.ClassGraph; import eu.svjatoslav.javainspect.structure.Utils; public class RenderJavaInspect { @@ -19,7 +19,7 @@ public class RenderJavaInspect { public static void main(final String[] args) throws FileNotFoundException { // Create graph - final Graph graph = new Graph(); + final ClassGraph graph = new ClassGraph(); // Add some object to the graph. graph.addObject(graph); diff --git a/src/main/java/eu/svjatoslav/javainspect/structure/ClassDescriptor.java b/src/main/java/eu/svjatoslav/javainspect/structure/ClassDescriptor.java index 86b07de..f1d6a51 100644 --- a/src/main/java/eu/svjatoslav/javainspect/structure/ClassDescriptor.java +++ b/src/main/java/eu/svjatoslav/javainspect/structure/ClassDescriptor.java @@ -44,7 +44,7 @@ public class ClassDescriptor implements GraphElement { boolean isArray; - private final Graph dump; + private final ClassGraph dump; List interfaces = new ArrayList(); @@ -55,7 +55,7 @@ public class ClassDescriptor implements GraphElement { */ private int referenceCount = 0; - public ClassDescriptor(final Class clazz, final Graph dump) { + public ClassDescriptor(final Class clazz, final ClassGraph dump) { this.dump = dump; fullyQualifiedName = clazz.getName(); diff --git a/src/main/java/eu/svjatoslav/javainspect/structure/ClassGraph.java b/src/main/java/eu/svjatoslav/javainspect/structure/ClassGraph.java new file mode 100644 index 0000000..180b2c7 --- /dev/null +++ b/src/main/java/eu/svjatoslav/javainspect/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.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 index 96b3602..a91da4d 100644 --- a/src/main/java/eu/svjatoslav/javainspect/structure/FieldDescriptor.java +++ b/src/main/java/eu/svjatoslav/javainspect/structure/FieldDescriptor.java @@ -23,7 +23,7 @@ public class FieldDescriptor implements GraphElement { List typeArguments = new ArrayList(); public FieldDescriptor(final Field field, final ClassDescriptor parent, - final Graph dump) { + final ClassGraph dump) { this.parent = parent; diff --git a/src/main/java/eu/svjatoslav/javainspect/structure/Graph.java b/src/main/java/eu/svjatoslav/javainspect/structure/Graph.java deleted file mode 100644 index e1f7ff8..0000000 --- a/src/main/java/eu/svjatoslav/javainspect/structure/Graph.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 Graph { - - /** - * Maps class fully qualified names to class descriptors. - */ - Map nameToClassMap = new HashMap(); - - public Graph() { - } - - public Graph(final Class clazz) { - addClass(clazz); - } - - public Graph(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/MethodDescriptor.java b/src/main/java/eu/svjatoslav/javainspect/structure/MethodDescriptor.java index a7ce38b..c541b03 100644 --- a/src/main/java/eu/svjatoslav/javainspect/structure/MethodDescriptor.java +++ b/src/main/java/eu/svjatoslav/javainspect/structure/MethodDescriptor.java @@ -24,7 +24,7 @@ public class MethodDescriptor implements GraphElement { List typeArguments = new ArrayList(); public MethodDescriptor(final Method method, final ClassDescriptor parent, - final Graph dump) { + final ClassGraph dump) { this.parent = parent;