Skip to content

Commit 27a120d

Browse files
committed
fix warning about Field.isAccessible()
'The method isAccessible() from the type AccessibleObject is deprecated since version 9' tested with WorkbenchThemeChangedHandlerTest, org.eclipse.ui.tests.commands.HelpContextIdTest.testHelpContextId()
1 parent 9dfd0f6 commit 27a120d

File tree

2 files changed

+18
-15
lines changed
  • bundles
    • org.eclipse.core.commands/src/org/eclipse/core/internal/commands/util
    • org.eclipse.e4.ui.workbench.renderers.swt/src/org/eclipse/e4/ui/workbench/renderers/swt

2 files changed

+18
-15
lines changed

bundles/org.eclipse.core.commands/src/org/eclipse/core/internal/commands/util/Util.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -255,14 +255,16 @@ public static final String getHelpContextId(Command command) {
255255

256256
String contextId = null;
257257
if (method != null) {
258-
boolean accessible = method.isAccessible();
259-
method.setAccessible(true);
260-
try {
261-
contextId = (String) method.invoke(command);
262-
} catch (Exception e) {
263-
// do nothing
258+
boolean accessible = method.canAccess(command);
259+
if (method.trySetAccessible()) {
260+
try {
261+
contextId = (String) method.invoke(command);
262+
} catch (Exception ignored) {
263+
// do nothing
264+
} finally {
265+
method.setAccessible(accessible);
266+
}
264267
}
265-
method.setAccessible(accessible);
266268
}
267269
return contextId;
268270
}

bundles/org.eclipse.e4.ui.workbench.renderers.swt/src/org/eclipse/e4/ui/workbench/renderers/swt/CTabRendering.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1254,14 +1254,15 @@ public ReflectionSupport(T instance) {
12541254
protected Object getFieldValue(Field field) {
12551255
Object value = null;
12561256
if (field != null) {
1257-
boolean accessible = field.isAccessible();
1258-
try {
1259-
field.setAccessible(true);
1260-
value = field.get(instance);
1261-
} catch (Exception exc) {
1262-
// do nothing
1263-
} finally {
1264-
field.setAccessible(accessible);
1257+
boolean accessible = field.canAccess(instance);
1258+
if (field.trySetAccessible()) {
1259+
try {
1260+
value = field.get(instance);
1261+
} catch (Exception ignored) {
1262+
// do nothing
1263+
} finally {
1264+
field.setAccessible(accessible);
1265+
}
12651266
}
12661267
}
12671268
return value;

0 commit comments

Comments
 (0)