8010310: [javadoc] Error processing sources with -private
Reviewed-by: vromero, mcimadamore
--- a/langtools/src/share/classes/com/sun/tools/javac/code/Symbol.java Wed Aug 28 10:17:23 2013 -0700
+++ b/langtools/src/share/classes/com/sun/tools/javac/code/Symbol.java Wed Aug 28 15:40:33 2013 -0700
@@ -1179,7 +1179,7 @@
/**
* The variable's constant value, if this is a constant.
* Before the constant value is evaluated, it points to an
- * initalizer environment. If this is not a constant, it can
+ * initializer environment. If this is not a constant, it can
* be used for other stuff.
*/
private Object data;
--- a/langtools/src/share/classes/com/sun/tools/javadoc/JavadocMemberEnter.java Wed Aug 28 10:17:23 2013 -0700
+++ b/langtools/src/share/classes/com/sun/tools/javadoc/JavadocMemberEnter.java Wed Aug 28 15:40:33 2013 -0700
@@ -30,9 +30,12 @@
import com.sun.tools.javac.code.Kinds;
import com.sun.tools.javac.code.Symbol.*;
import com.sun.tools.javac.comp.MemberEnter;
+import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.JCTree.*;
import com.sun.tools.javac.util.Context;
+import static com.sun.tools.javac.code.Flags.*;
+
/**
* Javadoc's own memberEnter phase does a few things above and beyond that
* done by javac.
@@ -86,6 +89,17 @@
@Override
public void visitVarDef(JCVariableDecl tree) {
+ if (tree.init != null) {
+ boolean isFinal = (tree.mods.flags & FINAL) != 0
+ || (env.enclClass.mods.flags & INTERFACE) != 0;
+ if (!isFinal || containsNonConstantExpression(tree.init)) {
+ // Avoid unnecessary analysis and release resources.
+ // In particular, remove non-constant expressions
+ // which may trigger Attr.attribClass, since
+ // method bodies are also removed, in visitMethodDef.
+ tree.init = null;
+ }
+ }
super.visitVarDef(tree);
if (tree.sym != null &&
tree.sym.kind == Kinds.VAR &&
@@ -101,4 +115,95 @@
private static boolean isParameter(VarSymbol var) {
return (var.flags() & Flags.PARAMETER) != 0;
}
+
+ /**
+ * Simple analysis of an expression tree to see if it contains tree nodes
+ * for any non-constant expression. This does not include checking references
+ * to other fields which may or may not be constant.
+ */
+ private static boolean containsNonConstantExpression(JCExpression tree) {
+ return new MaybeConstantExpressionScanner().containsNonConstantExpression(tree);
+ }
+
+ /**
+ * See JLS 15.18, Constant Expression
+ */
+ private static class MaybeConstantExpressionScanner extends JCTree.Visitor {
+ boolean maybeConstantExpr = true;
+
+ public boolean containsNonConstantExpression(JCExpression tree) {
+ scan(tree);
+ return !maybeConstantExpr;
+ }
+
+ public void scan(JCTree tree) {
+ // short circuit scan when end result is definitely false
+ if (maybeConstantExpr && tree != null)
+ tree.accept(this);
+ }
+
+ @Override
+ /** default for any non-overridden visit method. */
+ public void visitTree(JCTree tree) {
+ maybeConstantExpr = false;
+ }
+
+ @Override
+ public void visitBinary(JCBinary tree) {
+ switch (tree.getTag()) {
+ case MUL: case DIV: case MOD:
+ case PLUS: case MINUS:
+ case SL: case SR: case USR:
+ case LT: case LE: case GT: case GE:
+ case EQ: case NE:
+ case BITAND: case BITXOR: case BITOR:
+ case AND: case OR:
+ break;
+ default:
+ maybeConstantExpr = false;
+ }
+ }
+
+ @Override
+ public void visitConditional(JCConditional tree) {
+ scan(tree.cond);
+ scan(tree.truepart);
+ scan(tree.falsepart);
+ }
+
+ @Override
+ public void visitIdent(JCIdent tree) { }
+
+ @Override
+ public void visitLiteral(JCLiteral tree) { }
+
+ @Override
+ public void visitParens(JCParens tree) {
+ scan(tree.expr);
+ }
+
+ @Override
+ public void visitSelect(JCTree.JCFieldAccess tree) {
+ scan(tree.selected);
+ }
+
+ @Override
+ public void visitTypeCast(JCTypeCast tree) {
+ scan(tree.clazz);
+ scan(tree.expr);
+ }
+
+ @Override
+ public void visitTypeIdent(JCPrimitiveTypeTree tree) { }
+
+ @Override
+ public void visitUnary(JCUnary tree) {
+ switch (tree.getTag()) {
+ case POS: case NEG: case COMPL: case NOT:
+ break;
+ default:
+ maybeConstantExpr = false;
+ }
+ }
+ }
}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javadoc/nonConstExprs/Test.java Wed Aug 28 15:40:33 2013 -0700
@@ -0,0 +1,62 @@
+/*
+ * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/**
+ * @test
+ * @bug 8010310
+ * @summary Error processing sources with -private
+ */
+
+import java.io.File;
+
+public class Test {
+ public static void main(String... args) throws Exception {
+ File testSrc = new File(System.getProperty("test.src"));
+ String[] jdoc_args = {
+ "-d", "out",
+ new File(testSrc, Test.class.getSimpleName() + ".java").getPath()
+ };
+ int rc = com.sun.tools.javadoc.Main.execute(jdoc_args);
+ if (rc != 0)
+ throw new Exception("unexpected return code from javadoc: " + rc);
+ }
+
+ static int array[] = { 1, 2, 3};
+ static int method(int p) { return p; }
+ static int value = 0;
+
+ public int not_static_not_final = 1;
+ public static int static_not_final = 2;
+ public final int not_static_final = 3;
+ public static final int static_final = 4;
+
+ public static final int array_index = array[0];
+ public static final int method_call = method(0);
+ public static final int inner_class = new Test() { }.method(0);
+ public static final int new_class = new Test().method(0);
+ public static final int pre_inc = ++value;
+ public static final int pre_dec = --value;
+ public static final int post_inc = value++;
+ public static final int post_dec = value--;
+}
+