aoqi@0: /*
aoqi@0: * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0: *
aoqi@0: * This code is free software; you can redistribute it and/or modify it
aoqi@0: * under the terms of the GNU General Public License version 2 only, as
aoqi@0: * published by the Free Software Foundation. Oracle designates this
aoqi@0: * particular file as subject to the "Classpath" exception as provided
aoqi@0: * by Oracle in the LICENSE file that accompanied this code.
aoqi@0: *
aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0: * accompanied this code).
aoqi@0: *
aoqi@0: * You should have received a copy of the GNU General Public License version
aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0: *
aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0: * or visit www.oracle.com if you need additional information or have any
aoqi@0: * questions.
aoqi@0: */
aoqi@0:
aoqi@0: package com.sun.tools.javadoc;
aoqi@0:
aoqi@0: import com.sun.source.util.TreePath;
aoqi@0: import com.sun.tools.javac.code.Flags;
aoqi@0: import com.sun.tools.javac.code.Kinds;
aoqi@0: import com.sun.tools.javac.code.Symbol.*;
aoqi@0: import com.sun.tools.javac.comp.MemberEnter;
aoqi@0: import com.sun.tools.javac.tree.JCTree;
aoqi@0: import com.sun.tools.javac.tree.JCTree.*;
aoqi@0: import com.sun.tools.javac.util.Context;
aoqi@0:
aoqi@0: import static com.sun.tools.javac.code.Flags.*;
aoqi@0:
aoqi@0: /**
aoqi@0: * Javadoc's own memberEnter phase does a few things above and beyond that
aoqi@0: * done by javac.
aoqi@0: *
aoqi@0: *
This is NOT part of any supported API.
aoqi@0: * If you write code that depends on this, you do so at your own risk.
aoqi@0: * This code and its internal interfaces are subject to change or
aoqi@0: * deletion without notice.
aoqi@0: *
aoqi@0: * @author Neal Gafter
aoqi@0: */
aoqi@0: public class JavadocMemberEnter extends MemberEnter {
aoqi@0: public static JavadocMemberEnter instance0(Context context) {
aoqi@0: MemberEnter instance = context.get(memberEnterKey);
aoqi@0: if (instance == null)
aoqi@0: instance = new JavadocMemberEnter(context);
aoqi@0: return (JavadocMemberEnter)instance;
aoqi@0: }
aoqi@0:
aoqi@0: public static void preRegister(Context context) {
aoqi@0: context.put(memberEnterKey, new Context.Factory() {
aoqi@0: public MemberEnter make(Context c) {
aoqi@0: return new JavadocMemberEnter(c);
aoqi@0: }
aoqi@0: });
aoqi@0: }
aoqi@0:
aoqi@0: final DocEnv docenv;
aoqi@0:
aoqi@0: protected JavadocMemberEnter(Context context) {
aoqi@0: super(context);
aoqi@0: docenv = DocEnv.instance(context);
aoqi@0: }
aoqi@0:
aoqi@0: @Override
aoqi@0: public void visitMethodDef(JCMethodDecl tree) {
aoqi@0: super.visitMethodDef(tree);
aoqi@0: MethodSymbol meth = tree.sym;
aoqi@0: if (meth == null || meth.kind != Kinds.MTH) return;
aoqi@0: TreePath treePath = docenv.getTreePath(env.toplevel, env.enclClass, tree);
aoqi@0: if (meth.isConstructor())
aoqi@0: docenv.makeConstructorDoc(meth, treePath);
aoqi@0: else if (isAnnotationTypeElement(meth))
aoqi@0: docenv.makeAnnotationTypeElementDoc(meth, treePath);
aoqi@0: else
aoqi@0: docenv.makeMethodDoc(meth, treePath);
aoqi@0:
aoqi@0: // release resources
aoqi@0: tree.body = null;
aoqi@0: }
aoqi@0:
aoqi@0: @Override
aoqi@0: public void visitVarDef(JCVariableDecl tree) {
aoqi@0: if (tree.init != null) {
aoqi@0: boolean isFinal = (tree.mods.flags & FINAL) != 0
aoqi@0: || (env.enclClass.mods.flags & INTERFACE) != 0;
aoqi@0: if (!isFinal || containsNonConstantExpression(tree.init)) {
aoqi@0: // Avoid unnecessary analysis and release resources.
aoqi@0: // In particular, remove non-constant expressions
aoqi@0: // which may trigger Attr.attribClass, since
aoqi@0: // method bodies are also removed, in visitMethodDef.
aoqi@0: tree.init = null;
aoqi@0: }
aoqi@0: }
aoqi@0: super.visitVarDef(tree);
aoqi@0: if (tree.sym != null &&
aoqi@0: tree.sym.kind == Kinds.VAR &&
aoqi@0: !isParameter(tree.sym)) {
aoqi@0: docenv.makeFieldDoc(tree.sym, docenv.getTreePath(env.toplevel, env.enclClass, tree));
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: private static boolean isAnnotationTypeElement(MethodSymbol meth) {
aoqi@0: return ClassDocImpl.isAnnotationType(meth.enclClass());
aoqi@0: }
aoqi@0:
aoqi@0: private static boolean isParameter(VarSymbol var) {
aoqi@0: return (var.flags() & Flags.PARAMETER) != 0;
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Simple analysis of an expression tree to see if it contains tree nodes
aoqi@0: * for any non-constant expression. This does not include checking references
aoqi@0: * to other fields which may or may not be constant.
aoqi@0: */
aoqi@0: private static boolean containsNonConstantExpression(JCExpression tree) {
aoqi@0: return new MaybeConstantExpressionScanner().containsNonConstantExpression(tree);
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * See JLS 15.18, Constant Expression
aoqi@0: */
aoqi@0: private static class MaybeConstantExpressionScanner extends JCTree.Visitor {
aoqi@0: boolean maybeConstantExpr = true;
aoqi@0:
aoqi@0: public boolean containsNonConstantExpression(JCExpression tree) {
aoqi@0: scan(tree);
aoqi@0: return !maybeConstantExpr;
aoqi@0: }
aoqi@0:
aoqi@0: public void scan(JCTree tree) {
aoqi@0: // short circuit scan when end result is definitely false
aoqi@0: if (maybeConstantExpr && tree != null)
aoqi@0: tree.accept(this);
aoqi@0: }
aoqi@0:
aoqi@0: @Override
aoqi@0: /** default for any non-overridden visit method. */
aoqi@0: public void visitTree(JCTree tree) {
aoqi@0: maybeConstantExpr = false;
aoqi@0: }
aoqi@0:
aoqi@0: @Override
aoqi@0: public void visitBinary(JCBinary tree) {
aoqi@0: switch (tree.getTag()) {
aoqi@0: case MUL: case DIV: case MOD:
aoqi@0: case PLUS: case MINUS:
aoqi@0: case SL: case SR: case USR:
aoqi@0: case LT: case LE: case GT: case GE:
aoqi@0: case EQ: case NE:
aoqi@0: case BITAND: case BITXOR: case BITOR:
aoqi@0: case AND: case OR:
aoqi@0: break;
aoqi@0: default:
aoqi@0: maybeConstantExpr = false;
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: @Override
aoqi@0: public void visitConditional(JCConditional tree) {
aoqi@0: scan(tree.cond);
aoqi@0: scan(tree.truepart);
aoqi@0: scan(tree.falsepart);
aoqi@0: }
aoqi@0:
aoqi@0: @Override
aoqi@0: public void visitIdent(JCIdent tree) { }
aoqi@0:
aoqi@0: @Override
aoqi@0: public void visitLiteral(JCLiteral tree) { }
aoqi@0:
aoqi@0: @Override
aoqi@0: public void visitParens(JCParens tree) {
aoqi@0: scan(tree.expr);
aoqi@0: }
aoqi@0:
aoqi@0: @Override
aoqi@0: public void visitSelect(JCTree.JCFieldAccess tree) {
aoqi@0: scan(tree.selected);
aoqi@0: }
aoqi@0:
aoqi@0: @Override
aoqi@0: public void visitTypeCast(JCTypeCast tree) {
aoqi@0: scan(tree.clazz);
aoqi@0: scan(tree.expr);
aoqi@0: }
aoqi@0:
aoqi@0: @Override
aoqi@0: public void visitTypeIdent(JCPrimitiveTypeTree tree) { }
aoqi@0:
aoqi@0: @Override
aoqi@0: public void visitUnary(JCUnary tree) {
aoqi@0: switch (tree.getTag()) {
aoqi@0: case POS: case NEG: case COMPL: case NOT:
aoqi@0: break;
aoqi@0: default:
aoqi@0: maybeConstantExpr = false;
aoqi@0: }
aoqi@0: }
aoqi@0: }
aoqi@0: }