Generics Warnings yet again

R

Roedy Green

Is it possible to write code with this meaning without compiler
warning messages? Where Macro is the name of an interface.

I understand the code is just using a plain Class object, and hence as
no idea if forName actually returned a Class object that implements
Macro. I am using the cast here is the C++ , "trust me" sense.
Ideally I would like the cast to fail if the class does not implement
Macro.

// e.g. "com.mindprod.htmlmacros.Measure"
final Class<Macro> macroClass = ( Class<Macro> ) Class.forName(
packageName + "." + macroName );


[javac] E:\com\mindprod\htmlmacros\LoadCodeToProcessMacro.java:130:
warning: [unchecked] uncheck
cast
[javac] found : java.lang.Class<capture#419 of ?>
[javac] required: java.lang.Class<com.mindprod.htmlmacros.Macro>
[javac] final Class<Macro> macroClass = ( Class<Macro> )
Class.forName( packageName
.." + macroName );


The other way of asking this is, what is the best practice in this
ugly situation?
 
A

Arne Vajhøj

Roedy said:
Is it possible to write code with this meaning without compiler
warning messages? Where Macro is the name of an interface.

I understand the code is just using a plain Class object, and hence as
no idea if forName actually returned a Class object that implements
Macro. I am using the cast here is the C++ , "trust me" sense.
Ideally I would like the cast to fail if the class does not implement
Macro.

// e.g. "com.mindprod.htmlmacros.Measure"
final Class<Macro> macroClass = ( Class<Macro> ) Class.forName(
packageName + "." + macroName );


[javac] E:\com\mindprod\htmlmacros\LoadCodeToProcessMacro.java:130:
warning: [unchecked] uncheck
cast
[javac] found : java.lang.Class<capture#419 of ?>
[javac] required: java.lang.Class<com.mindprod.htmlmacros.Macro>
[javac] final Class<Macro> macroClass = ( Class<Macro> )
Class.forName( packageName
." + macroName );

The other way of asking this is, what is the best practice in this
ugly situation?

Due to type erasure then that cast will never fail.

When you try and do the matching cast on an instance, then it will fail.

Arne
 
D

Daniel Pitts

Roedy said:
Is it possible to write code with this meaning without compiler
warning messages? Where Macro is the name of an interface.

I understand the code is just using a plain Class object, and hence as
no idea if forName actually returned a Class object that implements
Macro. I am using the cast here is the C++ , "trust me" sense.
Ideally I would like the cast to fail if the class does not implement
Macro.

// e.g. "com.mindprod.htmlmacros.Measure"
final Class<Macro> macroClass = ( Class<Macro> ) Class.forName(
packageName + "." + macroName );


[javac] E:\com\mindprod\htmlmacros\LoadCodeToProcessMacro.java:130:
warning: [unchecked] uncheck
cast
[javac] found : java.lang.Class<capture#419 of ?>
[javac] required: java.lang.Class<com.mindprod.htmlmacros.Macro>
[javac] final Class<Macro> macroClass = ( Class<Macro> )
Class.forName( packageName
.." + macroName );


The other way of asking this is, what is the best practice in this
ugly situation?

Look at Class.asSubclass() and the like.
 
R

Roedy Green

Class<?> macroClass = Class.forName(...);
if (!Macro.class.isAssignableFrom(macroClass))
throw SomeSuitableException();

very nice. Errors are gone.

Class<?> macroClass ...

if ( !Macro.class.isAssignableFrom( macroClass ) )
{
throw new ClassNotFoundException( "Coding bug: The
code to process macro " + macroName + " does not implement the Macro
interface." );
}

// This cast will not fail.
return ( Macro ) macroClass.newInstance();

That cast is a true failable cast so now the isAssignableFrom can
removed.

The general rule is, don't try to make generics enforce conditions not
known 100% at compile time, e.g. the properties of dynamically loaded
code, or deserialised streams.
 

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

Similar Threads

Generics ? 14
generics puzzle 57
Class Generics 4
Generics annoyance 18
More Generics warnings. 5
Generics pains 5
Generics 12
Generics in 1.5, nonsense warnings? 4

Members online

Forum statistics

Threads
473,744
Messages
2,569,480
Members
44,900
Latest member
Nell636132

Latest Threads

Top