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

ConstantPatternCompile suggested fix improvements #1655

Closed
Stephan202 opened this issue May 31, 2020 · 0 comments · Fixed by #1741
Closed

ConstantPatternCompile suggested fix improvements #1655

Stephan202 opened this issue May 31, 2020 · 0 comments · Fixed by #1741

Comments

@Stephan202
Copy link
Contributor

Consider the following code:

import java.util.regex.Pattern;

final class Dummy {
    /**
     * This is my method.
     *
     * @param str This is its parameter.
     */
    public boolean myMethod(String str) {
        Pattern pattern = Pattern.compile(".*");
        return pattern.matcher(str).matches();
    }
}

Applying the ConstantPatternCompile check to this code:

wget \
  https://repo1.maven.org/maven2/com/google/errorprone/error_prone_core/2.4.0/error_prone_core-2.4.0-with-dependencies.jar
javac \
  -XDcompilePolicy=simple \
  -processorpath error_prone_core-2.4.0-with-dependencies.jar \
  '-Xplugin:ErrorProne -XepPatchChecks:ConstantPatternCompile -XepPatchLocation:/tmp' \
  Dummy.java

This yields:

--- Dummy.java
+++ Dummy.java
@@ -7,7 +7,7 @@
      * @param str This is its parameter.
      */
-    public boolean myMethod(String str) {
-        Pattern pattern = Pattern.compile(".*");
-        return pattern.matcher(str).matches();
+    static final Pattern PATTERN = Pattern.compile(".*");public boolean myMethod(String str) {
+        
+        return PATTERN.matcher(str).matches();
     }
 }

As can be seen:

  • The new constant isn't declared private, while it should be.
  • The new constant is declared between the method and its Javadoc, such that the Javadoc now appears to apply to the constant. (And on a subsequent compilation round InvalidBlockTag will suggest that the @param block tag be removed.)
  • Although one will likely want to reformat the code anyway, it'd be nice if a newline were inserted after the semicolon.
copybara-service bot pushed a commit that referenced this issue Jul 23, 2020
Also fix the spacing issue.

Suggestions from #1655

flume hits : http://unknown commit

RELNOTES : Variable created by ConstantPatternCompile should be private. Also fix the spacing issue.
PiperOrigin-RevId: 322705160
copybara-service bot pushed a commit that referenced this issue Jul 23, 2020
Also fix the spacing issue.

Fixes #1655

flume hits : http://unknown commit

RELNOTES : Variable created by ConstantPatternCompile should be private. Also fix the spacing issue.
PiperOrigin-RevId: 322705160
copybara-service bot pushed a commit that referenced this issue Jul 23, 2020
Also fix the spacing issue.

Fixes #1655

flume hits : http://unknown commit

RELNOTES : Variable created by ConstantPatternCompile should be private. Also fix the spacing issue.
PiperOrigin-RevId: 322860215
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 a pull request may close this issue.

1 participant