Skip to content

Fix ClassCastException when creating new class from Direct Menu Item#1993

Closed
Copilot wants to merge 1 commit intomasterfrom
copilot/fix-classcastexception-in-menu
Closed

Fix ClassCastException when creating new class from Direct Menu Item#1993
Copilot wants to merge 1 commit intomasterfrom
copilot/fix-classcastexception-in-menu

Conversation

Copy link
Contributor

Copilot AI commented Oct 7, 2025

Problem

When clicking on the "Class URI" link for a Direct Menu Item in the E4 application model editor to create a new class, a ClassCastException was thrown:

java.lang.ClassCastException: Cannot cast java.lang.String to org.eclipse.jdt.core.IPackageFragmentRoot
	at java.base/java.lang.Class.cast(Class.java:4067)
	at org.eclipse.core.databinding.UpdateStrategy$DefaultConverter.convert(UpdateStrategy.java:682)
	at org.eclipse.core.databinding.UpdateValueStrategy.convert(UpdateValueStrategy.java:1)
	at org.eclipse.core.databinding.ValueBinding.doUpdate(ValueBinding.java:163)

This prevented users from creating new handler classes through the application model editor UI.

Root Cause

The issue was in the data binding configuration for the "Source Folder" text field in AbstractNewClassPage.java. The target-to-model UpdateValueStrategy was missing a converter to properly transform the String value from the Text widget into an IPackageFragmentRoot object. When the databinding framework attempted to perform this conversion, it fell back to the default converter which attempted a direct cast, resulting in the exception.

Solution

This PR adds a new StringToPackageFragmentRootConverter class that properly converts String paths to IPackageFragmentRoot objects by:

  • Iterating through all Java projects in the workspace
  • Comparing the relative path representation of each package fragment root
  • Returning the matching IPackageFragmentRoot or null if not found

The converter is now used in the target-to-model UpdateValueStrategy, ensuring proper type conversion instead of attempting a direct cast.

Changes

  • Added StringToPackageFragmentRootConverter class extending Converter<String, IPackageFragmentRoot>
  • Updated the data binding configuration to use .setConverter(new StringToPackageFragmentRootConverter(fWorkspaceRoot))

The implementation follows the existing pattern used by similar converters in the same class (StringToPackageFragmentConverter, PackageFragmentRootToStringConverter) and properly handles edge cases like null/empty strings and conversion failures.

Testing

After this fix, users should be able to:

  1. Open an E4 application model editor
  2. Navigate to a Direct Menu Item
  3. Click on the "Class URI" link
  4. See the new class wizard open without exceptions
  5. Successfully create new handler classes

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • repo.eclipse.org
    • Triggering command: /usr/lib/jvm/temurin-17-jdk-amd64/bin/java --enable-native-access=ALL-UNNAMED -classpath /usr/share/apache-maven-3.9.11/boot/plexus-classworlds-2.9.0.jar -Dclassworlds.conf=/usr/share/apache-maven-3.9.11/bin/m2.conf -Dmaven.home=/usr/share/apache-maven-3.9.11 -Dlibrary.jansi.path=/usr/share/apache-maven-3.9.11/lib/jansi-native -Dmaven.multiModuleProjectDirectory=/home/REDACTED/work/eclipse.pde/eclipse.pde org.codehaus.plexus.classworlds.launcher.Launcher clean compile (dns block)
    • Triggering command: /usr/lib/jvm/temurin-17-jdk-amd64/bin/java --enable-native-access=ALL-UNNAMED -classpath /usr/share/apache-maven-3.9.11/boot/plexus-classworlds-2.9.0.jar -Dclassworlds.conf=/usr/share/apache-maven-3.9.11/bin/m2.conf -Dmaven.home=/usr/share/apache-maven-3.9.11 -Dlibrary.jansi.path=/usr/share/apache-maven-3.9.11/lib/jansi-native -Dmaven.multiModuleProjectDirectory=/home/REDACTED/work/eclipse.pde/eclipse.pde org.codehaus.plexus.classworlds.launcher.Launcher clean compile -pl e4tools/bundles/org.eclipse.e4.tools -am (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

This section details on the original issue you should resolve

<issue_title>java.lang.ClassCastException: Cannot cast java.lang.String to org.eclipse.jdt.core.IPackageFragmentRoot</issue_title>
<issue_description>If I click on the Class URI link for a Direct Menu item in the application model to create a new class I receive the following exception.

Image

java.lang.ClassCastException: Cannot cast java.lang.String to org.eclipse.jdt.core.IPackageFragmentRoot
at java.base/java.lang.Class.cast(Class.java:4067)
at org.eclipse.core.databinding.UpdateStrategy$DefaultConverter.convert(UpdateStrategy.java:682)
at org.eclipse.core.databinding.UpdateStrategy.convert(UpdateStrategy.java:655)
at org.eclipse.core.databinding.UpdateValueStrategy.convert(UpdateValueStrategy.java:1)
at org.eclipse.core.databinding.ValueBinding.lambda$0(ValueBinding.java:177)
at org.eclipse.core.databinding.Binding.lambda$3(Binding.java:193)
at org.eclipse.core.databinding.observable.Realm$1.run(Realm.java:155)
at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:47)
at org.eclipse.core.databinding.observable.Realm.safeRun(Realm.java:159)
at org.eclipse.core.databinding.observable.Realm.exec(Realm.java:177)
at org.eclipse.core.databinding.Binding.execAfterDisposalCheck(Binding.java:191)
at org.eclipse.core.databinding.ValueBinding.doUpdate(ValueBinding.java:163)
at org.eclipse.core.databinding.ValueBinding.validateTargetToModel(ValueBinding.java:240)
at org.eclipse.core.databinding.ValueBinding.postInit(ValueBinding.java:104)
at org.eclipse.core.databinding.Binding.init(Binding.java:82)
at org.eclipse.core.databinding.DataBindingContext.bindValue(DataBindingContext.java:201)
at org.eclipse.e4.internal.tools.wizards.classes.AbstractNewClassPage.createControl(AbstractNewClassPage.java:252)
at org.eclipse.jface.wizard.Wizard.createPageControls(Wizard.java:179)
at org.eclipse.jface.wizard.WizardDialog.createPageControls(WizardDialog.java:744)
at org.eclipse.jface.wizard.WizardDialog.createContents(WizardDialog.java:637)
at org.eclipse.jface.window.Window.create(Window.java:431)
at org.eclipse.jface.dialogs.Dialog.create(Dialog.java:1088)
at org.eclipse.jface.window.Window.open(Window.java:788)
at org.eclipse.e4.tools.emf.editor3x.extension.HandlerContributionEditor.createOpen(HandlerContributionEditor.java:60)
at org.eclipse.e4.tools.emf.editor3x.extension.HandlerContributionEditor.createOpen(HandlerContributionEditor.java:122)
at org.eclipse.e4.tools.emf.editor3x.extension.HandlerContributionEditor.createOpen(HandlerContributionEditor.java:49)
at org.eclipse.e4.tools.emf.ui.internal.common.component.ControlFactory$12.widgetSelected(ControlFactory.java:859)
at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:286)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:91)
at org.eclipse.swt.widgets.Display.sendEvent(Display.java:5884)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1656)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1682)
at org.eclipse.swt.widgets.Widget.sendSelectionEvent(Widget.java:1832)
at org.eclipse.swt.widgets.Link.gtk_button_release_event(Link.java:426)
at org.eclipse.swt.widgets.Widget.windowProc(Widget.java:2624)
at org.eclipse.swt.widgets.Control.windowProc(Control.java:6868)
at org.eclipse.swt.widgets.Display.windowProc(Display.java:6191)
at org.eclipse.swt.internal.gtk3.GTK3.gtk_main_do_event(Native Method)
at org.eclipse.swt.widgets.Display.eventProc(Display.java:1619)
at org.eclipse.swt.internal.gtk3.GTK3.gtk_main_iteration_do(Native Method)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:4537)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$5.run(PartRenderingEngine.java:1147)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:339)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:1038)
at org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:153)
at org.eclipse.ui.internal.Workbench.lambda$3(Workbench.java:677)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:339)
at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:583)
at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:173)
at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:185)
at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:219)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:149)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:115)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:467)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(E...

Fixes #1992

💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copilot AI changed the title [WIP] Fix ClassCastException when creating new class from menu Fix ClassCastException when creating new class from Direct Menu Item Oct 7, 2025
Copilot AI requested a review from vogella October 7, 2025 14:37
@vogella vogella force-pushed the copilot/fix-classcastexception-in-menu branch from 29e0801 to 865fc4f Compare October 7, 2025 15:38
Creating a new class via the "Class URI" link in the E4 application
model
editor threw a ClassCastException due to an invalid type conversion in
the
data binding configuration of AbstractNewClassPage.java.

The target-to-model UpdateValueStrategy was missing a converter to
transform
the String value from the text field into an IPackageFragmentRoot,
leading
to a failed cast by the default converter.

This change introduces StringToPackageFragmentRootConverter to properly
map String paths to IPackageFragmentRoot instances and integrates it
into
the data binding setup. Users can now open the new class wizard and
create
handler classes without errors.
@vogella vogella force-pushed the copilot/fix-classcastexception-in-menu branch from 865fc4f to f4a3bee Compare October 7, 2025 15:39
@github-actions
Copy link

github-actions bot commented Oct 7, 2025

Test Results

  471 files   -   281    471 suites   - 281   47m 58s ⏱️ - 1m 5s
3 611 tests ±    0  3 555 ✅  -     1   54 💤 ± 0  1 ❌ ±0  1 🔥 +1 
5 533 runs   - 5 106  5 416 ✅  - 5 066  115 💤  - 41  1 ❌ ±0  1 🔥 +1 

For more details on these failures and errors, see this check.

Results for commit f4a3bee. ± Comparison against base commit b584f1d.

@vogella vogella closed this Oct 7, 2025
@vogella vogella deleted the copilot/fix-classcastexception-in-menu branch October 7, 2025 19:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

java.lang.ClassCastException: Cannot cast java.lang.String to org.eclipse.jdt.core.IPackageFragmentRoot

2 participants