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

SOLR-16271: remove wildcard imports #938

Merged
merged 17 commits into from
Jul 19, 2022

Conversation

epugh
Copy link
Contributor

@epugh epugh commented Jul 12, 2022

https://issues.apache.org/jira/browse/SOLR-16271

Description

Remove wildcard imports from the build. In later issue, will add a spotless based check.

Solution

made the changes.

Tests

manually run tests.

Checklist

Please review the following and check all that apply:

  • I have reviewed the guidelines for How to Contribute and my code conforms to the standards described there to the best of my ability.
  • I have created a Jira issue and added the issue ID to my pull request title.
  • I have given Solr maintainers access to contribute to my PR branch. (optional but recommended)
  • I have developed this patch against the main branch.
  • I have run ./gradlew check.
  • I have added tests for my changes.
  • I have added documentation for the Reference Guide

@epugh epugh requested a review from dsmiley July 12, 2022 17:53
@epugh epugh marked this pull request as draft July 12, 2022 17:53
@@ -16,7 +16,13 @@
*/
package org.apache.solr.handler;

import static org.apache.solr.handler.ReplicationTestHelper.*;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@dsmiley what do you think of this style?

@dsmiley
Copy link
Contributor

dsmiley commented Jul 12, 2022

Thanks for giving this a shot @epugh!

I now remember what I found when Googling how to get Spotless to do this some months ago. It's this technique used on another project (Geode): https://apache.googlesource.com/geode/+/refs/tags/rel/v1.11.0/gradle/spotless.gradle I like that it's done as a build hook into Spotless's flexibility as opposed to our home-grown validateSourcePatterns. Maybe we don't need validateSourcePatterns if we have Spotless?

CC @uschindler -- when discussing source validations in general, I recall you have strong opinions

@epugh
Copy link
Contributor Author

epugh commented Jul 13, 2022

Just pushed up the spotless version. We don't get the nice report, since it just stops on the first one with an exception:

> Task :solr:core:spotlessJava FAILED
Step 'Refuse wildcard imports' found problem in 'src/test/org/apache/solr/handler/TestReplicationHandler.java':
Do not use wildcard imports.  'spotlessApply' cannot resolve this issue.
java.lang.AssertionError: Do not use wildcard imports.  'spotlessApply' cannot resolve this issue.
        at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
        at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
        at org.codehaus.groovy.reflection.CachedConstructor.invoke(CachedConstructor.java:72)
        at org.codehaus.groovy.runtime.callsite.ConstructorSite$ConstructorSiteNoUnwrapNoCoerce.callConstructor(ConstructorSite.java:105)
        at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallConstructor(CallSiteArray.java:59)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:263)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:277)
        at spotless_9enkbgsd2kcvij6tpbbr3dzhe$_run_closure1$_closure2$_closure8$_closure10$_closure11.doCall(/Users/epugh/Documents/projects/solr-epugh-2/gradle/validation/spotless.gradle:66)

@epugh
Copy link
Contributor Author

epugh commented Jul 13, 2022

@dsmiley before I go futhur, what do you think of the way I fixed the imports? Does this pattern for both regular and statics make sense?

@dsmiley
Copy link
Contributor

dsmiley commented Jul 13, 2022

No wildcards -- not because of me or you or whatever IntelliJ or other IDE ships by default, but because we are trying to adhere to a particular style guide: https://google.github.io/styleguide/javaguide.html#s3.3.1-wildcard-imports

@dsmiley dsmiley requested a review from uschindler July 13, 2022 18:36
@epugh
Copy link
Contributor Author

epugh commented Jul 13, 2022

awesome, so, is everything in this style guide "solr community accepted"? As in, can I reference this to understand what our style is? We should have a FAQ entry about that for the next person ;-)

@uschindler
Copy link
Contributor

Lucene agreed to the Spotless code formatting with the Google Java. In Lucene's precommit will fail, if you have code not formatted like that. In general it's plain easy: run "./gradlew tidy" before commit. No need to take care anything. If your IDE has slightly different style it does not matter if you run the command before commit. 👍

@uschindler
Copy link
Contributor

I don't think this PR is needed. I'd just go with spotless.

@epugh
Copy link
Contributor Author

epugh commented Jul 13, 2022

I believe that we have to manually deal with the import statements, that spotless can alert, but can't fix it....?

@uschindler
Copy link
Contributor

From my knowledge, spotless can deal with imports. It uses javac to scan the code, so it understands how to expand wildcards.

@dsmiley
Copy link
Contributor

dsmiley commented Jul 13, 2022

Our repo already documents adherence to "Google Java Format" -- https://github.com/apache/solr/blob/main/help/formatting.txt but perhaps we should restate this in more places.

Spotless supports multiple formats, including "Google Java Style Guide", but it has limitations.

Also CC @dweiss as you may be interested in this.

@epugh
Copy link
Contributor Author

epugh commented Jul 13, 2022

From my knowledge, spotless can deal with imports. It uses javac to scan the code, so it understands how to expand wildcards.

From my investigation, spotless can reorder imports, but not expand them.... Any links/docs on how to enable it?

@dsmiley
Copy link
Contributor

dsmiley commented Jul 15, 2022

Just a quick tip: IntelliJ can do all the work here across the project; no need to open any source file at all. Just select the root of the project in the navigation tree and choose "Optimize Imports" in the Code menu. It might be influenced by having the "Google-java-format" plugin enabled.

@dsmiley
Copy link
Contributor

dsmiley commented Jul 15, 2022

To keep the build changes clearly visible from a massive commit that makes us adhere, I propose doing this in two PRs; first actually optimizes imports via the IDE (a different PR), and the second (this PR) then enforces the rule.

@epugh
Copy link
Contributor Author

epugh commented Jul 15, 2022

To keep the build changes clearly visible from a massive commit that makes us adhere, I propose doing this in two PRs; first actually optimizes imports via the IDE (a different PR), and the second (this PR) then enforces the rule.

Could we flip it? Only that this PR says "remove wildcard imports", and I'll add another that is "Check and prevent wildcard imports" ???

@epugh
Copy link
Contributor Author

epugh commented Jul 15, 2022

Just a quick tip: IntelliJ can do all the work here across the project; no need to open any source file at all. Just select the root of the project in the navigation tree and choose "Optimize Imports" in the Code menu. It might be influenced by having the "Google-java-format" plugin enabled.

Trying it now! Still learning my way around intellij, thanks for sharing!

@epugh
Copy link
Contributor Author

epugh commented Jul 15, 2022

@dsmiley the organize imports work great, however intellij had different order then spotless, so re-ran spotless to restore orginal order ,and now I am about to push up the explicit import changes.

Will move the enforcement code to another issue.

@madrob
Copy link
Contributor

madrob commented Jul 15, 2022

Just a note that some imports exist because of javadoc references and I think automated tools sometimes has issues in the past with removing them too aggressively. Worth a check after changes instead of blindly committing, and if you see it hopefully this is enough warning that you won't be surprised :)

@epugh epugh marked this pull request as ready for review July 15, 2022 14:51
@epugh
Copy link
Contributor Author

epugh commented Jul 15, 2022

@madrob I did notice the // jdoc got removed.... I never did figure out what they did!

@epugh
Copy link
Contributor Author

epugh commented Jul 15, 2022

I ran gw clean and then gw assemble and the ref guide seemed okay. Linked to the solr javadocs.... The solrj pulling in code in the ref guide seemed good as well.

Other suggestions on what to verify?

Copy link
Contributor

@cpoerschke cpoerschke left a comment

Choose a reason for hiding this comment

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

Thanks for tackling this!

119 / 119 files viewed

Comment on lines +5 to -20
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.queryparser.charstream.CharStream;
import org.apache.lucene.queryparser.charstream.FastCharStream;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.Query;
import org.apache.solr.search.QParser;
import org.apache.solr.search.SyntaxError;

import java.io.StringReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.Query;
import org.apache.solr.search.SyntaxError;
import org.apache.solr.search.QParser;

import org.apache.lucene.queryparser.charstream.CharStream;
import org.apache.lucene.queryparser.charstream.FastCharStream;

Copy link
Contributor

Choose a reason for hiding this comment

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

minor: technically not wildcard import removal

@madrob
Copy link
Contributor

madrob commented Jul 15, 2022

I did notice the // jdoc got removed.... I never did figure out what they did!

It looks like the comment was the only thing removed, not the import itself. If we can leave the comment somehow then I'd try to keep it, otherwise somebody in the future is inevitably going to try to flag those as unused imports and then stub their toe on it (that person is going to be me, I am the one that will stub his toe)

@epugh
Copy link
Contributor Author

epugh commented Jul 15, 2022

I did notice the // jdoc got removed.... I never did figure out what they did!

It looks like the comment was the only thing removed, not the import itself. If we can leave the comment somehow then I'd try to keep it, otherwise somebody in the future is inevitably going to try to flag those as unused imports and then stub their toe on it (that person is going to be me, I am the one that will stub his toe)

are you saying the text // jdoc is literally the comment? COuld we expand it to be something like // import to support Javadocs or something like that? // jdoc just didn't really mean anything to me!

@epugh
Copy link
Contributor Author

epugh commented Jul 15, 2022

I imagined // jdoc to not be a comment, but to be some sort of crazy annotation or maybe a reminder to someone to come back and "jdoc" that line (whatever that meant!)... So a more verbose comment would make it clearer...

epugh and others added 2 commits July 15, 2022 11:43
…ntTest.java

Co-authored-by: Christine Poerschke <cpoerschke@apache.org>
@epugh
Copy link
Contributor Author

epugh commented Jul 15, 2022

Here is what I found for the text jdoc in the source:
image

@epugh
Copy link
Contributor Author

epugh commented Jul 15, 2022

Okay, I think I am starting to understand... For the example of SwitchQParserPlugin.java, we reference SearchHandler in the javadocs, and so the import is needed. If I remove the import, then IntelliJ does flag it that it is needed....

So, I guess, if we only have those three files that follow this pattern, then we can leave the comment in, but maybe change it from // jdoc to // support javadoc linking ???

Or, maybe since intellij flags the missing import if you don't have it, then we don't need the comment at all?

Copy link
Contributor

@cpoerschke cpoerschke left a comment

Choose a reason for hiding this comment

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

LGTM but also okay with jdoc comments being retained or changed to something similar.

@epugh epugh merged commit 6e61811 into apache:main Jul 19, 2022
epugh added a commit that referenced this pull request Jul 19, 2022
This also removes the // jdoc comments, as IntelliJ does properly flag imports missing that are referenced in JavaDocs.   

Co-authored-by: Christine Poerschke <cpoerschke@apache.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants