Re: How to reduce hard-coding IDs [message #604674] |
Mon, 15 February 2010 06:29 |
Prakash G.R. Messages: 621 Registered: July 2009 |
Senior Member |
|
|
On 14/02/10 10:25 PM, Eugene wrote:
> I've always been frustrated by having to include various IDs in my
> eclipse implemenation classes. For example, If I use the wizards
> extension to contribute a new wizard, that wizard gets and ID, which may
> or may not be needed at some point.
>
> I've found that when I change the ID in the plugin.xml for any reason,
> that any other place the ID is listed will get out of sync, and there is
> no refactoring support for this since the IDs are lightweight strings.
>
> I've come up with this approach to extract the IDs from the extension
> registry, I would like your feedback (positive or negative) on this method:
>
> /**
> * Configuration elements in the typical plugin.xml file can specify class,
> * id, and other similar attributes. This method can be used to determine
> the
> * unique ID assigned to a class in the plugin.xml file. However, if the
> same
> * class is assigned multiple unique IDs, then this method will fail and
> throw
> * a RuntimeException.
> * <br><br>
> * Example use: String id = getUniqueID(getClass(), "defaultHandler");
> * <br><br>
> * Lazy loading features are preserved when using this method.
> * * @param clazz the class for which a unique ID should be retrieved.
> * @param specification the name of the attribute whose value is the class
> * @return the unique ID for the class, or null if one is not specified
> * @throws RuntimeException if more than one valid ID is found.
> */
> public static String getUniqueID(Class<?> clazz, String specification) {
> String id = classToIdMap.get(clazz);
> if(id == null) {
> try {
> IExtensionRegistry extensionRegistry = Platform.getExtensionRegistry();
> IExtensionPoint[] extensionPoints = extensionRegistry.getExtensionPoints();
> for(IExtensionPoint extensionPoint : extensionPoints) {
> IConfigurationElement[] configurationElements
> = extensionRegistry.getConfigurationElementsFor(extensionPoint .
> getUniqueIdentifier());
>
> int idCount = 0;
> for(IConfigurationElement configurationElement : configurationElements) {
> String attribute = configurationElement.getAttribute(specification);
>
> String namespaceIdentifier = configurationElement.getNamespaceIdentifier();
> if(attribute != null
> && !namespaceIdentifier.equals("org.eclipse.emf.ecore.editor") // ignore
> this namespace, it causes problems
> && attribute.equals(clazz.getName())) {
> Object object =
> configurationElement.createExecutableExtension(specification );
> if(object != null) {
> Class<?> class1 = object.getClass();
> Class<?> class2 = clazz;
> if(class1.equals(class2)) {
> if(idCount == 0) {
> id = configurationElement.getAttribute("id");
> idCount++;
> }
>
> else {
> throw new RuntimeException("Multiple IDs" +
> " found for " + specification + " " + clazz.getName());
> }
> }
> }
> }
> }
> }
> }
>
> catch (Exception ex) {
> LogUtil.error(ex);
> }
> }
>
> return id;
> }
It would be great if PDE can automagically search and update the
java/text files when the id field in an extension changes. If it doable,
you can file an enhancement request in Bugzilla. cc-ing pde newsgroup
for comments.
- Prakash
Platform UI Team, IBM
Blog <http://blog.eclipse-tips.com>
Twitter <http://www.twitter.com/Eclipse_Tips>
|
|
|
Powered by
FUDForum. Page generated in 0.02744 seconds