Path: blob/master/src/jdk.javadoc/share/classes/jdk/javadoc/internal/tool/JavadocMemberEnter.java
41161 views
/*1* Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package jdk.javadoc.internal.tool;2627import com.sun.source.util.TreePath;28import com.sun.tools.javac.code.Flags;29import com.sun.tools.javac.code.Symbol.*;30import com.sun.tools.javac.comp.MemberEnter;31import com.sun.tools.javac.tree.JCTree;32import com.sun.tools.javac.tree.JCTree.*;33import com.sun.tools.javac.tree.TreeInfo;34import com.sun.tools.javac.util.Context;35import com.sun.tools.javac.util.List;3637import static com.sun.tools.javac.code.Flags.*;38import static com.sun.tools.javac.code.Kinds.Kind.*;3940/**41* Javadoc's own memberEnter phase does a few things above and beyond that42* done by javac.43*44* <p><b>This is NOT part of any supported API.45* If you write code that depends on this, you do so at your own risk.46* This code and its internal interfaces are subject to change or47* deletion without notice.</b>48*/49public class JavadocMemberEnter extends MemberEnter {50public static JavadocMemberEnter instance0(Context context) {51MemberEnter instance = context.get(memberEnterKey);52if (instance == null)53instance = new JavadocMemberEnter(context);54return (JavadocMemberEnter)instance;55}5657public static void preRegister(Context context) {58context.put(memberEnterKey, (Context.Factory<MemberEnter>)JavadocMemberEnter::new);59}6061final ToolEnvironment toolEnv;626364protected JavadocMemberEnter(Context context) {65super(context);66toolEnv = ToolEnvironment.instance(context);67}6869@Override70public void visitMethodDef(JCMethodDecl tree) {71super.visitMethodDef(tree);72MethodSymbol meth = tree.sym;73if (meth == null || meth.kind != MTH) return;74TreePath 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 in77// tree best to let other logic determine the TreePath.78if (env.enclClass.defs.contains(tree)) {79toolEnv.setElementToTreePath(meth, treePath);80}81// release resources82// handle constructors for record types specially, because of downstream checks83if ((env.enclClass.mods.flags & Flags.RECORD) != 0 && TreeInfo.isConstructor(tree)) {84tree.body.stats = List.nil();85} else {86tree.body = null;87}88}8990@Override91public void visitVarDef(JCVariableDecl tree) {92if (tree.init != null) {93boolean isFinal = (tree.mods.flags & FINAL) != 094|| (env.enclClass.mods.flags & INTERFACE) != 0;95if (!isFinal || containsNonConstantExpression(tree.init)) {96// Avoid unnecessary analysis and release resources.97// In particular, remove non-constant expressions98// which may trigger Attr.attribClass, since99// method bodies are also removed, in visitMethodDef.100tree.init = null;101}102}103super.visitVarDef(tree);104if (tree.sym != null && tree.sym.kind == VAR && !isParameter(tree.sym)) {105toolEnv.setElementToTreePath(tree.sym, toolEnv.getTreePath(env.toplevel, env.enclClass, tree));106}107}108109private static boolean isParameter(VarSymbol var) {110return (var.flags() & Flags.PARAMETER) != 0;111}112113/**114* Simple analysis of an expression tree to see if it contains tree nodes115* for any non-constant expression. This does not include checking references116* to other fields which may or may not be constant.117*/118private static boolean containsNonConstantExpression(JCExpression tree) {119return new MaybeConstantExpressionScanner().containsNonConstantExpression(tree);120}121122/**123* See JLS 15.18, Constant Expression124*/125private static class MaybeConstantExpressionScanner extends JCTree.Visitor {126boolean maybeConstantExpr = true;127128public boolean containsNonConstantExpression(JCExpression tree) {129scan(tree);130return !maybeConstantExpr;131}132133public void scan(JCTree tree) {134// short circuit scan when end result is definitely false135if (maybeConstantExpr && tree != null)136tree.accept(this);137}138139@Override140/** default for any non-overridden visit method. */141public void visitTree(JCTree tree) {142maybeConstantExpr = false;143}144145@Override146public void visitBinary(JCBinary tree) {147switch (tree.getTag()) {148case MUL: case DIV: case MOD:149case PLUS: case MINUS:150case SL: case SR: case USR:151case LT: case LE: case GT: case GE:152case EQ: case NE:153case BITAND: case BITXOR: case BITOR:154case AND: case OR:155break;156default:157maybeConstantExpr = false;158}159}160161@Override162public void visitConditional(JCConditional tree) {163scan(tree.cond);164scan(tree.truepart);165scan(tree.falsepart);166}167168@Override169public void visitIdent(JCIdent tree) { }170171@Override172public void visitLiteral(JCLiteral tree) { }173174@Override175public void visitParens(JCParens tree) {176scan(tree.expr);177}178179@Override180public void visitSelect(JCTree.JCFieldAccess tree) {181scan(tree.selected);182}183184@Override185public void visitTypeCast(JCTypeCast tree) {186scan(tree.clazz);187scan(tree.expr);188}189190@Override191public void visitTypeIdent(JCPrimitiveTypeTree tree) { }192193@Override194public void visitUnary(JCUnary tree) {195switch (tree.getTag()) {196case POS: case NEG: case COMPL: case NOT:197break;198default:199maybeConstantExpr = false;200}201}202}203}204205206