This is a list of all the rules that are currently implemented in jSparrow.
Coding Conventions, Formatting, Free, Java 1.1, Java 1.2, Java 1.3, Java 1.4, Java 1.5, Java 1.8, Java 10, Java 5, Java 6, Java 7, Java 8, Java 9, Lambda, Logging, Loop, Old Language Constructs, Performance, Readability, String Manipulation
Free rules in jSparrow Starter
The following rules are included in jSparrow Starter. See the documentation at: Registration for 15 free rules.
Simplifies arithmetic operations that an compound operator where possible.
Checks if braces are used to encapsulate control statements and adds them if they aren't present.
Replaces While-Loops and For-Loops that are using 'BufferedReader::readLine' to iterate through lines of a file by a stream generated with 'BufferedReader::lines'.
Collapses, when possible, the nested if-statements into a single one by concatenating the conditions with the infix operator &&.
Simplifies the code by replacing all occurrences of 'removeAll()' which have the current collection as parameter with 'clear()'.
Replaces the invocations of 'Collections.unmodifiableList/Set/Map' with the corresponding factory method 'List.of', 'Set.of' and 'Map.ofEntries' introduced in Java 9.
Some 'java.util.Date' constructors like 'new Date(int year, int month, int day)', 'new Date(int year, int month, int date, int hrs, int min)' and 'new Date(int year, int month, int date, int hrs, int min, int sec)' are deprecated. A 'Calendar' instance should be used instead. This rule searches for deprecated date constructors, introduces calendar instances, sets the time corresponding to the parameters in the deprecated constructor, and replaces the latter with an invocation of 'Calendar.getTime()'.
Since Java 1.7 the Diamond Operator (<>) can be used to simplify instance creation where generics are involved.
Replaces occurrences of enhanced for-loops which are only used to initialize or return a boolean variable with 'Stream::anyMatch', 'Stream::allMatch' or 'Stream::noneMatch'. The stream syntax is more concise and improves readability.
Replaces enhanced for-loops which are used to find an element within a collection by a stream and uses 'Stream::findFirst' to find the result. By using the stream syntax, a multi-line control statement can be reduced to a single line.
This rule replaces enhanced for loops (for-each-loops) with an invocation of 'java.util.stream.Stream::forEach' method and passes the body of the for-loop as a lambda Consumer parameter.
Transforms enhanced for-loops which are only used for summing up the elements of a collection to a 'Stream::sum' invocation.
The Stream API in Java 9 is extended with the 'takeWhile' method to get the prefix of a stream. This rule replaces the enhanced for-loops with a stream iterating over the prefix of a collection with 'Stream::takeWhile'.
Replaces occurrences of 'equals()' on Enum constants with an identity comparison (==). In the case the equals relation is wrapped with an boolean negation the result will be an not equals (!=).
Renames the non-final fields to comply with the naming convention "^[a-z][a-zA-Z0-9]*$" i.e. a lower case prefix followed by any sequence of alpha-numeric characters
Nested For-Loops or invocations of forEach commonly used to iterate over all elements of a collection of collections, can be avoided by using flatMap(). Using 'flatMap()' makes code much more readable and can be combined with other stream functions.
Transforms all possible for loops with iterators to a ForEach loop.
Converts anonymous inner classes to equivalent lambda expressions.
Hides the default constructor of utility classes by adding a private constructor.
Wraps the initialization of a final collection with 'Collections.unmodifiable...()'
This rule replaces calls to 'indexOf()' on instances of Strings or Collections with calls to the 'contains()' method. 'contains()' was introduced in Java 1.4 and helps to make the code more readable.
All calls to a constructor of a primitive type will be replaced by the corresponding static 'valueOf()' method. For example 'new Integer("1")' becomes 'Integer.valueOf("1")'.
Finds the Enhanced For-loops whose sole purpose is to compute a boolean value without causing side effects and inserts a break statement immediately after the boolean value is computed. Thus, eliminating redundant loop iterations.
Replaces 'Stream::forEach' with 'Stream::collect' if the argument of the 'forEach' statement is only used for adding elements to a list. This simplifies adding elements to a list.
This rule transforms an if-Statement (without an else statement), which wraps the whole execution block of a 'Stream::forEach' method into a call to 'Stream::filter' with a lambda expression (Predicate) as parameter. This lambda is constructed using the expression from the if-Statement.
This rule replaces the types on the local variable declarations with the reserved word var introduced in Java 10.
This rule declares private fields and local variables final, if they are effectively final
Java 8 introduced 'Map::getOrDefault' which offers the possibility to return a default value if the map does not contain a mapping for the given key. This rule replaces the invocations of 'Map::get' followed by a null-check with 'Map::getOrDefault'.
Java 7 introduced the possibility to merge multiple catch clauses into a single multi-catch clause. Merging is only possible if the catch statements are identical.
Multiple field or variable declarations on the same line could cause confusion about their types and initial values. That also makes it harder to read and to understand the code. In order to improve readability, each field or variable should be declared on a separate line. This is recommended by the Code Conventions for the Java Programming Language.
It is common to have an else-statement following an Optional.isPresent check. One of the extensions of the Optional API in Java 9 is Optional.ifPresentOrElse, which performs either a Consumer or a Runnable depending on the presence of the value. This rule replaces an 'isPresent' check followed by an else-statement with a single 'ifPresentOrElse' invocation.
The usage of 'Optional.get' should be avoided in general because it can potentially throw a NoSuchElementException (it is likely to be deprecated in future releases). It is often the case that the invocation of 'Optional.get' is wrapped by a condition that uses 'Optional.isPresent'. Such cases can be replaced with the 'Optional.ifPresent(Consumer<? super T> consumer)'.
Organize Imports according to the Eclipse's built in Organize Imports functionality.
This rule adds the '@Override' annotation to methods overriding or implementing parent class methods.
This rule replaces the infix operators "==" and "!=" with 'equals()' when used on primitive objects.
When calling 'toString()' on a boxed primitive no new instance of that primitive has to be created. This rule replaces occurrences of such code with a static method
If 'map.put(..)' is wrapped with a condition verifying the existence of an element one can use 'map.putIfAbsent(...)' instead.
This rule removes interfaces from class declaration, which are already implemented by a super class. These interfaces are inherited from the super class.
This rule rearranges the body declarations of a class based on the Oracle Code Convention for File Organization.
Removes unnecessary explicit call to the default constructor of the super class.
Removes the 'public' modifiers from method declarations and 'public static final' modifiers from field declarations in Java interfaces.
Removes all class instantiations from 'String' if its parameter is empty or a 'String'.
Finds and removes null-checks before occurrences of instanceof. Since null is not an instance of anything, the null-check is redundant.
Removes all invocations of 'toString()' method used on a 'String' element.
Finds and removes unused parameters in private method declarations. Updates the affected method invocations accordingly.
Removes the RuntimeExceptions, duplications and exceptions which are subtypes of already thrown exceptions on the method signatures.
Adds the modifiers static and final to SerialVersionUid long variables when they are absent.
This rule reorders the modifiers on Type, Field and Method declarations.
Replaces the standard output statements with logger statements when possible.
If the body of the lambda statement contains only a single expression, the braces are optional. It can be reduced to a lambda expression. This is comparable to if-statements or loops with a single expression inside their body, where braces are also optional.
This rule changes the type of local variables from 'StringBuffer()' to 'StringBuilder()'.
This rule refactors the enhanced for loops which are only being used for concatenating the elements of collections or arrays.
This rule replaces the 'concat()' method on 'Strings' with the + operator.
It is generally preferable better to use "%n", which will produce the platform-specific line separator.
Moves the string literals in the left-hand-side of 'equals()' or 'equalsIgnoreCase()' when checking for equality.
Replaces various 'String' methods with their null-safe counterparts from 'StringUtils'.
This rule adds the try-with-resources statement introduced in Java 7. Closing statements are removed as the construct takes care of that.
Replaces Arrays.asList with 0 or 1 parameters respectively with Collections.emptyList() or Collections.singletonList(..)
This rule replaces comparisons of 'length()' or 'size()' with 0 with a call to 'isEmpty()'. Any occurrences of such a comparison will be replaced, regardless of surrounding control structure.
Replace static invocations of 'Collections.sort(List, Comparator)' with 'List.sort(Comparator)'.
Replaces the infix operator + on String concatenation by 'StringBuilder::append'
While-loops over Iterators which could be expressed with a for-loop, are transformed to an equivalent for-loop.
This rule simplifies expression lambdas by using method reference. The rule can only be applied if the parameters of the lambda expression and the method match.