Skip to main content



      Home
Home » Language IDEs » Java Development Tools (JDT) » Is one class a superclass of another?
Is one class a superclass of another? [message #200996] Thu, 14 April 2005 20:42 Go to next message
Eclipse UserFriend
I'm trying to write a method that receives a Class object and later tries
to create an object of that class. For sanity check this object must be
derived from another class. Something like that:

class B extends A {};

void method(Class bClass)
{
// sanity check goes here
}

void anotherMethod()
{
method(B.class);
}

With all possible warnings on in 3.1M6 with JDK 5.0 it compiles fine. But
when I try to implement sanity check to see if bClass is actually a
subclass of A, I get all kinds of warnings.

If I use 'if (!bClass.isAssignableFrom(A.class))' I get warning 'Type
safety: The method isAssignableType(Class) belongs to raw type Class.
References to generic type Class<T> should be parametrized.'

If I parametrize method to look like 'void method(Class<A> bClass)', this
warning disappears, but I get an error when I try to invoke method() in
anotherMethod: 'The method method(Class<A>) is not applicable for the
arguments (Class<B>)'.

If instead of isAssignableForm() I use newInstance() and isInstance(), on
newInstance() line it gives me the same warning. Simply put, if I use any
Class method without parametrizing the bClass type, I get a warning. If I
parametrize it, I get an error.

Is there a right solution available or should I just go with the lesser of
two evils and have a warning there?

Thanks
Re: Is one class a superclass of another? [message #201004 is a reply to message #200996] Thu, 14 April 2005 20:45 Go to previous messageGo to next message
Eclipse UserFriend
> If I use 'if (!bClass.isAssignableFrom(A.class))' I get warning 'Type
> safety: The method isAssignableType(Class) belongs to raw type Class.
> References to generic type Class<T> should be parametrized.'

Note to self: I had it backwards, 'if (!A.class.isAssignableFrom(bClass))'
didn't complain about anything. Whether it works, I don't know yet.
Re: Is one class a superclass of another? [message #201012 is a reply to message #201004] Fri, 15 April 2005 02:47 Go to previous messageGo to next message
Eclipse UserFriend
I think that "isAssignableFrom() is the right way to check subclassing".
I used it and it works.
If you use generics you can of course enforce subclassing in a more
clean way.

Genady Beryozkin
http://www.genady.net/



Aare Tali wrote:

>
>> If I use 'if (!bClass.isAssignableFrom(A.class))' I get warning 'Type
>> safety: The method isAssignableType(Class) belongs to raw type Class.
>> References to generic type Class<T> should be parametrized.'
>
>
> Note to self: I had it backwards, 'if
> (!A.class.isAssignableFrom(bClass))' didn't complain about anything.
> Whether it works, I don't know yet.
>
Re: Is one class a superclass of another? [message #203167 is a reply to message #201012] Wed, 11 May 2005 13:38 Go to previous message
Eclipse UserFriend
Because java.lang.Class is now generic, the raw form is best avoided. The
simplest rewrite is to make the sanity method look like:

void method(Class<?> bclass) { ... }

But you could even write this to provide "static" type checking:

void method2(Class<? extends A> bclass) { /* do nothing */ }

In your example this will work, because the type of B.class is now Class<B>.
This works well with class literals which can be strongly typed, but what
about classes that are dynamically loaded? For example: the type of
Class.forName("B") is Class<?>, so this invocation would fail at compile
time:

method2(Class.forName("B"))

Instead you could use the new "asSubclass" method:

method2(Class.forName("B").asSubclass(A.class));

This allows you to have stronger typing at compile time, then get rid of the
sanity method, and replace all references to the raw Class with Class<?
extends A>. This has the added advantage of not having to cast the result
Class.newInstance(). For example:

Class<? extends A> clazz = Class.forName("B").asSubclass(A.class);
A newobj = clazz.newInstance();


"Genady" <eclipse@genady.org> wrote in message
news:d3nkur$ot9$1@news.eclipse.org...
>I think that "isAssignableFrom() is the right way to check subclassing". I
>used it and it works.
> If you use generics you can of course enforce subclassing in a more clean
> way.
>
> Genady Beryozkin
> http://www.genady.net/
>
>
>
> Aare Tali wrote:
>
>>
>>> If I use 'if (!bClass.isAssignableFrom(A.class))' I get warning 'Type
>>> safety: The method isAssignableType(Class) belongs to raw type Class.
>>> References to generic type Class<T> should be parametrized.'
>>
>>
>> Note to self: I had it backwards, 'if
>> (!A.class.isAssignableFrom(bClass))' didn't complain about anything.
>> Whether it works, I don't know yet.
>>
Previous Topic:Modification the CompilationUnit Header via AST
Next Topic:requesting help with eclipse and using resources
Goto Forum:
  


Current Time: Sun Nov 09 04:16:20 EST 2025

Powered by FUDForum. Page generated in 0.03502 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top