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