Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2006

Tip: Looking for answers? Try searching our database.

How to pass class type argument?

Thread view: 
Allen - 16 Oct 2006 08:44 GMT
In my PluginRegisterManager class, there is a Vector registerVector
which stores all kinds of registered handlers as Object type. So I
need to get specified kind of handlers, i.e.

PluginFileHandler[] handlers = manager.getPluginFileHandler();

Because registerVector is defined as:

Vector<Object> registerVector;

Then if I want to get paint handlers, I have to write exactly the same
codes as getPluginFileHandler() except check instanceof
PluginPaintHandler.

How to use class type as argument? If this is feasible, I just need
only one function named getHandler(<classtype>).
Tom Forsmo - 17 Oct 2006 11:57 GMT
> In my PluginRegisterManager class, there is a Vector registerVector
> which stores all kinds of registered handlers as Object type. So I
[quoted text clipped - 12 lines]
> How to use class type as argument? If this is feasible, I just need
> only one function named getHandler(<classtype>).

As far as I understand it, you are storing many different kind of
handlers in a vector and you want to be able to retrieve those handlers
without writing separate getHanderXX() functions for each type of
handler, correct.

The easy answer is you can use generics for this. All classes need to
have a common super class or implemented interface and then you specify
the that common class or interface as the type for the vector and the
get method. e.g.

class PluginHandler  {}
class FilePluginHandler extends PluginHandler {}

Vector<? extends PluginHandler> registerVector

or just

interface PluginHandler {}
Vector<PluginHandler> registerVector;

the method is in any case:

<PluginHandler> getHandler()

The difficult part is how do you know what data is being returned to
you, since the vector can store any type. To solve that, every handler
should implement some common methods specified in the interface or
superclass. And make sure you only use the methods defined in the common
interface to handle the handlers. That way it does not matter which type
of handler it is, because the interface will take care of it for you.
This is what is known as programming to interfaces. Have a look at the
List interface (in the API doc) and then at all the specific
implementations of the List interface. It will help you understand it
better.

tom
Allen - 18 Oct 2006 04:16 GMT
Thank you, Tom.

To use instance as the common method argument, I have the easiest way
to do it.

Object[] getHandler(Class cls);

For example, to get all PluginFileAdapter, just need an instance
PluginFileAdapter adapter = new PluginFileAdapter();
Object[] fileAdapters = manager.getHandler(adapter.getClass);

I use these lines to check type:

Iterator itr = registerVector.iterator();
...
Object entry = iterator.next();
if (cls == entry.getClass()) {
 // add to returned array
}

The only inconvenient thing is to have an PluginFileAdapter instance.

> > In my PluginRegisterManager class, there is a Vector registerVector
> > which stores all kinds of registered handlers as Object type. So I
[quoted text clipped - 49 lines]
>
> tom
Tom Forsmo - 18 Oct 2006 10:51 GMT
> Thank you, Tom.
>
> To use instance as the common method argument, I have the easiest way
> to do it.
>
> Object[] getHandler(Class cls);

I would recommend using an interface (with a common set of methods) as
the data type instead of a class, such as done with Collections, e.g.

Handlers[] getHandler(Class cls);

> For example, to get all PluginFileAdapter, just need an instance
> PluginFileAdapter adapter = new PluginFileAdapter();
[quoted text clipped - 10 lines]
>
> The only inconvenient thing is to have an PluginFileAdapter instance.

I am not sure I understand what you mean here. Is the problem that you
in addition to having a set of handlers, also have an an adapter in the
vector? How is it a problem?

tom


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.