M
mortoray
We have a library that implements a simple scripting language. Our
implementation appears to have been deprecated by Java 5 and we need
some input on how to make the code compliant. (The code still compiles
and works but it spits out the "unchecked type" warnings, and Sun's
docs seem to indicate this may be disallowed in future versions).
The basic situation:
1) We have a variable map stored in a Map<Object,Object>
2) We have a function called Put which takes an object and puts inside
a Collection stored in the variable map
3) Previously we did this simply as (abbreviated syntax)
void put( String variable, Object data ) {
Object obj = variables.get( variable );
if( !(obj instanceof Collection) )
throw ...;
((Collection)obj).add( data );
}
The caller of the function was responsible for inserting the correct
types into the correct collections. Additionally, the actual type of
the collection is not known, if may be of Numbers, Strings, or just
Objects.
4) With the new Java (5) this causes unchecked type warnings because
"add" uses a generic type in its signature. (We have an equivalent
case with "get", but that works fine since it isn't typed).
Out initial idea was to do the following:
Object obj = variables.get( variable );
if( !(obj instanceof Collection<? extends Object>) )
throw ...;
((Collection<? extends Object>)obj).add( data );
But that just produces two warnings instead of one because you can't
use instanceof with wildcards and the following cast is also converting
from untyped to typed.
This disallowed casting concerns me additionally, since even if we
rewrite the code such that we always know a COllection<Object> is
stored in the variable, it doesn't appear to help, since we still need
to do the cast and get the unchecked warning.
Has this type of functionaly truly been deprecated? Or is there someway
to achieve this without gettnig the warning messages?
implementation appears to have been deprecated by Java 5 and we need
some input on how to make the code compliant. (The code still compiles
and works but it spits out the "unchecked type" warnings, and Sun's
docs seem to indicate this may be disallowed in future versions).
The basic situation:
1) We have a variable map stored in a Map<Object,Object>
2) We have a function called Put which takes an object and puts inside
a Collection stored in the variable map
3) Previously we did this simply as (abbreviated syntax)
void put( String variable, Object data ) {
Object obj = variables.get( variable );
if( !(obj instanceof Collection) )
throw ...;
((Collection)obj).add( data );
}
The caller of the function was responsible for inserting the correct
types into the correct collections. Additionally, the actual type of
the collection is not known, if may be of Numbers, Strings, or just
Objects.
4) With the new Java (5) this causes unchecked type warnings because
"add" uses a generic type in its signature. (We have an equivalent
case with "get", but that works fine since it isn't typed).
Out initial idea was to do the following:
Object obj = variables.get( variable );
if( !(obj instanceof Collection<? extends Object>) )
throw ...;
((Collection<? extends Object>)obj).add( data );
But that just produces two warnings instead of one because you can't
use instanceof with wildcards and the following cast is also converting
from untyped to typed.
This disallowed casting concerns me additionally, since even if we
rewrite the code such that we always know a COllection<Object> is
stored in the variable, it doesn't appear to help, since we still need
to do the cast and get the unchecked warning.
Has this type of functionaly truly been deprecated? Or is there someway
to achieve this without gettnig the warning messages?