Skip to content

Commit 003342e

Browse files
committed
[chore] Tidy code for modern Java versions
Addresses some deprecations in JRuby that have been removed in 10.1. (cherry picked from commit d633632 - excludes generics change to interfaces)
1 parent 4ba1b76 commit 003342e

11 files changed

Lines changed: 52 additions & 70 deletions

src/main/java/org/jruby/rack/DefaultErrorApplication.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,7 @@ static Exception getException(RackEnvironment env) {
7575
private class Response implements RackResponse {
7676

7777
private int status = 500;
78-
@SuppressWarnings("rawtypes")
79-
private Map headers = Collections.EMPTY_MAP;
78+
private Map<String, ?> headers = Collections.emptyMap();
8079
private String body;
8180

8281
protected final RackEnvironment env;
@@ -99,9 +98,9 @@ public Map getHeaders() {
9998
return headers;
10099
}
101100

102-
@SuppressWarnings("unused")
101+
@SuppressWarnings({"unused", "unchecked"})
103102
public void setHeaders(@SuppressWarnings("rawtypes") Map headers) {
104-
this.headers = headers == null ? Collections.EMPTY_MAP : headers;
103+
this.headers = headers == null ? Collections.emptyMap() : headers;
105104
}
106105

107106
@Override

src/main/java/org/jruby/rack/DefaultRackApplication.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,8 @@ public RackResponse call(final RackEnvironment env) {
5050
final IRubyObject app = getApplication();
5151
final Ruby runtime = getRuntime();
5252
final IRubyObject servlet_env = JavaEmbedUtils.javaToRuby(runtime, env);
53-
//try { // app.call(env) :
5453
final IRubyObject response = app.callMethod(runtime.getCurrentContext(), "call", servlet_env);
55-
return (RackResponse) response.toJava(RackResponse.class);
56-
//}
57-
//catch (RuntimeException e) {
58-
// throw ExceptionUtils.wrapException(runtime, e);
59-
//}
54+
return response.toJava(RackResponse.class);
6055
}
6156

6257
@Override

src/main/java/org/jruby/rack/DefaultRackApplicationFactory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ public IRubyObject createErrorApplicationObject(final Ruby runtime) {
202202

203203
public RackApplication newErrorApplication() {
204204
Boolean error = rackContext.getConfig().getBooleanProperty("jruby.rack.error");
205-
if ( error != null && ! error.booleanValue() ) { // jruby.rack.error = false
205+
if ( error != null && !error) { // jruby.rack.error = false
206206
return new DefaultErrorApplication(rackContext);
207207
}
208208
try {
@@ -585,7 +585,7 @@ private void configureDefaults() {
585585
if (iniSize == null) iniSize = RewindableInputStream.INI_BUFFER_SIZE;
586586
Integer maxSize = config.getMaximumMemoryBufferSize();
587587
if (maxSize == null) maxSize = RewindableInputStream.MAX_BUFFER_SIZE;
588-
if (iniSize.intValue() > maxSize.intValue()) iniSize = maxSize;
588+
if (iniSize > maxSize) iniSize = maxSize;
589589

590590
RewindableInputStream.setDefaultInitialBufferSize(iniSize);
591591
RewindableInputStream.setDefaultMaximumBufferSize(maxSize);

src/main/java/org/jruby/rack/DefaultRackConfig.java

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public boolean isSerialInitialization() {
130130
}
131131
}
132132
}
133-
return serial.booleanValue();
133+
return serial;
134134
}
135135

136136
@Override
@@ -282,8 +282,7 @@ public Map<String, String> getRuntimeEnvironment() {
282282
static boolean isIgnoreRUBYOPT(RackConfig config) {
283283
// RUBYOPT ignored if jruby.runtime.env.rubyopt = false
284284
Boolean rubyopt = config.getBooleanProperty("jruby.runtime.env.rubyopt");
285-
if ( rubyopt == null ) return ! config.isIgnoreEnvironment();
286-
return rubyopt != null && ! rubyopt.booleanValue();
285+
return rubyopt == null ? !config.isIgnoreEnvironment() : !rubyopt;
287286
}
288287

289288
@Override
@@ -297,14 +296,11 @@ public boolean isThrowInitException() {
297296

298297
static boolean isThrowInitException(RackConfig config) {
299298
Boolean error = config.getBooleanProperty("jruby.rack.error");
300-
if ( error != null && ! error.booleanValue() ) {
299+
if ( error != null && !error) {
301300
return true; // jruby.rack.error = false
302301
}
303302
error = config.getBooleanProperty("jruby.rack.exception");
304-
if ( error != null && ! error.booleanValue() ) {
305-
return true; // jruby.rack.exception = false
306-
}
307-
return false;
303+
return error != null && !error; // jruby.rack.exception = false
308304
}
309305

310306
@Override
@@ -350,7 +346,7 @@ private Integer getPositiveInteger(String key) {
350346
if (value == null) return null;
351347
try {
352348
int i = Integer.parseInt(value);
353-
if (i > 0) return Integer.valueOf(i);
349+
if (i > 0) return i;
354350
} catch (Exception e) { /* ignored */ }
355351
return null;
356352
}
@@ -382,12 +378,12 @@ public static Number toNumber(String value, Number defaultValue) {
382378
}
383379
if ( number == ((int) number) )
384380
if ( number > Integer.MAX_VALUE ) {
385-
return Long.valueOf((long) number);
381+
return (long) number;
386382
}
387383
else {
388-
return Integer.valueOf((int) number);
384+
return (int) number;
389385
}
390-
return Float.valueOf(number);
386+
return number;
391387
}
392388
catch (Exception e) { /* ignored */ }
393389
return defaultValue;
@@ -409,7 +405,7 @@ private Map<String, String> toStringMap(final String env) {
409405
for ( final String entry : entries ) {
410406
String[] pair = entry.split("=", 2);
411407
if ( pair.length == 1 ) { // no = separator
412-
if ( entry.trim().length() == 0 ) continue;
408+
if ( entry.trim().isEmpty() ) continue;
413409
if ( lastKey == null ) continue; // missing key
414410
map.put( lastKey, lastVal = lastVal + ',' + entry );
415411
}

src/main/java/org/jruby/rack/DefaultRackDispatcher.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ protected void afterException(
5555
// TODO seems redundant maybe we should let the container decide ?!
5656
context.log(RackLogger.ERROR, "error app failed to handle exception: " + e, re);
5757
Integer errorCode = getErrorApplicationFailureStatusCode();
58-
if ( errorCode != null && errorCode.intValue() > 0 ) {
58+
if ( errorCode != null && errorCode > 0 ) {
5959
response.sendError(errorCode);
6060
}
6161
else {

src/main/java/org/jruby/rack/PoolingRackApplicationFactory.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -390,14 +390,14 @@ private int getInitialPoolSizeWait() {
390390
if ( waitNum != null ) {
391391
int wait = waitNum.intValue();
392392
if (maximumSize != null && wait > maximumSize) {
393-
wait = maximumSize.intValue();
393+
wait = maximumSize;
394394
}
395395
return wait;
396396
}
397397
// otherwise we assume it to be a boolean true/false flag :
398398
Boolean waitFlag = getConfig().getBooleanProperty("jruby.runtime.init.wait");
399399
if ( waitFlag == null ) waitFlag = Boolean.TRUE;
400-
return waitFlag ? ( initialSize == null ? 1 : initialSize.intValue() ) : 0;
400+
return waitFlag ? ( initialSize == null ? 1 : initialSize) : 0;
401401
// NOTE: this slightly changes the behavior in 1.1.8, in previous
402402
// versions the initialization only waited for 1 application instance
403403
// to be available in the pool - here by default we wait till initial
@@ -406,7 +406,7 @@ private int getInitialPoolSizeWait() {
406406

407407
@Override
408408
public Collection<RackApplication> getManagedApplications() {
409-
int initSize = initialSize != null ? initialSize.intValue() : -1;
409+
int initSize = initialSize != null ? initialSize : -1;
410410
synchronized (applicationPool) {
411411
if ( applicationPool.isEmpty() ) {
412412
if ( initSize > 0 ) return null; // ~ init error

src/main/java/org/jruby/rack/UnmappedRackFilter.java

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@
88

99
import java.io.FileNotFoundException;
1010
import java.io.IOException;
11-
import java.util.Collection;
12-
import java.util.Collections;
13-
import java.util.HashSet;
14-
import java.util.Set;
11+
import java.util.*;
12+
import java.util.stream.Collectors;
1513
import javax.servlet.FilterChain;
1614
import javax.servlet.FilterConfig;
1715
import javax.servlet.ServletException;
@@ -46,7 +44,7 @@ public class UnmappedRackFilter extends AbstractFilter {
4644

4745
private static final Collection<Integer> RESPONSE_NOT_HANDLED_STATUSES;
4846
static {
49-
final HashSet<Integer> statuses = new HashSet<>(8, 1);
47+
final Set<Integer> statuses = new HashSet<>(8, 1);
5048
statuses.add( 404 );
5149
// 403 due containers not supporting PUT/DELETE correctly (Tomcat 6)
5250
statuses.add( 403 );
@@ -61,7 +59,7 @@ public class UnmappedRackFilter extends AbstractFilter {
6159
private RackContext context;
6260
private RackDispatcher dispatcher;
6361

64-
public UnmappedRackFilter() { /** constructor used by container */ }
62+
public UnmappedRackFilter() { /* constructor used by container */ }
6563

6664
/**
6765
* Dependency-injected constructor for testing
@@ -89,14 +87,11 @@ public void init(FilterConfig config) throws ServletException {
8987
// ResponseCapture.notHandledStatuses e.g. "403,404,500"
9088
value = config.getInitParameter("responseNotHandledStatuses");
9189
if ( value != null ) {
92-
final Set<Integer> statuses = new HashSet<>();
93-
for ( String status : value.split(",") ) {
94-
status = status.trim();
95-
if ( status.length() > 0 ) {
96-
statuses.add( Integer.parseInt(status) );
97-
}
98-
}
99-
responseNotHandledStatuses = statuses;
90+
responseNotHandledStatuses = Arrays.stream(value.split(","))
91+
.map(String::trim)
92+
.filter(status -> !status.isEmpty())
93+
.map(Integer::parseInt)
94+
.collect(Collectors.toSet());
10095
}
10196
// ResponseCapture.handledByDefault true/false (true by default)
10297
value = config.getInitParameter("responseHandledByDefault");
@@ -180,7 +175,7 @@ public boolean isResetUnhandledResponse() {
180175
}
181176

182177
public void setResetUnhandledResponse(boolean reset) {
183-
this.resetUnhandledResponse = Boolean.valueOf(reset);
178+
this.resetUnhandledResponse = reset;
184179
}
185180

186181
public boolean isResetUnhandledResponseBuffer() {
@@ -204,10 +199,9 @@ public Collection<Integer> getResponseNotHandledStatuses() {
204199
return this.responseNotHandledStatuses;
205200
}
206201

207-
@SuppressWarnings("unchecked")
208202
public void setDefaultNotHandledStatuses(final Collection<Integer> responseNotHandledStatuses) {
209203
this.responseNotHandledStatuses =
210-
responseNotHandledStatuses == null ? Collections.EMPTY_SET : responseNotHandledStatuses;
204+
responseNotHandledStatuses == null ? Collections.emptySet() : responseNotHandledStatuses;
211205
}
212206

213207
public boolean isResponseHandledByDefault() {

src/main/java/org/jruby/rack/ext/Response.java

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,12 @@
5050
import org.jruby.rack.RackException;
5151
import org.jruby.rack.RackResponse;
5252
import org.jruby.rack.RackResponseEnvironment;
53-
import org.jruby.runtime.Arity;
5453
import org.jruby.runtime.Block;
5554
import org.jruby.runtime.BlockBody;
5655
import org.jruby.runtime.Helpers;
5756
import org.jruby.runtime.JavaInternalBlockBody;
5857
import org.jruby.runtime.ObjectAllocator;
58+
import org.jruby.runtime.Signature;
5959
import org.jruby.runtime.ThreadContext;
6060
import org.jruby.runtime.Visibility;
6161
import org.jruby.runtime.builtin.IRubyObject;
@@ -93,7 +93,7 @@ public static IRubyObject is_swallow_client_abort(final ThreadContext context, f
9393
@JRubyMethod(name = "swallow_client_abort=", meta = true, required = 1)
9494
public static IRubyObject set_swallow_client_abort(final IRubyObject self, final IRubyObject value) {
9595
if ( value instanceof RubyBoolean ) {
96-
swallowClientAbort = ((RubyBoolean) value).isTrue();
96+
swallowClientAbort = value.isTrue();
9797
}
9898
else {
9999
swallowClientAbort = ! value.isNil();
@@ -124,7 +124,7 @@ public static IRubyObject is_dechunk(final ThreadContext context, final IRubyObj
124124
@JRubyMethod(name = "dechunk=", meta = true, required = 1)
125125
public static IRubyObject set_dechunk(final IRubyObject self, final IRubyObject value) {
126126
if ( value instanceof RubyBoolean ) {
127-
dechunk = ((RubyBoolean) value).isTrue();
127+
dechunk = value.isTrue();
128128
}
129129
else {
130130
dechunk = ! value.isNil();
@@ -138,7 +138,7 @@ public static IRubyObject set_dechunk(final IRubyObject self, final IRubyObject
138138
* Returns the channel chunk size to be used e.g. when a (send) file
139139
* response is detected. By setting this value to nil you force an "explicit"
140140
* byte buffer to be used when copying between channels.
141-
*
141+
* <p>
142142
* Note: High values won't hurt when sending small files since most Java
143143
* (file) channel implementations handle this gracefully. However if you're
144144
* on Windows it is recommended to not set this higher than the "magic"
@@ -169,7 +169,7 @@ public static IRubyObject set_channel_chunk_size(final IRubyObject self, final I
169169
}
170170
else {
171171
final long val = value.convertToInteger("to_i").getLongValue();
172-
channelChunkSize = Integer.valueOf((int) val);
172+
channelChunkSize = (int) val;
173173
}
174174
return value;
175175
}
@@ -204,7 +204,7 @@ public static IRubyObject set_channel_buffer_size(final IRubyObject self, final
204204
}
205205
else {
206206
final long val = value.convertToInteger("to_i").getLongValue();
207-
channelBufferSize = Integer.valueOf((int) val);
207+
channelBufferSize = (int) val;
208208
}
209209
return value;
210210
}
@@ -228,7 +228,7 @@ protected Response(Ruby runtime, RubyClass metaClass) {
228228
@JRubyMethod(required = 1)
229229
public IRubyObject initialize(final ThreadContext context, final IRubyObject arg) {
230230
if ( arg instanceof RubyArray ) {
231-
final RubyArray arr = (RubyArray) arg;
231+
final RubyArray<?> arr = (RubyArray<?>) arg;
232232
if ( arr.size() < 3 ) {
233233
throw context.runtime.newArgumentError("expected 3 array elements (rack-respose)");
234234
}
@@ -297,10 +297,10 @@ public String getBody() {
297297
try {
298298
final StringBuilder bodyParts = new StringBuilder();
299299
invoke(context, this.body, "each",
300-
new JavaInternalBlockBody(context.runtime, Arity.ONE_REQUIRED) {
300+
new JavaInternalBlockBody(context.runtime, Signature.ONE_REQUIRED) {
301301
@Override
302302
public IRubyObject yield(ThreadContext context, IRubyObject[] args) {
303-
return yield(context, args[0]);
303+
return this.yield(context, args[0]);
304304
}
305305

306306
@Override
@@ -347,7 +347,7 @@ public void respond(final RackResponseEnvironment response) throws RackException
347347

348348
@JRubyMethod(name = "write_status")
349349
public IRubyObject write_status(final ThreadContext context, final IRubyObject response) {
350-
writeStatus( (RackResponseEnvironment) response.toJava(RackResponseEnvironment.class) );
350+
writeStatus(response.toJava(RackResponseEnvironment.class));
351351
return context.nil;
352352
}
353353

@@ -358,7 +358,7 @@ protected void writeStatus(final RackResponseEnvironment response) {
358358
@JRubyMethod(name = "write_headers")
359359
public IRubyObject write_headers(final ThreadContext context, final IRubyObject response)
360360
throws IOException {
361-
writeHeaders( (RackResponseEnvironment) response.toJava(RackResponseEnvironment.class) );
361+
writeHeaders(response.toJava(RackResponseEnvironment.class));
362362
return context.nil;
363363
}
364364

@@ -394,10 +394,10 @@ public void visit(final IRubyObject key, final IRubyObject val) {
394394
final RubyString newLine = RubyString.newString(context.runtime, NEW_LINE);
395395
// value.each_line { |val| response.addHeader(key.to_s, val.chomp("\n")) }
396396
invoke(context, val, each_line ? "each_line" : "each",
397-
new JavaInternalBlockBody(context.runtime, Arity.ONE_REQUIRED) {
397+
new JavaInternalBlockBody(context.runtime, Signature.ONE_REQUIRED) {
398398
@Override
399399
public IRubyObject yield(ThreadContext context, IRubyObject[] args) {
400-
return yield(context, args[0]);
400+
return this.yield(context, args[0]);
401401
}
402402

403403
@Override
@@ -430,7 +430,7 @@ else if ( val instanceof RubyTime ) {
430430
@JRubyMethod(name = "write_body")
431431
public IRubyObject write_body(final ThreadContext context, final IRubyObject response)
432432
throws IOException {
433-
writeBody( (RackResponseEnvironment) response.toJava(RackResponseEnvironment.class) );
433+
writeBody(response.toJava(RackResponseEnvironment.class));
434434
return context.nil;
435435
}
436436

@@ -463,7 +463,7 @@ protected void writeBody(final RackResponseEnvironment response) throws IOExcept
463463
else {
464464
final ThreadContext context = currentContext();
465465
final IRubyObject channel = body.callMethod(context, "to_channel");
466-
bodyChannel = (Channel) channel.toJava(Channel.class);
466+
bodyChannel = channel.toJava(Channel.class);
467467
}
468468
if ( bodyChannel instanceof FileChannel ) {
469469
transferChannel( (FileChannel) bodyChannel, response.getOutputStream() );
@@ -486,10 +486,10 @@ protected void writeBody(final RackResponseEnvironment response) throws IOExcept
486486
final String method = body.respondsTo("each_line") ? "each_line" : "each";
487487
try {
488488
invoke(context, body, method,
489-
new JavaInternalBlockBody(context.runtime, Arity.ONE_REQUIRED) {
489+
new JavaInternalBlockBody(context.runtime, Signature.ONE_REQUIRED) {
490490
@Override
491491
public IRubyObject yield(ThreadContext context, IRubyObject[] args) {
492-
return yield(context, args[0]);
492+
return this.yield(context, args[0]);
493493
}
494494

495495
@Override

src/main/java/org/jruby/rack/servlet/RequestCapture.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ private boolean parseRequestParams() {
155155
}
156156
params.put(key, newValues);
157157
}
158-
} catch (UnsupportedEncodingException e) { /* UTF-8 should be fine */ }
158+
} catch (UnsupportedEncodingException ignore) { /* UTF-8 should be fine */ }
159159
}
160160

161161
this.requestParams = params;

0 commit comments

Comments
 (0)