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)



At 11:43 AM -0700 11/25/02, Greg Guerin wrote:
Dmitry Markman <email@hidden> wrote:

public class AlignCommand extends AbstractCommand {
public static abstract class Alignment {
public final static Alignment LEFTS = new Alignment("Lefts") {
public void moveBy(Figure f, Rectangle anchor) {
Rectangle rr = f.displayBox();
f.moveBy(anchor.x-rr.x, 0);
}
};

private Alignment(String newDescription) {
...
}
public abstract void moveBy(Figure f, Rectangle anchor);
}
}

I think the way to avoid the problem is to not declare Alignment as abstract, and to define Alignment.moveBy() as an empty method by default, with an override in each anonymous subclass.

I agree, that should work, but I would consider it a workaround to a compiler bug -- again, assuming the language spec is unambiguous about this.

Since all the possible definitions of Alignment's subclasses are entirely under your control in exactly one source file, I don't see how you're gaining anything by declaring it abstract.

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.

I still think it would be worthwhile to run 'javap' on the output of 'javac', to discover what it's doing about the private constructor. I suspect it's being promoted to the package-visibility

Here's some code you can fool around with, that I compiled with javac. 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.


----- 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);
}
----- end Foo.java -----


----- output of "javap -c Foo" -----
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)
0 aload_0
1 invokespecial #2 <Method java.lang.Object()>
4 aload_0
5 new #3 <Class Foo$1>
8 dup
9 aload_0
10 ldc #4 <String "test">
12 invokespecial #5 <Method Foo$1(Foo,java.lang.String)>
15 putfield #6 <Field Foo foo>
18 aload_0
19 new #7 <Class Foo$2>
22 dup
23 aload_0
24 ldc #4 <String "test">
26 invokespecial #8 <Method Foo$2(Foo,java.lang.String)>
29 putfield #9 <Field Foo foo2>
32 return

Method Foo(java.lang.String,Foo$1)
0 aload_0
1 aload_1
2 invokespecial #1 <Method Foo(java.lang.String)>
5 return
----- end output of "javap Foo" -----


--Andy
_______________________________________________
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.

References: 
 >Re: problem with compiling abstract class with CW (MAC OS X) (From: Greg Guerin <email@hidden>)



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.