ClassCastException and $1

Discussion in 'Java' started by Kai Schlamp, Feb 20, 2009.

  1. Kai Schlamp

    Kai Schlamp Guest

    Hy.

    This may be some kind of java newbie question, but I have never
    encountered such a problem until yet.
    I get the following error during casting an object:
    java.lang.ClassCastException:
    org.eclipse.core.databinding.AggregateValidationStatus$1 cannot be
    cast to org.eclipse.core.databinding.AggregateValidationStatus

    What does the $1 mean? Why does the cast fail?

    Best regards,
    Kai
     
    Kai Schlamp, Feb 20, 2009
    #1
    1. Advertising

  2. Kai Schlamp

    Stefan Ram Guest

    Kai Schlamp <> writes:
    >org.eclipse.core.databinding.AggregateValidationStatus$1 cannot be
    >cast to org.eclipse.core.databinding.AggregateValidationStatus
    >What does the $1 mean?


    (If answering to this post, please do not quote all of it, but
    only short part you directly refer to.)

    It is part of the simple name »AggregateValidationStatus$1«,
    which might be an inner class.

    Why does the cast fail?

    It should be a downcast. A downcast

    ( A )b

    with B being the type of the object b will fail,
    if A is not a supertype of B (in the sense where
    every type is taken to be a supertype of itself).

    More elaborated:


    This explanation of casts begins with some words about
    reference types and references. It ends with the explanation
    of »downcasts«, such as a cast from »Graphics« to »Graphics2D«
    is.

    Reference Types
    ===============

    Supertypes
    ----------

    A reference type »P« here is called a »supertype« of a type »B« (P>=B)
    iff »B« directly or indirectly extends or implements »P« or is »P«.

    Examples:
    »java.lang.String« is a supertype of »java.lang.String«.
    »java.lang.Object« is a supertype of »java.lang.String«.
    »java.lang.String« is not a supertype of »java.lang.Object«.

    Exercises:
    Is »java.lang.Integer« a supertype of »java.lang.String«?
    Is »java.lang.String« a supertype of »java.lang.Integer«?

    Proper Subtypes
    ---------------

    If »B« (as above) is not »P«, then »B« is called a »proper subtype«
    of »P« (B<P).

    Examples:
    »java.lang.String« is a proper subertype of »java.lang.Object«.
    »java.lang.Object« is not a proper subtype of »java.lang.Object«.

    Exercises:
    Is java.lang.Integer a proper subtype of java.lang.String?
    Is java.lang.String a proper subtype of java.lang.Integer?

    Types of Expressions and Objects
    --------------------------------

    In Java, both expression (entities of the source-code model) and
    objects (entities of the run-time model) have a type.

    Types of expressions are known at compile time, while
    types of objects are known only at run time in the general case.

    Example:
    In Java SE 1.6, the type of the expression »java.lang.System.in« is
    java.io.InputStream, the type of the object »java.lang.System.in« is
    java.io.BufferedInputStream.

    Exercise:
    What is the type of the expression »"alpha"«?
    What is the type of the object »"alpha"«?
    What is the type of the expression »java.lang.System.out«?
    What is the type of the object »java.lang.System.out«?

    Reference expressions
    =====================

    A reference expression is an expression of reference type.

    .-----------------------------------------------------.
    | |
    | Fundamental requirement for any reference |
    | expression |
    | |
    | B1 The type »R« of a reference expression must |
    | be a supertype of the type »O« of the |
    | object it refers to (R>=O) |
    | |
    '-----------------------------------------------------'

    Example:
    In Java SE 1.6, the expression »java.lang.System.in« has the
    type »java.io.InputStream« and it references an object of the
    type »java.io.BufferedInputStream«. And indeed, »java.io.InputStream«
    is a supertype of the type »java.io.BufferedInputStream«.

    Casts of Reference expressions
    ==============================

    Let r be a reference-valued expression (syntactically, a
    »UnaryExpressionNotPlusMinus«).

    Let T be a reference type (syntactically, a »ReferenceType«).

    Then

    (T)r

    is an expression, called a »cast expression« (»CastExpression«)
    or just »cast«.

    .-----------------------------------------------------.
    | |
    | Fundamental properties of the reference cast |
    | |
    | A1 The type of the expression »(T)r« is »T«. |
    | |
    | A2 If the value of »r« is null, the value of |
    | »(T)r« also is null. |
    | |
    | A3 If »r« refers to an object, the expression |
    | »(T)r« refers to the same object »o« as |
    | the expression »r« refers to. |
    | |
    '-----------------------------------------------------'

    Example:
    »( java.lang.Object )"alpha"« is an expression of type
    »java.lang.Object« referencing an object of type »java.lang.String«.

    Upcasts
    =======

    Let »R« be the type of the expression »r«. If »r« refers to an object,
    let »O« be the type of the object »r« refers to. (»O« is a class.)

    If »T« is a supertype of »R« (T>=R) the cast is always allowed and
    is called an »upcast«. (Proof: T>=R, and by B1, R>=O, thus, T>=O (B1)
    is fulfilled; or r is null.)

    Example:
    »( java.lang.Object )"alpha"« is an upcast, because java.lang.Object >=
    java.lang.String.

    Downcasts
    =========

    Now, assume that »T« is a proper subtype of »R« (T<R): Because
    a possible type of »O« of a refered object is only known at run time, it can only be known
    at runtime, whether B1 is fulfilled (T<R and R>=O does not imply T>=O.)
    Such a cast is called a »downcast«.

    The Java Machine will check at runtime whether a downcast fullfils B1
    (while it does not check anything for an upcast, which always fullfils B1).

    Example:
    »( java.lang.String )( java.lang.Object )"alpha"« is a downcast,
    because java.lang.String < java.lang.Object.

    Exercises:
    Assuming that »String« is a proper subtype of »Object« (String<Object),
    which of the following expressions (one expression per line) are upcasts,
    which are downcast, which will create an error during compilation and
    which will create an exception during evaluation?
    ( Object )new String()
    ( String )new String()
    ( Object )new Object()
    ( String )new Object()
     
    Stefan Ram, Feb 20, 2009
    #2
    1. Advertising

  3. Kai Schlamp

    Stefan Ram Guest

    -berlin.de (Stefan Ram) writes:
    >It is part of the simple name »AggregateValidationStatus$1«,
    >which might be an inner class.


    better:

    which might be an /anonymous/ inner class of
    »AggregateValidationStatus«.
     
    Stefan Ram, Feb 20, 2009
    #3
  4. Kai Schlamp wrote:
    > What does the $1 mean? Why does the cast fail?


    A dollar sign in a class name is a reference to an inner or nested
    class. The $1 is a sign that the class has no apparent fully-qualified
    name, i.e., it is an anonymous inner class.

    As to why the cast fails, it is obviously because the anonymous inner
    class is being cast to a type that it is not. In lieu of more
    information, i.e., a SSCCE, that is all I can say.

    --
    Beware of bugs in the above code; I have only proved it correct, not
    tried it. -- Donald E. Knuth
     
    Joshua Cranmer, Feb 20, 2009
    #4
  5. Kai Schlamp

    Mark Space Guest

    Kai Schlamp wrote:
    > Hy.
    >
    > This may be some kind of java newbie question, but I have never
    > encountered such a problem until yet.
    > I get the following error during casting an object:
    > java.lang.ClassCastException:
    > org.eclipse.core.databinding.AggregateValidationStatus$1 cannot be
    > cast to org.eclipse.core.databinding.AggregateValidationStatus


    As the others have said, we'll need an SSCCE.

    <http://sscce.org/>

    But basically it's because the first thing isn't the second, which
    should be pretty obvious by now. The $1 is a legal identifier in Java:

    int $1 = 2;

    works fine, no problem. It's just unusual to see, that's all.
     
    Mark Space, Feb 20, 2009
    #5
  6. Kai Schlamp

    Arne Vajhøj Guest

    Kai Schlamp wrote:
    > This may be some kind of java newbie question, but I have never
    > encountered such a problem until yet.
    > I get the following error during casting an object:
    > java.lang.ClassCastException:
    > org.eclipse.core.databinding.AggregateValidationStatus$1 cannot be
    > cast to org.eclipse.core.databinding.AggregateValidationStatus
    >
    > What does the $1 mean? Why does the cast fail?


    Here are a very simple piece of code giving a similar
    error:

    public class CCE {
    public Object demo() {
    return new Object() { };
    }
    public static void main(String[] args) {
    CCE o1 = new CCE();
    CCE o2 = (CCE) o1.demo();
    }
    }

    It should give you an idea about what to look for in your code.

    Arne
     
    Arne Vajhøj, Feb 21, 2009
    #6
  7. Kai Schlamp

    Yasser Farghaly

    Joined:
    Feb 24, 2011
    Messages:
    2
    .addMouseListener(new java.awt.event.MouseAdapter() {
    @Override
    public void mouseClicked(java.awt.event.MouseEvent evt) {

    ........ code was here ..... > Causes casting Error


    }

    is it like this?
     
    Last edited: Mar 14, 2011
    Yasser Farghaly, Feb 24, 2011
    #7
  8. Kai Schlamp

    Yasser Farghaly

    Joined:
    Feb 24, 2011
    Messages:
    2
    if yes

    .addMouseListener(new java.awt.event.MouseAdapter() {
    @Override
    public void mouseClicked(java.awt.event.MouseEvent evt) {

    ........ code was here ..... > Causes casting Error


    }

    you should correct your code and move your code outside inner class

    www.shcalendar.com
     
    Last edited: Mar 14, 2011
    Yasser Farghaly, Mar 14, 2011
    #8
    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. Luther Baker

    ClassCastException and ClassLoaders

    Luther Baker, Sep 9, 2003, in forum: Java
    Replies:
    3
    Views:
    890
    Luther Baker
    Sep 10, 2003
  2. Richard A. DeVenezia
    Replies:
    2
    Views:
    613
    Andrew Thompson
    Jan 8, 2004
  3. Robert Paris
    Replies:
    2
    Views:
    1,201
    Anton Spaans
    Feb 17, 2004
  4. Bob Dubery
    Replies:
    5
    Views:
    593
    Bob Dubery
    Jun 19, 2004
  5. Affan
    Replies:
    0
    Views:
    452
    Affan
    Jun 24, 2004
Loading...

Share This Page