Streamline code, smaller optimizations.

This commit is contained in:
Erich Schubert 2018-10-21 01:59:00 +02:00
parent f3e945c646
commit fe2c0c3df1
12 changed files with 87 additions and 200 deletions

View File

@ -72,7 +72,13 @@ class ClassGraph {
NORMAL, ABSTRACT, CLASS, CLASS_ABSTRACT, TAG, PACKAGE NORMAL, ABSTRACT, CLASS, CLASS_ABSTRACT, TAG, PACKAGE
} }
enum Align { enum Align {
LEFT, CENTER, RIGHT LEFT, CENTER, RIGHT;
public final String lower;
private Align() {
this.lower = toString().toLowerCase();
}
}; };
public static Map<RelationType, String> associationMap = new HashMap<RelationType, String>(); public static Map<RelationType, String> associationMap = new HashMap<RelationType, String>();
static { static {
@ -186,50 +192,41 @@ class ClassGraph {
/** Print the method parameter p */ /** Print the method parameter p */
private String parameter(Options opt, Parameter p[]) { private String parameter(Options opt, Parameter p[]) {
String par = ""; StringBuilder par = new StringBuilder(1000);
for (int i = 0; i < p.length; i++) { for (int i = 0; i < p.length; i++) {
par += p[i].name() + typeAnnotation(opt, p[i].type()); par.append(p[i].name() + typeAnnotation(opt, p[i].type()));
if (i + 1 < p.length) if (i + 1 < p.length)
par += ", "; par.append(", ");
} }
return par; return par.toString();
} }
/** Print a a basic type t */ /** Print a a basic type t */
private String type(Options opt, Type t, boolean generics) { private String type(Options opt, Type t, boolean generics) {
String type; return ((generics ? opt.showQualifiedGenerics : opt.showQualified) ?//
if (generics ? opt.showQualifiedGenerics : opt.showQualified) t.qualifiedTypeName() : t.typeName()) //
type = t.qualifiedTypeName(); + typeParameters(opt, t.asParameterizedType());
else
type = t.typeName();
type += typeParameters(opt, t.asParameterizedType());
return type;
} }
/** Print the parameters of the parameterized type t */ /** Print the parameters of the parameterized type t */
private String typeParameters(Options opt, ParameterizedType t) { private String typeParameters(Options opt, ParameterizedType t) {
String tp = "";
if (t == null) if (t == null)
return tp; return "";
StringBuffer tp = new StringBuffer(1000).append("&lt;");
Type args[] = t.typeArguments(); Type args[] = t.typeArguments();
tp += "&lt;";
for (int i = 0; i < args.length; i++) { for (int i = 0; i < args.length; i++) {
tp += type(opt, args[i], true); tp.append(type(opt, args[i], true));
if (i != args.length - 1) if (i != args.length - 1)
tp += ", "; tp.append(", ");
} }
tp += "&gt;"; return tp.append("&gt;").toString();
return tp;
} }
/** Annotate an field/argument with its type t */ /** Annotate an field/argument with its type t */
private String typeAnnotation(Options opt, Type t) { private String typeAnnotation(Options opt, Type t) {
if (t.typeName().equals("void")) if (t.typeName().equals("void"))
return ""; return "";
String ta = " : "; return " : " + type(opt, t, false) + t.dimension();
ta += type(opt, t, false);
ta += t.dimension();
return ta;
} }
/** Print the class's attributes fd */ /** Print the class's attributes fd */
@ -237,9 +234,8 @@ class ClassGraph {
for (FieldDoc f : fd) { for (FieldDoc f : fd) {
if (hidden(f)) if (hidden(f))
continue; continue;
String att;
stereotype(opt, f, Align.LEFT); stereotype(opt, f, Align.LEFT);
att = visibility(opt, f) + f.name(); String att = visibility(opt, f) + f.name();
if (opt.showType) if (opt.showType)
att += typeAnnotation(opt, f.type()); att += typeAnnotation(opt, f.type());
tableLine(Align.LEFT, att); tableLine(Align.LEFT, att);
@ -373,11 +369,7 @@ class ClassGraph {
/** Return true if the class name is associated to an hidden class or matches a hide expression */ /** Return true if the class name is associated to an hidden class or matches a hide expression */
private boolean hidden(String s) { private boolean hidden(String s) {
ClassInfo ci = getClassInfo(s); ClassInfo ci = getClassInfo(s);
Options opt = optionProvider.getOptionsFor(s); return (ci != null && ci.hidden) || optionProvider.getOptionsFor(s).matchesHideExpression(s);
if(ci != null)
return ci.hidden || opt.matchesHideExpression(s);
else
return opt.matchesHideExpression(s);
} }
@ -603,9 +595,7 @@ class ClassGraph {
* whose outline is rendered through an inner table. * whose outline is rendered through an inner table.
*/ */
private String relationNode(ClassDoc c) { private String relationNode(ClassDoc c) {
Options opt = optionProvider.getOptionsFor(c); return getNodeName(c) + optionProvider.getOptionsFor(c).shape.landingPort();
String name = getNodeName(c);
return name + opt.shape.landingPort();
} }
/** Return the full name of a relation's node c. /** Return the full name of a relation's node c.
@ -615,13 +605,8 @@ class ClassGraph {
* @param cName the node's class name * @param cName the node's class name
*/ */
private String relationNode(ClassDoc c, String cName) { private String relationNode(ClassDoc c, String cName) {
Options opt; Options opt = c == null ? optionProvider.getOptionsFor(cName) : optionProvider.getOptionsFor(c);
if (c == null) return getNodeName(cName) + opt.shape.landingPort();
opt = optionProvider.getOptionsFor(cName);
else
opt = optionProvider.getOptionsFor(c);
String name = getNodeName(cName);
return name + opt.shape.landingPort();
} }
/** Print a class's relations */ /** Print a class's relations */
@ -930,7 +915,7 @@ class ClassGraph {
// ... go up with ".." to reach the common root // ... go up with ".." to reach the common root
StringBuilder buf = new StringBuilder(); StringBuilder buf = new StringBuilder();
if (i == contextClassPath.length) { if (i == contextClassPath.length) {
buf.append(".").append(FILE_SEPARATOR); buf.append('.').append(FILE_SEPARATOR);
} else { } else {
for (int j = i; j < contextClassPath.length; j++) { for (int j = i; j < contextClassPath.length; j++) {
buf.append("..").append(FILE_SEPARATOR); buf.append("..").append(FILE_SEPARATOR);
@ -963,16 +948,14 @@ class ClassGraph {
/** Convert the class name into a corresponding URL */ /** Convert the class name into a corresponding URL */
public String classToUrl(String className) { public String classToUrl(String className) {
String docRoot = mapApiDocRoot(className); String docRoot = mapApiDocRoot(className);
if (docRoot != null) { if (docRoot == null)
StringBuilder buf = new StringBuilder(docRoot); return null;
buf.append(getPackageName(className).replace('.', FILE_SEPARATOR) + FILE_SEPARATOR); return new StringBuilder(docRoot) //
buf.append(getUnqualifiedName(className)); .append(getPackageName(className).replace('.', FILE_SEPARATOR)) //
buf.append(".html"); .append(FILE_SEPARATOR) //
return buf.toString(); .append(getUnqualifiedName(className)) //
} else { .append(".html").toString();
return null;
}
} }
/** /**
@ -981,15 +964,11 @@ class ClassGraph {
* the final diagram to the associated JavaDoc page. * the final diagram to the associated JavaDoc page.
*/ */
private String mapApiDocRoot(String className) { private String mapApiDocRoot(String className) {
String root;
/* If no packages are specified, we use apiDocRoot for all of them. */ /* If no packages are specified, we use apiDocRoot for all of them. */
if (rootClasses.contains(className)) { if (rootClasses.contains(className))
root = optionProvider.getGlobalOptions().apiDocRoot; return optionProvider.getGlobalOptions().apiDocRoot;
} else { else
Options globalOptions = optionProvider.getGlobalOptions(); return optionProvider.getGlobalOptions().getApiDocRoot(className);
root = globalOptions.getApiDocRoot(className);
}
return root;
} }
@ -1004,11 +983,7 @@ class ClassGraph {
else { else {
// prepare output file. Use the output file name as a full path unless the output // prepare output file. Use the output file name as a full path unless the output
// directory is specified // directory is specified
File file; File file = new File(opt.outputDirectory, opt.outputFileName);
if (opt.outputDirectory != null)
file = new File(opt.outputDirectory, opt.outputFileName);
else
file = new File(opt.outputFileName);
// make sure the output directory are there, otherwise create them // make sure the output directory are there, otherwise create them
if (file.getParentFile() != null if (file.getParentFile() != null
&& !file.getParentFile().exists()) && !file.getParentFile().exists())
@ -1049,13 +1024,8 @@ class ClassGraph {
} }
private void externalTableStart(Options opt, String name, String url) { private void externalTableStart(Options opt, String name, String url) {
String bgcolor = ""; String bgcolor = opt.nodeFillColor == null ? "" : (" bgcolor=\"" + opt.nodeFillColor + "\"");
if (opt.nodeFillColor != null) String href = url == null ? "" : (" href=\"" + url + "\" target=\"_parent\"");
bgcolor = " bgcolor=\""+ opt.nodeFillColor + "\"";
String href = "";
if (url != null)
href = " href=\"" + url + "\" target=\"_parent\"";
w.print("<<table title=\"" + name + "\" border=\"0\" cellborder=\"" + w.print("<<table title=\"" + name + "\" border=\"0\" cellborder=\"" +
opt.shape.cellBorder() + "\" cellspacing=\"0\" " + opt.shape.cellBorder() + "\" cellspacing=\"0\" " +
"cellpadding=\"2\" port=\"p\"" + bgcolor + href + ">" + linePostfix); "cellpadding=\"2\" port=\"p\"" + bgcolor + href + ">" + linePostfix);
@ -1096,23 +1066,9 @@ class ClassGraph {
} }
private void tableLine(Align align, String text, Options opt, Font font) { private void tableLine(Align align, String text, Options opt, Font font) {
String open; w.print("<tr><td align=\"" + align.lower + "\" balign=\"" + align.lower + "\">" //
String close = "</td></tr>"; + fontWrap(" " + text + " ", opt, font) //
String prefix = linePrefix + linePrefix + linePrefix; + "</td></tr>" + linePostfix);
String alignText;
if(align == Align.CENTER)
alignText = "center";
else if(align == Align.LEFT)
alignText = "left";
else if(align == Align.RIGHT)
alignText = "right";
else
throw new RuntimeException("Unknown alignement type " + align);
text = fontWrap(" " + text + " ", opt, font);
open = "<tr><td align=\"" + alignText + "\" balign=\"" + alignText + "\">";
w.print(open + text + close + linePostfix);
} }
/** /**
@ -1128,12 +1084,9 @@ class ClassGraph {
} else if(font == Font.CLASS) { } else if(font == Font.CLASS) {
return fontWrap(text, opt.nodeFontClassName, opt.nodeFontClassSize); return fontWrap(text, opt.nodeFontClassName, opt.nodeFontClassSize);
} else if(font == Font.CLASS_ABSTRACT) { } else if(font == Font.CLASS_ABSTRACT) {
String name; return fontWrap(text,
if(opt.nodeFontClassAbstractName == null) opt.nodeFontClassAbstractName == null ? opt.nodeFontAbstractName : opt.nodeFontClassAbstractName,
name = opt.nodeFontAbstractName; opt.nodeFontClassSize);
else
name = opt.nodeFontClassAbstractName;
return fontWrap(text, name, opt.nodeFontClassSize);
} else if(font == Font.PACKAGE) { } else if(font == Font.PACKAGE) {
return fontWrap(text, opt.nodeFontPackageName, opt.nodeFontPackageSize); return fontWrap(text, opt.nodeFontPackageName, opt.nodeFontPackageSize);
} else if(font == Font.TAG) { } else if(font == Font.TAG) {

View File

@ -20,7 +20,6 @@
package org.umlgraph.doclet; package org.umlgraph.doclet;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
/** /**
@ -47,7 +46,7 @@ class ClassInfo {
ClassInfo(boolean p, boolean h) { ClassInfo(boolean p, boolean h) {
nodePrinted = p; nodePrinted = p;
hidden = h; hidden = h;
name = "c" + (new Integer(classNumber)).toString(); name = "c" + classNumber;
classNumber++; classNumber++;
} }

View File

@ -91,7 +91,7 @@ public class ContextView implements OptionProvider {
} }
public Options getOptionsFor(String name) { public Options getOptionsFor(String name) {
Options opt; Options opt;
if (!matcher.matches(name)) if (!matcher.matches(name))
opt = hideOptions; opt = hideOptions;
else if (name.equals(cd.name())) else if (name.equals(cd.name()))

View File

@ -26,23 +26,17 @@ public class InterfaceMatcher implements ClassMatcher {
// for each interface, recurse, since classes and interfaces // for each interface, recurse, since classes and interfaces
// are treated the same in the doclet API // are treated the same in the doclet API
for (ClassDoc iface : cd.interfaces()) { for (ClassDoc iface : cd.interfaces())
if(matches(iface)) if(matches(iface))
return true; return true;
}
// recurse on supeclass, if available // recurse on supeclass, if available
if(cd.superclass() != null) return cd.superclass() == null ? false : matches(cd.superclass());
return matches(cd.superclass());
return false;
} }
public boolean matches(String name) { public boolean matches(String name) {
ClassDoc cd = root.classNamed(name); ClassDoc cd = root.classNamed(name);
if(cd == null) return cd == null ? false : matches(cd);
return false;
return matches(cd);
} }
} }

View File

@ -288,10 +288,10 @@ public class Options implements Cloneable, OptionProvider {
showType = true; showType = true;
} else if (opt[0].equals("-!types")) { } else if (opt[0].equals("-!types")) {
showType = false; showType = false;
} else if(opt[0].equals("-autoSize")) { } else if(opt[0].equals("-autoSize")) {
autoSize = true; autoSize = true;
} else if (opt[0].equals("-!autoSize")) { } else if (opt[0].equals("-!autoSize")) {
autoSize = false; autoSize = false;
} else if(opt[0].equals("-commentname")) { } else if(opt[0].equals("-commentname")) {
showComment = true; showComment = true;
} else if (opt[0].equals("-!commentname")) { } else if (opt[0].equals("-!commentname")) {
@ -589,11 +589,10 @@ public class Options implements Cloneable, OptionProvider {
userMap.load(is); userMap.load(is);
for (Map.Entry<?, ?> mapEntry : userMap.entrySet()) { for (Map.Entry<?, ?> mapEntry : userMap.entrySet()) {
try { try {
Pattern regex = Pattern.compile((String) mapEntry.getKey());
String thisRoot = (String) mapEntry.getValue(); String thisRoot = (String) mapEntry.getValue();
if (thisRoot != null) { if (thisRoot != null) {
thisRoot = fixApiDocRoot(thisRoot); thisRoot = fixApiDocRoot(thisRoot);
apiDocMap.put(regex, thisRoot); apiDocMap.put(Pattern.compile((String) mapEntry.getKey()), thisRoot);
} else { } else {
System.err.println("No URL for pattern " + mapEntry.getKey()); System.err.println("No URL for pattern " + mapEntry.getKey());
} }
@ -614,7 +613,7 @@ public class Options implements Cloneable, OptionProvider {
* match the class name against the regular expressions specified in the * match the class name against the regular expressions specified in the
* <code>apiDocMap</code>; if a match is found, the associated URL * <code>apiDocMap</code>; if a match is found, the associated URL
* will be returned. * will be returned.
* * <p>
* <b>NOTE:</b> The match order of the match attempts is the one specified by the * <b>NOTE:</b> The match order of the match attempts is the one specified by the
* constructor of the api doc root, so it depends on the order of "-link" and "-apiDocMap" * constructor of the api doc root, so it depends on the order of "-link" and "-apiDocMap"
* parameters. * parameters.
@ -624,9 +623,7 @@ public class Options implements Cloneable, OptionProvider {
apiDocMap.put(Pattern.compile(".*"), DEFAULT_EXTERNAL_APIDOC); apiDocMap.put(Pattern.compile(".*"), DEFAULT_EXTERNAL_APIDOC);
for (Map.Entry<Pattern, String> mapEntry : apiDocMap.entrySet()) { for (Map.Entry<Pattern, String> mapEntry : apiDocMap.entrySet()) {
Pattern regex = mapEntry.getKey(); if (mapEntry.getKey().matcher(className).matches())
Matcher matcher = regex.matcher(className);
if (matcher.matches())
return mapEntry.getValue(); return mapEntry.getValue();
} }
return null; return null;
@ -634,16 +631,15 @@ public class Options implements Cloneable, OptionProvider {
/** Trim and append a file separator to the string */ /** Trim and append a file separator to the string */
private String fixApiDocRoot(String str) { private String fixApiDocRoot(String str) {
String fixed = null; if (str == null)
if (str != null) { return null;
fixed = str.trim(); String fixed = str.trim();
if (fixed.length() > 0) { if (fixed.isEmpty())
if (!File.separator.equals("/")) return "";
fixed = fixed.replace(File.separator.charAt(0), '/'); if (File.separatorChar != '/')
if (!fixed.endsWith("/")) fixed = fixed.replace(File.separatorChar, '/');
fixed = fixed + "/"; if (!fixed.endsWith("/"))
} fixed = fixed + "/";
}
return fixed; return fixed;
} }
@ -678,13 +674,8 @@ public class Options implements Cloneable, OptionProvider {
return true; return true;
Matcher m = hidePattern.matcher(s); Matcher m = hidePattern.matcher(s);
if (strictMatching) { if (strictMatching ? m.matches() : m.find())
if (m.matches()) {
return true;
}
} else if (m.find()) {
return true; return true;
}
} }
return false; return false;
} }
@ -697,13 +688,8 @@ public class Options implements Cloneable, OptionProvider {
public boolean matchesIncludeExpression(String s) { public boolean matchesIncludeExpression(String s) {
for (Pattern includePattern : includePatterns) { for (Pattern includePattern : includePatterns) {
Matcher m = includePattern.matcher(s); Matcher m = includePattern.matcher(s);
if (strictMatching) { if (strictMatching ? m.matches() : m.find())
if (m.matches()) {
return true;
}
} else if (m.find()) {
return true; return true;
}
} }
return false; return false;
} }
@ -716,13 +702,8 @@ public class Options implements Cloneable, OptionProvider {
public boolean matchesCollPackageExpression(String s) { public boolean matchesCollPackageExpression(String s) {
for (Pattern collPattern : collPackages) { for (Pattern collPattern : collPackages) {
Matcher m = collPattern.matcher(s); Matcher m = collPattern.matcher(s);
if (strictMatching) { if (strictMatching ? m.matches() : m.find())
if (m.matches()) {
return true;
}
} else if (m.find()) {
return true; return true;
}
} }
return false; return false;
} }

View File

@ -16,10 +16,9 @@ public class PackageMatcher implements ClassMatcher {
} }
public boolean matches(String name) { public boolean matches(String name) {
for (ClassDoc cd : packageDoc.allClasses()) { for (ClassDoc cd : packageDoc.allClasses())
if (cd.qualifiedName().equals(name)) if (cd.qualifiedName().equals(name))
return true; return true;
}
return false; return false;
} }

View File

@ -16,7 +16,6 @@
*/ */
package org.umlgraph.doclet; package org.umlgraph.doclet;
import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import com.sun.javadoc.ClassDoc; import com.sun.javadoc.ClassDoc;
@ -38,8 +37,7 @@ public class PatternMatcher implements ClassMatcher {
} }
public boolean matches(String name) { public boolean matches(String name) {
Matcher matcher = pattern.matcher(name); return pattern.matcher(name).matches();
return matcher.matches();
} }
} }

View File

@ -13,12 +13,9 @@ public enum RelationDirection {
* @return * @return
*/ */
public RelationDirection sum(RelationDirection d) { public RelationDirection sum(RelationDirection d) {
if (this == NONE) // Handle same and nones first:
return d; return (this == d || d == NONE) ? this : //
this == NONE ? d : BOTH; // They are different and not none.
if ((this == IN && d == OUT) || (this == OUT && d == IN) || this == BOTH || d == BOTH)
return BOTH;
return this;
} }
/** /**
@ -28,10 +25,7 @@ public enum RelationDirection {
* @return * @return
*/ */
public boolean contains(RelationDirection d) { public boolean contains(RelationDirection d) {
if (this == BOTH) return this == BOTH ? true : (d == this);
return true;
else
return d == this;
} }
/** /**
@ -40,12 +34,7 @@ public enum RelationDirection {
* @return * @return
*/ */
public RelationDirection inverse() { public RelationDirection inverse() {
if (this == IN) return this == IN ? OUT : this == OUT ? IN : this;
return OUT;
else if (this == OUT)
return IN;
else
return this;
} }
}; };

View File

@ -18,16 +18,6 @@
package org.umlgraph.doclet; package org.umlgraph.doclet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import com.sun.javadoc.ClassDoc;
import com.sun.javadoc.Doc;
import com.sun.javadoc.LanguageVersion;
import com.sun.javadoc.RootDoc;
/** /**
* Properties of node shapes * Properties of node shapes
* *
@ -79,10 +69,7 @@ public class Shape {
/** Return the shape's GraphViz landing port */ /** Return the shape's GraphViz landing port */
String landingPort() { String landingPort() {
if (name.equals("class") || name.equals("activeclass")) return (name.equals("class") || name.equals("activeclass")) ? ":p" : "";
return ":p";
else
return "";
} }
/** Return the table border required for the shape */ /** Return the table border required for the shape */

View File

@ -25,17 +25,12 @@ public class SubclassMatcher implements ClassMatcher {
return true; return true;
// recurse on supeclass, if available // recurse on supeclass, if available
if(cd.superclass() != null) return cd.superclass() == null ? false : matches(cd.superclass());
return matches(cd.superclass());
return false;
} }
public boolean matches(String name) { public boolean matches(String name) {
ClassDoc cd = root.classNamed(name); ClassDoc cd = root.classNamed(name);
if(cd == null) return cd == null ? false : matches(cd);
return false;
return matches(cd);
} }
} }

View File

@ -66,8 +66,7 @@ public class UmlGraphDoc {
generatePackageDiagrams(root, opt, outputFolder); generatePackageDiagrams(root, opt, outputFolder);
generateContextDiagrams(root, opt, outputFolder); generateContextDiagrams(root, opt, outputFolder);
} catch(Throwable t) { } catch(Throwable t) {
root.printWarning("Error!"); root.printWarning("Error: " + t.toString());
root.printWarning(t.toString());
t.printStackTrace(); t.printStackTrace();
return false; return false;
} }

View File

@ -281,9 +281,7 @@ public class WrappedClassDoc implements ClassDoc {
} }
public String name() { public String name() {
if (name == null) return name != null ? name : (name = wrapped.name());
name = wrapped.name();
return name;
} }
public SourcePosition position() { public SourcePosition position() {
@ -331,9 +329,7 @@ public class WrappedClassDoc implements ClassDoc {
} }
public Tag[] tags() { public Tag[] tags() {
if (tags == null) return tags != null ? tags : (tags = wrapped.tags());
tags = wrapped.tags();
return tags;
} }
public Tag[] tags(String arg0) { public Tag[] tags(String arg0) {
@ -341,10 +337,7 @@ public class WrappedClassDoc implements ClassDoc {
} }
public String toString() { public String toString() {
if (toString == null) { return toString != null ? toString : (toString = wrapped.toString());
toString = wrapped.toString();
}
return toString;
} }
public String typeName() { public String typeName() {