Skip to content

Commit d7659ef

Browse files
committed
Fix ClassCastException when creating new class from Direct Menu Item
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.
1 parent b66c8ab commit d7659ef

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

e4tools/bundles/org.eclipse.e4.tools/src/org/eclipse/e4/internal/tools/wizards/classes/AbstractNewClassPage.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ public void createControl(Composite parent) {
253253
WidgetProperties.text().observe(t),
254254
BeanProperties.value(FRAGMENT_ROOT, IPackageFragmentRoot.class).observe(clazz),
255255
new UpdateValueStrategy<String, IPackageFragmentRoot>()
256+
.setConverter(new StringToPackageFragmentRootConverter(fWorkspaceRoot))
256257
.setBeforeSetValidator(new PFRootValidator()),
257258
UpdateValueStrategy.create(new PackageFragmentRootToStringConverter()));
258259

@@ -585,4 +586,37 @@ public IPackageFragment convert(String fromObject) {
585586

586587
}
587588
}
589+
590+
static class StringToPackageFragmentRootConverter extends Converter<String, IPackageFragmentRoot> {
591+
592+
private final IWorkspaceRoot workspaceRoot;
593+
594+
public StringToPackageFragmentRootConverter(IWorkspaceRoot workspaceRoot) {
595+
super(String.class, IPackageFragmentRoot.class);
596+
this.workspaceRoot = workspaceRoot;
597+
}
598+
599+
@Override
600+
public IPackageFragmentRoot convert(String fromObject) {
601+
if (fromObject == null || fromObject.isEmpty()) {
602+
return null;
603+
}
604+
605+
try {
606+
IJavaModel javaModel = JavaCore.create(workspaceRoot);
607+
// Try to find the package fragment root by its path
608+
for (IJavaProject javaProject : javaModel.getJavaProjects()) {
609+
for (IPackageFragmentRoot root : javaProject.getPackageFragmentRoots()) {
610+
if (root.getPath().makeRelative().toString().equals(fromObject)) {
611+
return root;
612+
}
613+
}
614+
}
615+
} catch (JavaModelException e) {
616+
// Return null if conversion fails
617+
}
618+
619+
return null;
620+
}
621+
}
588622
}

0 commit comments

Comments
 (0)