1

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

kadrian
  • 4,401
  • 8
  • 35
  • 60

2 Answers2

2

are the Objects in Object[] instances of IProject ?

see casting Object array to Integer array error

Community
  • 1
  • 1
oliholz
  • 7,357
  • 2
  • 41
  • 77
2

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.

Tom Anderson
  • 44,913
  • 16
  • 86
  • 129