src/share/classes/com/sun/tools/javadoc/JavadocMemberEnter.java

Tue, 24 Dec 2013 09:17:37 -0800

author
ksrini
date
Tue, 24 Dec 2013 09:17:37 -0800
changeset 2227
998b10c43157
parent 1984
189942cdf585
child 2525
2eb010b6cb22
permissions
-rw-r--r--

8029230: Update copyright year to match last edit in jdk8 langtools repository for 2013
Reviewed-by: ksrini
Contributed-by: steve.sides@oracle.com

duke@1 1 /*
jjg@1490 2 * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
duke@1 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1 4 *
duke@1 5 * This code is free software; you can redistribute it and/or modify it
duke@1 6 * under the terms of the GNU General Public License version 2 only, as
ohair@554 7 * published by the Free Software Foundation. Oracle designates this
duke@1 8 * particular file as subject to the "Classpath" exception as provided
ohair@554 9 * by Oracle in the LICENSE file that accompanied this code.
duke@1 10 *
duke@1 11 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1 14 * version 2 for more details (a copy is included in the LICENSE file that
duke@1 15 * accompanied this code).
duke@1 16 *
duke@1 17 * You should have received a copy of the GNU General Public License version
duke@1 18 * 2 along with this work; if not, write to the Free Software Foundation,
duke@1 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1 20 *
ohair@554 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@554 22 * or visit www.oracle.com if you need additional information or have any
ohair@554 23 * questions.
duke@1 24 */
duke@1 25
duke@1 26 package com.sun.tools.javadoc;
duke@1 27
jjg@1443 28 import com.sun.source.util.TreePath;
duke@1 29 import com.sun.tools.javac.code.Flags;
duke@1 30 import com.sun.tools.javac.code.Kinds;
duke@1 31 import com.sun.tools.javac.code.Symbol.*;
duke@1 32 import com.sun.tools.javac.comp.MemberEnter;
jjg@1984 33 import com.sun.tools.javac.tree.JCTree;
duke@1 34 import com.sun.tools.javac.tree.JCTree.*;
jjg@1280 35 import com.sun.tools.javac.util.Context;
duke@1 36
jjg@1984 37 import static com.sun.tools.javac.code.Flags.*;
jjg@1984 38
duke@1 39 /**
duke@1 40 * Javadoc's own memberEnter phase does a few things above and beyond that
duke@1 41 * done by javac.
jjg@1359 42 *
jjg@1359 43 * <p><b>This is NOT part of any supported API.
jjg@1359 44 * If you write code that depends on this, you do so at your own risk.
jjg@1359 45 * This code and its internal interfaces are subject to change or
jjg@1359 46 * deletion without notice.</b>
jjg@1359 47 *
duke@1 48 * @author Neal Gafter
duke@1 49 */
ksrini@1051 50 public class JavadocMemberEnter extends MemberEnter {
duke@1 51 public static JavadocMemberEnter instance0(Context context) {
duke@1 52 MemberEnter instance = context.get(memberEnterKey);
duke@1 53 if (instance == null)
duke@1 54 instance = new JavadocMemberEnter(context);
duke@1 55 return (JavadocMemberEnter)instance;
duke@1 56 }
duke@1 57
jjg@893 58 public static void preRegister(Context context) {
duke@1 59 context.put(memberEnterKey, new Context.Factory<MemberEnter>() {
jjg@893 60 public MemberEnter make(Context c) {
jjg@893 61 return new JavadocMemberEnter(c);
duke@1 62 }
duke@1 63 });
duke@1 64 }
duke@1 65
duke@1 66 final DocEnv docenv;
duke@1 67
duke@1 68 protected JavadocMemberEnter(Context context) {
duke@1 69 super(context);
duke@1 70 docenv = DocEnv.instance(context);
duke@1 71 }
duke@1 72
jjg@1280 73 @Override
duke@1 74 public void visitMethodDef(JCMethodDecl tree) {
duke@1 75 super.visitMethodDef(tree);
jjg@74 76 MethodSymbol meth = tree.sym;
duke@1 77 if (meth == null || meth.kind != Kinds.MTH) return;
jjg@1490 78 TreePath treePath = docenv.getTreePath(env.toplevel, env.enclClass, tree);
duke@1 79 if (meth.isConstructor())
jjg@1443 80 docenv.makeConstructorDoc(meth, treePath);
duke@1 81 else if (isAnnotationTypeElement(meth))
jjg@1443 82 docenv.makeAnnotationTypeElementDoc(meth, treePath);
duke@1 83 else
jjg@1443 84 docenv.makeMethodDoc(meth, treePath);
jjg@1391 85
jjg@1391 86 // release resources
jjg@1391 87 tree.body = null;
duke@1 88 }
duke@1 89
jjg@1280 90 @Override
duke@1 91 public void visitVarDef(JCVariableDecl tree) {
jjg@1984 92 if (tree.init != null) {
jjg@1984 93 boolean isFinal = (tree.mods.flags & FINAL) != 0
jjg@1984 94 || (env.enclClass.mods.flags & INTERFACE) != 0;
jjg@1984 95 if (!isFinal || containsNonConstantExpression(tree.init)) {
jjg@1984 96 // Avoid unnecessary analysis and release resources.
jjg@1984 97 // In particular, remove non-constant expressions
jjg@1984 98 // which may trigger Attr.attribClass, since
jjg@1984 99 // method bodies are also removed, in visitMethodDef.
jjg@1984 100 tree.init = null;
jjg@1984 101 }
jjg@1984 102 }
duke@1 103 super.visitVarDef(tree);
duke@1 104 if (tree.sym != null &&
duke@1 105 tree.sym.kind == Kinds.VAR &&
duke@1 106 !isParameter(tree.sym)) {
jjg@1490 107 docenv.makeFieldDoc(tree.sym, docenv.getTreePath(env.toplevel, env.enclClass, tree));
duke@1 108 }
duke@1 109 }
duke@1 110
duke@1 111 private static boolean isAnnotationTypeElement(MethodSymbol meth) {
duke@1 112 return ClassDocImpl.isAnnotationType(meth.enclClass());
duke@1 113 }
duke@1 114
duke@1 115 private static boolean isParameter(VarSymbol var) {
duke@1 116 return (var.flags() & Flags.PARAMETER) != 0;
duke@1 117 }
jjg@1984 118
jjg@1984 119 /**
jjg@1984 120 * Simple analysis of an expression tree to see if it contains tree nodes
jjg@1984 121 * for any non-constant expression. This does not include checking references
jjg@1984 122 * to other fields which may or may not be constant.
jjg@1984 123 */
jjg@1984 124 private static boolean containsNonConstantExpression(JCExpression tree) {
jjg@1984 125 return new MaybeConstantExpressionScanner().containsNonConstantExpression(tree);
jjg@1984 126 }
jjg@1984 127
jjg@1984 128 /**
jjg@1984 129 * See JLS 15.18, Constant Expression
jjg@1984 130 */
jjg@1984 131 private static class MaybeConstantExpressionScanner extends JCTree.Visitor {
jjg@1984 132 boolean maybeConstantExpr = true;
jjg@1984 133
jjg@1984 134 public boolean containsNonConstantExpression(JCExpression tree) {
jjg@1984 135 scan(tree);
jjg@1984 136 return !maybeConstantExpr;
jjg@1984 137 }
jjg@1984 138
jjg@1984 139 public void scan(JCTree tree) {
jjg@1984 140 // short circuit scan when end result is definitely false
jjg@1984 141 if (maybeConstantExpr && tree != null)
jjg@1984 142 tree.accept(this);
jjg@1984 143 }
jjg@1984 144
jjg@1984 145 @Override
jjg@1984 146 /** default for any non-overridden visit method. */
jjg@1984 147 public void visitTree(JCTree tree) {
jjg@1984 148 maybeConstantExpr = false;
jjg@1984 149 }
jjg@1984 150
jjg@1984 151 @Override
jjg@1984 152 public void visitBinary(JCBinary tree) {
jjg@1984 153 switch (tree.getTag()) {
jjg@1984 154 case MUL: case DIV: case MOD:
jjg@1984 155 case PLUS: case MINUS:
jjg@1984 156 case SL: case SR: case USR:
jjg@1984 157 case LT: case LE: case GT: case GE:
jjg@1984 158 case EQ: case NE:
jjg@1984 159 case BITAND: case BITXOR: case BITOR:
jjg@1984 160 case AND: case OR:
jjg@1984 161 break;
jjg@1984 162 default:
jjg@1984 163 maybeConstantExpr = false;
jjg@1984 164 }
jjg@1984 165 }
jjg@1984 166
jjg@1984 167 @Override
jjg@1984 168 public void visitConditional(JCConditional tree) {
jjg@1984 169 scan(tree.cond);
jjg@1984 170 scan(tree.truepart);
jjg@1984 171 scan(tree.falsepart);
jjg@1984 172 }
jjg@1984 173
jjg@1984 174 @Override
jjg@1984 175 public void visitIdent(JCIdent tree) { }
jjg@1984 176
jjg@1984 177 @Override
jjg@1984 178 public void visitLiteral(JCLiteral tree) { }
jjg@1984 179
jjg@1984 180 @Override
jjg@1984 181 public void visitParens(JCParens tree) {
jjg@1984 182 scan(tree.expr);
jjg@1984 183 }
jjg@1984 184
jjg@1984 185 @Override
jjg@1984 186 public void visitSelect(JCTree.JCFieldAccess tree) {
jjg@1984 187 scan(tree.selected);
jjg@1984 188 }
jjg@1984 189
jjg@1984 190 @Override
jjg@1984 191 public void visitTypeCast(JCTypeCast tree) {
jjg@1984 192 scan(tree.clazz);
jjg@1984 193 scan(tree.expr);
jjg@1984 194 }
jjg@1984 195
jjg@1984 196 @Override
jjg@1984 197 public void visitTypeIdent(JCPrimitiveTypeTree tree) { }
jjg@1984 198
jjg@1984 199 @Override
jjg@1984 200 public void visitUnary(JCUnary tree) {
jjg@1984 201 switch (tree.getTag()) {
jjg@1984 202 case POS: case NEG: case COMPL: case NOT:
jjg@1984 203 break;
jjg@1984 204 default:
jjg@1984 205 maybeConstantExpr = false;
jjg@1984 206 }
jjg@1984 207 }
jjg@1984 208 }
duke@1 209 }

mercurial