How to pass class type argument?

A

Allen

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>).
 
T

Tom Forsmo

Allen said:
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>).

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
 
A

Allen

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.

Tom said:
Allen said:
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>).

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
 
T

Tom Forsmo

Allen said:
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();
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.

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
 

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

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top