diff options
author | Christian W. Damus | 2016-02-11 02:48:20 +0000 |
---|---|---|
committer | Gerrit Code Review @ Eclipse.org | 2016-02-12 15:31:41 +0000 |
commit | ecd4928b327f5561364c5068c9ff5f1668e92d13 (patch) | |
tree | 7c34f46cf82a1d65ac753fa92c2a5d55371b8dba /plugins/infra/core/org.eclipse.papyrus.infra.tools/src/org/eclipse/papyrus/infra/tools/util/ClassLoaderHelper.java | |
parent | 751a204d74e15eb2db6b41c937691fc56dcc1252 (diff) | |
download | org.eclipse.papyrus-ecd4928b327f5561364c5068c9ff5f1668e92d13.tar.gz org.eclipse.papyrus-ecd4928b327f5561364c5068c9ff5f1668e92d13.tar.xz org.eclipse.papyrus-ecd4928b327f5561364c5068c9ff5f1668e92d13.zip |
Bug 485220: [Architecture] Provide a more modular architecture
https://bugs.eclipse.org/bugs/show_bug.cgi?id=485220
Factor UI dependencies out of the UML Element Types bundle. This includes
moving some advices that interact with the user into a new
org.eclipse.papyrus.uml.service.types.ui bundle.
Pull up the PasteCommandService and IPasteCommandProvider API into the
Infra Diagram layer where the extension point is defined. Deprecate the
old API in the UML layer.
Introduce a service for participation of languages in CSS styling:
* styling reset actions in the Reset Style command
* access to semantic model classes and properties to make available to CSS
Factor PapyrusObservableValue and cohorts out of the UML Tools bundle
into the Infra Layer for more general reuse and to relieve the
Diagram Infrastructure layer of UML dependencies. The old API remains as
deprecated.
Remove the Infra Diagram Layer dependency on UML Layer for property
testers governing deletion in the diagram. Includes introduction of
a new IGraphicalDeletionHelper OSGi service for delegation of the
determination of whether an element can be deleted from the diagram and
replacement of the XML expression properties
* org.eclipse.papyrus.uml.diagram.common.isSemanticDeletion
* org.eclipse.papyrus.uml.diagram.common.isReadOnly
by
* org.eclipse.papyrus.infra.gmfdiag.common.isSemanticDeletion
* org.eclipse.papyrus.infra.gmfdiag.common.canDelete
(where the latter is the negation of the property that it supersedes)
Extract UML dependencies from the Diagram Outline and CSS Editor bundles.
Remove unused MDTUtil APIs that referenced a UML-specific annotation.
Move the Diagram Infrastructure CSS Palette bundle into the UML layer because it
serves to provide extensions on the Palette Service, which is an overtly
UML-specific capability.
All client APIs for the Properties View are moved from
org.eclipse.papyrus.views.properties bundle to a new
org.eclipse.papyrus.infra.properties.ui bundle. This
includes renaming of:
* extension points
* label-provider contexts
* XWT namespaces
Add an "all UI tests" suite.
Define a componentized hierarchical build layout of the main plug-ins
Change-Id: I43f8f3644857a18b69715f5a2f1da9b1cf286d67
Diffstat (limited to 'plugins/infra/core/org.eclipse.papyrus.infra.tools/src/org/eclipse/papyrus/infra/tools/util/ClassLoaderHelper.java')
-rw-r--r-- | plugins/infra/core/org.eclipse.papyrus.infra.tools/src/org/eclipse/papyrus/infra/tools/util/ClassLoaderHelper.java | 152 |
1 files changed, 152 insertions, 0 deletions
diff --git a/plugins/infra/core/org.eclipse.papyrus.infra.tools/src/org/eclipse/papyrus/infra/tools/util/ClassLoaderHelper.java b/plugins/infra/core/org.eclipse.papyrus.infra.tools/src/org/eclipse/papyrus/infra/tools/util/ClassLoaderHelper.java new file mode 100644 index 00000000000..46751c90c81 --- /dev/null +++ b/plugins/infra/core/org.eclipse.papyrus.infra.tools/src/org/eclipse/papyrus/infra/tools/util/ClassLoaderHelper.java @@ -0,0 +1,152 @@ +/***************************************************************************** + * Copyright (c) 2010 CEA LIST. + * + * 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 + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Camille Letavernier (CEA LIST) camille.letavernier@cea.fr - Initial API and implementation + *****************************************************************************/ +package org.eclipse.papyrus.infra.tools.util; + +import java.util.HashMap; +import java.util.Map; + +import org.eclipse.papyrus.infra.tools.Activator; + +/** + * A Helper class for Class Loading. + * + * @author Camille Letavernier + */ +// This class needs the "BuddyPolicy" set to "dependent" in the Manifest.MF, +// in order to be able to retrieve the classes it loads +// +// This is the org.eclipse.papyrus.infra.tools class loader which is used for loading +// a class, instead of each caller's ClassLoader +// +// Plug-ins using this class should also either set their Buddy-policy to dependent or +// reexport the dependency to oep.infra.tools +public class ClassLoaderHelper { + + /** + * Usually, there are few classes with many different accesses. Using a cache, we can improve + * the performances between 10 and 20 times, with really few memory consumption + */ + private static final Map<String, Class<?>> classes = new HashMap<String, Class<?>>(); + + /** + * Loads the class matching the given className. Exceptions are caught and sent + * to the Logger. + * + * @param className + * The qualified name of the Class to load. + * @return + * The loaded Class, or null if an error occured + */ + public static Class<?> loadClass(String className) { + try { + Class<?> result = classes.get(className); + if (result == null) { + result = Activator.getDefault().getBundle().loadClass(className); + classes.put(className, result); + } + return result; + } catch (ClassNotFoundException ex) { + Activator.log.error(String.format("The class %s doesn't exist", className), ex); //$NON-NLS-1$ + } catch (NullPointerException ex) { + Activator.log.error("Cannot load class " + className, ex); //$NON-NLS-1$ + } + + return null; + } + + /** + * Loads and returns the class denoted by the given className. + * Checks that the loaded class is a subtype of the given Class. + * + * @param className + * The qualified name of the class to be loaded + * @param asSubClass + * The interface or class that the loaded class must implement or extend + * @return + * The loaded class, or null if the class doesn't exist or is invalid. + * In such a case, the exception is logged. + */ + public static <T> Class<? extends T> loadClass(String className, Class<T> asSubClass) { + Class<?> theClass = loadClass(className); + if (theClass == null) { + return null; + } + + try { + Class<? extends T> typedClass = theClass.asSubclass(asSubClass); + return typedClass; + } catch (ClassCastException ex) { + Activator.log.error(String.format("The class %1$s doesn't extend or implement %2$s", className, asSubClass.getName()), ex); //$NON-NLS-1$ + } + + return null; + } + + /** + * Creates a new instance of class denoted by the given className. + * Checks that the instantiated class is a subtype of the given class + * + * @param className + * The qualified name of the class to be instantiated + * @param asSubclass + * The interface or class that the loaded class must implement or extend + * @return + * An instance of the loaded class, or null if a valid instance + * cannot be created. In such a case, the exception is logged. + */ + public static <T> T newInstance(String className, Class<T> asSubclass) { + Class<? extends T> typedClass = loadClass(className, asSubclass); + if (typedClass == null) { + return null; + } + + return newInstance(typedClass); + } + + /** + * Returns a new Instance of the given class + * + * @param className + * The qualified name of the Class to instantiate + * @return + * A new instance of the given class, or null if the class couldn't be + * instantiated + */ + public static Object newInstance(String className) { + return newInstance(loadClass(className)); + } + + /** + * Returns a new Instance of the given class + * + * @param theClass + * The Class to instantiate + * @return + * A new instance of the given class, or null if the class couldn't be + * instantiated + */ + public static <T extends Object> T newInstance(Class<T> theClass) { + if (theClass == null) { + return null; + } + + try { + return theClass.newInstance(); + } catch (IllegalAccessException ex) { + Activator.log.error("Cannot find a valid public constructor for the class " + theClass.getName(), ex); //$NON-NLS-1$ + } catch (InstantiationException ex) { + Activator.log.error(String.format("The class %s cannot be instantiated.", theClass.getName()), ex); //$NON-NLS-1$ + } + + return null; + } +} |