1 /* 2 * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 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 com.sun.tools.javadoc; 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> 59 context.put(memberEnterKey, new Context.Factory<MemberEnter>() { 60 public MemberEnter make(Context c) { 61 return new JavadocMemberEnter(c); 62 } 63 }); 64 } 65 66 final DocEnv docenv; 67 68 protected JavadocMemberEnter(Context context) { 69 super(context); 70 docenv = DocEnv.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 = docenv.getTreePath(env.toplevel, env.enclClass, tree); 79 if (meth.isConstructor()) 80 docenv.makeConstructorDoc(meth, treePath); 81 else if (isAnnotationTypeElement(meth)) 82 docenv.makeAnnotationTypeElementDoc(meth, treePath); 83 else 84 docenv.makeMethodDoc(meth, treePath); 85 86 // release resources 87 tree.body = null; 88 } 89 90 @Override 91 public void visitVarDef(JCVariableDecl tree) { 92 if (tree.init != null) { 93 boolean isFinal = (tree.mods.flags & FINAL) != 0 94 || (env.enclClass.mods.flags & INTERFACE) != 0; 95 if (!isFinal || containsNonConstantExpression(tree.init)) { 96 // Avoid unnecessary analysis and release resources. 97 // In particular, remove non-constant expressions 98 // which may trigger Attr.attribClass, since 99 // method bodies are also removed, in visitMethodDef. 100 tree.init = null; 101 } 102 } 103 super.visitVarDef(tree); 104 if (tree.sym != null && 105 tree.sym.kind == VAR && 106 !isParameter(tree.sym)) { 107 docenv.makeFieldDoc(tree.sym, docenv.getTreePath(env.toplevel, env.enclClass, tree)); 108 } 109 } 110 111 private static boolean isAnnotationTypeElement(MethodSymbol meth) { 112 return ClassDocImpl.isAnnotationType(meth.enclClass()); 113 } 114 115 private static boolean isParameter(VarSymbol var) { 116 return (var.flags() & Flags.PARAMETER) != 0; 117 } 118 119 /** 120 * Simple analysis of an expression tree to see if it contains tree nodes 121 * for any non-constant expression. This does not include checking references 122 * to other fields which may or may not be constant. 123 */ 124 private static boolean containsNonConstantExpression(JCExpression tree) { 125 return new MaybeConstantExpressionScanner().containsNonConstantExpression(tree); 126 } 127 128 /** 129 * See JLS 15.18, Constant Expression 130 */ 131 private static class MaybeConstantExpressionScanner extends JCTree.Visitor { 132 boolean maybeConstantExpr = true; 133 134 public boolean containsNonConstantExpression(JCExpression tree) { | 1 /* 2 * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 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> 59 context.put(memberEnterKey, new Context.Factory<MemberEnter>() { 60 public MemberEnter make(Context c) { 61 return new JavadocMemberEnter(c); 62 } 63 }); 64 } 65 66 final DocEnv docenv; 67 68 protected JavadocMemberEnter(Context context) { 69 super(context); 70 docenv = DocEnv.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 = docenv.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 docenv.setElementToTreePath(meth, treePath); 84 } 85 // release resources 86 tree.body = null; 87 } 88 89 @Override 90 public void visitVarDef(JCVariableDecl tree) { 91 if (tree.init != null) { 92 boolean isFinal = (tree.mods.flags & FINAL) != 0 93 || (env.enclClass.mods.flags & INTERFACE) != 0; 94 if (!isFinal || containsNonConstantExpression(tree.init)) { 95 // Avoid unnecessary analysis and release resources. 96 // In particular, remove non-constant expressions 97 // which may trigger Attr.attribClass, since 98 // method bodies are also removed, in visitMethodDef. 99 tree.init = null; 100 } 101 } 102 super.visitVarDef(tree); 103 if (tree.sym != null && tree.sym.kind == VAR && !isParameter(tree.sym)) { 104 docenv.setElementToTreePath(tree.sym, docenv.getTreePath(env.toplevel, env.enclClass, tree)); 105 } 106 } 107 108 private static boolean isParameter(VarSymbol var) { 109 return (var.flags() & Flags.PARAMETER) != 0; 110 } 111 112 /** 113 * Simple analysis of an expression tree to see if it contains tree nodes 114 * for any non-constant expression. This does not include checking references 115 * to other fields which may or may not be constant. 116 */ 117 private static boolean containsNonConstantExpression(JCExpression tree) { 118 return new MaybeConstantExpressionScanner().containsNonConstantExpression(tree); 119 } 120 121 /** 122 * See JLS 15.18, Constant Expression 123 */ 124 private static class MaybeConstantExpressionScanner extends JCTree.Visitor { 125 boolean maybeConstantExpr = true; 126 127 public boolean containsNonConstantExpression(JCExpression tree) { |