sgammon/GUST

View on GitHub
java/gust/backend/builtin/SitemapHandler.java

Summary

Maintainability
A
45 mins
Test Coverage

Avoid deeply nested control flow statements.
Open

            if (isSet(canonical)) {
              //noinspection OptionalGetWithoutIsPresent
              String lastModifiedValue = lastModified.get();
              if (lastModifiedValue.length() == 10)
                entry.setLastModified(lastModifiedValue);
Severity: Major
Found in java/gust/backend/builtin/SitemapHandler.java - About 45 mins to fix

Refactor this method to reduce its Cognitive Complexity from 23 to the 15 allowed.
Open

  private @Nonnull HttpResponse<PageRender> renderSitemap(

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

Call "lastModified.isPresent()" before accessing the value.
Open

              String lastModifiedValue = lastModified.get();

Optional value can hold either a value or not. The value held in the Optional can be accessed using the get() method, but it will throw a

NoSuchElementException if there is no value present. To avoid the exception, calling the isPresent() or ! isEmpty() method should always be done before any call to get().

Alternatively, note that other methods such as orElse(...), orElseGet(...) or orElseThrow(...) can be used to specify what to do with an empty Optional.

Noncompliant Code Example

Optional<String> value = this.getOptionalValue();

// ...

String stringValue = value.get(); // Noncompliant

Compliant Solution

Optional<String> value = this.getOptionalValue();

// ...

if (value.isPresent()) {
  String stringValue = value.get();
}

or

Optional<String> value = this.getOptionalValue();

// ...

String stringValue = value.orElse("default");

See

Ensure this "Optional" could never be null and remove this null-check.
Open

    if (value == null || value.isEmpty())

The concept of Optional is that it will be used when null could cause errors. In a way, it replaces null, and when Optional is in use, there should never be a question of returning or receiving null from a call.

Noncompliant Code Example

public void doSomething () {
  Optional<String> optional = getOptional();
  if (optional != null) {  // Noncompliant
    // do something with optional...
  }
  Optional<String> text = null; // Noncompliant, a variable whose type is Optional should never itself be null
  // ...
}

@Nullable // Noncompliant
public Optional<String> getOptional() {
  // ...
  return null;  // Noncompliant
}

Compliant Solution

public void doSomething () {
  Optional<String> optional = getOptional();
  optional.ifPresent(
    // do something with optional...
  );
  Optional<String> text = Optional.empty();
  // ...
}

public Optional<String> getOptional() {
  // ...
  return Optional.empty();
}

Use already-defined constant 'sitemapKey' instead of duplicating its value here.
Open

        Optional<Boolean> enableSitemap = pageAnnotation.booleanValue("sitemap");

Duplicated string literals make the process of refactoring error-prone, since you must be sure to update all occurrences.

On the other hand, constants can be referenced from many places, but only need to be updated in a single place.

Noncompliant Code Example

With the default threshold of 3:

public void run() {
  prepare("action1");                              // Noncompliant - "action1" is duplicated 3 times
  execute("action1");
  release("action1");
}

@SuppressWarning("all")                            // Compliant - annotations are excluded
private void method1() { /* ... */ }
@SuppressWarning("all")
private void method2() { /* ... */ }

public String method3(String a) {
  System.out.println("'" + a + "'");               // Compliant - literal "'" has less than 5 characters and is excluded
  return "";                                       // Compliant - literal "" has less than 5 characters and is excluded
}

Compliant Solution

private static final String ACTION_1 = "action1";  // Compliant

public void run() {
  prepare(ACTION_1);                               // Compliant
  execute(ACTION_1);
  release(ACTION_1);
}

Exceptions

To prevent generating some false-positives, literals having less than 5 characters are excluded.

WhitespaceAround: 'for' is not followed by whitespace. Empty blocks may only be represented as {} when not part of a multi-block statement (4.1.3)
Open

    for(BeanDefinition definition : definitions) {

Checks that a token is surrounded by whitespace. Empty constructor,method, class, enum, interface, loop bodies (blocks), lambdas of the form

<source>public MyClass() {} // empty constructor<br>public void func() {} // empty method<br>public interface Foo {} // empty interface<br>public class Foo {} // empty class<br>public enum Foo {} // empty enum<br>MyClass c = new MyClass() {}; // empty anonymous class<br>while (i = 1) {} // empty while loop<br>for (int i = 1; i &gt; 1; i++) {} // empty for loop<br>do {} while (i = 1); // empty do-while loop<br>Runnable noop = () -&gt; {}; // empty lambda<br>public @interface Beta {} // empty annotation type<br> </source>

may optionally be exempted from the policy using the allowEmptyMethods, allowEmptyConstructors,allowEmptyTypes, allowEmptyLoops,allowEmptyLambdas and allowEmptyCatchesproperties.

This check does not flag as violation double brace initialization like:

new Properties() {{setProperty("key", "value");}};

Parameter allowEmptyCatches allows to suppress violations when tokenlist contains SLIST to check if beginning of block is surrounded bywhitespace and catch block is empty, for example:

try {k = 5 / i;} catch (ArithmeticException ex) {}

With this property turned off, this raises violation because the beginning of thecatch block (left curly bracket) is not separated from the end of the catchblock (right curly bracket).

This documentation is written and maintained by the Checkstyle community and is covered under the same license as the Checkstyle project.

'static' modifier out of order with the JLS suggestions.
Open

  private final static @Nonnull String sitemapKey = "sitemap";

Checks that the order of modifiers conforms to the suggestions inthe JavaLanguage specification, ยง 8.1.1, 8.3.1, 8.4.3 and9.4. The correct order is:

  1. public
  2. protected
  3. private
  4. abstract
  5. default
  6. static
  7. final
  8. transient
  9. volatile
  10. synchronized
  11. native
  12. strictfp

In additional, modifiers are checked to ensure all annotations aredeclared before all other modifiers.

Rationale: Code is easier to read if everybody follows a standard.

ATTENTION: We skiptype annotations from validation.

This documentation is written and maintained by the Checkstyle community and is covered under the same license as the Checkstyle project.

Rename this constant name to match the regular expression '^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$'.
Open

  private final static @Nonnull String sitemapKey = "sitemap";

Shared coding conventions allow teams to collaborate efficiently. This rule checks that all constant names match a provided regular expression.

Noncompliant Code Example

With the default regular expression ^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$:

public class MyClass {
  public static final int first = 1;
}

public enum MyEnum {
  first;
}

Compliant Solution

public class MyClass {
  public static final int FIRST = 1;
}

public enum MyEnum {
  FIRST;
}

There are no issues that match your filters.

Category
Status