Skip to content

Implement java.lang.AutoCloseable in class org.eclipse.swt.graphics.GC #955

@tobiasmelcher

Description

@tobiasmelcher

I suggest to implement the java.lang.AutoCloseable interface in org.eclipse.swt.graphics.GC so that the try-with-resources Java feature can be used.

Here a concrete example in org.eclipse.jface.dialogs.Dialog where the GC class is used.

protected void initializeDialogUnits(Control control) {
	// Compute and store a font metric
	GC gc = new GC(control);
	gc.setFont(JFaceResources.getDialogFont());
	fontMetrics = gc.getFontMetrics();
	gc.dispose();
}

gc.dispose() is not called when JFaceResources.getDialogFont() throws an exception.

Wouldn't it be much nicer if the try-with-resource approach would be used here:

protected void initializeDialogUnits(Control control) {
	// Compute and store a font metric
	try (GC gc = new GC(control)) {
	  gc.setFont(JFaceResources.getDialogFont());
	  fontMetrics = gc.getFontMetrics();
	}
}

Did you already discuss this topic? Are there any drawbacks of implementing the AutoCloseable interface in GC?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions