Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: problem with compiling abstract class with CW (MAC OS X)



Andy Lee <email@hidden> wrote:

>I think it depends how fussy one is about semantics and defensive
>programming. Declaring moveBy() abstract forces you (and anyone who
>maintains the class after you) to implement it in each inner
>subclass. It also prevents any other code within the class from
>creating a direct instance of Alignment by mistake.

You can declare a class abstract even though it has no abstract methods.
That may not avoid the problem, though.

If some maintainer can't figure out that moveBy() should be implemented by
each inner class, they probably shouldn't be doing maintenance. There
would be doc-comments concisely explaining this abstract-to-anonymous
relationship, too.

If a do-nothing method is insufficient, throwing a RuntimeException in the
superclass's default method body prevents the method from being used in
practice, yet avoids the 'abstract' qualifier. I've used this approach
before. SecurityManager's default method bodies all used to do this: throw
a SecurityException by default. This forced subclasses to explicilty
override every desired method to not throw a SecurityException. Defensive
programming of a different sort.


>----- Foo.java -----
>public abstract class Foo
>{
> public Foo foo =
> new Foo("test")
> {
> public void foo(Object x)
> {
> }
> };
>
> public Foo foo2 =
> new Foo("test")
> {
> public void foo(Object x)
> {
> System.out.println(x);
> }
> };
>
> private Foo(String str)
> {
> }
>
> public abstract void foo(Object x);
>}

You're not using 'static' in the same places the original had it. As a
result, you have two Foo instance variables, not static variables, and they
are being initialized in the Foo constructor, not in a static initializer.
That also changes the nature of the relationship between the outer class
and the inner anonymous ones, as far as the compiler is concerned, because
it binds the inner anonymous instances to an outer instance.

Also, Dmitry's Alignment class was a nested class itself (public static
abstract class), and I'm not sure if that makes a difference to the
compiler or not. It makes a difference to the class name, but I'm not sure
what else in this situation.


>The javap output is semi-Greek to me, but it looks like the
>compiler creates a special constructor with an extra invisible
>argument containing the concrete instance being initialized. And if
>I'm reading correctly, it is indeed package-visible.
>
>Compiled from Foo.java
>public abstract class Foo extends java.lang.Object {
> public Foo foo;
> public Foo foo2;
> public abstract void foo(java.lang.Object);
> Foo(java.lang.String,Foo$1);
>}
>
>Method Foo(java.lang.String)
> [..snip bytecodes..]
>
>Method Foo(java.lang.String,Foo$1)
> [..snip bytecodes..]

There are two constructor bodies disassembled, but only one is listed in
the class-members: the package-visible synthesized constructor. I suspect
the Foo(String) one is private (try "-private" option to javap to list all
members).

However, the code shown illustrates how a private constructor can be
invoked from a subclass: simply synthesize another constructor in the
superclass with an additional (apparently unused) arg, thereby yielding a
different signature.

Although maybe that's not an unused arg in the instance-variable situation.
Hard to say. I'd have to add 'static' to the variables to mimic the
original case, and examine the disassembled byte-codes in the Foo$1 and
Foo$2 constructors, too.


and Dmitry Markman <email@hidden> wrote:

>as I found in java language specification (6.6.8):
>"A private class member or constructor is accessible only within the
>class body in which the member is declared and is not inherited by
>subclasses"
>so it's looks like that anonymous inner class can access private
>constructor of
>the enclosing class (as I understood Andy Lee mentioned it earlier), so
>CW has a problem
>and I have to report a bug

That section of the spec reads ambiguously to me. The phrase "within the
class body" needs to be qualified by whether it refers to the source or to
the resulting class-file. If it refers to the source, then anonymous inner
classes are indeed "within the class body", and the compiler synthesizes
members to provide access. If it refers to the class-file, then inner
classes are never "within the class body", since the current class-file
format always has exactly one class defined in each class-file. The
compiler synthesizes class-names to accomplish this.

-- GG
_______________________________________________
java-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/java-dev
Be sure to read the FAQ http://developer.apple.com/java/faq/ before posting
Do not post admin requests to the list. They will be ignored.



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.