Showing 117 of 158 total issues
Parameter name 'x' must match pattern '^[a-z][a-z0-9][a-zA-Z0-9]*$'. Open
void floodFill(int x, int y) {
- Read upRead up
- Exclude checks
Checks that method parameter names conform to a specified pattern.By using accessModifiers
property it is possibleto specify different formats for methods at different visibility levels.
To validate catch
parameters please useCatchParameterName.
To validate lambda parameters please useLambdaParameterName.
This documentation is written and maintained by the Checkstyle community and is covered under the same license as the Checkstyle project.
Parameter name 'y' must match pattern '^[a-z][a-z0-9][a-zA-Z0-9]*$'. Open
private void linearFill(int x, int y) {
- Read upRead up
- Exclude checks
Checks that method parameter names conform to a specified pattern.By using accessModifiers
property it is possibleto specify different formats for methods at different visibility levels.
To validate catch
parameters please useCatchParameterName.
To validate lambda parameters please useLambdaParameterName.
This documentation is written and maintained by the Checkstyle community and is covered under the same license as the Checkstyle project.
Member name 'y' must match pattern '^[a-z][a-z0-9][a-zA-Z0-9]*$'. Open
private int y;
- Read upRead up
- Exclude checks
Checks that instance variable names conform to a specified pattern.
This documentation is written and maintained by the Checkstyle community and is covered under the same license as the Checkstyle project.
Local variable name 'x' must match pattern '^[a-z][a-z0-9][a-zA-Z0-9]*$'. Open
int x = getRandomIntWithinBounds(paintView.getWidth());
- Read upRead up
- Exclude checks
Checks that local, non-final
variable names conform to a specified pattern.A catch parameter is considered to be a local variable.
This documentation is written and maintained by the Checkstyle community and is covered under the same license as the Checkstyle project.
'}' at column 31 should be alone on a line. Open
private RoomAttributes() {}
- Read upRead up
- Exclude checks
Checks the placement of right curly braces ('}'
) for code blocks.This check supports if-else, try-catch-finally blocks, while-loops, for-loops,method definitions, class definitions, constructor definitions,instance, static initialization blocks, annotation definitions and enum definitions.For right curly brace of expression blocks of arrays, lambdas and class instancesplease follow issue#5945.For right curly brace of enum constant please follow issue#7519.
This documentation is written and maintained by the Checkstyle community and is covered under the same license as the Checkstyle project.
Member name 'y' must match pattern '^[a-z][a-z0-9][a-zA-Z0-9]*$'. Open
private float y;
- Read upRead up
- Exclude checks
Checks that instance variable names conform to a specified pattern.
This documentation is written and maintained by the Checkstyle community and is covered under the same license as the Checkstyle project.
Local variable name 'y' must match pattern '^[a-z][a-z0-9][a-zA-Z0-9]*$'. Open
int y = getRandomIntWithinBounds(paintView.getHeight());
- Read upRead up
- Exclude checks
Checks that local, non-final
variable names conform to a specified pattern.A catch parameter is considered to be a local variable.
This documentation is written and maintained by the Checkstyle community and is covered under the same license as the Checkstyle project.
'}' at column 34 should be alone on a line. Open
private AccountAttributes() {}
- Read upRead up
- Exclude checks
Checks the placement of right curly braces ('}'
) for code blocks.This check supports if-else, try-catch-finally blocks, while-loops, for-loops,method definitions, class definitions, constructor definitions,instance, static initialization blocks, annotation definitions and enum definitions.For right curly brace of expression blocks of arrays, lambdas and class instancesplease follow issue#5945.For right curly brace of enum constant please follow issue#7519.
This documentation is written and maintained by the Checkstyle community and is covered under the same license as the Checkstyle project.
Parameter name 'x' must match pattern '^[a-z][a-z0-9][a-zA-Z0-9]*$'. Open
private void linearFill(int x, int y) {
- Read upRead up
- Exclude checks
Checks that method parameter names conform to a specified pattern.By using accessModifiers
property it is possibleto specify different formats for methods at different visibility levels.
To validate catch
parameters please useCatchParameterName.
To validate lambda parameters please useLambdaParameterName.
This documentation is written and maintained by the Checkstyle community and is covered under the same license as the Checkstyle project.
Catch Exception instead of Throwable. Open
} catch (Throwable throwable) {
- Read upRead up
- Exclude checks
Throwable
is the superclass of all errors and exceptions in Java. Error
is the superclass of all errors, which are not
meant to be caught by applications.
Catching either Throwable
or Error
will also catch OutOfMemoryError
and InternalError
, from
which an application should not attempt to recover.
Noncompliant Code Example
try { /* ... */ } catch (Throwable t) { /* ... */ } try { /* ... */ } catch (Error e) { /* ... */ }
Compliant Solution
try { /* ... */ } catch (RuntimeException e) { /* ... */ } try { /* ... */ } catch (MyException e) { /* ... */ }
See
- MITRE, CWE-396 - Declaration of Catch for Generic Exception
- C++ Core Guidelines E.14 - Use purpose-designed user-defined types as exceptions (not built-in types)
Catch Exception instead of Throwable. Open
} catch (Throwable throwable) {
- Read upRead up
- Exclude checks
Throwable
is the superclass of all errors and exceptions in Java. Error
is the superclass of all errors, which are not
meant to be caught by applications.
Catching either Throwable
or Error
will also catch OutOfMemoryError
and InternalError
, from
which an application should not attempt to recover.
Noncompliant Code Example
try { /* ... */ } catch (Throwable t) { /* ... */ } try { /* ... */ } catch (Error e) { /* ... */ }
Compliant Solution
try { /* ... */ } catch (RuntimeException e) { /* ... */ } try { /* ... */ } catch (MyException e) { /* ... */ }
See
- MITRE, CWE-396 - Declaration of Catch for Generic Exception
- C++ Core Guidelines E.14 - Use purpose-designed user-defined types as exceptions (not built-in types)
Make this anonymous inner class a lambda (sonar.java.source not set. Assuming 8 or greater.) Open
executeOnUiThread(new Runnable() {
- Read upRead up
- Exclude checks
Before Java 8, the only way to partially support closures in Java was by using anonymous inner classes. But the syntax of anonymous classes may seem unwieldy and unclear.
With Java 8, most uses of anonymous inner classes should be replaced by lambdas to highly increase the readability of the source code.
Note that this rule is automatically disabled when the project's sonar.java.source
is lower than 8
.
Noncompliant Code Example
myCollection.stream().map(new Mapper<String,String>() { public String map(String input) { return new StringBuilder(input).reverse().toString(); } }); Predicate<String> isEmpty = new Predicate<String> { boolean test(String myString) { return myString.isEmpty(); } }
Compliant Solution
myCollection.stream().map(input -> new StringBuilder(input).reverse().toString()); Predicate<String> isEmpty = myString -> myString.isEmpty();
Make this anonymous inner class a lambda (sonar.java.source not set. Assuming 8 or greater.) Open
runOnUiThread(new Runnable() {
- Read upRead up
- Exclude checks
Before Java 8, the only way to partially support closures in Java was by using anonymous inner classes. But the syntax of anonymous classes may seem unwieldy and unclear.
With Java 8, most uses of anonymous inner classes should be replaced by lambdas to highly increase the readability of the source code.
Note that this rule is automatically disabled when the project's sonar.java.source
is lower than 8
.
Noncompliant Code Example
myCollection.stream().map(new Mapper<String,String>() { public String map(String input) { return new StringBuilder(input).reverse().toString(); } }); Predicate<String> isEmpty = new Predicate<String> { boolean test(String myString) { return myString.isEmpty(); } }
Compliant Solution
myCollection.stream().map(input -> new StringBuilder(input).reverse().toString()); Predicate<String> isEmpty = myString -> myString.isEmpty();
Make this anonymous inner class a lambda (sonar.java.source not set. Assuming 8 or greater.) Open
activityRule.getActivity().runOnUiThread(new Runnable() {
- Read upRead up
- Exclude checks
Before Java 8, the only way to partially support closures in Java was by using anonymous inner classes. But the syntax of anonymous classes may seem unwieldy and unclear.
With Java 8, most uses of anonymous inner classes should be replaced by lambdas to highly increase the readability of the source code.
Note that this rule is automatically disabled when the project's sonar.java.source
is lower than 8
.
Noncompliant Code Example
myCollection.stream().map(new Mapper<String,String>() { public String map(String input) { return new StringBuilder(input).reverse().toString(); } }); Predicate<String> isEmpty = new Predicate<String> { boolean test(String myString) { return myString.isEmpty(); } }
Compliant Solution
myCollection.stream().map(input -> new StringBuilder(input).reverse().toString()); Predicate<String> isEmpty = myString -> myString.isEmpty();
Make this anonymous inner class a lambda (sonar.java.source not set. Assuming 8 or greater.) Open
activityRule.getActivity().runOnUiThread(new Runnable() {
- Read upRead up
- Exclude checks
Before Java 8, the only way to partially support closures in Java was by using anonymous inner classes. But the syntax of anonymous classes may seem unwieldy and unclear.
With Java 8, most uses of anonymous inner classes should be replaced by lambdas to highly increase the readability of the source code.
Note that this rule is automatically disabled when the project's sonar.java.source
is lower than 8
.
Noncompliant Code Example
myCollection.stream().map(new Mapper<String,String>() { public String map(String input) { return new StringBuilder(input).reverse().toString(); } }); Predicate<String> isEmpty = new Predicate<String> { boolean test(String myString) { return myString.isEmpty(); } }
Compliant Solution
myCollection.stream().map(input -> new StringBuilder(input).reverse().toString()); Predicate<String> isEmpty = myString -> myString.isEmpty();
Make this anonymous inner class a lambda (sonar.java.source not set. Assuming 8 or greater.) Open
runOnUiThread(new Runnable() {
- Read upRead up
- Exclude checks
Before Java 8, the only way to partially support closures in Java was by using anonymous inner classes. But the syntax of anonymous classes may seem unwieldy and unclear.
With Java 8, most uses of anonymous inner classes should be replaced by lambdas to highly increase the readability of the source code.
Note that this rule is automatically disabled when the project's sonar.java.source
is lower than 8
.
Noncompliant Code Example
myCollection.stream().map(new Mapper<String,String>() { public String map(String input) { return new StringBuilder(input).reverse().toString(); } }); Predicate<String> isEmpty = new Predicate<String> { boolean test(String myString) { return myString.isEmpty(); } }
Compliant Solution
myCollection.stream().map(input -> new StringBuilder(input).reverse().toString()); Predicate<String> isEmpty = myString -> myString.isEmpty();
Refactor this method to reduce its Cognitive Complexity from 17 to the 15 allowed. Open
private void retrieveDrawingsFromDatabaseStorage() {
- Read upRead up
- Exclude checks
Cognitive Complexity is a measure of how hard the control flow of a method is to understand. Methods with high Cognitive Complexity will be difficult to maintain.
See
Constructor has 12 parameters, which is greater than 7 authorized. Open
private Account(Context context, ConstantsWrapper constantsWrapper, String username,
- Read upRead up
- Exclude checks
A long parameter list can indicate that a new structure should be created to wrap the numerous parameters or that the function is doing too many things.
Noncompliant Code Example
With a maximum number of 4 parameters:
public void doSomething(int param1, int param2, int param3, String param4, long param5) { ... }
Compliant Solution
public void doSomething(int param1, int param2, int param3, String param4) { ... }
Exceptions
Methods annotated with :
- Spring's
@RequestMapping
(and related shortcut annotations, like@GetRequest
) - JAX-RS API annotations (like
@javax.ws.rs.GET
) - Bean constructor injection with
@org.springframework.beans.factory.annotation.Autowired
- CDI constructor injection with
@javax.inject.Inject
-
@com.fasterxml.jackson.annotation.JsonCreator
may have a lot of parameters, encapsulation being possible. Such methods are therefore ignored.
Remove this unused method parameter "view". Open
public boolean onTouch(View view, MotionEvent event) {
- Read upRead up
- Exclude checks
Unused parameters are misleading. Whatever the values passed to such parameters, the behavior will be the same.
Noncompliant Code Example
void doSomething(int a, int b) { // "b" is unused compute(a); }
Compliant Solution
void doSomething(int a) { compute(a); }
Exceptions
The rule will not raise issues for unused parameters:
- that are annotated with
@javax.enterprise.event.Observes
- in overrides and implementation methods
- in interface
default
methods - in non-private methods that only
throw
or that have empty bodies - in annotated methods, unless the annotation is
@SuppressWarning("unchecked")
or@SuppressWarning("rawtypes")
, in which case the annotation will be ignored - in overridable methods (non-final, or not member of a final class, non-static, non-private), if the parameter is documented with a proper javadoc.
@Override void doSomething(int a, int b) { // no issue reported on b compute(a); } public void foo(String s) { // designed to be extended but noop in standard case } protected void bar(String s) { //open-closed principle } public void qix(String s) { throw new UnsupportedOperationException("This method should be implemented in subclasses"); } /** * @param s This string may be use for further computation in overriding classes */ protected void foobar(int a, String s) { // no issue, method is overridable and unused parameter has proper javadoc compute(a); }
See
- CERT, MSC12-C. - Detect and remove code that has no effect or is never executed
Make this anonymous inner class a lambda (sonar.java.source not set. Assuming 8 or greater.) Open
runOnUiThread(new Runnable() {
- Read upRead up
- Exclude checks
Before Java 8, the only way to partially support closures in Java was by using anonymous inner classes. But the syntax of anonymous classes may seem unwieldy and unclear.
With Java 8, most uses of anonymous inner classes should be replaced by lambdas to highly increase the readability of the source code.
Note that this rule is automatically disabled when the project's sonar.java.source
is lower than 8
.
Noncompliant Code Example
myCollection.stream().map(new Mapper<String,String>() { public String map(String input) { return new StringBuilder(input).reverse().toString(); } }); Predicate<String> isEmpty = new Predicate<String> { boolean test(String myString) { return myString.isEmpty(); } }
Compliant Solution
myCollection.stream().map(input -> new StringBuilder(input).reverse().toString()); Predicate<String> isEmpty = myString -> myString.isEmpty();