Friday 15 June 2012

Can't cast or instanceof to true from Object to IProject || Java Eclipse Plugin-Development -



Can't cast or instanceof to true from Object to IProject || Java Eclipse Plugin-Development -

why can not cast content array of type object, eclipse framework method getting selection of bundle explorer returns, iproject? instanceof returns false , casting without check causes exception. debugger shows content of object[]-array have instance-variables , of iproject-type:

private list<iproject> getvalidselectedprojects(iworkbenchwindow window) { final iselectionservice service = window.getselectionservice(); final istructuredselection structured = (istructuredselection) service.getselection("org.eclipse.jdt.ui.packageexplorer"); if (structured == null){ homecoming null; } else { final object[] selectedprojects = structured.toarray(); arraylist<iproject> validatedprojects = new arraylist<iproject>(); (final object projectobj : selectedprojects){ if (projectobj instanceof iproject){ iproject project = (iproject) projectobj; //do } //rest of method; returning list of validated projects

i tried how convert between object[] , interface (iproject[]) in java?

but there exception @ line:

system.arraycopy(objectarray, 0, projectarray, 0, objectarray.length);

user interface objects returned getselection() not straight instances of iproject, ifile or iresource.

to iproject ... utilize iadaptermanager:

iproject project = (iproject)platform.getadaptermanager().getadapter(projectobj, iproject.class);

sometimes object not provide adapter straight iproject, seek more general iresource:

iresource resource = (iresource)platform.getadaptermanager().adapt(projectobj, iresource.class); if (resource instanceof iproject) { iproject project = (iproject)resource; ... code }

java eclipse casting eclipse-plugin instanceof

No comments:

Post a Comment