From 8c6d3c398dba7427cf2ed2299379735237bd8c26 Mon Sep 17 00:00:00 2001 From: Michael Keppler Date: Thu, 7 Dec 2023 19:23:31 +0100 Subject: [PATCH] ClassCastException in property page When a Java project sub element is selected, the property page registration in plugin.xml wants to show the properties of the containing project, but the Java code doesn't adapt such sub elements to their project. java.lang.ClassCastException: class org.eclipse.jdt.internal.core.CompilationUnit cannot be cast to class org.eclipse.core.resources.IProject (org.eclipse.jdt.internal.core.CompilationUnit is in unnamed module of loader org.eclipse.osgi.internal.loader.EquinoxClassLoader @1b446f3e; org.eclipse.core.resources.IProject is in unnamed module of loader org.eclipse.osgi.internal.loader.EquinoxClassLoader @2e94e7e4) at org.moreunit.properties.MoreUnitPropertyPage.getJavaProject(MoreUnitPropertyPage.java:133) --- .../moreunit/properties/MoreUnitPropertyPage.java | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/org.moreunit.plugin/src/org/moreunit/properties/MoreUnitPropertyPage.java b/org.moreunit.plugin/src/org/moreunit/properties/MoreUnitPropertyPage.java index 3af91ff1..ab770f2e 100644 --- a/org.moreunit.plugin/src/org/moreunit/properties/MoreUnitPropertyPage.java +++ b/org.moreunit.plugin/src/org/moreunit/properties/MoreUnitPropertyPage.java @@ -3,6 +3,8 @@ import java.io.IOException; import org.eclipse.core.resources.IProject; +import org.eclipse.core.runtime.Adapters; +import org.eclipse.core.runtime.IAdaptable; import org.eclipse.jdt.core.IJavaProject; import org.eclipse.jdt.core.JavaCore; import org.eclipse.jface.preference.IPreferenceStore; @@ -126,11 +128,15 @@ private Composite fixesSecondTabStyle(Composite tab) private IJavaProject getJavaProject() { - if(getElement() instanceof IJavaProject) + IAdaptable selection = getElement(); + if(selection instanceof IJavaProject) { - return (IJavaProject) getElement(); + return (IJavaProject) selection; } - return JavaCore.create((IProject) getElement()); + // when files are selected, they need to be adapted to their project + // first (see enabledWhen clause of plugin.xml) + IProject project = Adapters.adapt(selection, IProject.class); + return JavaCore.create(project); } private void handleCheckboxSelectionChanged()