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()