2170 |
2170 |
2171 /************************************************************************** |
2171 /************************************************************************** |
2172 * Code for enabling/disabling assertions. |
2172 * Code for enabling/disabling assertions. |
2173 *************************************************************************/ |
2173 *************************************************************************/ |
2174 |
2174 |
|
2175 private ClassSymbol assertionsDisabledClassCache; |
|
2176 |
|
2177 /**Used to create an auxiliary class to hold $assertionsDisabled for interfaces. |
|
2178 */ |
|
2179 private ClassSymbol assertionsDisabledClass() { |
|
2180 if (assertionsDisabledClassCache != null) return assertionsDisabledClassCache; |
|
2181 |
|
2182 assertionsDisabledClassCache = makeEmptyClass(STATIC | SYNTHETIC, outermostClassDef.sym).sym; |
|
2183 |
|
2184 return assertionsDisabledClassCache; |
|
2185 } |
|
2186 |
2175 // This code is not particularly robust if the user has |
2187 // This code is not particularly robust if the user has |
2176 // previously declared a member named '$assertionsDisabled'. |
2188 // previously declared a member named '$assertionsDisabled'. |
2177 // The same faulty idiom also appears in the translation of |
2189 // The same faulty idiom also appears in the translation of |
2178 // class literals above. We should report an error if a |
2190 // class literals above. We should report an error if a |
2179 // previous declaration is not synthetic. |
2191 // previous declaration is not synthetic. |
2180 |
2192 |
2181 private JCExpression assertFlagTest(DiagnosticPosition pos) { |
2193 private JCExpression assertFlagTest(DiagnosticPosition pos) { |
2182 // Outermost class may be either true class or an interface. |
2194 // Outermost class may be either true class or an interface. |
2183 ClassSymbol outermostClass = outermostClassDef.sym; |
2195 ClassSymbol outermostClass = outermostClassDef.sym; |
2184 |
2196 |
2185 // note that this is a class, as an interface can't contain a statement. |
2197 //only classes can hold a non-public field, look for a usable one: |
2186 ClassSymbol container = currentClass; |
2198 ClassSymbol container = !currentClass.isInterface() ? currentClass : |
|
2199 assertionsDisabledClass(); |
2187 |
2200 |
2188 VarSymbol assertDisabledSym = |
2201 VarSymbol assertDisabledSym = |
2189 (VarSymbol)lookupSynthetic(dollarAssertionsDisabled, |
2202 (VarSymbol)lookupSynthetic(dollarAssertionsDisabled, |
2190 container.members()); |
2203 container.members()); |
2191 if (assertDisabledSym == null) { |
2204 if (assertDisabledSym == null) { |
2206 containerDef.pos()), |
2219 containerDef.pos()), |
2207 desiredAssertionStatusSym))); |
2220 desiredAssertionStatusSym))); |
2208 JCVariableDecl assertDisabledDef = make.VarDef(assertDisabledSym, |
2221 JCVariableDecl assertDisabledDef = make.VarDef(assertDisabledSym, |
2209 notStatus); |
2222 notStatus); |
2210 containerDef.defs = containerDef.defs.prepend(assertDisabledDef); |
2223 containerDef.defs = containerDef.defs.prepend(assertDisabledDef); |
|
2224 |
|
2225 if (currentClass.isInterface()) { |
|
2226 //need to load the assertions enabled/disabled state while |
|
2227 //initializing the interface: |
|
2228 JCClassDecl currentClassDef = classDef(currentClass); |
|
2229 make_at(currentClassDef.pos()); |
|
2230 JCStatement dummy = make.If(make.QualIdent(assertDisabledSym), make.Skip(), null); |
|
2231 JCBlock clinit = make.Block(STATIC, List.<JCStatement>of(dummy)); |
|
2232 currentClassDef.defs = currentClassDef.defs.prepend(clinit); |
|
2233 } |
2211 } |
2234 } |
2212 make_at(pos); |
2235 make_at(pos); |
2213 return makeUnary(NOT, make.Ident(assertDisabledSym)); |
2236 return makeUnary(NOT, make.Ident(assertDisabledSym)); |
2214 } |
2237 } |
2215 |
2238 |