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 #1070

Merged
merged 9 commits into from
May 26, 2020
Merged

Code cleanup #1070

merged 9 commits into from
May 26, 2020

Conversation

NewbieOrange
Copy link
Contributor

This PR removes redundant modifiers and initializations, unused variables, incorrect javadoc references, etc.

The static modifiers in inner interfaces are removed since they are static by default, and removing them should not break binary compatibility (the md5sums are the same). Let me know if those are preferred to be kept.

@codecov-commenter
Copy link

codecov-commenter commented May 25, 2020

Codecov Report

Merging #1070 into master will increase coverage by 0.02%.
The diff coverage is 96.77%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master    #1070      +/-   ##
============================================
+ Coverage     94.41%   94.43%   +0.02%     
  Complexity      448      448              
============================================
  Files             2        2              
  Lines          6591     6564      -27     
  Branches       1763     1765       +2     
============================================
- Hits           6223     6199      -24     
  Misses           94       94              
+ Partials        274      271       -3     
Impacted Files Coverage Δ Complexity Δ
src/main/java/picocli/CommandLine.java 94.28% <96.72%> (+0.02%) 309.00 <7.00> (ø)
src/main/java/picocli/AutoComplete.java 96.98% <100.00%> (-0.06%) 139.00 <0.00> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 65256cc...701e48e. Read the comment docs.

@NewbieOrange
Copy link
Contributor Author

Also there are a lot of @SuppressWarnings("deprecation") removed since we're at language level 1.5 and should not trigger any warnings.

Copy link
Owner

@remkop remkop left a comment

Choose a reason for hiding this comment

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

Overall some nice improvements, thank you!
I put some comments, can you take a look?

@@ -7732,7 +7724,6 @@ void initFromMixin(UsageMessageSpec mixin, CommandSpec commandSpec) {
if (initializable(sortOptions, mixin.sortOptions(), DEFAULT_SORT_OPTIONS)) {sortOptions = mixin.sortOptions();}
if (initializable(synopsisHeading, mixin.synopsisHeading(), DEFAULT_SYNOPSIS_HEADING)) {synopsisHeading = mixin.synopsisHeading();}
if (initializable(synopsisSubcommandLabel, mixin.synopsisSubcommandLabel(), DEFAULT_SYNOPSIS_SUBCOMMANDS)) {synopsisHeading = mixin.synopsisHeading();}
if (initializable(width, mixin.width(), DEFAULT_USAGE_WIDTH)) {width = mixin.width();}
Copy link
Owner

Choose a reason for hiding this comment

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

Why is this removed?

Copy link
Contributor Author

@NewbieOrange NewbieOrange May 25, 2020

Choose a reason for hiding this comment

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

This is never reached. initializable checks whether the varaible is null and is not the default value, but an int is never null.

private static boolean initializable(Object current, Object candidate, Object defaultValue) {
    return current == null && isNonDefault(candidate, defaultValue);
}

Copy link
Contributor Author

@NewbieOrange NewbieOrange May 25, 2020

Choose a reason for hiding this comment

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

If we want to keep this, we may introduce initializables with each primitive type.

Copy link
Owner

Choose a reason for hiding this comment

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

This is a bug: #1072
Fixing this now.

Copy link
Owner

Choose a reason for hiding this comment

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

Please revert this one; the warning that this condition is always false should go away with the latest master.

@@ -5138,9 +5133,8 @@ public Help create(CommandSpec commandSpec, Help.ColorScheme colorScheme) {
return cls.cast(new LinkedHashMap<Object, Object>());
}
try {
@SuppressWarnings("deprecation") // Class.newInstance is deprecated in Java 9
T result = cls.newInstance();
return result;
Copy link
Owner

Choose a reason for hiding this comment

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

This @SuppressWarnings("deprecation") annotation suppresses the warning one gets when building the project with Java 9. I think it is better to keep it.

@@ -235,7 +235,7 @@ public void run() {
}
}

private static interface Function<T, V> {
Copy link
Owner

Choose a reason for hiding this comment

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

No problem, I am okay with removing static for inner interfaces.

@@ -1470,7 +1470,7 @@ public void clearExecutionResults() {
* @see RunAll
* @deprecated Use {@link IExecutionStrategy} instead.
* @since 2.0 */
@Deprecated public static interface IParseResultHandler {
@Deprecated public interface IParseResultHandler {
Copy link
Owner

Choose a reason for hiding this comment

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

Again, no problem removing static for inner interfaces.

@@ -1758,7 +1758,7 @@ protected R throwOrExit(ExecutionException ex) {
* @since 2.0 */
@Deprecated public static class DefaultExceptionHandler<R> extends AbstractHandler<R, DefaultExceptionHandler<R>> implements IExceptionHandler, IExceptionHandler2<R> {
public List<Object> handleException(ParameterException ex, PrintStream out, Help.Ansi ansi, String... args) {
internalHandleParseException(ex, new PrintWriter(out, true), Help.defaultColorScheme(ansi)); return Collections.<Object>emptyList(); }
Copy link
Owner

Choose a reason for hiding this comment

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

I think I introduced this to get rid of a "raw collections" warning when building with Java 8 or higher... Probably better to keep the generified form with <Object>.

src/main/java/picocli/CommandLine.java Show resolved Hide resolved
src/main/java/picocli/CommandLine.java Show resolved Hide resolved
@@ -10038,7 +10025,6 @@ public MethodParam(Method method, int paramIndex) {
}
@Override public Annotation[] getDeclaredAnnotations() { return method.getParameterAnnotations()[paramIndex]; }
@Override public void setAccessible(boolean flag) throws SecurityException { method.setAccessible(flag); }
@SuppressWarnings("deprecation")
Copy link
Owner

Choose a reason for hiding this comment

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

Method.isAccessible will give deprecation warnings when building on Java 9+.
I believe we want to suppress this.
Try this:

%JAVA_11_HOME%\bin\javac -Xlint:all -d c:\Temp picocli\CommandLine.java

Copy link
Owner

Choose a reason for hiding this comment

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

When I tried compiling on java 11 with -Xlint:all, I saw deprecation warnings for method.isAccessible.
You did not get this?

@@ -14943,7 +14921,7 @@ public IParamLabelRenderer createDefaultParamLabelRenderer() {
* OptionSpec#description()} array, and these rows look like {@code {"", "", "", option.description()[i]}}.</p>
*/
static class DefaultOptionRenderer implements IOptionRenderer {
private String requiredMarker = " ";
private String requiredMarker;
private boolean showDefaultValues;
Copy link
Owner

Choose a reason for hiding this comment

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

Can we make requiredMarker and showDefaultValues final fields?

Copy link
Owner

Choose a reason for hiding this comment

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

Can these be final too?

src/main/java/picocli/CommandLine.java Show resolved Hide resolved
@remkop remkop added this to the 4.4 milestone May 25, 2020
@NewbieOrange
Copy link
Contributor Author

I have checked the other removed @SuppressWarnings does not introduce any new warning when compiled with OpenJDK 11.0.7 and -Xlint:all.

@NewbieOrange NewbieOrange requested a review from remkop May 25, 2020 09:56
@NewbieOrange
Copy link
Contributor Author

NewbieOrange commented May 25, 2020

The added final modifiers seem to break the codegen graalvm-aot tests, since the generated fields have an entra "allowWrite" : true.

Edit: The field should not be made final, reverted.

@NewbieOrange
Copy link
Contributor Author

On a side note, I think we should also remove the broken kloudtek logo (HTTPS cert expired) and machine-translated article (从Commons CLI迁移到Picocli) from README.

@remkop
Copy link
Owner

remkop commented May 25, 2020

I have checked the other removed @SuppressWarnings does not introduce any new warning when compiled with OpenJDK 11.0.7 and -Xlint:all.

Thank you!

The added final modifiers seem to break the codegen graalvm-aot tests, since the generated fields have an entra "allowWrite" : true.

Edit: The field should not be made final, reverted.

Interesting! I’ll take a look tomorrow.

On a side note, I think we should also remove the broken kloudtek logo (HTTPS cert expired) and machine-translated article (从Commons CLI迁移到Picocli) from README.

Is the translation that bad? :-)
Even if it is, does it reflect badly on picocli?
I was thinking it’s better to have more links than fewer, to spread awareness of the library.

@NewbieOrange
Copy link
Contributor Author

NewbieOrange commented May 25, 2020

The added final modifiers seem to break the codegen graalvm-aot tests, since the generated fields have an entra "allowWrite" : true.
Edit: The field should not be made final, reverted.

Interesting! I’ll take a look tomorrow.

This is due to a private field being made final, but it is accessed by reflection from annotations. I have removed the final modifier that caused the issue.

On a side note, I think we should also remove the broken kloudtek logo (HTTPS cert expired) and machine-translated article (从Commons CLI迁移到Picocli) from README.

Is the translation that bad? :-)
Even if it is, does it reflect badly on picocli?
I was thinking it’s better to have more links than fewer, to spread awareness of the library.

The translation is simply google translate I think. It is not unreadable tho, but some code snippets are being 'translated', and it is also not the original post (it is a repost). The original post is here (the content is the same, but with better formatting): https://blog.csdn.net/Tybyqi/article/details/85787550

Copy link
Owner

@remkop remkop left a comment

Choose a reason for hiding this comment

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

Please change the initFromMixin bit; other than that, looks good.

@@ -14943,7 +14921,7 @@ public IParamLabelRenderer createDefaultParamLabelRenderer() {
* OptionSpec#description()} array, and these rows look like {@code {"", "", "", option.description()[i]}}.</p>
*/
static class DefaultOptionRenderer implements IOptionRenderer {
private String requiredMarker = " ";
private String requiredMarker;
private boolean showDefaultValues;
Copy link
Owner

Choose a reason for hiding this comment

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

Can these be final too?

@@ -10038,7 +10025,6 @@ public MethodParam(Method method, int paramIndex) {
}
@Override public Annotation[] getDeclaredAnnotations() { return method.getParameterAnnotations()[paramIndex]; }
@Override public void setAccessible(boolean flag) throws SecurityException { method.setAccessible(flag); }
@SuppressWarnings("deprecation")
Copy link
Owner

Choose a reason for hiding this comment

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

When I tried compiling on java 11 with -Xlint:all, I saw deprecation warnings for method.isAccessible.
You did not get this?

@@ -7732,7 +7724,6 @@ void initFromMixin(UsageMessageSpec mixin, CommandSpec commandSpec) {
if (initializable(sortOptions, mixin.sortOptions(), DEFAULT_SORT_OPTIONS)) {sortOptions = mixin.sortOptions();}
if (initializable(synopsisHeading, mixin.synopsisHeading(), DEFAULT_SYNOPSIS_HEADING)) {synopsisHeading = mixin.synopsisHeading();}
if (initializable(synopsisSubcommandLabel, mixin.synopsisSubcommandLabel(), DEFAULT_SYNOPSIS_SUBCOMMANDS)) {synopsisHeading = mixin.synopsisHeading();}
if (initializable(width, mixin.width(), DEFAULT_USAGE_WIDTH)) {width = mixin.width();}
Copy link
Owner

Choose a reason for hiding this comment

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

This is a bug: #1072
Fixing this now.

@@ -7732,7 +7724,6 @@ void initFromMixin(UsageMessageSpec mixin, CommandSpec commandSpec) {
if (initializable(sortOptions, mixin.sortOptions(), DEFAULT_SORT_OPTIONS)) {sortOptions = mixin.sortOptions();}
if (initializable(synopsisHeading, mixin.synopsisHeading(), DEFAULT_SYNOPSIS_HEADING)) {synopsisHeading = mixin.synopsisHeading();}
if (initializable(synopsisSubcommandLabel, mixin.synopsisSubcommandLabel(), DEFAULT_SYNOPSIS_SUBCOMMANDS)) {synopsisHeading = mixin.synopsisHeading();}
if (initializable(width, mixin.width(), DEFAULT_USAGE_WIDTH)) {width = mixin.width();}
Copy link
Owner

Choose a reason for hiding this comment

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

Please revert this one; the warning that this condition is always false should go away with the latest master.

@NewbieOrange NewbieOrange requested a review from remkop May 26, 2020 12:07
@NewbieOrange
Copy link
Contributor Author

NewbieOrange commented May 26, 2020

Can these be final too?

Made the fields final.

When I tried compiling on java 11 with -Xlint:all, I saw deprecation warnings for method.isAccessible.
You did not get this?

No.. I think it should, but for some reason it is not giving me warning. I have added the @SupressWarnings annotation back.

Please revert this one; the warning that this condition is always false should go away with the latest master.

Done.

@remkop remkop merged commit 035f658 into remkop:master May 26, 2020
remkop added a commit that referenced this pull request May 26, 2020
@remkop
Copy link
Owner

remkop commented May 26, 2020

Merged into master.
Thank you for the contribution! Much appreciated!

@NewbieOrange NewbieOrange deleted the code_cleanup branch May 27, 2020 14:36
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.

3 participants