14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package jdk.javadoc.internal.tool;
27
28 import com.sun.source.util.TreePath;
29 import com.sun.tools.javac.code.Flags;
30 import com.sun.tools.javac.code.Symbol.*;
31 import com.sun.tools.javac.comp.MemberEnter;
32 import com.sun.tools.javac.tree.JCTree;
33 import com.sun.tools.javac.tree.JCTree.*;
34 import com.sun.tools.javac.util.Context;
35
36 import static com.sun.tools.javac.code.Flags.*;
37 import static com.sun.tools.javac.code.Kinds.Kind.*;
38
39 /**
40 * Javadoc's own memberEnter phase does a few things above and beyond that
41 * done by javac.
42 *
43 * <p><b>This is NOT part of any supported API.
44 * If you write code that depends on this, you do so at your own risk.
45 * This code and its internal interfaces are subject to change or
46 * deletion without notice.</b>
47 *
48 * @author Neal Gafter
49 */
50 public class JavadocMemberEnter extends MemberEnter {
51 public static JavadocMemberEnter instance0(Context context) {
52 MemberEnter instance = context.get(memberEnterKey);
53 if (instance == null)
54 instance = new JavadocMemberEnter(context);
55 return (JavadocMemberEnter)instance;
56 }
57
58 public static void preRegister(Context context) {
59 context.put(memberEnterKey, (Context.Factory<MemberEnter>)JavadocMemberEnter::new);
60 }
61
62 final ToolEnvironment toolEnv;
63
64 protected JavadocMemberEnter(Context context) {
65 super(context);
66 toolEnv = ToolEnvironment.instance(context);
67 }
68
69 @Override
70 public void visitMethodDef(JCMethodDecl tree) {
71 super.visitMethodDef(tree);
72 MethodSymbol meth = tree.sym;
73 if (meth == null || meth.kind != MTH) return;
74 TreePath treePath = toolEnv.getTreePath(env.toplevel, env.enclClass, tree);
75 // do not add those methods that may be mandated by the spec,
76 // or those that are synthesized, thus if it does not exist in
77 // tree best to let other logic determine the TreePath.
78 if (env.enclClass.defs.contains(tree)) {
79 toolEnv.setElementToTreePath(meth, treePath);
80 }
81 // release resources
82 tree.body = null;
83 }
84
85 @Override
86 public void visitVarDef(JCVariableDecl tree) {
87 if (tree.init != null) {
88 boolean isFinal = (tree.mods.flags & FINAL) != 0
89 || (env.enclClass.mods.flags & INTERFACE) != 0;
90 if (!isFinal || containsNonConstantExpression(tree.init)) {
91 // Avoid unnecessary analysis and release resources.
92 // In particular, remove non-constant expressions
93 // which may trigger Attr.attribClass, since
94 // method bodies are also removed, in visitMethodDef.
95 tree.init = null;
96 }
97 }
98 super.visitVarDef(tree);
99 if (tree.sym != null && tree.sym.kind == VAR && !isParameter(tree.sym)) {
100 toolEnv.setElementToTreePath(tree.sym, toolEnv.getTreePath(env.toplevel, env.enclClass, tree));
101 }
102 }
|
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package jdk.javadoc.internal.tool;
27
28 import com.sun.source.util.TreePath;
29 import com.sun.tools.javac.code.Flags;
30 import com.sun.tools.javac.code.Symbol.*;
31 import com.sun.tools.javac.comp.MemberEnter;
32 import com.sun.tools.javac.tree.JCTree;
33 import com.sun.tools.javac.tree.JCTree.*;
34 import com.sun.tools.javac.tree.TreeInfo;
35 import com.sun.tools.javac.util.Context;
36 import com.sun.tools.javac.util.List;
37 import com.sun.tools.javac.util.Names;
38
39 import static com.sun.tools.javac.code.Flags.*;
40 import static com.sun.tools.javac.code.Kinds.Kind.*;
41
42 /**
43 * Javadoc's own memberEnter phase does a few things above and beyond that
44 * done by javac.
45 *
46 * <p><b>This is NOT part of any supported API.
47 * If you write code that depends on this, you do so at your own risk.
48 * This code and its internal interfaces are subject to change or
49 * deletion without notice.</b>
50 *
51 * @author Neal Gafter
52 */
53 public class JavadocMemberEnter extends MemberEnter {
54 public static JavadocMemberEnter instance0(Context context) {
55 MemberEnter instance = context.get(memberEnterKey);
56 if (instance == null)
57 instance = new JavadocMemberEnter(context);
58 return (JavadocMemberEnter)instance;
59 }
60
61 public static void preRegister(Context context) {
62 context.put(memberEnterKey, (Context.Factory<MemberEnter>)JavadocMemberEnter::new);
63 }
64
65 final ToolEnvironment toolEnv;
66
67
68 protected JavadocMemberEnter(Context context) {
69 super(context);
70 toolEnv = ToolEnvironment.instance(context);
71 }
72
73 @Override
74 public void visitMethodDef(JCMethodDecl tree) {
75 super.visitMethodDef(tree);
76 MethodSymbol meth = tree.sym;
77 if (meth == null || meth.kind != MTH) return;
78 TreePath treePath = toolEnv.getTreePath(env.toplevel, env.enclClass, tree);
79 // do not add those methods that may be mandated by the spec,
80 // or those that are synthesized, thus if it does not exist in
81 // tree best to let other logic determine the TreePath.
82 if (env.enclClass.defs.contains(tree)) {
83 toolEnv.setElementToTreePath(meth, treePath);
84 }
85 // release resources
86 // handle constructors for record types specially, because of downstream checks
87 if ((env.enclClass.mods.flags & Flags.RECORD) != 0 && TreeInfo.isConstructor(tree)) {
88 tree.body.stats = List.nil();
89 } else {
90 tree.body = null;
91 }
92 }
93
94 @Override
95 public void visitVarDef(JCVariableDecl tree) {
96 if (tree.init != null) {
97 boolean isFinal = (tree.mods.flags & FINAL) != 0
98 || (env.enclClass.mods.flags & INTERFACE) != 0;
99 if (!isFinal || containsNonConstantExpression(tree.init)) {
100 // Avoid unnecessary analysis and release resources.
101 // In particular, remove non-constant expressions
102 // which may trigger Attr.attribClass, since
103 // method bodies are also removed, in visitMethodDef.
104 tree.init = null;
105 }
106 }
107 super.visitVarDef(tree);
108 if (tree.sym != null && tree.sym.kind == VAR && !isParameter(tree.sym)) {
109 toolEnv.setElementToTreePath(tree.sym, toolEnv.getTreePath(env.toplevel, env.enclClass, tree));
110 }
111 }
|