Tuesday, March 16, 2010

IFile.getContentDescription() returns null on files from the workbench. Advice?

When Bioclipse reads filed its workspace, it used IFile.getContentDescription() in version 2.0 and 2.2. However, I now note that unit tests that use this method fail where they used to work in earlier versions. Instead of returning something, I get a null. An example unit test looks like:
  propane = cdk.loadMolecule(path);
  Assert.assertTrue(propane.getResource() instanceof IFile);
  IFile resource = (IFile)propane.getResource();
  IContentType type = resource.getContentDescription().getContentType();
  IChemFormat format = cdk.determineFormat(type);
  Assert.assertEquals(MDLV2000Format.getInstance(), format);
This test uses the getContentDescription() to get a content description and converts it to a CDK library specific format type.

The JavaDoc lists this methods as more efficient alternative:
    Calling this method produces a similar effect as calling getDescriptionFor(getContents(), getName(), IContentDescription.ALL) on IContentTypeManager, but provides better opportunities for improved performance.

As it used to work, I am considering the option it is a bug. But at the same time, maybe best practices have change? Should I keep using this method, explore the cause, perhaps file a bug report, or start using getDescriptionFor(getContents(), getName(), IContentDescription.ALL)?


  1. Technically it could always return null. You'll probably want to fall back on IContentTypeMatcher#findContentTypesFor(String) if that happens in production code, but you should probably test that the resource is #isAccessible() in your test as well.

  2. Thanx for the tip! I will look into that API.