From e7ed31884ab79edf135c9540efdb3409f251adfb Mon Sep 17 00:00:00 2001 From: Paul Elder Date: Wed, 17 Apr 2013 09:24:24 -0400 Subject: 398925: After prolonged usage Eclipse becomes unusable : java.lang.Object cannot be cast to org.eclipse.e4.core.commands.EHandlerService Added defensive code that checks for unexpected return values from IEvaluationContext.getVariable, logs a message and takes a reasonable default action. Change-Id: I6eb4e2db49f4f465ce957f87211828a150879861 --- .../workbench/swt/PartRenderingEngine.java | 3 +- .../org/eclipse/ui/internal/menus/MenuHelper.java | 34 ++++++++++++++++++---- 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/bundles/org.eclipse.e4.ui.workbench.swt/src/org/eclipse/e4/ui/internal/workbench/swt/PartRenderingEngine.java b/bundles/org.eclipse.e4.ui.workbench.swt/src/org/eclipse/e4/ui/internal/workbench/swt/PartRenderingEngine.java index 7dc829271f5..76d18ec1d33 100644 --- a/bundles/org.eclipse.e4.ui.workbench.swt/src/org/eclipse/e4/ui/internal/workbench/swt/PartRenderingEngine.java +++ b/bundles/org.eclipse.e4.ui.workbench.swt/src/org/eclipse/e4/ui/internal/workbench/swt/PartRenderingEngine.java @@ -876,7 +876,8 @@ public class PartRenderingEngine implements IPresentationEngine { IEclipseContext lclContext = ctxt.getContext(); if (lclContext != null) { IEclipseContext parentContext = lclContext.getParent(); - IEclipseContext child = parentContext.getActiveChild(); + IEclipseContext child = parentContext != null ? parentContext + .getActiveChild() : null; if (child == lclContext) { child.deactivate(); } diff --git a/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/menus/MenuHelper.java b/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/menus/MenuHelper.java index 22a863db9d0..8eea2f3f302 100644 --- a/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/menus/MenuHelper.java +++ b/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/menus/MenuHelper.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2010, 2012 IBM Corporation and others. + * Copyright (c) 2010, 2013 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -237,10 +237,15 @@ public class MenuHelper { Expression visWhen = new Expression() { @Override public EvaluationResult evaluate(IEvaluationContext context) { - EHandlerService service = (EHandlerService) context - .getVariable(EHandlerService.class.getName()); - ICommandService commandService = (ICommandService) context - .getVariable(ICommandService.class.getName()); + EHandlerService service = getFromContext(context, + EHandlerService.class); + ICommandService commandService = getFromContext(context, + ICommandService.class); + if (service == null || commandService == null) { + WorkbenchPlugin + .log("Could not retrieve EHandlerService or ICommandService from context evaluation context."); //$NON-NLS-1$ + return EvaluationResult.FALSE; + } Command c = commandService.getCommand(commandId); ParameterizedCommand generateCommand = ParameterizedCommand .generateCommand(c, Collections.EMPTY_MAP); @@ -274,6 +279,25 @@ public class MenuHelper { return null; } + /** + * Do a type-safe extraction of an object from the evalation context + * + * @param context + * the evaluation context + * @param expectedType + * the expected type + * @return an object of the expected type or null + * @throws NullPointerException + * if either argument is null + */ + protected static T getFromContext(IEvaluationContext context, Class expectedType) { + if (context == null || expectedType == null) { + throw new NullPointerException(); + } + final Object rawValue = context.getVariable(expectedType.getName()); + return (expectedType.isInstance(rawValue)) ? expectedType.cast(rawValue) : null; + } + /* * Support Utilities */ -- cgit v1.2.3