kadrian
kadrian

Reputation: 5009

How to convert between an Object[] and an interface (IProject[]) in Java?

I have an Object[] in Java and want to convert it to IProject[], which is a Java interface (org.eclipse.core.resources.IProject), in order to write a plugin for eclipse.

Is this possible?

Regards

Upvotes: 1

Views: 144

Answers (2)

Tom Anderson
Tom Anderson

Reputation: 47233

You can't convert the array itself - arrays know the type of their slots, so you can't just cast an instance of Object[] to an expression of type IProject[], even if the array happens to contain only instances of IProject (unless you happen to have a variable of type Object[] which actually points to an instance of IProject[]).

Instead, you'll need to make a new array with the same contents:

Object[] objects;
IProject[] projects = new IProject[objects.length];
System.arraycopy(objects, 0, projects, 0, objects.length);

Array stores are dynamically type-checked, so if your Object[] contains any objects which are not instances of IProject, you'll get an ArrayStoreException.

Upvotes: 2

oliholz
oliholz

Reputation: 7507

are the Objects in Object[] instances of IProject ?

see casting Object array to Integer array error

Upvotes: 2

Related Questions