1 /* 2 * Copyright (c) 2003, 2016, 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.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 } 112 113 private static boolean isParameter(VarSymbol var) { 114 return (var.flags() & Flags.PARAMETER) != 0; 115 } 116 117 /** 118 * Simple analysis of an expression tree to see if it contains tree nodes 119 * for any non-constant expression. This does not include checking references 120 * to other fields which may or may not be constant. 121 */ 122 private static boolean containsNonConstantExpression(JCExpression tree) { 123 return new MaybeConstantExpressionScanner().containsNonConstantExpression(tree); 124 } 125 126 /** 127 * See JLS 15.18, Constant Expression 128 */ 129 private static class MaybeConstantExpressionScanner extends JCTree.Visitor { 130 boolean maybeConstantExpr = true; 131 132 public boolean containsNonConstantExpression(JCExpression tree) { 133 scan(tree); 134 return !maybeConstantExpr; 135 } 136 137 public void scan(JCTree tree) { 138 // short circuit scan when end result is definitely false 139 if (maybeConstantExpr && tree != null) 140 tree.accept(this); 141 } 142 143 @Override 144 /** default for any non-overridden visit method. */ 145 public void visitTree(JCTree tree) { 146 maybeConstantExpr = false; 147 } 148 149 @Override 150 public void visitBinary(JCBinary tree) { 151 switch (tree.getTag()) { 152 case MUL: case DIV: case MOD: 153 case PLUS: case MINUS: 154 case SL: case SR: case USR: 155 case LT: case LE: case GT: case GE: 156 case EQ: case NE: 157 case BITAND: case BITXOR: case BITOR: 158 case AND: case OR: 159 break; 160 default: 161 maybeConstantExpr = false; 162 } 163 } 164 165 @Override 166 public void visitConditional(JCConditional tree) { 167 scan(tree.cond); 168 scan(tree.truepart); 169 scan(tree.falsepart); 170 } 171 172 @Override 173 public void visitIdent(JCIdent tree) { } 174 175 @Override 176 public void visitLiteral(JCLiteral tree) { } 177 178 @Override 179 public void visitParens(JCParens tree) { 180 scan(tree.expr); 181 } 182 183 @Override 184 public void visitSelect(JCTree.JCFieldAccess tree) { 185 scan(tree.selected); 186 } 187 188 @Override 189 public void visitTypeCast(JCTypeCast tree) { 190 scan(tree.clazz); 191 scan(tree.expr); 192 } 193 194 @Override 195 public void visitTypeIdent(JCPrimitiveTypeTree tree) { } 196 197 @Override 198 public void visitUnary(JCUnary tree) { 199 switch (tree.getTag()) { 200 case POS: case NEG: case COMPL: case NOT: 201 break; 202 default: 203 maybeConstantExpr = false; 204 } 205 } 206 } 207 }