Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix warning about Field.isAccessible() #1927

Merged
merged 2 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bundles/org.eclipse.core.commands/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: %pluginName
Bundle-SymbolicName: org.eclipse.core.commands
Bundle-Version: 3.12.100.qualifier
Bundle-Version: 3.12.200.qualifier
Bundle-Vendor: %providerName
Bundle-Localization: plugin
Export-Package: org.eclipse.core.commands,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,14 +255,16 @@ public static final String getHelpContextId(Command command) {

String contextId = null;
if (method != null) {
boolean accessible = method.isAccessible();
method.setAccessible(true);
try {
contextId = (String) method.invoke(command);
} catch (Exception e) {
// do nothing
boolean accessible = method.canAccess(command);
if (method.trySetAccessible()) {
try {
contextId = (String) method.invoke(command);
} catch (Exception ignored) {
// do nothing
} finally {
method.setAccessible(accessible);
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wouldn't the finally block also need be adjusted?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also: Command#getHelpContextId() says clients shally use CommandManager#getHelpContextId(Command) instead. Can't we call that here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is no final block here, `CommandManager does a lot more then just calling the method. I don't want to break anything.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, right, there is no finally block here. but the same line of thought applies:

The condition is changed from isAccessible() to canAccess(), and now the defensive if(tryAccess()) is used, but in line 266 setAccessible() is still called unconditionally which can then still fail.

I propose to also move that 'undo/cleanup' line inside the if(try) block, since otherwise there is nothing to undo and it could needlessly raise an exception.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK

method.setAccessible(accessible);
}
return contextId;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-SymbolicName: org.eclipse.e4.ui.workbench.renderers.swt;singleton:=true
Bundle-Version: 0.16.400.qualifier
Bundle-Version: 0.16.500.qualifier
Bundle-Name: %pluginName
Bundle-Vendor: %providerName
Bundle-Localization: plugin
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1254,14 +1254,15 @@ public ReflectionSupport(T instance) {
protected Object getFieldValue(Field field) {
Object value = null;
if (field != null) {
boolean accessible = field.isAccessible();
try {
field.setAccessible(true);
value = field.get(instance);
} catch (Exception exc) {
// do nothing
} finally {
field.setAccessible(accessible);
boolean accessible = field.canAccess(instance);
if (field.trySetAccessible()) {
try {
value = field.get(instance);
} catch (Exception ignored) {
// do nothing
} finally {
field.setAccessible(accessible);
}
}
}
return value;
Expand Down
Loading