Is the following legal?

Discussion in 'Java' started by bluke, May 17, 2004.

  1. bluke

    bluke Guest

    I have the following:
    package test1;

    class Test {
    public final Color RED = new Color("red");

    protected static final class Color {
    private String id;
    private Color (String id) {
    this.id = id;
    }
    public String toString() {
    return id;
    }
    }
    }

    package test1;
    public class Test1 extends Test {
    }

    package test2;
    public class Test2 extends Test1 {
    someMethod(RED);
    }


    I am getting an IllegalAccessError with this because Class Test is
    package protected and Test2 is in a different package, so when Test2
    tries to access RED I get an IllegalAccessError that tried to access
    class test1.Test from class test2.Test2. The interesting thing about
    this is that it compiles. This means that there is either a VM bug or
    a compiler bug, because if it compiles I should not get an
    IllegalAccessError. Is this actually legal?
    bluke, May 17, 2004
    #1
    1. Advertising

  2. bluke

    kaeli Guest

    In article <>,
    enlightened us with...
    >
    >
    > I am getting an IllegalAccessError with this because Class Test is
    > package protected and Test2 is in a different package, so when Test2
    > tries to access RED I get an IllegalAccessError that tried to access
    > class test1.Test from class test2.Test2. The interesting thing about
    > this is that it compiles. This means that there is either a VM bug or
    > a compiler bug, because if it compiles I should not get an
    > IllegalAccessError. Is this actually legal?


    This is a runtime error, not a compile time error, so no, the compiler
    won't catch it.
    It is not allowed (illegal) to do what you're trying to do.

    --
    --
    ~kaeli~
    He's your God, they're your rules - you burn in Hell.
    http://www.ipwebdesign.net/wildAtHeart
    http://www.ipwebdesign.net/kaelisSpace
    kaeli, May 17, 2004
    #2
    1. Advertising

  3. bluke wrote:

    > I have the following:
    > package test1;
    >
    > class Test {
    > public final Color RED = new Color("red");
    >
    > protected static final class Color {
    > private String id;
    > private Color (String id) {
    > this.id = id;
    > }
    > public String toString() {
    > return id;
    > }
    > }
    > }
    >
    > package test1;
    > public class Test1 extends Test {
    > }
    >
    > package test2;
    > public class Test2 extends Test1 {
    > someMethod(RED);
    > }
    >
    >
    > I am getting an IllegalAccessError with this because Class Test is
    > package protected and Test2 is in a different package,


    I'll believe the IllegalAccessError. If you know the cause already,
    however then why are you asking here?

    > so when Test2
    > tries to access RED I get an IllegalAccessError that tried to access
    > class test1.Test from class test2.Test2. The interesting thing about
    > this is that it compiles.


    No it doesn't. That is to say, what you gave above is not syntactically
    correct Java, and it cannot compile, regardless of any questions of
    access control. Moreover, even if it did compile it does not form a
    complete Java program so there is no way for you to run it to produce
    your error.

    > This means that there is either a VM bug or
    > a compiler bug, because if it compiles I should not get an
    > IllegalAccessError.


    That much is correct, in the sense that any situation that produces an
    IllegalAccessError should be caught at compile time -- *if the compiler
    is allowed to see it*. This sort of problem likely means that you
    managed to create a class version mismatch; the version of Test that you
    compiled Test1 or Test2 against is different from the version that was
    loaded at runtime.

    > Is this actually legal?


    It is not syntactically valid (see above). The access itself could be
    legal, however, if the syntax problems were corrected. I generated a
    valid program based on your example and without changing any access
    modifiers; it compiled and ran fine. If you have further questions then
    please post an actual code with which your problem can be reproduced.


    John Bollinger
    John C. Bollinger, May 17, 2004
    #3
  4. bluke

    bluke Guest

    Silly nitpicking

    I only posted snippets of code to illustrate the problem, of course
    the posted posted doesn't actually compile. I didn't think that I
    needed to post complete Java code to illustrate the problem.

    I cannot see how I crteated a class version mismatch, I conpiled the
    code and then ran it all within the same IDE.

    "John C. Bollinger" <> wrote in message news:<c8betj$ql3$>...
    > bluke wrote:
    >
    > > I have the following:
    > > package test1;
    > >
    > > class Test {
    > > public final Color RED = new Color("red");
    > >
    > > protected static final class Color {
    > > private String id;
    > > private Color (String id) {
    > > this.id = id;
    > > }
    > > public String toString() {
    > > return id;
    > > }
    > > }
    > > }
    > >
    > > package test1;
    > > public class Test1 extends Test {
    > > }
    > >
    > > package test2;
    > > public class Test2 extends Test1 {
    > > someMethod(RED);
    > > }
    > >
    > >
    > > I am getting an IllegalAccessError with this because Class Test is
    > > package protected and Test2 is in a different package,

    >
    > I'll believe the IllegalAccessError. If you know the cause already,
    > however then why are you asking here?
    >
    > > so when Test2
    > > tries to access RED I get an IllegalAccessError that tried to access
    > > class test1.Test from class test2.Test2. The interesting thing about
    > > this is that it compiles.

    >
    > No it doesn't. That is to say, what you gave above is not syntactically
    > correct Java, and it cannot compile, regardless of any questions of
    > access control. Moreover, even if it did compile it does not form a
    > complete Java program so there is no way for you to run it to produce
    > your error.
    >
    > > This means that there is either a VM bug or
    > > a compiler bug, because if it compiles I should not get an
    > > IllegalAccessError.

    >
    > That much is correct, in the sense that any situation that produces an
    > IllegalAccessError should be caught at compile time -- *if the compiler
    > is allowed to see it*. This sort of problem likely means that you
    > managed to create a class version mismatch; the version of Test that you
    > compiled Test1 or Test2 against is different from the version that was
    > loaded at runtime.
    >
    > > Is this actually legal?

    >
    > It is not syntactically valid (see above). The access itself could be
    > legal, however, if the syntax problems were corrected. I generated a
    > valid program based on your example and without changing any access
    > modifiers; it compiled and ran fine. If you have further questions then
    > please post an actual code with which your problem can be reproduced.
    >
    >
    > John Bollinger
    >
    bluke, May 18, 2004
    #4
  5. bluke

    bluke Guest

    kaeli <> wrote in message et an
    >
    > This is a runtime error, not a compile time error, so no, the compiler
    > won't catch it.
    > It is not allowed (illegal) to do what you're trying to do.
    >

    It should be a compile time error. The documentation for IllegalAccessError states:
    " Normally, this error is caught by the compiler; this error can
    only occur at run time if the definition of a class has
    incompatibly changed. "
    In this case the definition of teh class has not changed.
    bluke, May 18, 2004
    #5
  6. Re: Rude Supplicant

    Please don't top post.

    bluke wrote:

    > I only posted snippets of code to illustrate the problem, of course
    > the posted posted doesn't actually compile. I didn't think that I
    > needed to post complete Java code to illustrate the problem.


    If you cannot identify a specific problem in your code, then you don't
    really know where the trouble is. In that case, you risk leaving out
    relevant parts when you post only excerpts, and indeed, in my experience
    leaving out important bits is the rule rather than the exception in such
    situations. Sometimes the work of producing a minimal example to
    demonstrate the problem is enough to help you discover it for yourself.
    If it isn't, then often that's precisely because the problem is not in
    the parts that *you* think are relevant / important.

    In your case, I produced a working Java program from your snippets.
    That is irrefutable proof that either you did not post enough code to
    demonstrate the problem, or the problem isn't in the code itself. I
    can't tell you which without seeing a complete example. Furthermore,
    you asked about the legality of a Java construct, but did not provide
    enough context for anyone to provide a well-considered answer to the
    question. The answers to such questions frequently hinge on context and
    details, so here again a fuller example is necessary, and a complete
    program would be best.

    > I cannot see how I crteated a class version mismatch, I conpiled the
    > code and then ran it all within the same IDE.


    That does not necessarily protect you. In your example, class Test2
    depends on class Test1, which depends on class Test. If you compile
    Test, Test1, and Test2, then modify Test and recompile it, Test1 and
    Test2 are not necessarily recompiled. Depending on what you did to
    Test, classes Test1 and / or Test2 might not need to be recompiled. How
    this is handled in a Java IDE varies by IDE. If your IDE has a "rebuild
    all" (or equivalent) feature then try using it.


    I hope that is enough to help you solve your problem, because I'm done
    with you. I don't need to spend any more time being accused of
    nitpicking for giving you good advice and valid critique.


    John Bollinger
    John C. Bollinger, May 18, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Valentin Tihomirov

    Is this legal?

    Valentin Tihomirov, Oct 21, 2003, in forum: VHDL
    Replies:
    20
    Views:
    1,227
    Jan Decaluwe
    Oct 29, 2003
  2. Divyang M
    Replies:
    9
    Views:
    608
    Divyang M
    May 18, 2005
  3. Divyang M
    Replies:
    1
    Views:
    558
    Jerzy Gbur
    May 15, 2005
  4. Weng Tianxiang
    Replies:
    12
    Views:
    1,386
  5. Kiuhnm
    Replies:
    30
    Views:
    904
    Alf P. Steinbach
    Mar 20, 2006
Loading...

Share This Page