Skip to content

Commit 493c646

Browse files
ShahzaibIbrahimfedejeanne
authored andcommitted
Make FontData key in map immutable
Creating clone of FontData to be used as a key in map of ScalingSWTFontRegistry and DefaultSWTFontRegistry to make it immutable and inaccessible from outside of the class
1 parent 0fe4a76 commit 493c646

File tree

3 files changed

+6
-3
lines changed

3 files changed

+6
-3
lines changed

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/internal/DefaultSWTFontRegistry.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ public Font getFont(FontData fontData, int zoom) {
7272
}
7373

7474
private Font registerFont(FontData fontData, Font font) {
75-
fontsMap.put(fontData, font);
75+
FontData clonedFontData = new FontData(fontData.toString());
76+
fontsMap.put(clonedFontData, font);
7677
return font;
7778
}
7879

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/internal/ScalingSWTFontRegistry.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,9 @@ public Font getFont(FontData fontData, int zoom) {
130130
if (customFontsKeyMap.containsKey(fontData)) {
131131
container = customFontsKeyMap.get(fontData);
132132
} else {
133-
container = new ScaledCustomFontContainer(fontData);
134-
customFontsKeyMap.put(fontData, container);
133+
FontData clonedFontData = new FontData(fontData.toString());
134+
container = new ScaledCustomFontContainer(clonedFontData);
135+
customFontsKeyMap.put(clonedFontData, container);
135136
}
136137
return container.getScaledFont(zoom);
137138
}

tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_graphics_FontData.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public void test_ConstructorLjava_lang_String() {
4545
FontData fd = new FontData(SwtTestUtil.testFontName, 10, SWT.NORMAL);
4646
FontData reconstructedFontData = new FontData(fd.toString());
4747
assertEquals(fd, reconstructedFontData);
48+
assertEquals(fd.hashCode(), reconstructedFontData.hashCode());
4849
}
4950

5051
@Test

0 commit comments

Comments
 (0)