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

Code Cleanup/Deprecation fixes: Class#newInstance, underscore identifier, better temp files, avoid toUpper/LowerCase and more #7053

Merged
merged 12 commits into from Feb 21, 2024

Conversation

mbien
Copy link
Member

@mbien mbien commented Feb 10, 2024

Contains project wide cleanups/refactorings/deprecation fixes:

  • Prefer Files#createTempFile over File#createTempFile.
    • Files.createTempFile uses more restrictive file permissions if possible.
  • Prefer String.equalsIgnoreCase() over toUpper/LowerCase patterns
  • Replace single underscore identifiers with regular names. _ is a reserved keyword since java 8
  • Fix Class#newInstance deprecation warnings
    • c.getDeclaredConstructor().newInstance() can be used instead
    • simplified exception handling in related code
  • Prefer list.sort(comp) over Collections.sort(list, comp)
    • more fluent / one import less in many cases
    • (there is no sort method without Comparator, we could in theory map Collections.sort(list) to list.sort(null), but I left those cases out since this might look confusing)
  • More efficient col.toArray()
    • zeroing step is skipped with col.toArray(new T[0]) or (T::new)
    • easier to optimize for the JVM
    • shorter/easier to read
    • large commit which is split into 6 parts along cluster boundaries

All changes were made using either jackpot scripts or built-in NetBeans code transformations. The Class#newInstance change needed some adjustments per hand, since due to slightly different exception handling.

I looked through the patch files twice, except the toArray() change which I only looked through once due to its size. I only had to tweak minor formatting anomalies here and there.

@mbien mbien added this to the NB22 milestone Feb 10, 2024
@mbien mbien changed the title Code Cleanup/Deprecation fixes: Class#newInstance, underscore identifier, better temp files and avoid toUpper/LowerCase Code Cleanup/Deprecation fixes: Class#newInstance, underscore identifier, better temp files, avoid toUpper/LowerCase and more Feb 14, 2024
@mbien mbien marked this pull request as ready for review February 14, 2024 11:29
@mbien
Copy link
Member Author

mbien commented Feb 14, 2024

@junichi11 @troizet want to look through the PHP commit? (or any other commits you are interested in)

@junichi11
Copy link
Member

We have the PHP feature branch for NB22. So, please merge this after we merge the PHP feature branch. (I'm going to merge it into the master after NB21 is released.) Thanks.

cc: @tmysik

@mbien
Copy link
Member Author

mbien commented Feb 14, 2024

@junichi11 yes saw it already, I won't merge before some of the larger pending PRs are integrated.

@tmysik
Copy link
Member

tmysik commented Feb 14, 2024

@mbien

Thanks a lot for doing this, Michael!

@mbien
Copy link
Member Author

mbien commented Feb 15, 2024

I think a good way of reviewing cleanups like this is to let github generate a diff patch for a commit, e.g:
https://github.com/apache/netbeans/pull/7053/commits/32a42b5994652483cc5bd9a4713a6941d175ae4b.diff (simply add .diff to any URL)
unfortunately the browser doesn't show this in color, so I download it and open it in NB. This makes scanning through it really easy and fast.

Also for the reviewers: please mention what parts you reviewed in the approve comment so that I get an idea of what is reviewed and what is not

Copy link
Contributor

@matthiasblaesing matthiasblaesing left a comment

Choose a reason for hiding this comment

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

I eyeballed samples from all commits. The changes looks sane to me and nothing out of the ordinary jumped out. Thank you.

}
});
projectsArray.sort((Object o1, Object o2) ->
getDisplayName((Provider) o1).toLowerCase().compareTo(getDisplayName((Provider) o2).toLowerCase())
Copy link
Member

Choose a reason for hiding this comment

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

Can't use compareToIgnoreCase?

Copy link
Member Author

Choose a reason for hiding this comment

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

oh interesting, I didn't know that this method existed. I did only transform to equalsIgnoreCase()

I might do this at a later point since this PR is already large enough.
But i am going to add this to my hints collection for sure https://github.com/mbien/jackpot-inspections
thanks!

Copy link
Member Author

Choose a reason for hiding this comment

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

looking closer at the method it likely has unintended side effects since it sorts the array in-place, and then it returns a copy via toArray() (which makes no sense).

other implementations do copy first, sort it and return the copy:
https://github.com/search?q=repo%3Aapache%2Fnetbeans%20getSortedProjects&type=code

should I change it to:

    public static Provider[] getSortedProjects(Provider[] projects) {
        Provider[] sorted = Arrays.copyOf(projects, projects.length);
        Arrays.sort(sorted, (p1, p2) -> getDisplayName(p1).compareToIgnoreCase(getDisplayName(p2)));
        return sorted;
    }

@matthiasblaesing ?

Copy link
Member Author

@mbien mbien Feb 16, 2024

Choose a reason for hiding this comment

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

added it as extra commit (the other ProjectUtilities class in ...profiler.nbimpl.project is deprecated and not used for the getSortedProject utility method, only the new ProjectUtilities had this behavior)

Copy link
Member

@junichi11 junichi11 left a comment

Choose a reason for hiding this comment

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

Looks good to me. I've glanced through each commit. Thank you for your work!

Files.createTempFile uses more restrictive file permissions if
possible.

project wide refactoring, skipped most tests and some dead code
to shrink the change set.
String.toUpper/LowerCase() without setting Locale.ROOT can lead to
unintended results on certain locales.

String.equalsIgnoreCase() can be sometimes used instead and the
method does not depend on the locale.

This reduces the toUpper/LowerCase usage a bit further before
the great Locale.ROOT refactoring starts.
 - '_' is a reserved keyword since java 8 and causes javac warnings
 - c.getDeclaredConstructor().newInstance() can be used instead.
 - simplified exception handling in related code
 - more fluent/direct
 - one less import in many cases
 - zeroing step is skipped with col.toArray(new T[0]) or (T::new)
 - easier to optimize for the JVM
 - shorter/easier to read
 - zeroing step is skipped with col.toArray(new T[0]) or (T::new)
 - easier to optimize for the JVM
 - shorter/easier to read
 - zeroing step is skipped with col.toArray(new T[0]) or (T::new)
 - easier to optimize for the JVM
 - shorter/easier to read
 - zeroing step is skipped with col.toArray(new T[0]) or (T::new)
 - easier to optimize for the JVM
 - shorter/easier to read
 - zeroing step is skipped with col.toArray(new T[0]) or (T::new)
 - easier to optimize for the JVM
 - shorter/easier to read
 - zeroing step is skipped with col.toArray(new T[0]) or (T::new)
 - easier to optimize for the JVM
 - shorter/easier to read
 - original impl sorted the view of the input array in place and made
   a copy afterwards, which is unusual and likely unintended
 - new impl copies input, sorts it and returns the copy
@mbien
Copy link
Member Author

mbien commented Feb 21, 2024

planning to merge once green again

the other PRs should do fine hopefully, I don't expect many conflicts there:

gh pr list --limit 30 --json number,title,changedFiles --template '{{range .}}{{tablerow .number .changedFiles .title}}{{end}}'                                                                                  7089  2     README and CHANGELOG update                                                                                                                                                                                                    
7088  1     Java Code Templates for records and sealed types
7087  11    maven indexing: update lucene from 9.9.1 to 9.10.0.
7082  3     Integrate hints preferences option in the vscode extension
7081  4     Add open containing folder action.
7080  13    Fix detection of support for generation session beans from entity classes and support jakarta package names
7077  3     Fix: Settings persistence does not work for inner-class hints
7076  9     Replace usage of package `org.codehaus.plexus.util.IOUtil` with Java native methods.
7075  1     Escape java hint fix String for html and fill in multi variables.
7070  10    New file created by copy/paste in git repo ignores commit exclusion flag
7063  9     Improve priming from LSP client
7062  26    Cleanup HashMap raw type warnings..
7055  1     Adding support for Wildfly 31
7053  1687  Code Cleanup/Deprecation fixes: Class#newInstance, underscore identifier, better temp files, avoid toUpper/LowerCase and more
7034  14    Adjusting some license texts.
7032  8     Add support for GlassFish 7.0.12
7030  3     Fixed concurrency issues in GradleDaemonExecutor
7024  15    Gradle Project shall use the Java from the tooling not runtime.
7020  38    Final changes for JakartaEE adjustments
7012  1     Improved Toolbar overflow impl.
7004  9     Update guava from 32.1.2-jre to 33.0.0-jre
6999  2     JDK 21 support to Payara Platform tools v6.x in Apache NetBeans IDE
6988  223   Add support for Jakarta EE 11
6985  4     Enable code completion in JSF and JSP pages in Jakarta based projects
6984  15    Enable Servlet/Filter/Listener generation for Jakarta based projects
6892  28    Embedded Chromium Brower into NetBeans
6866  5     Allow user to manipulate Truffle breakpoints
6844  71    [Rust] Rust workspaces support.
6834  11    Added go to test ability for test/tested class in lsp server
6829  12    Support for multiple NetBeans Gradle Tooling

thanks for review!

@mbien mbien merged commit 30e2275 into apache:master Feb 21, 2024
43 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants