Abstract JavaFileEditorInput changes between Eclipse 3.1 and Eclipse 3.2

From Aptana Development

This is definitely not recommended as a long-term solution, but in a pinch, this helps manage the difference between Eclipse 3.1 and Eclipse 3.2. Eclipse 3.2 introduced the concept of IFileStorage, which abstracts file systems. This is great, except they changed the constructor on JavaFileEditorInput, which means you need to build a version for 3.1 and one for 3.2. With reflection, we make that easy:

static
{
   String version = System.getProperty("osgi.framework.version"); //$NON-NLS-1$
   if (version != null && version.startsWith("3.2")) //$NON-NLS-1$
   {
       inEclipse32 = true;
   }
}
/**
 * Creates a new JavaFileEditorInput, abstracting the difference in constructors between Eclipse 3.1 and Eclipse 3.2
 * @param file the file to create
 * @return a new JavaFileEditorInput
 */
public static IEditorInput createJavaFileEditorInput(File file)
{
   IEditorInput input = null;
   try
   {
       if(inEclipse32)
       {
           BundleClassLoader bcl = new BundleClassLoader();
           Bundle bundle = Platform.getBundle("org.eclipse.core.filesystem");
           bcl.addBundle(bundle);
           Class efs = bcl.loadClass("org.eclipse.core.filesystem.EFS");
           Method getLocalFileSystem = efs.getDeclaredMethod("getLocalFileSystem", new Class[] {});
           Object fs = getLocalFileSystem.invoke(null, new Object[] {});

           Class localFileSystemClass = bcl.loadClass("org.eclipse.core.filesystem.IFileSystem");
           Method fromLocalFile = localFileSystemClass.getDeclaredMethod("fromLocalFile", new Class[] {File.class});
           Object localFile = fromLocalFile.invoke(fs, new Object[] {file});

           Class iFileStoreClass = bcl.loadClass("org.eclipse.core.filesystem.IFileStore");

           Class jfei = Class.forName("org.eclipse.ui.internal.editors.text.JavaFileEditorInput"); //$NON-NLS-1$
           Constructor constructor = jfei.getDeclaredConstructor(new Class[] {iFileStoreClass});
           input = (IEditorInput)constructor.newInstance(new Object[] {localFile});
       }
       else
       {
           Class jfei = Class.forName("org.eclipse.ui.internal.editors.text.JavaFileEditorInput"); //$NON-NLS-1$
           Constructor constructor = jfei.getDeclaredConstructor(new Class[] {File.class});
           input = (IEditorInput)constructor.newInstance(new Object[] {file});
       }
   }
   catch(Exception e)
   {
       IdeLog.logError(AptanaCorePlugin.getDefault(), "Unable to create new JavaFileEditorInput", e);
   }
   return input;
}

Aptana Developer Home