Skip to content

Commit 560ea69

Browse files
author
Isabel Drost-Fromm
committed
Remove several code warnings.
Enabled check for resource leakage, (un-)boxing, missing annotations and unused exceptions. Fixing warnings raised by those checks.
1 parent 627449a commit 560ea69

File tree

9 files changed

+115
-89
lines changed

9 files changed

+115
-89
lines changed

src/main/java/org/elasticsearch/index/query/BaseTermQueryBuilder.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public BaseTermQueryBuilder(String fieldName, String value) {
5959
* @param fieldName The name of the field
6060
* @param value The value of the term
6161
*/
62+
@SuppressWarnings("boxing")
6263
public BaseTermQueryBuilder(String fieldName, int value) {
6364
this(fieldName, (Object) value);
6465
}
@@ -69,6 +70,7 @@ public BaseTermQueryBuilder(String fieldName, int value) {
6970
* @param fieldName The name of the field
7071
* @param value The value of the term
7172
*/
73+
@SuppressWarnings("boxing")
7274
public BaseTermQueryBuilder(String fieldName, long value) {
7375
this(fieldName, (Object) value);
7476
}
@@ -79,6 +81,7 @@ public BaseTermQueryBuilder(String fieldName, long value) {
7981
* @param fieldName The name of the field
8082
* @param value The value of the term
8183
*/
84+
@SuppressWarnings("boxing")
8285
public BaseTermQueryBuilder(String fieldName, float value) {
8386
this(fieldName, (Object) value);
8487
}
@@ -89,6 +92,7 @@ public BaseTermQueryBuilder(String fieldName, float value) {
8992
* @param fieldName The name of the field
9093
* @param value The value of the term
9194
*/
95+
@SuppressWarnings("boxing")
9296
public BaseTermQueryBuilder(String fieldName, double value) {
9397
this(fieldName, (Object) value);
9498
}
@@ -99,6 +103,7 @@ public BaseTermQueryBuilder(String fieldName, double value) {
99103
* @param fieldName The name of the field
100104
* @param value The value of the term
101105
*/
106+
@SuppressWarnings("boxing")
102107
public BaseTermQueryBuilder(String fieldName, boolean value) {
103108
this(fieldName, (Object) value);
104109
}
@@ -197,12 +202,13 @@ public QueryValidationException validate() {
197202
return validationException;
198203
}
199204

200-
205+
@SuppressWarnings("boxing")
201206
@Override
202207
public int hashCode() {
203208
return Objects.hash(getClass(), fieldName, value, boost, queryName);
204209
}
205210

211+
@SuppressWarnings("boxing")
206212
@Override
207213
public boolean equals(Object obj) {
208214
if (this == obj) {

src/main/java/org/elasticsearch/index/query/SpanTermQueryBuilder.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,22 @@ public SpanTermQueryBuilder(String name, String value) {
3636
super(name, (Object) value);
3737
}
3838
/** @see BaseTermQueryBuilder#BaseTermQueryBuilder(String, int) */
39+
@SuppressWarnings("boxing")
3940
public SpanTermQueryBuilder(String name, int value) {
4041
super(name, (Object) value);
4142
}
4243
/** @see BaseTermQueryBuilder#BaseTermQueryBuilder(String, long) */
44+
@SuppressWarnings("boxing")
4345
public SpanTermQueryBuilder(String name, long value) {
4446
super(name, (Object) value);
4547
}
4648
/** @see BaseTermQueryBuilder#BaseTermQueryBuilder(String, float) */
49+
@SuppressWarnings("boxing")
4750
public SpanTermQueryBuilder(String name, float value) {
4851
super(name, (Object) value);
4952
}
5053
/** @see BaseTermQueryBuilder#BaseTermQueryBuilder(String, double) */
54+
@SuppressWarnings("boxing")
5155
public SpanTermQueryBuilder(String name, double value) {
5256
super(name, (Object) value);
5357
}

src/main/java/org/elasticsearch/index/query/SpanTermQueryParser.java

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,15 @@
1919

2020
package org.elasticsearch.index.query;
2121

22-
import com.carrotsearch.randomizedtesting.annotations.Repeat;
23-
2422
import org.elasticsearch.common.Strings;
2523
import org.elasticsearch.common.inject.Inject;
2624
import org.elasticsearch.common.xcontent.XContentParser;
2725

2826
import java.io.IOException;
2927

3028
/**
31-
*
29+
* Parses the json representation of a spantermquery into the Elasticsearch internal
30+
* query builder representation.
3231
*/
3332
public class SpanTermQueryParser extends BaseQueryParser {
3433

@@ -45,45 +44,48 @@ public String[] names() {
4544

4645
@Override
4746
public QueryBuilder fromXContent(QueryParseContext parseContext) throws IOException, QueryParsingException {
48-
XContentParser parser = parseContext.parser();
49-
50-
XContentParser.Token token = parser.currentToken();
51-
if (token == XContentParser.Token.START_OBJECT) {
52-
token = parser.nextToken();
53-
}
54-
55-
assert token == XContentParser.Token.FIELD_NAME;
56-
String fieldName = parser.currentName();
57-
58-
47+
String fieldName = null;
5948
Object value = null;
6049
float boost = 1.0f;
6150
String queryName = null;
62-
token = parser.nextToken();
63-
if (token == XContentParser.Token.START_OBJECT) {
64-
String currentFieldName = null;
65-
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
66-
if (token == XContentParser.Token.FIELD_NAME) {
67-
currentFieldName = parser.currentName();
68-
} else {
69-
if ("term".equals(currentFieldName)) {
70-
value = parser.objectBytes();
71-
} else if ("value".equals(currentFieldName)) {
72-
value = parser.objectBytes();
73-
} else if ("boost".equals(currentFieldName)) {
74-
boost = parser.floatValue();
75-
} else if ("_name".equals(currentFieldName)) {
76-
queryName = parser.text();
51+
52+
try (XContentParser parser = parseContext.parser()) {
53+
54+
XContentParser.Token token = parser.currentToken();
55+
if (token == XContentParser.Token.START_OBJECT) {
56+
token = parser.nextToken();
57+
}
58+
59+
assert token == XContentParser.Token.FIELD_NAME;
60+
fieldName = parser.currentName();
61+
62+
63+
token = parser.nextToken();
64+
if (token == XContentParser.Token.START_OBJECT) {
65+
String currentFieldName = null;
66+
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
67+
if (token == XContentParser.Token.FIELD_NAME) {
68+
currentFieldName = parser.currentName();
7769
} else {
78-
throw new QueryParsingException(parseContext, "[span_term] query does not support [" + currentFieldName + "]");
70+
if ("term".equals(currentFieldName)) {
71+
value = parser.objectBytes();
72+
} else if ("value".equals(currentFieldName)) {
73+
value = parser.objectBytes();
74+
} else if ("boost".equals(currentFieldName)) {
75+
boost = parser.floatValue();
76+
} else if ("_name".equals(currentFieldName)) {
77+
queryName = parser.text();
78+
} else {
79+
throw new QueryParsingException(parseContext, "[span_term] query does not support [" + currentFieldName + "]");
80+
}
7981
}
8082
}
83+
parser.nextToken();
84+
} else {
85+
value = parser.objectBytes();
86+
// move to the next token
87+
parser.nextToken();
8188
}
82-
parser.nextToken();
83-
} else {
84-
value = parser.objectBytes();
85-
// move to the next token
86-
parser.nextToken();
8789
}
8890

8991
if (value == null) {

src/main/java/org/elasticsearch/index/query/TermQueryBuilder.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,26 +35,31 @@ public TermQueryBuilder(String fieldName, String value) {
3535
}
3636

3737
/** @see BaseTermQueryBuilder#BaseTermQueryBuilder(String, int) */
38+
@SuppressWarnings("boxing")
3839
public TermQueryBuilder(String fieldName, int value) {
3940
super(fieldName, (Object) value);
4041
}
4142

4243
/** @see BaseTermQueryBuilder#BaseTermQueryBuilder(String, long) */
44+
@SuppressWarnings("boxing")
4345
public TermQueryBuilder(String fieldName, long value) {
4446
super(fieldName, (Object) value);
4547
}
4648

4749
/** @see BaseTermQueryBuilder#BaseTermQueryBuilder(String, float) */
50+
@SuppressWarnings("boxing")
4851
public TermQueryBuilder(String fieldName, float value) {
4952
super(fieldName, (Object) value);
5053
}
5154

5255
/** @see BaseTermQueryBuilder#BaseTermQueryBuilder(String, double) */
56+
@SuppressWarnings("boxing")
5357
public TermQueryBuilder(String fieldName, double value) {
5458
super(fieldName, (Object) value);
5559
}
5660

5761
/** @see BaseTermQueryBuilder#BaseTermQueryBuilder(String, boolean) */
62+
@SuppressWarnings("boxing")
5863
public TermQueryBuilder(String fieldName, boolean value) {
5964
super(fieldName, (Object) value);
6065
}

src/main/java/org/elasticsearch/index/query/TermQueryParser.java

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -40,48 +40,51 @@ public String[] names() {
4040
return new String[]{NAME};
4141
}
4242

43+
@Override
4344
public QueryBuilder fromXContent(QueryParseContext parseContext) throws IOException, QueryParsingException {
44-
XContentParser parser = parseContext.parser();
45-
4645
String queryName = null;
4746
String fieldName = null;
4847
Object value = null;
4948
float boost = 1.0f;
50-
String currentFieldName = null;
51-
XContentParser.Token token;
52-
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
53-
if (token == XContentParser.Token.FIELD_NAME) {
54-
currentFieldName = parser.currentName();
55-
} else if (parseContext.isDeprecatedSetting(currentFieldName)) {
56-
// skip
57-
} else if (token == XContentParser.Token.START_OBJECT) {
58-
// also support a format of "term" : {"field_name" : { ... }}
59-
fieldName = currentFieldName;
60-
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
61-
if (token == XContentParser.Token.FIELD_NAME) {
62-
currentFieldName = parser.currentName();
63-
} else {
64-
if ("term".equals(currentFieldName)) {
65-
value = parser.objectBytes();
66-
} else if ("value".equals(currentFieldName)) {
67-
value = parser.objectBytes();
68-
} else if ("_name".equals(currentFieldName)) {
69-
queryName = parser.text();
70-
} else if ("boost".equals(currentFieldName)) {
71-
boost = parser.floatValue();
49+
50+
try (XContentParser parser = parseContext.parser()) {
51+
52+
String currentFieldName = null;
53+
XContentParser.Token token;
54+
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
55+
if (token == XContentParser.Token.FIELD_NAME) {
56+
currentFieldName = parser.currentName();
57+
} else if (parseContext.isDeprecatedSetting(currentFieldName)) {
58+
// skip
59+
} else if (token == XContentParser.Token.START_OBJECT) {
60+
// also support a format of "term" : {"field_name" : { ... }}
61+
fieldName = currentFieldName;
62+
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
63+
if (token == XContentParser.Token.FIELD_NAME) {
64+
currentFieldName = parser.currentName();
7265
} else {
73-
throw new QueryParsingException(parseContext, "[term] query does not support [" + currentFieldName + "]");
66+
if ("term".equals(currentFieldName)) {
67+
value = parser.objectBytes();
68+
} else if ("value".equals(currentFieldName)) {
69+
value = parser.objectBytes();
70+
} else if ("_name".equals(currentFieldName)) {
71+
queryName = parser.text();
72+
} else if ("boost".equals(currentFieldName)) {
73+
boost = parser.floatValue();
74+
} else {
75+
throw new QueryParsingException(parseContext, "[term] query does not support [" + currentFieldName + "]");
76+
}
7477
}
7578
}
76-
}
77-
} else if (token.isValue()) {
78-
if ("_name".equals(currentFieldName)) {
79-
queryName = parser.text();
80-
} else if ("boost".equals(currentFieldName)) {
81-
boost = parser.floatValue();
82-
} else {
83-
fieldName = currentFieldName;
84-
value = parser.objectBytes();
79+
} else if (token.isValue()) {
80+
if ("_name".equals(currentFieldName)) {
81+
queryName = parser.text();
82+
} else if ("boost".equals(currentFieldName)) {
83+
boost = parser.floatValue();
84+
} else {
85+
fieldName = currentFieldName;
86+
value = parser.objectBytes();
87+
}
8588
}
8689
}
8790
}

src/test/java/org/elasticsearch/index/query/BaseQueryTestCase.java

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public abstract class BaseQueryTestCase<QB extends BaseQueryBuilder & Streamable
7575
* Setup for the whole base test class.
7676
*/
7777
@BeforeClass
78-
public static void init() throws IOException {
78+
public static void init() {
7979
Settings settings = ImmutableSettings.settingsBuilder()
8080
.put("name", BaseQueryTestCase.class.toString())
8181
.put("path.home", createTempDir())
@@ -143,13 +143,14 @@ public void testFromXContent() throws IOException {
143143
testQuery = createTestQueryBuilder();
144144
QueryParseContext context = createContext();
145145
String contentString = testQuery.toString();
146-
XContentParser parser = XContentFactory.xContent(contentString).createParser(contentString);
147-
context.reset(parser);
148-
assertQueryHeader(parser, testQuery.parserName());
149-
150-
QueryBuilder newQuery = queryParserService.queryParser(testQuery.parserName()).fromXContent(context);
151-
assertNotSame(testQuery, newQuery);
152-
assertEquals(testQuery, newQuery);
146+
try (XContentParser parser = XContentFactory.xContent(contentString).createParser(contentString)) {
147+
context.reset(parser);
148+
assertQueryHeader(parser, testQuery.parserName());
149+
150+
QueryBuilder newQuery = queryParserService.queryParser(testQuery.parserName()).fromXContent(context);
151+
assertNotSame(testQuery, newQuery);
152+
assertEquals(testQuery, newQuery);
153+
}
153154
}
154155
}
155156

@@ -174,15 +175,17 @@ public void testToQuery() throws IOException {
174175
public void testSerialization() throws IOException {
175176
for (int i = 0; i < RANDOM_REPS; i++) {
176177
testQuery = createTestQueryBuilder();
177-
BytesStreamOutput output = new BytesStreamOutput();
178-
testQuery.writeTo(output);
178+
try (BytesStreamOutput output = new BytesStreamOutput()) {
179+
testQuery.writeTo(output);
179180

180-
BytesStreamInput in = new BytesStreamInput(output.bytes());
181-
QB deserializedQuery = createEmptyQueryBuilder();
182-
deserializedQuery.readFrom(in);
181+
try (BytesStreamInput in = new BytesStreamInput(output.bytes())) {
182+
QB deserializedQuery = createEmptyQueryBuilder();
183+
deserializedQuery.readFrom(in);
183184

184-
assertEquals(deserializedQuery, testQuery);
185-
assertNotSame(deserializedQuery, testQuery);
185+
assertEquals(deserializedQuery, testQuery);
186+
assertNotSame(deserializedQuery, testQuery);
187+
}
188+
}
186189
}
187190
}
188191

src/test/java/org/elasticsearch/index/query/BaseTermQueryTestCase.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,12 @@
2323
import org.junit.Ignore;
2424
import org.junit.Test;
2525

26-
import java.io.IOException;
27-
2826
import static org.hamcrest.Matchers.is;
2927

3028
@Ignore
3129
public abstract class BaseTermQueryTestCase<QB extends BaseQueryBuilder & Streamable> extends BaseQueryTestCase<QB> {
3230

31+
@SuppressWarnings("boxing")
3332
protected Object createRandomValueObject() {
3433
Object value = null;
3534
switch (randomIntBetween(0, 3)) {
@@ -56,8 +55,9 @@ protected Object createRandomValueObject() {
5655

5756
protected abstract QB createQueryBuilder(String fieldName, Object value);
5857

58+
@SuppressWarnings("boxing")
5959
@Test
60-
public void testValidate() throws QueryParsingException, IOException {
60+
public void testValidate() throws QueryParsingException {
6161

6262
QB queryBuilder = createQueryBuilder("all", "good");
6363
assertNull(queryBuilder.validate());

0 commit comments

Comments
 (0)