8004828: refactor init of *DocImpl classes

Reviewed-by: darcy
This commit is contained in:
Jonathan Gibbons 2012-12-11 15:05:55 -08:00
parent 8fc2d739bd
commit 1c8e65a20e
15 changed files with 128 additions and 107 deletions

View file

@ -27,13 +27,13 @@ package com.sun.tools.javadoc;
import com.sun.javadoc.*;
import com.sun.source.util.TreePath;
import com.sun.tools.javac.code.Kinds;
import com.sun.tools.javac.code.Scope;
import com.sun.tools.javac.code.Symbol.*;
import com.sun.tools.javac.tree.JCTree.*;
import com.sun.tools.javac.util.List;
import com.sun.tools.javac.util.Names;
import com.sun.tools.javac.util.Position;
/**
* Represents an annotation type.
@ -51,12 +51,11 @@ public class AnnotationTypeDocImpl
extends ClassDocImpl implements AnnotationTypeDoc {
public AnnotationTypeDocImpl(DocEnv env, ClassSymbol sym) {
this(env, sym, null, null, null);
this(env, sym, null);
}
public AnnotationTypeDocImpl(DocEnv env, ClassSymbol sym,
String doc, JCClassDecl tree, Position.LineMap lineMap) {
super(env, sym, doc, tree, lineMap);
public AnnotationTypeDocImpl(DocEnv env, ClassSymbol sym, TreePath treePath) {
super(env, sym, treePath);
}
/**

View file

@ -27,9 +27,9 @@ package com.sun.tools.javadoc;
import com.sun.javadoc.*;
import com.sun.source.util.TreePath;
import com.sun.tools.javac.code.Symbol.*;
import com.sun.tools.javac.tree.JCTree.*;
import com.sun.tools.javac.util.Position;
/**
* Represents an element of an annotation type.
@ -50,9 +50,8 @@ public class AnnotationTypeElementDocImpl
super(env, sym);
}
public AnnotationTypeElementDocImpl(DocEnv env, MethodSymbol sym,
String doc, JCMethodDecl tree, Position.LineMap lineMap) {
super(env, sym, doc, tree, lineMap);
public AnnotationTypeElementDocImpl(DocEnv env, MethodSymbol sym, TreePath treePath) {
super(env, sym, treePath);
}
/**

View file

@ -31,13 +31,14 @@ import java.lang.reflect.Modifier;
import java.net.URI;
import java.util.HashSet;
import java.util.Set;
import javax.tools.FileObject;
import javax.tools.JavaFileManager.Location;
import javax.tools.StandardJavaFileManager;
import javax.tools.StandardLocation;
import com.sun.javadoc.*;
import com.sun.source.util.TreePath;
import com.sun.tools.javac.code.Flags;
import com.sun.tools.javac.code.Kinds;
import com.sun.tools.javac.code.Scope;
@ -45,22 +46,17 @@ import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.code.Symbol.*;
import com.sun.tools.javac.code.Type;
import com.sun.tools.javac.code.Type.ClassType;
import com.sun.tools.javac.comp.AttrContext;
import com.sun.tools.javac.comp.Env;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.JCTree.JCClassDecl;
import com.sun.tools.javac.tree.JCTree.JCFieldAccess;
import com.sun.tools.javac.tree.JCTree.JCImport;
import com.sun.tools.javac.tree.TreeInfo;
import com.sun.tools.javac.util.List;
import com.sun.tools.javac.util.ListBuffer;
import com.sun.tools.javac.util.Name;
import com.sun.tools.javac.util.Names;
import com.sun.tools.javac.util.Position;
import static com.sun.tools.javac.code.Kinds.*;
import static com.sun.tools.javac.code.TypeTag.CLASS;
import static com.sun.tools.javac.tree.JCTree.Tag.*;
@ -100,15 +96,14 @@ public class ClassDocImpl extends ProgramElementDocImpl implements ClassDoc {
* Constructor
*/
public ClassDocImpl(DocEnv env, ClassSymbol sym) {
this(env, sym, null, null, null);
this(env, sym, null);
}
/**
* Constructor
*/
public ClassDocImpl(DocEnv env, ClassSymbol sym, String documentation,
JCClassDecl tree, Position.LineMap lineMap) {
super(env, sym, documentation, tree, lineMap);
public ClassDocImpl(DocEnv env, ClassSymbol sym, TreePath treePath) {
super(env, sym, treePath);
this.type = (ClassType)sym.type;
this.tsym = sym;
}

View file

@ -27,10 +27,9 @@ package com.sun.tools.javadoc;
import com.sun.javadoc.*;
import com.sun.source.util.TreePath;
import com.sun.tools.javac.code.Symbol.ClassSymbol;
import com.sun.tools.javac.code.Symbol.MethodSymbol;
import com.sun.tools.javac.tree.JCTree.JCMethodDecl;
import com.sun.tools.javac.util.Position;
/**
* Represents a constructor of a java class.
@ -58,9 +57,8 @@ public class ConstructorDocImpl
/**
* constructor.
*/
public ConstructorDocImpl(DocEnv env, MethodSymbol sym,
String docComment, JCMethodDecl tree, Position.LineMap lineMap) {
super(env, sym, docComment, tree, lineMap);
public ConstructorDocImpl(DocEnv env, MethodSymbol sym, TreePath treePath) {
super(env, sym, treePath);
}
/**

View file

@ -27,18 +27,20 @@ package com.sun.tools.javadoc;
import java.lang.reflect.Modifier;
import java.util.*;
import javax.tools.JavaFileManager;
import com.sun.javadoc.*;
import com.sun.source.util.TreePath;
import com.sun.tools.javac.api.JavacTrees;
import com.sun.tools.javac.code.*;
import com.sun.tools.javac.code.Symbol.*;
import com.sun.tools.javac.code.Type.ClassType;
import com.sun.tools.javac.comp.Check;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.JCTree.*;
import com.sun.tools.javac.util.Context;
import com.sun.tools.javac.util.Names;
import com.sun.tools.javac.util.Position;
/**
* Holds the environment for a run of javadoc.
@ -104,6 +106,8 @@ public class DocEnv {
JavaFileManager fileManager;
Context context;
WeakHashMap<JCTree, TreePath> treePaths = new WeakHashMap<JCTree, TreePath>();
/** Allow documenting from class files? */
boolean docClasses = false;
@ -540,13 +544,12 @@ public class DocEnv {
/**
* Create the PackageDoc (or a subtype) for a package symbol.
*/
void makePackageDoc(PackageSymbol pack, String docComment, JCCompilationUnit tree) {
void makePackageDoc(PackageSymbol pack, TreePath treePath) {
PackageDocImpl result = packageMap.get(pack);
if (result != null) {
if (docComment != null) result.setRawCommentText(docComment);
if (tree != null) result.setTree(tree);
if (treePath != null) result.setTreePath(treePath);
} else {
result = new PackageDocImpl(this, pack, docComment, tree);
result = new PackageDocImpl(this, pack, treePath);
packageMap.put(pack, result);
}
}
@ -572,17 +575,16 @@ public class DocEnv {
/**
* Create the ClassDoc (or a subtype) for a class symbol.
*/
protected void makeClassDoc(ClassSymbol clazz, String docComment, JCClassDecl tree, Position.LineMap lineMap) {
protected void makeClassDoc(ClassSymbol clazz, TreePath treePath) {
ClassDocImpl result = classMap.get(clazz);
if (result != null) {
if (docComment != null) result.setRawCommentText(docComment);
if (tree != null) result.setTree(tree);
if (treePath != null) result.setTreePath(treePath);
return;
}
if (isAnnotationType(tree)) { // flags of clazz may not yet be set
result = new AnnotationTypeDocImpl(this, clazz, docComment, tree, lineMap);
if (isAnnotationType((JCClassDecl) treePath.getLeaf())) { // flags of clazz may not yet be set
result = new AnnotationTypeDocImpl(this, clazz, treePath);
} else {
result = new ClassDocImpl(this, clazz, docComment, tree, lineMap);
result = new ClassDocImpl(this, clazz, treePath);
}
classMap.put(clazz, result);
}
@ -610,13 +612,12 @@ public class DocEnv {
/**
* Create a FieldDoc for a var symbol.
*/
protected void makeFieldDoc(VarSymbol var, String docComment, JCVariableDecl tree, Position.LineMap lineMap) {
protected void makeFieldDoc(VarSymbol var, TreePath treePath) {
FieldDocImpl result = fieldMap.get(var);
if (result != null) {
if (docComment != null) result.setRawCommentText(docComment);
if (tree != null) result.setTree(tree);
if (treePath != null) result.setTreePath(treePath);
} else {
result = new FieldDocImpl(this, var, docComment, tree, lineMap);
result = new FieldDocImpl(this, var, treePath);
fieldMap.put(var, result);
}
}
@ -627,14 +628,12 @@ public class DocEnv {
* Create a MethodDoc for this MethodSymbol.
* Should be called only on symbols representing methods.
*/
protected void makeMethodDoc(MethodSymbol meth, String docComment,
JCMethodDecl tree, Position.LineMap lineMap) {
protected void makeMethodDoc(MethodSymbol meth, TreePath treePath) {
MethodDocImpl result = (MethodDocImpl)methodMap.get(meth);
if (result != null) {
if (docComment != null) result.setRawCommentText(docComment);
if (tree != null) result.setTree(tree);
if (treePath != null) result.setTreePath(treePath);
} else {
result = new MethodDocImpl(this, meth, docComment, tree, lineMap);
result = new MethodDocImpl(this, meth, treePath);
methodMap.put(meth, result);
}
}
@ -656,14 +655,12 @@ public class DocEnv {
* Create the ConstructorDoc for a MethodSymbol.
* Should be called only on symbols representing constructors.
*/
protected void makeConstructorDoc(MethodSymbol meth, String docComment,
JCMethodDecl tree, Position.LineMap lineMap) {
protected void makeConstructorDoc(MethodSymbol meth, TreePath treePath) {
ConstructorDocImpl result = (ConstructorDocImpl)methodMap.get(meth);
if (result != null) {
if (docComment != null) result.setRawCommentText(docComment);
if (tree != null) result.setTree(tree);
if (treePath != null) result.setTreePath(treePath);
} else {
result = new ConstructorDocImpl(this, meth, docComment, tree, lineMap);
result = new ConstructorDocImpl(this, meth, treePath);
methodMap.put(meth, result);
}
}
@ -685,16 +682,14 @@ public class DocEnv {
* Create the AnnotationTypeElementDoc for a MethodSymbol.
* Should be called only on symbols representing annotation type elements.
*/
protected void makeAnnotationTypeElementDoc(MethodSymbol meth,
String docComment, JCMethodDecl tree, Position.LineMap lineMap) {
protected void makeAnnotationTypeElementDoc(MethodSymbol meth, TreePath treePath) {
AnnotationTypeElementDocImpl result =
(AnnotationTypeElementDocImpl)methodMap.get(meth);
if (result != null) {
if (docComment != null) result.setRawCommentText(docComment);
if (tree != null) result.setTree(tree);
if (treePath != null) result.setTreePath(treePath);
} else {
result =
new AnnotationTypeElementDocImpl(this, meth, docComment, tree, lineMap);
new AnnotationTypeElementDocImpl(this, meth, treePath);
methodMap.put(meth, result);
}
}
@ -730,6 +725,18 @@ public class DocEnv {
// return result;
}
TreePath getTreePath(JCCompilationUnit tree) {
TreePath p = treePaths.get(tree);
if (p == null)
treePaths.put(tree, p = new TreePath(tree));
return p;
}
TreePath getTreePath(JCCompilationUnit toplevel, JCTree tree) {
// don't bother to cache paths for classes and members
return new TreePath(getTreePath(toplevel), tree);
}
/**
* Set the encoding.
*/

View file

@ -35,6 +35,9 @@ import java.util.regex.Pattern;
import javax.tools.FileObject;
import com.sun.javadoc.*;
import com.sun.source.util.TreePath;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
import com.sun.tools.javac.util.Position;
/**
@ -60,6 +63,12 @@ public abstract class DocImpl implements Doc, Comparable<Object> {
*/
protected final DocEnv env; //### Rename this everywhere to 'docenv' ?
/**
* Back pointer to the tree node for this doc item.
* May be null if there is no associated tree.
*/
protected TreePath treePath;
/**
* The complex comment object, lazily initialized.
*/
@ -88,11 +97,21 @@ public abstract class DocImpl implements Doc, Comparable<Object> {
/**
* Constructor.
*/
DocImpl(DocEnv env, String documentation) {
this.documentation = documentation;
DocImpl(DocEnv env, TreePath treePath) {
this.treePath = treePath;
this.documentation = getCommentText(treePath);
this.env = env;
}
private static String getCommentText(TreePath p) {
if (p == null)
return null;
JCCompilationUnit topLevel = (JCCompilationUnit) p.getCompilationUnit();
JCTree tree = (JCTree) p.getLeaf();
return topLevel.docComments.getCommentText(tree);
}
/**
* So subclasses have the option to do lazy initialization of
* "documentation" string.
@ -213,10 +232,20 @@ public abstract class DocImpl implements Doc, Comparable<Object> {
* operations like internalization.
*/
public void setRawCommentText(String rawDocumentation) {
treePath = null;
documentation = rawDocumentation;
comment = null;
}
/**
* Set the full unprocessed text of the comment and tree path.
*/
void setTreePath(TreePath treePath) {
this.treePath = treePath;
documentation = getCommentText(treePath);
comment = null;
}
/**
* return a key for sorting.
*/

View file

@ -30,13 +30,12 @@ import java.text.CollationKey;
import com.sun.javadoc.*;
import com.sun.source.util.TreePath;
import com.sun.tools.javac.code.Flags;
import com.sun.tools.javac.code.Symbol.*;
import com.sun.tools.javac.code.Type;
import com.sun.tools.javac.tree.JCTree.JCMethodDecl;
import com.sun.tools.javac.util.List;
import com.sun.tools.javac.util.ListBuffer;
import com.sun.tools.javac.util.Position;
/**
* Represents a method or constructor of a java class.
@ -60,9 +59,8 @@ public abstract class ExecutableMemberDocImpl
/**
* Constructor.
*/
public ExecutableMemberDocImpl(DocEnv env, MethodSymbol sym,
String rawDocs, JCMethodDecl tree, Position.LineMap lineMap) {
super(env, sym, rawDocs, tree, lineMap);
public ExecutableMemberDocImpl(DocEnv env, MethodSymbol sym, TreePath treePath) {
super(env, sym, treePath);
this.sym = sym;
}
@ -70,7 +68,7 @@ public abstract class ExecutableMemberDocImpl
* Constructor.
*/
public ExecutableMemberDocImpl(DocEnv env, MethodSymbol sym) {
this(env, sym, null, null, null);
this(env, sym, null);
}
/**

View file

@ -25,6 +25,7 @@
package com.sun.tools.javadoc;
import com.sun.source.util.TreePath;
import java.lang.reflect.Modifier;
import com.sun.javadoc.*;
@ -61,9 +62,8 @@ public class FieldDocImpl extends MemberDocImpl implements FieldDoc {
/**
* Constructor.
*/
public FieldDocImpl(DocEnv env, VarSymbol sym,
String rawDocs, JCVariableDecl tree, Position.LineMap lineMap) {
super(env, sym, rawDocs, tree, lineMap);
public FieldDocImpl(DocEnv env, VarSymbol sym, TreePath treePath) {
super(env, sym, treePath);
this.sym = sym;
}
@ -71,7 +71,7 @@ public class FieldDocImpl extends MemberDocImpl implements FieldDoc {
* Constructor.
*/
public FieldDocImpl(DocEnv env, VarSymbol sym) {
this(env, sym, null, null, null);
this(env, sym, null);
}
/**

View file

@ -25,13 +25,14 @@
package com.sun.tools.javadoc;
import javax.tools.JavaFileObject;
import com.sun.source.util.TreePath;
import com.sun.tools.javac.code.Kinds;
import com.sun.tools.javac.code.Symbol.*;
import com.sun.tools.javac.comp.Enter;
import com.sun.tools.javac.tree.JCTree.*;
import com.sun.tools.javac.tree.TreeInfo;
import com.sun.tools.javac.util.Context;
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
import com.sun.tools.javac.util.List;
@ -85,8 +86,7 @@ public class JavadocEnter extends Enter {
public void visitTopLevel(JCCompilationUnit tree) {
super.visitTopLevel(tree);
if (tree.sourcefile.isNameCompatible("package-info", JavaFileObject.Kind.SOURCE)) {
String comment = TreeInfo.getCommentText(env, tree);
docenv.makePackageDoc(tree.packge, comment, tree);
docenv.makePackageDoc(tree.packge, docenv.getTreePath(tree));
}
}
@ -95,9 +95,8 @@ public class JavadocEnter extends Enter {
super.visitClassDef(tree);
if (tree.sym == null) return;
if (tree.sym.kind == Kinds.TYP || tree.sym.kind == Kinds.ERR) {
String comment = TreeInfo.getCommentText(env, tree);
ClassSymbol c = tree.sym;
docenv.makeClassDoc(c, comment, tree, env.toplevel.lineMap);
docenv.makeClassDoc(c, docenv.getTreePath(env.toplevel, tree));
}
}

View file

@ -25,14 +25,13 @@
package com.sun.tools.javadoc;
import com.sun.source.util.TreePath;
import com.sun.tools.javac.code.Flags;
import com.sun.tools.javac.code.Kinds;
import com.sun.tools.javac.code.Symbol.*;
import com.sun.tools.javac.comp.MemberEnter;
import com.sun.tools.javac.tree.JCTree.*;
import com.sun.tools.javac.tree.TreeInfo;
import com.sun.tools.javac.util.Context;
import com.sun.tools.javac.util.Position;
/**
* Javadoc's own memberEnter phase does a few things above and beyond that
@ -73,14 +72,13 @@ public class JavadocMemberEnter extends MemberEnter {
super.visitMethodDef(tree);
MethodSymbol meth = tree.sym;
if (meth == null || meth.kind != Kinds.MTH) return;
String docComment = TreeInfo.getCommentText(env, tree);
Position.LineMap lineMap = env.toplevel.lineMap;
TreePath treePath = docenv.getTreePath(env.toplevel, tree);
if (meth.isConstructor())
docenv.makeConstructorDoc(meth, docComment, tree, lineMap);
docenv.makeConstructorDoc(meth, treePath);
else if (isAnnotationTypeElement(meth))
docenv.makeAnnotationTypeElementDoc(meth, docComment, tree, lineMap);
docenv.makeAnnotationTypeElementDoc(meth, treePath);
else
docenv.makeMethodDoc(meth, docComment, tree, lineMap);
docenv.makeMethodDoc(meth, treePath);
// release resources
tree.body = null;
@ -92,9 +90,7 @@ public class JavadocMemberEnter extends MemberEnter {
if (tree.sym != null &&
tree.sym.kind == Kinds.VAR &&
!isParameter(tree.sym)) {
String docComment = TreeInfo.getCommentText(env, tree);
Position.LineMap lineMap = env.toplevel.lineMap;
docenv.makeFieldDoc(tree.sym, docComment, tree, lineMap);
docenv.makeFieldDoc(tree.sym, docenv.getTreePath(env.toplevel, tree));
}
}

View file

@ -27,9 +27,8 @@ package com.sun.tools.javadoc;
import com.sun.javadoc.*;
import com.sun.source.util.TreePath;
import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.util.Position;
/**
* Represents a member of a java class: field, constructor, or method.
@ -57,8 +56,8 @@ public abstract class MemberDocImpl
/**
* constructor.
*/
public MemberDocImpl(DocEnv env, Symbol sym, String doc, JCTree tree, Position.LineMap lineMap) {
super(env, sym, doc, tree, lineMap);
public MemberDocImpl(DocEnv env, Symbol sym, TreePath treePath) {
super(env, sym, treePath);
}
/**

View file

@ -28,12 +28,10 @@ package com.sun.tools.javadoc;
import java.lang.reflect.Modifier;
import com.sun.javadoc.*;
import com.sun.source.util.TreePath;
import com.sun.tools.javac.code.*;
import com.sun.tools.javac.code.Symbol.*;
import com.sun.tools.javac.code.Type;
import com.sun.tools.javac.tree.JCTree.JCMethodDecl;
import com.sun.tools.javac.util.Position;
import static com.sun.tools.javac.code.TypeTag.CLASS;
/**
@ -62,9 +60,8 @@ public class MethodDocImpl
/**
* constructor.
*/
public MethodDocImpl(DocEnv env, MethodSymbol sym,
String docComment, JCMethodDecl tree, Position.LineMap lineMap) {
super(env, sym, docComment, tree, lineMap);
public MethodDocImpl(DocEnv env, MethodSymbol sym, TreePath treePath) {
super(env, sym, treePath);
}
/**

View file

@ -31,6 +31,7 @@ import java.io.InputStream;
import javax.tools.FileObject;
import com.sun.javadoc.*;
import com.sun.source.util.TreePath;
import com.sun.tools.javac.code.Attribute;
import com.sun.tools.javac.code.Scope;
import com.sun.tools.javac.code.Symbol.ClassSymbol;
@ -75,17 +76,16 @@ public class PackageDocImpl extends DocImpl implements PackageDoc {
* Constructor
*/
public PackageDocImpl(DocEnv env, PackageSymbol sym) {
this(env, sym, null, null);
this(env, sym, null);
}
/**
* Constructor
*/
public PackageDocImpl(DocEnv env, PackageSymbol sym,
String documentation, JCTree tree) {
super(env, documentation);
public PackageDocImpl(DocEnv env, PackageSymbol sym, TreePath treePath) {
super(env, treePath);
this.sym = sym;
this.tree = (JCCompilationUnit) tree;
this.tree = (treePath == null) ? null : (JCCompilationUnit) treePath.getCompilationUnit();
foundDoc = (documentation != null);
}
@ -93,8 +93,8 @@ public class PackageDocImpl extends DocImpl implements PackageDoc {
this.tree = (JCCompilationUnit) tree;
}
public void setRawCommentText(String rawDocumentation) {
super.setRawCommentText(rawDocumentation);
public void setTreePath(TreePath treePath) {
super.setTreePath(treePath);
checkDoc();
}

View file

@ -29,10 +29,12 @@ import java.lang.reflect.Modifier;
import java.text.CollationKey;
import com.sun.javadoc.*;
import com.sun.source.util.TreePath;
import com.sun.tools.javac.code.Attribute;
import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.code.Symbol.ClassSymbol;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
import com.sun.tools.javac.util.Position;
/**
@ -66,16 +68,20 @@ public abstract class ProgramElementDocImpl
// Cache for getModifiers().
private int modifiers = -1;
protected ProgramElementDocImpl(DocEnv env, Symbol sym,
String doc, JCTree tree, Position.LineMap lineMap) {
super(env, doc);
protected ProgramElementDocImpl(DocEnv env, Symbol sym, TreePath treePath) {
super(env, treePath);
this.sym = sym;
this.tree = tree;
this.lineMap = lineMap;
if (treePath != null) {
tree = (JCTree) treePath.getLeaf();
lineMap = ((JCCompilationUnit) treePath.getCompilationUnit()).lineMap;
}
}
void setTree(JCTree tree) {
this.tree = tree;
@Override
void setTreePath(TreePath treePath) {
super.setTreePath(treePath);
this.tree = (JCTree) treePath.getLeaf();
this.lineMap = ((JCCompilationUnit) treePath.getCompilationUnit()).lineMap;
}
/**

View file

@ -331,7 +331,6 @@ public class RootDocImpl extends DocImpl implements RootDoc {
@Override
protected String documentation() {
if (documentation == null) {
int cnt = options.length();
JavaFileObject overviewPath = getOverviewPath();
if (overviewPath == null) {
// no doc file to be had