ClassCastException and $1

K

Kai Schlamp

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
 
S

Stefan Ram

Kai Schlamp said:
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()
 
S

Stefan Ram

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«.
 
J

Joshua Cranmer

Kai said:
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.
 
M

Mark Space

Kai said:
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.
 
A

Arne Vajhøj

Kai said:
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
 
Joined
Feb 24, 2011
Messages
2
Reaction score
0
.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:
Joined
Feb 24, 2011
Messages
2
Reaction score
0
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:

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

Staff online

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top