Skip to content

Commit 407a403

Browse files
ptzieglerHeikoKlare
authored andcommitted
Throw ERROR_UNSUPPORTED_FORMAT when SVGRasterizer could not be loaded
Trying to load an SVG while the JSVG rasterizer is not in the classpath throws a ServiceConfigurationError. This error should be caught and transformed into an SWTError, as specified by the contract of the ImageLoader. By catching the ServiceConfigurationError, the RASTERIZER field is initialized with NULL. In this state, calling any method of the SVGFileFormat class automatically throws the expected SWTError. To reproduce, load an SVG while `org.eclipse.swt.svg` is in the classpath, but `com.github.weisj.jsvg` is not. This call should fail with a: > Provider org.eclipse.swt.svg.JSVGRasterizer could not be instantiated Due to a NoClassDefFoundError when trying to load the JSVGRasterizer class, caused by the unresolvable dependency.
1 parent ad438ae commit 407a403

File tree

1 file changed

+12
-2
lines changed
  • bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal/image

1 file changed

+12
-2
lines changed

bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal/image/SVGFileFormat.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,18 @@
3131
public class SVGFileFormat extends FileFormat {
3232

3333
/** The instance of the registered {@link SVGRasterizer}. */
34-
private static final SVGRasterizer RASTERIZER = ServiceLoader
35-
.load(SVGRasterizer.class, SVGFileFormat.class.getClassLoader()).findFirst().orElse(null);
34+
private static final SVGRasterizer RASTERIZER;
35+
36+
static {
37+
SVGRasterizer rasterizer = null;
38+
try {
39+
rasterizer = ServiceLoader
40+
.load(SVGRasterizer.class, SVGFileFormat.class.getClassLoader()).findFirst().orElse(null);
41+
} catch (ServiceConfigurationError e) {
42+
// rasterizer not in classpath or could not be instantiated
43+
}
44+
RASTERIZER = rasterizer;
45+
}
3646

3747
@Override
3848
boolean isFileFormat(LEDataInputStream stream) throws IOException {

0 commit comments

Comments
 (0)