Path: blob/aarch64-shenandoah-jdk8u272-b10/langtools/src/share/classes/com/sun/tools/javadoc/JavadocMemberEnter.java
38899 views
/*1* Copyright (c) 2003, 2013, 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 com.sun.tools.javadoc;2627import com.sun.source.util.TreePath;28import com.sun.tools.javac.code.Flags;29import com.sun.tools.javac.code.Kinds;30import com.sun.tools.javac.code.Symbol.*;31import com.sun.tools.javac.comp.MemberEnter;32import com.sun.tools.javac.tree.JCTree;33import com.sun.tools.javac.tree.JCTree.*;34import com.sun.tools.javac.util.Context;3536import static com.sun.tools.javac.code.Flags.*;3738/**39* Javadoc's own memberEnter phase does a few things above and beyond that40* done by javac.41*42* <p><b>This is NOT part of any supported API.43* If you write code that depends on this, you do so at your own risk.44* This code and its internal interfaces are subject to change or45* deletion without notice.</b>46*47* @author Neal Gafter48*/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, new Context.Factory<MemberEnter>() {59public MemberEnter make(Context c) {60return new JavadocMemberEnter(c);61}62});63}6465final DocEnv docenv;6667protected JavadocMemberEnter(Context context) {68super(context);69docenv = DocEnv.instance(context);70}7172@Override73public void visitMethodDef(JCMethodDecl tree) {74super.visitMethodDef(tree);75MethodSymbol meth = tree.sym;76if (meth == null || meth.kind != Kinds.MTH) return;77TreePath treePath = docenv.getTreePath(env.toplevel, env.enclClass, tree);78if (meth.isConstructor())79docenv.makeConstructorDoc(meth, treePath);80else if (isAnnotationTypeElement(meth))81docenv.makeAnnotationTypeElementDoc(meth, treePath);82else83docenv.makeMethodDoc(meth, treePath);8485// release resources86tree.body = null;87}8889@Override90public void visitVarDef(JCVariableDecl tree) {91if (tree.init != null) {92boolean isFinal = (tree.mods.flags & FINAL) != 093|| (env.enclClass.mods.flags & INTERFACE) != 0;94if (!isFinal || containsNonConstantExpression(tree.init)) {95// Avoid unnecessary analysis and release resources.96// In particular, remove non-constant expressions97// which may trigger Attr.attribClass, since98// method bodies are also removed, in visitMethodDef.99tree.init = null;100}101}102super.visitVarDef(tree);103if (tree.sym != null &&104tree.sym.kind == Kinds.VAR &&105!isParameter(tree.sym)) {106docenv.makeFieldDoc(tree.sym, docenv.getTreePath(env.toplevel, env.enclClass, tree));107}108}109110private static boolean isAnnotationTypeElement(MethodSymbol meth) {111return ClassDocImpl.isAnnotationType(meth.enclClass());112}113114private static boolean isParameter(VarSymbol var) {115return (var.flags() & Flags.PARAMETER) != 0;116}117118/**119* Simple analysis of an expression tree to see if it contains tree nodes120* for any non-constant expression. This does not include checking references121* to other fields which may or may not be constant.122*/123private static boolean containsNonConstantExpression(JCExpression tree) {124return new MaybeConstantExpressionScanner().containsNonConstantExpression(tree);125}126127/**128* See JLS 15.18, Constant Expression129*/130private static class MaybeConstantExpressionScanner extends JCTree.Visitor {131boolean maybeConstantExpr = true;132133public boolean containsNonConstantExpression(JCExpression tree) {134scan(tree);135return !maybeConstantExpr;136}137138public void scan(JCTree tree) {139// short circuit scan when end result is definitely false140if (maybeConstantExpr && tree != null)141tree.accept(this);142}143144@Override145/** default for any non-overridden visit method. */146public void visitTree(JCTree tree) {147maybeConstantExpr = false;148}149150@Override151public void visitBinary(JCBinary tree) {152switch (tree.getTag()) {153case MUL: case DIV: case MOD:154case PLUS: case MINUS:155case SL: case SR: case USR:156case LT: case LE: case GT: case GE:157case EQ: case NE:158case BITAND: case BITXOR: case BITOR:159case AND: case OR:160break;161default:162maybeConstantExpr = false;163}164}165166@Override167public void visitConditional(JCConditional tree) {168scan(tree.cond);169scan(tree.truepart);170scan(tree.falsepart);171}172173@Override174public void visitIdent(JCIdent tree) { }175176@Override177public void visitLiteral(JCLiteral tree) { }178179@Override180public void visitParens(JCParens tree) {181scan(tree.expr);182}183184@Override185public void visitSelect(JCTree.JCFieldAccess tree) {186scan(tree.selected);187}188189@Override190public void visitTypeCast(JCTypeCast tree) {191scan(tree.clazz);192scan(tree.expr);193}194195@Override196public void visitTypeIdent(JCPrimitiveTypeTree tree) { }197198@Override199public void visitUnary(JCUnary tree) {200switch (tree.getTag()) {201case POS: case NEG: case COMPL: case NOT:202break;203default:204maybeConstantExpr = false;205}206}207}208}209210211