Enabling assertions for inner class

I

Ian Pilcher

I am attempting to use a static inner class for unit testing. As part
its class initialization I want to check that assertions are enabled for
both the inner class and its enclosing class.

Here is an example:

package temp;

public class TestMe
{
// Stuff to test goes here

private static boolean assertionsEnabled()
{
try
{
assert false;
return false;
}
catch (AssertionError e)
{
return true;
}
}

private static class __UnitTest__
{
// Ensure assertions are enabled for this class and TestMe

static
{
try
{
assert false;
throw new Error("assertions not enabled"); // 29
}
catch (AssertionError e)
{
assert TestMe.assertionsEnabled(); // 33
}
}

public static void main(String[] args)
{
// Tests go here
}
}
}

When I run with assertions disabled, things go as expected:

[pilcher@home temp]$ java temp.TestMe\$__UnitTest__
Exception in thread "main" java.lang.Error: assertions not enabled
at temp.TestMe$__UnitTest__.<clinit>(TestMe.java:29)
[pilcher@home temp]$

Turning all (non-system) assertions on also works as expected:

[pilcher@home temp]$ java -ea temp.TestMe\$__UnitTest__
[pilcher@home temp]$

Turning assertions on for the enclosing class seems to also enable them
for the inner class:

[pilcher@home temp]$ java -ea:temp.TestMe temp.TestMe\$__UnitTest__
[pilcher@home temp]$

Attempting to enable assertions for *only* the inner class does not
seem to work, however:

[pilcher@home temp]$ java -ea:temp.TestMe\$__UnitTest__
temp.TestMe\$__UnitTest__
Exception in thread "main" java.lang.Error: assertions not enabled
at temp.TestMe$__UnitTest__.<clinit>(TestMe.java:29)
[pilcher@home temp]$ java -ea:temp.TestMe.__UnitTest__
temp.TestMe\$__UnitTest__
Exception in thread "main" java.lang.Error: assertions not enabled
at temp.TestMe$__UnitTest__.<clinit>(TestMe.java:29)
[pilcher@home temp]$

Is this expected behavior? If so, is it guaranteed? (The latter would
be nice, because it would allow me to dispense with the
assertionsEnabled() method in the outer class.)

Thanks!
 
T

Tony Morris

G'day Tony,


And isn't the word 'static' redundant there, Tony?

Ricky.

Hello Ricky :)
No there is no redundancy, you can have non-static nested classes i.e. inner
classes.
 
R

ricky.clarkson

You're correct, I checked the JLS. I had it in my head that nested was
static, and inner was non-static.

Sorry for the bother.
 
I

Ian Pilcher

You're correct, I checked the JLS. I had it in my head that nested was
static, and inner was non-static.

Glad I'm not the only one who has trouble remembering which is which.

Let me see if I've got this right:

class Outer
{
static class Nested {}
class Inner {}
class NonStaticNested {}
}

Seems needlessly confusing to me.
 
T

Tony Morris

Hi Roedy,
You might want to also make it clear on your web page here:
# anonymous classes: unnamed inner class defined in the middle of a method.
# local classes: named inner class defined in the middle of a method.

The distinction between a local and anonymous class is not quite correct.
An anonymous class is a type of local class - the other type being a named
class.
Local class is to anonymous class as nested class is to inner class - one is
a more general type of the other.

Also, a method is not just the only context in which local classes can be
used.
Specifically, a method, constructor or initialiser are all possible contexts
of local (and therefore, anonymous) classes.

--
Tony Morris
http://tmorris.net/

Java Questions and Answers
http://jqa.tmorris.net/
 
T

Tony Morris

Tony Morris said:
The distinction between a local and anonymous class is not quite correct.
An anonymous class is a type of local class - the other type being a named
class.

That's certainly an internally consistent use of terminology, but it is
not the same as the usage within the Java Language Specification...
which says:

§8
"This chapter discusses the common semantics of all classes -- top
level (§7.6) and nested (including member classes (§8.5, §9.5),
local classes (§14.3) and anonymous classes (§15.9.5))."

§14.3
"A local class is a nested class (§8) that is not a member of any
class and that has a name."

The first statement implies, and the second states outright, that an
anonymous class does NOT qualify as a "local class" in language spec
words. They certainly do share a lot of characteristics, though.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

Hello Chris,
I hadn't come across that section of the JLS. I can find other sections of
the JLS where there is clearly an implication that an anonymous class is a
local class, but certainly nothing as explicit as you mention. I put it down
to yet another contradiction in the JLS - and change my recommendation to
either stating the premise of the section of the JLS that you cite or change
the wording to avoid potential confusion.

I still think that it should not be explicitly stated that local/anonymous
classes are restricted to methods, since obviously, they are not.


Tony Morris
http://tmorris.net/
Java Questions and Answers
http://jqa.tmorris.net/
 
P

P.Hill

Ian said:
Seems needlessly confusing to me.

Inner class is the slightly older term which was formalized in the JLS
with "nested" and "top-level" thus producing more than one common term
for the same thing.

-Paul
 
C

Chris Smith

Tony Morris said:
The distinction between a local and anonymous class is not quite correct.
An anonymous class is a type of local class - the other type being a named
class.

That's certainly an internally consistent use of terminology, but it is
not the same as the usage within the Java Language Specification...
which says:

§8
"This chapter discusses the common semantics of all classes -- top
level (§7.6) and nested (including member classes (§8.5, §9.5),
local classes (§14.3) and anonymous classes (§15.9.5))."

§14.3
"A local class is a nested class (§8) that is not a member of any
class and that has a name."

The first statement implies, and the second states outright, that an
anonymous class does NOT qualify as a "local class" in language spec
words. They certainly do share a lot of characteristics, though.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top