Bug 78168 Cannot get preferences when running 'headless' (i.e. through Ant)
I'm a member of the Rational Studio Architect team and have been tasked with ensuring that our transformations can be run through Ant in headless mode. I'm having a problem with our "Uml to Java" transform because it wants to use the code 'templates' defined in the JDT's prefstore when performing its code generation (reasonable, IMO). It does this by trying to use the JDT's 'CodeGeneration' class. However, the jdt.ui plugin (the owner of CodeGeneration class) fails to load. The reason is that the JavaWorkBenchAdapter creates a new instance of the JavaElementImageProvider which has initializer code that does a 'getWorkbench.getSharedImages()' which, through AbstractUIPlugin, calls 'PlatformUI.getWorkbench()' which, in turn, throws an 'IllegalStateException'...we're boned. <soapbox on> BTW, a quick hack shows that out of ALL of eclipse only 4 plugins fail to load in a headless environment: org.eclipse.cdt.debug.ui org.eclipse.cdt.ui org.eclipse.jdt.ui org.eclipse.team.cvs.ui For performance reasons(since plugins load during class instantiations) I'd expect that we'd want the Plugin initialization to be as lightweight as possible. Perhaps changing to a philosophy where ALL plugins (including UI) should at least LOAD headless would not only solve this issue but increase performance as well. Note that in this case we certainly don't need 'shared images' so this initialization is wasted on us. Certainly if we load a UI plugin headless and then attempt to do GUI stuff (i.e. displaying a dialog...) then we shoot ourselves in the foot (at least...;-) and pay the price. <soapbox off>
org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/JavaElementAdapterFactory.java
}
				return null;
			case IJavaElement.PACKAGE_FRAGMENT_ROOT:
			case IJavaElement.JAVA_PROJECT:
			case IJavaElement.JAVA_MODEL:
				return element.getResource();
			default:
				return null;
		}		
    }
    /*
     * @see org.eclipse.ui.IContributorResourceAdapter#getAdaptedResource(org.eclipse.core.runtime.IAdaptable)
     */
    public IResource getAdaptedResource(IAdaptable adaptable) {
    	IJavaElement je= getJavaElement(adaptable);
    	if (je != null)
    		return getResource((IJavaElement)adaptable);
    	return null;
    }
    
	private IJavaElement getJavaElement(Object element) {
		if (element instanceof IJavaElement)
			return (IJavaElement)element;
		if (element instanceof IClassFileEditorInput)
			return ((IClassFileEditorInput)element).getClassFile().getPrimaryElement();
		return null;
	}
	
	private IResource getProject(IJavaElement element) {
		return element.getJavaProject().getProject();