Skip to content

Commit 54536da

Browse files
committed
[code quality] Sonar bug fixing
1 parent 78622e6 commit 54536da

File tree

27 files changed

+945
-919
lines changed

27 files changed

+945
-919
lines changed

build/sonar.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/bash
2+
mvn -DdryRun=true -Dsonar.organization=jooby -Dsonar.host.url=https://sonarcloud.io -Dsonar.jacoco.reportPaths=modules/coverage-report/target/jacoco.exec -Dsonar.login=$SONAR_TOKEN -Dsonar.branch=master clean install coveralls:report sonar:sonar -P coverage

jooby/src/main/java/org/jooby/Asset.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -206,10 +206,12 @@
206206
import static java.util.Objects.requireNonNull;
207207

208208
import java.io.InputStream;
209+
import java.net.URISyntaxException;
209210
import java.net.URL;
210211

211212
import com.google.common.io.BaseEncoding;
212213
import com.google.common.primitives.Longs;
214+
import org.jooby.funzy.Throwing;
213215

214216
import javax.annotation.Nonnull;
215217

@@ -320,16 +322,20 @@ default String name() {
320322
*/
321323
@Nonnull
322324
default String etag() {
323-
StringBuilder b = new StringBuilder(32);
324-
b.append("W/\"");
325+
try {
326+
StringBuilder b = new StringBuilder(32);
327+
b.append("W/\"");
325328

326-
BaseEncoding b64 = BaseEncoding.base64();
327-
int lhash = resource().hashCode();
329+
BaseEncoding b64 = BaseEncoding.base64();
330+
int lhash = resource().toURI().hashCode();
328331

329-
b.append(b64.encode(Longs.toByteArray(lastModified() ^ lhash)));
330-
b.append(b64.encode(Longs.toByteArray(length() ^ lhash)));
331-
b.append('"');
332-
return b.toString();
332+
b.append(b64.encode(Longs.toByteArray(lastModified() ^ lhash)));
333+
b.append(b64.encode(Longs.toByteArray(length() ^ lhash)));
334+
b.append('"');
335+
return b.toString();
336+
} catch (URISyntaxException x) {
337+
throw Throwing.sneakyThrow(x);
338+
}
333339
}
334340

335341
/**

jooby/src/main/java/org/jooby/Deferred.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@
206206
import static java.util.Objects.requireNonNull;
207207

208208
import javax.annotation.Nonnull;
209+
import javax.annotation.Nullable;
209210
import java.util.Optional;
210211
import java.util.concurrent.Callable;
211212
import java.util.concurrent.Executor;
@@ -368,7 +369,7 @@ public interface Initializer {
368369
* @since 0.10.0
369370
*/
370371
public interface Handler {
371-
void handle(Result result, Throwable exception);
372+
void handle(@Nullable Result result, Throwable exception);
372373
}
373374

374375
/** Deferred initializer. Optional. */
@@ -415,7 +416,7 @@ public Deferred(final Initializer initializer) {
415416
* @param executor Executor to use.
416417
* @param initializer An initializer.
417418
*/
418-
public Deferred(final String executor, final Initializer initializer) {
419+
public Deferred(@Nullable final String executor, final Initializer initializer) {
419420
this.executor = executor;
420421
this.initializer = requireNonNull(initializer, "Initializer is required.");
421422
this.callerThread = Thread.currentThread().getName();
@@ -470,8 +471,7 @@ public String callerThread() {
470471
*
471472
* @param value A value for this deferred.
472473
*/
473-
@Nonnull
474-
public void resolve(final Object value) {
474+
public void resolve(@Nullable final Object value) {
475475
if (value == null) {
476476
handler.handle(null, null);
477477
} else {

jooby/src/main/java/org/jooby/Env.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,7 @@ interface Builder {
493493
* @param config A config instance.
494494
* @return A new environment.
495495
*/
496+
@Nonnull
496497
default Env build(final Config config) {
497498
return build(config, null, Locale.getDefault());
498499
}
@@ -507,7 +508,8 @@ default Env build(final Config config) {
507508
* @param locale App locale.
508509
* @return A new environment.
509510
*/
510-
Env build(Config config, Router router, Locale locale);
511+
@Nonnull
512+
Env build(Config config, @Nullable Router router, Locale locale);
511513
}
512514

513515
/**

jooby/src/main/java/org/jooby/Err.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,8 @@
214214

215215
import com.google.common.base.Throwables;
216216

217+
import javax.annotation.Nullable;
218+
217219
/**
218220
* An exception that carry a {@link Status}. The status field will be set in the HTTP
219221
* response.
@@ -484,7 +486,7 @@ public Map<String, Object> toMap(boolean stacktrace) {
484486
* @param tail A message to append.
485487
* @return An error message.
486488
*/
487-
private static String message(final Status status, final String tail) {
489+
private static String message(final Status status, @Nullable final String tail) {
488490
return message(status.reason(), status.value(), tail);
489491
}
490492

@@ -496,7 +498,7 @@ private static String message(final Status status, final String tail) {
496498
* @param tail A message to append.
497499
* @return An error message.
498500
*/
499-
private static String message(final String reason, final int status, final String tail) {
501+
private static String message(final String reason, final int status, @Nullable final String tail) {
500502
return reason + "(" + status + ")" + (tail == null ? "" : ": " + tail);
501503
}
502504

jooby/src/main/java/org/jooby/Jooby.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@
288288
import org.slf4j.LoggerFactory;
289289

290290
import javax.annotation.Nonnull;
291+
import javax.annotation.Nullable;
291292
import javax.inject.Singleton;
292293
import javax.net.ssl.SSLContext;
293294
import java.io.File;
@@ -925,7 +926,7 @@ public Jooby use(final Jooby app) {
925926
return use(prefixPath(null), app);
926927
}
927928

928-
private Optional<String> prefixPath(String tail) {
929+
private Optional<String> prefixPath(@Nullable String tail) {
929930
return path.size() == 0
930931
? tail == null ? Optional.empty() : Optional.of(Route.normalize(tail))
931932
: Optional.of(path.stream()
@@ -2196,7 +2197,7 @@ private void start(final String[] args, final Consumer<List<Route.Definition>> r
21962197
this.injector = bootstrap(args(args), routes);
21972198

21982199
// shutdown hook
2199-
Runtime.getRuntime().addShutdownHook(new Thread(() -> stop()));
2200+
Runtime.getRuntime().addShutdownHook(new Thread(this::stop));
22002201

22012202
Config conf = injector.getInstance(Config.class);
22022203

@@ -3395,11 +3396,10 @@ private Config defaultConfig(final Config conf, final String cpath) {
33953396
defs = defs.withValue("application.charset", fromAnyRef(charset.name()));
33963397
}
33973398
if (port != null) {
3398-
defs = defs.withValue("application.port", fromAnyRef(port.intValue()));
3399+
defs = defs.withValue("application.port", fromAnyRef(port));
33993400
}
34003401
if (securePort != null) {
3401-
defs = defs.withValue("application.securePort",
3402-
fromAnyRef(securePort.intValue()));
3402+
defs = defs.withValue("application.securePort", fromAnyRef(securePort));
34033403
}
34043404
if (dateFormat != null) {
34053405
defs = defs.withValue("application.dateFormat", fromAnyRef(dateFormat));
@@ -3495,8 +3495,8 @@ static String logback(final Config conf) {
34953495
files.add(new File(confdir, "logback.xml"));
34963496
logback = files.build()
34973497
.stream()
3498-
.filter(f -> f.exists())
3499-
.map(f -> f.getAbsolutePath())
3498+
.filter(File::exists)
3499+
.map(File::getAbsolutePath)
35003500
.findFirst()
35013501
.orElseGet(() -> {
35023502
return Optional.ofNullable(Jooby.class.getResource("/logback." + env + ".xml"))

jooby/src/main/java/org/jooby/MediaType.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -438,19 +438,19 @@ private Optional<MediaType> doFirst(final List<MediaType> candidates) {
438438
public static final List<MediaType> ALL = ImmutableList.of(MediaType.all);
439439

440440
/** Form multipart-data media type. */
441-
public static MediaType multipart = new MediaType("multipart", "form-data");
441+
public static final MediaType multipart = new MediaType("multipart", "form-data");
442442

443443
/** Form url encoded. */
444-
public static MediaType form = new MediaType("application", "x-www-form-urlencoded");
444+
public static final MediaType form = new MediaType("application", "x-www-form-urlencoded");
445445

446446
/** Xml media type. */
447-
public static MediaType xml = new MediaType("application", "xml");
447+
public static final MediaType xml = new MediaType("application", "xml");
448448

449449
/** Server sent event type. */
450-
public static MediaType sse = new MediaType("text", "event-stream");
450+
public static final MediaType sse = new MediaType("text", "event-stream");
451451

452452
/** Xml like media type. */
453-
private static MediaType xmlLike = new MediaType("application", "*+xml");
453+
private static final MediaType xmlLike = new MediaType("application", "*+xml");
454454

455455
/**
456456
* Track the type of this media type.

jooby/src/main/java/org/jooby/Request.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@
212212
import org.jooby.scope.RequestScoped;
213213

214214
import javax.annotation.Nonnull;
215+
import javax.annotation.Nullable;
215216
import java.io.IOException;
216217
import java.nio.charset.Charset;
217218
import java.util.List;
@@ -397,7 +398,7 @@ public boolean is(final String... types) {
397398
@Override
398399
public boolean isSet(final String name) {
399400
return req.isSet(name);
400-
};
401+
}
401402

402403
@Override
403404
public Mutant params() {
@@ -1338,7 +1339,7 @@ default Flash flash() throws Err {
13381339
* @return This request.
13391340
*/
13401341
@Nonnull
1341-
default Request flash(final String name, final Object value) {
1342+
default Request flash(final String name, @Nullable final Object value) {
13421343
requireNonNull(name, "Attribute's name is required.");
13431344
Map<String, String> flash = flash();
13441345
if (value == null) {

jooby/src/main/java/org/jooby/Response.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@
216216
import com.google.common.collect.ImmutableList;
217217

218218
import javax.annotation.Nonnull;
219+
import javax.annotation.Nullable;
219220

220221
/**
221222
* Give you access to the actual HTTP response. You can read/write headers and write HTTP body.
@@ -635,7 +636,7 @@ default Response type(final String type) {
635636
* @param result The HTTP body.
636637
* @throws Exception If the response write fails.
637638
*/
638-
default void send(final Object result) throws Throwable {
639+
default void send(final @Nullable Object result) throws Throwable {
639640
if (result instanceof Result) {
640641
send((Result) result);
641642
} else if (result != null) {

jooby/src/main/java/org/jooby/Result.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -497,8 +497,7 @@ public <T> T get(final List<MediaType> types) {
497497
public Result header(final String name, final Object value) {
498498
requireNonNull(name, "Header's name is required.");
499499
requireNonNull(value, "Header's value is required.");
500-
501-
put(name, value);
500+
setHeader(name, value);
502501
return this;
503502
}
504503

@@ -530,8 +529,7 @@ public Result header(final String name, final Object... values) {
530529
public Result header(final String name, final Iterable<Object> values) {
531530
requireNonNull(name, "Header's name is required.");
532531
requireNonNull(values, "Header's values are required.");
533-
534-
put(name, values);
532+
setHeader(name, values);
535533
return this;
536534
}
537535

@@ -545,7 +543,7 @@ protected Result clone() {
545543
return result;
546544
}
547545

548-
private void put(final String name, final Object val) {
546+
private void setHeader(final String name, final Object val) {
549547
headers = ImmutableMap.<String, Object> builder()
550548
.putAll(headers)
551549
.put(name, val)

0 commit comments

Comments
 (0)