28 import com.sun.source.util.TreePath; |
28 import com.sun.source.util.TreePath; |
29 import com.sun.tools.javac.code.Flags; |
29 import com.sun.tools.javac.code.Flags; |
30 import com.sun.tools.javac.code.Kinds; |
30 import com.sun.tools.javac.code.Kinds; |
31 import com.sun.tools.javac.code.Symbol.*; |
31 import com.sun.tools.javac.code.Symbol.*; |
32 import com.sun.tools.javac.comp.MemberEnter; |
32 import com.sun.tools.javac.comp.MemberEnter; |
|
33 import com.sun.tools.javac.tree.JCTree; |
33 import com.sun.tools.javac.tree.JCTree.*; |
34 import com.sun.tools.javac.tree.JCTree.*; |
34 import com.sun.tools.javac.util.Context; |
35 import com.sun.tools.javac.util.Context; |
|
36 |
|
37 import static com.sun.tools.javac.code.Flags.*; |
35 |
38 |
36 /** |
39 /** |
37 * Javadoc's own memberEnter phase does a few things above and beyond that |
40 * Javadoc's own memberEnter phase does a few things above and beyond that |
38 * done by javac. |
41 * done by javac. |
39 * |
42 * |
84 tree.body = null; |
87 tree.body = null; |
85 } |
88 } |
86 |
89 |
87 @Override |
90 @Override |
88 public void visitVarDef(JCVariableDecl tree) { |
91 public void visitVarDef(JCVariableDecl tree) { |
|
92 if (tree.init != null) { |
|
93 boolean isFinal = (tree.mods.flags & FINAL) != 0 |
|
94 || (env.enclClass.mods.flags & INTERFACE) != 0; |
|
95 if (!isFinal || containsNonConstantExpression(tree.init)) { |
|
96 // Avoid unnecessary analysis and release resources. |
|
97 // In particular, remove non-constant expressions |
|
98 // which may trigger Attr.attribClass, since |
|
99 // method bodies are also removed, in visitMethodDef. |
|
100 tree.init = null; |
|
101 } |
|
102 } |
89 super.visitVarDef(tree); |
103 super.visitVarDef(tree); |
90 if (tree.sym != null && |
104 if (tree.sym != null && |
91 tree.sym.kind == Kinds.VAR && |
105 tree.sym.kind == Kinds.VAR && |
92 !isParameter(tree.sym)) { |
106 !isParameter(tree.sym)) { |
93 docenv.makeFieldDoc(tree.sym, docenv.getTreePath(env.toplevel, env.enclClass, tree)); |
107 docenv.makeFieldDoc(tree.sym, docenv.getTreePath(env.toplevel, env.enclClass, tree)); |
99 } |
113 } |
100 |
114 |
101 private static boolean isParameter(VarSymbol var) { |
115 private static boolean isParameter(VarSymbol var) { |
102 return (var.flags() & Flags.PARAMETER) != 0; |
116 return (var.flags() & Flags.PARAMETER) != 0; |
103 } |
117 } |
|
118 |
|
119 /** |
|
120 * Simple analysis of an expression tree to see if it contains tree nodes |
|
121 * for any non-constant expression. This does not include checking references |
|
122 * to other fields which may or may not be constant. |
|
123 */ |
|
124 private static boolean containsNonConstantExpression(JCExpression tree) { |
|
125 return new MaybeConstantExpressionScanner().containsNonConstantExpression(tree); |
|
126 } |
|
127 |
|
128 /** |
|
129 * See JLS 15.18, Constant Expression |
|
130 */ |
|
131 private static class MaybeConstantExpressionScanner extends JCTree.Visitor { |
|
132 boolean maybeConstantExpr = true; |
|
133 |
|
134 public boolean containsNonConstantExpression(JCExpression tree) { |
|
135 scan(tree); |
|
136 return !maybeConstantExpr; |
|
137 } |
|
138 |
|
139 public void scan(JCTree tree) { |
|
140 // short circuit scan when end result is definitely false |
|
141 if (maybeConstantExpr && tree != null) |
|
142 tree.accept(this); |
|
143 } |
|
144 |
|
145 @Override |
|
146 /** default for any non-overridden visit method. */ |
|
147 public void visitTree(JCTree tree) { |
|
148 maybeConstantExpr = false; |
|
149 } |
|
150 |
|
151 @Override |
|
152 public void visitBinary(JCBinary tree) { |
|
153 switch (tree.getTag()) { |
|
154 case MUL: case DIV: case MOD: |
|
155 case PLUS: case MINUS: |
|
156 case SL: case SR: case USR: |
|
157 case LT: case LE: case GT: case GE: |
|
158 case EQ: case NE: |
|
159 case BITAND: case BITXOR: case BITOR: |
|
160 case AND: case OR: |
|
161 break; |
|
162 default: |
|
163 maybeConstantExpr = false; |
|
164 } |
|
165 } |
|
166 |
|
167 @Override |
|
168 public void visitConditional(JCConditional tree) { |
|
169 scan(tree.cond); |
|
170 scan(tree.truepart); |
|
171 scan(tree.falsepart); |
|
172 } |
|
173 |
|
174 @Override |
|
175 public void visitIdent(JCIdent tree) { } |
|
176 |
|
177 @Override |
|
178 public void visitLiteral(JCLiteral tree) { } |
|
179 |
|
180 @Override |
|
181 public void visitParens(JCParens tree) { |
|
182 scan(tree.expr); |
|
183 } |
|
184 |
|
185 @Override |
|
186 public void visitSelect(JCTree.JCFieldAccess tree) { |
|
187 scan(tree.selected); |
|
188 } |
|
189 |
|
190 @Override |
|
191 public void visitTypeCast(JCTypeCast tree) { |
|
192 scan(tree.clazz); |
|
193 scan(tree.expr); |
|
194 } |
|
195 |
|
196 @Override |
|
197 public void visitTypeIdent(JCPrimitiveTypeTree tree) { } |
|
198 |
|
199 @Override |
|
200 public void visitUnary(JCUnary tree) { |
|
201 switch (tree.getTag()) { |
|
202 case POS: case NEG: case COMPL: case NOT: |
|
203 break; |
|
204 default: |
|
205 maybeConstantExpr = false; |
|
206 } |
|
207 } |
|
208 } |
104 } |
209 } |