-
Notifications
You must be signed in to change notification settings - Fork 5.8k
8048199: Replace anonymous inner classes with lambdas, where applicable, in JNDI #3416
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
Conversation
👋 Welcome back ccleary! A progress list of the required criteria for merging this PR into |
Webrevs
|
} | ||
}); | ||
(PrivilegedAction<String>) () -> System.getProperty(propName, defVal) | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you want to avoid the cast then you could create the PrivilegedAction explicitly, e.g.
PrivilegedAction pa = () -> System.getProperty(propName, defVal);
return AccessController.doPrivileged(pa);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An alternative here would be to use sun.security.action.privilegedGetProperty(prop, default).
The package is already exported from java.base to java.desktop, etc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is a very neat alternative yes. Approaching the problem like that especially improves the readability JdkLDAP.java in my opinion.
I don't think casting has any major performance hits for these fixes, so is your suggestion mostly for readability's sake?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, not a performance problem. Just simpler to use an existing method to read a privileged property.
And there will be one less doPriv.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I may change the PR to reflect what Alan suggested. Its more readable than the lambda-with-cast solution in that an action is created and then an action is carried out as supposed to fitting it all into one call. Its also as concise.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good.
A general reminder about one important difference between an anonymous inner class and a lambda.
An anonymous has identity but a lambda does not; or only accidentally.
None of the uses here require identity.
} | ||
}); | ||
(PrivilegedAction<String>) () -> System.getProperty(propName, defVal) | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An alternative here would be to use sun.security.action.privilegedGetProperty(prop, default).
The package is already exported from java.base to java.desktop, etc.
private static final long getLong(final String propName, final long defVal) { | ||
return AccessController.doPrivileged( | ||
(PrivilegedAction<Long>) () -> Long.getLong(propName, defVal).longValue() | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And GetIntegerAction here. Though it only supports an int value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the suggestion Roger, I think the privilegedGetProperty(prop, default)
for the getProperty()
method looks great.
WRT to using it for getInt()
and getLong()
, I think its reasonable to use other means for these methods in the interest of consistency due to, as you pointed out, only int
being supported. Would you think? Or would it be better to use the same means in all 3 methods?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Its a slippery slope that might require a bit more investigation to check the expected value sizes to see if a change to the number of bits in the value for each property might break something. Technical debt can take you down a rabbit hole. Quickest to just convert to the lamba as you proposed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have stuck with the lambda conversion solution in the current changes, however WRT to Alan's previous feedback on creating a PrivilegedAction explicitly as it makes everything a bit more readable.
new PrivilegedAction<String>() { | ||
public String run() { | ||
try { | ||
return System.getProperty(propName, defVal); | ||
} catch (SecurityException e) { | ||
return defVal; | ||
} | ||
} | ||
}); | ||
private static final String getProperty(final String propName, final String defVal) { | ||
PrivilegedAction<String> pa = () -> System.getProperty(propName, defVal); | ||
return AccessController.doPrivileged(pa); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmmm... This is not strictly equivalent but will work because java.naming is loaded by the boot loader and has the permission to read all system properties. I guess the code on the left-hand side was written at a time where JNDI was still in a stand-alone library?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dfuch I noticed the same about the swallowed exception that returns the default value. This wouldn't cause any regression?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As I noted - the SecurityException should never occur so I don't expect this could cause a regression.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
@c-cleary This change now passes all automated pre-integration checks. ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details. After integration, the commit message for the final commit will be:
You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed. At the time when this comment was updated there had been 41 new commits pushed to the
As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details. As you do not have Committer status in this project an existing Committer must agree to sponsor your change. Possible candidates are the reviewers of this PR (@RogerRiggs, @dfuch, @AlekseiEfimov, @ChrisHegarty) but any other Committer may sponsor as well. ➡️ To flag this PR as ready for integration with the above commit message, type |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The change looks good to me with one small suggestion:
} | ||
} | ||
); | ||
PrivilegedAction<ClassLoader> pa = () -> Thread.currentThread().getContextClassLoader(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can use here an instance method reference to beautify code a little bit more:
PrivilegedAction<ClassLoader> pa = Thread.currentThread()::getContextClassLoader;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea, also would fit in with the style of the method just after, priviligedHasNext()
as that also uses a functional interface.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changes included as described in commit 5d6ecd3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here again this is not strictly equivalent but AFAIK Thread::currentThread
doesn't require any permission, so this should be OK.
/integrate |
/sponsor |
@AlekseiEfimov @c-cleary Since your change was applied there have been 92 commits pushed to the
Your commit was automatically rebased without conflicts. Pushed as commit 4e90d74. 💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored. |
Description
This fix is part of a previous effort to both cleanup/modernise JNDI code, the details of which can be seen in JDK-8048091. A number JNDI methods under
java.naming
use Anonymous Inner Classes in cases where only a single object unique to the requirements of the method is used. The issues these occurrences of AICs cause are highlighted below.Issue
Fixes
Progress
Issue
Reviewers
Reviewing
Using
git
Checkout this PR locally:
$ git fetch https://git.openjdk.java.net/jdk pull/3416/head:pull/3416
$ git checkout pull/3416
Update a local copy of the PR:
$ git checkout pull/3416
$ git pull https://git.openjdk.java.net/jdk pull/3416/head
Using Skara CLI tools
Checkout this PR locally:
$ git pr checkout 3416
View PR using the GUI difftool:
$ git pr show -t 3416
Using diff file
Download this PR as a diff file:
https://git.openjdk.java.net/jdk/pull/3416.diff