Make some ObjectParsers final (#50471) (#50556)

We have about 800 `ObjectParsers` in Elasticsearch, about 700 of which
are final. This is *probably* the right way to declare them because in
practice we never mutate them after they are built. And we certainly
don't change the static reference. Anyway, this adds `final` to a bunch
of these parsers, mostly the ones in xpack and their "paired" parsers in
the high level rest client. I picked these just to have somewhere to
break the up the change so it wouldn't be huge.

I found the non-final parsers with this:
```
diff \
  <(find . -type f -name '*.java' -exec grep -iHe 'static.*PARSER\s*=' {} \+ | sort) \
  <(find . -type f -name '*.java' -exec grep -iHe 'static.*final.*PARSER\s*=' {} \+ | sort) \
  2>&1 | grep '^<'
```
This commit is contained in:
Nik Everett 2020-01-02 10:47:38 -05:00 committed by GitHub
parent 55107ce8ae
commit b36a8ab141
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
23 changed files with 42 additions and 35 deletions

View File

@ -27,8 +27,7 @@ import java.util.Objects;
public class MainResponse {
@SuppressWarnings("unchecked")
private static ConstructingObjectParser<MainResponse, Void> PARSER =
private static final ConstructingObjectParser<MainResponse, Void> PARSER =
new ConstructingObjectParser<>(MainResponse.class.getName(), true,
args -> {
return new MainResponse((String) args[0], (Version) args[1], (String) args[2], (String) args[3], (String) args[4]);

View File

@ -44,7 +44,7 @@ public class LifecyclePolicy implements ToXContentObject {
static final ParseField PHASES_FIELD = new ParseField("phases");
@SuppressWarnings("unchecked")
public static ConstructingObjectParser<LifecyclePolicy, String> PARSER = new ConstructingObjectParser<>("lifecycle_policy", true,
public static final ConstructingObjectParser<LifecyclePolicy, String> PARSER = new ConstructingObjectParser<>("lifecycle_policy", true,
(a, name) -> {
List<Phase> phases = (List<Phase>) a[0];
Map<String, Phase> phaseMap = phases.stream().collect(Collectors.toMap(Phase::getName, Function.identity()));

View File

@ -57,7 +57,7 @@ public class FieldSelection implements ToXContentObject {
}
@SuppressWarnings("unchecked")
public static ConstructingObjectParser<FieldSelection, Void> PARSER = new ConstructingObjectParser<>("field_selection", true,
public static final ConstructingObjectParser<FieldSelection, Void> PARSER = new ConstructingObjectParser<>("field_selection", true,
a -> new FieldSelection((String) a[0], new HashSet<>((List<String>) a[1]), (boolean) a[2], (boolean) a[3], (FeatureType) a[4],
(String) a[5]));

View File

@ -89,7 +89,7 @@ public final class CreateApiKeyResponse {
&& Objects.equals(expiration, other.expiration);
}
static ConstructingObjectParser<CreateApiKeyResponse, Void> PARSER = new ConstructingObjectParser<>("create_api_key_response",
static final ConstructingObjectParser<CreateApiKeyResponse, Void> PARSER = new ConstructingObjectParser<>("create_api_key_response",
args -> new CreateApiKeyResponse((String) args[0], (String) args[1], new SecureString((String) args[2]),
(args[3] == null) ? null : Instant.ofEpochMilli((Long) args[3])));
static {

View File

@ -73,9 +73,10 @@ public final class GetApiKeyResponse {
}
@SuppressWarnings("unchecked")
static ConstructingObjectParser<GetApiKeyResponse, Void> PARSER = new ConstructingObjectParser<>("get_api_key_response", args -> {
return (args[0] == null) ? GetApiKeyResponse.emptyResponse() : new GetApiKeyResponse((List<ApiKey>) args[0]);
});
static final ConstructingObjectParser<GetApiKeyResponse, Void> PARSER = new ConstructingObjectParser<>(
"get_api_key_response",
args -> { return (args[0] == null) ? GetApiKeyResponse.emptyResponse() : new GetApiKeyResponse((List<ApiKey>) args[0]); }
);
static {
PARSER.declareObjectArray(optionalConstructorArg(), (p, c) -> ApiKey.fromXContent(p), new ParseField("api_keys"));
}

View File

@ -74,10 +74,12 @@ public final class InvalidateApiKeyResponse {
}
@SuppressWarnings("unchecked")
static ConstructingObjectParser<InvalidateApiKeyResponse, Void> PARSER = new ConstructingObjectParser<>("invalidate_api_key_response",
args -> {
return new InvalidateApiKeyResponse((List<String>) args[0], (List<String>) args[1], (List<ElasticsearchException>) args[3]);
});
static final ConstructingObjectParser<InvalidateApiKeyResponse, Void> PARSER = new ConstructingObjectParser<>(
"invalidate_api_key_response",
args -> {
return new InvalidateApiKeyResponse((List<String>) args[0], (List<String>) args[1], (List<ElasticsearchException>) args[3]);
}
);
static {
PARSER.declareStringArray(constructorArg(), new ParseField("invalidated_api_keys"));
PARSER.declareStringArray(constructorArg(), new ParseField("previously_invalidated_api_keys"));

View File

@ -127,7 +127,7 @@ public final class ApiKey {
&& Objects.equals(realm, other.realm);
}
static ConstructingObjectParser<ApiKey, Void> PARSER = new ConstructingObjectParser<>("api_key", args -> {
static final ConstructingObjectParser<ApiKey, Void> PARSER = new ConstructingObjectParser<>("api_key", args -> {
return new ApiKey((String) args[0], (String) args[1], Instant.ofEpochMilli((Long) args[2]),
(args[3] == null) ? null : Instant.ofEpochMilli((Long) args[3]), (Boolean) args[4], (String) args[5], (String) args[6]);
});

View File

@ -151,7 +151,7 @@ public class ParsedMatrixStats extends ParsedAggregation implements MatrixStats
return values.get(fieldName);
}
private static ObjectParser<ParsedMatrixStats, Void> PARSER =
private static final ObjectParser<ParsedMatrixStats, Void> PARSER =
new ObjectParser<>(ParsedMatrixStats.class.getSimpleName(), true, ParsedMatrixStats::new);
static {
declareAggregationFields(PARSER);

View File

@ -46,7 +46,7 @@ public class ShardFollowTask extends ImmutableFollowParameters implements XPackP
private static final ParseField HEADERS = new ParseField("headers");
@SuppressWarnings("unchecked")
private static ConstructingObjectParser<ShardFollowTask, Void> PARSER = new ConstructingObjectParser<>(NAME,
private static final ConstructingObjectParser<ShardFollowTask, Void> PARSER = new ConstructingObjectParser<>(NAME,
(a) -> new ShardFollowTask((String) a[0],
new ShardId((String) a[1], (String) a[2], (int) a[3]), new ShardId((String) a[4], (String) a[5], (int) a[6]),
(Integer) a[7], (Integer) a[8], (Integer) a[9], (Integer) a[10], (ByteSizeValue) a[11], (ByteSizeValue) a[12],

View File

@ -47,7 +47,7 @@ public class LifecyclePolicy extends AbstractDiffable<LifecyclePolicy>
public static final ParseField PHASES_FIELD = new ParseField("phases");
@SuppressWarnings("unchecked")
public static ConstructingObjectParser<LifecyclePolicy, String> PARSER = new ConstructingObjectParser<>("lifecycle_policy", false,
public static final ConstructingObjectParser<LifecyclePolicy, String> PARSER = new ConstructingObjectParser<>("lifecycle_policy", false,
(a, name) -> {
List<Phase> phases = (List<Phase>) a[0];
Map<String, Phase> phaseMap = phases.stream().collect(Collectors.toMap(Phase::getName, Function.identity()));

View File

@ -41,7 +41,7 @@ public class CloseJobAction extends ActionType<CloseJobAction.Response> {
public static final ParseField TIMEOUT = new ParseField("timeout");
public static final ParseField FORCE = new ParseField("force");
public static final ParseField ALLOW_NO_JOBS = new ParseField("allow_no_jobs");
public static ObjectParser<Request, Void> PARSER = new ObjectParser<>(NAME, Request::new);
public static final ObjectParser<Request, Void> PARSER = new ObjectParser<>(NAME, Request::new);
static {
PARSER.declareString(Request::setJobId, Job.ID);

View File

@ -46,8 +46,7 @@ public class IsolateDatafeedAction extends ActionType<IsolateDatafeedAction.Resp
public static class Request extends BaseTasksRequest<Request> implements ToXContentObject {
public static ObjectParser<Request, Void> PARSER = new ObjectParser<>(NAME, Request::new);
public static final ObjectParser<Request, Void> PARSER = new ObjectParser<>(NAME, Request::new);
static {
PARSER.declareString((request, datafeedId) -> request.datafeedId = datafeedId, DatafeedConfig.ID);
}

View File

@ -124,7 +124,7 @@ public class OpenJobAction extends ActionType<AcknowledgedResponse> {
public static final ParseField TIMEOUT = new ParseField("timeout");
public static final ParseField JOB = new ParseField("job");
public static ObjectParser<JobParams, Void> PARSER = new ObjectParser<>(MlTasks.JOB_TASK_NAME, true, JobParams::new);
public static final ObjectParser<JobParams, Void> PARSER = new ObjectParser<>(MlTasks.JOB_TASK_NAME, true, JobParams::new);
static {
PARSER.declareString(JobParams::setJobId, Job.ID);
PARSER.declareString((params, val) ->

View File

@ -43,8 +43,7 @@ public class RevertModelSnapshotAction extends ActionType<RevertModelSnapshotAct
public static final ParseField SNAPSHOT_ID = new ParseField("snapshot_id");
public static final ParseField DELETE_INTERVENING = new ParseField("delete_intervening_results");
private static ObjectParser<Request, Void> PARSER = new ObjectParser<>(NAME, Request::new);
private static final ObjectParser<Request, Void> PARSER = new ObjectParser<>(NAME, Request::new);
static {
PARSER.declareString((request, jobId) -> request.jobId = jobId, Job.ID);
PARSER.declareString((request, snapshotId) -> request.snapshotId = snapshotId, SNAPSHOT_ID);

View File

@ -157,7 +157,7 @@ public class StartDataFrameAnalyticsAction extends ActionType<AcknowledgedRespon
private static final ParseField PROGRESS_ON_START = new ParseField("progress_on_start");
@SuppressWarnings("unchecked")
public static ConstructingObjectParser<TaskParams, Void> PARSER = new ConstructingObjectParser<>(
public static final ConstructingObjectParser<TaskParams, Void> PARSER = new ConstructingObjectParser<>(
MlTasks.DATA_FRAME_ANALYTICS_TASK_NAME, true,
a -> new TaskParams((String) a[0], (String) a[1], (List<PhaseProgress>) a[2], (Boolean) a[3]));

View File

@ -136,7 +136,11 @@ public class StartDatafeedAction extends ActionType<AcknowledgedResponse> {
public static final ParseField INDICES = new ParseField("indices");
public static ObjectParser<DatafeedParams, Void> PARSER = new ObjectParser<>(MlTasks.DATAFEED_TASK_NAME, true, DatafeedParams::new);
public static final ObjectParser<DatafeedParams, Void> PARSER = new ObjectParser<>(
MlTasks.DATAFEED_TASK_NAME,
true,
DatafeedParams::new
);
static {
PARSER.declareString((params, datafeedId) -> params.datafeedId = datafeedId, DatafeedConfig.ID);
PARSER.declareString((params, startTime) -> params.startTime = parseDateOrThrow(

View File

@ -45,8 +45,7 @@ public class StopDatafeedAction extends ActionType<StopDatafeedAction.Response>
public static final ParseField FORCE = new ParseField("force");
public static final ParseField ALLOW_NO_DATAFEEDS = new ParseField("allow_no_datafeeds");
public static ObjectParser<Request, Void> PARSER = new ObjectParser<>(NAME, Request::new);
public static final ObjectParser<Request, Void> PARSER = new ObjectParser<>(NAME, Request::new);
static {
PARSER.declareString((request, datafeedId) -> request.datafeedId = datafeedId, DatafeedConfig.ID);
PARSER.declareString((request, val) ->

View File

@ -46,7 +46,8 @@ public class FieldSelection implements ToXContentObject, Writeable {
}
}
public static ConstructingObjectParser<FieldSelection, Void> PARSER = new ConstructingObjectParser<>("field_selection",
@SuppressWarnings("unchecked")
public static final ConstructingObjectParser<FieldSelection, Void> PARSER = new ConstructingObjectParser<>("field_selection",
a -> new FieldSelection((String) a[0], new HashSet<>((List<String>) a[1]), (boolean) a[2], (boolean) a[3], (FeatureType) a[4],
(String) a[5]));

View File

@ -147,7 +147,7 @@ public final class ApiKey implements ToXContentObject, Writeable {
&& Objects.equals(realm, other.realm);
}
static ConstructingObjectParser<ApiKey, Void> PARSER = new ConstructingObjectParser<>("api_key", args -> {
static final ConstructingObjectParser<ApiKey, Void> PARSER = new ConstructingObjectParser<>("api_key", args -> {
return new ApiKey((String) args[0], (String) args[1], Instant.ofEpochMilli((Long) args[2]),
(args[3] == null) ? null : Instant.ofEpochMilli((Long) args[3]), (Boolean) args[4], (String) args[5], (String) args[6]);
});

View File

@ -31,7 +31,7 @@ import static org.elasticsearch.common.xcontent.ConstructingObjectParser.optiona
*/
public final class CreateApiKeyResponse extends ActionResponse implements ToXContentObject {
static ConstructingObjectParser<CreateApiKeyResponse, Void> PARSER = new ConstructingObjectParser<>("create_api_key_response",
static final ConstructingObjectParser<CreateApiKeyResponse, Void> PARSER = new ConstructingObjectParser<>("create_api_key_response",
args -> new CreateApiKeyResponse((String) args[0], (String) args[1], new SecureString((String) args[2]),
(args[3] == null) ? null : Instant.ofEpochMilli((Long) args[3])));
static {

View File

@ -63,7 +63,7 @@ public final class GetApiKeyResponse extends ActionResponse implements ToXConten
}
@SuppressWarnings("unchecked")
static ConstructingObjectParser<GetApiKeyResponse, Void> PARSER = new ConstructingObjectParser<>("get_api_key_response", args -> {
static final ConstructingObjectParser<GetApiKeyResponse, Void> PARSER = new ConstructingObjectParser<>("get_api_key_response", args -> {
return (args[0] == null) ? GetApiKeyResponse.emptyResponse() : new GetApiKeyResponse((List<ApiKey>) args[0]);
});
static {

View File

@ -109,10 +109,13 @@ public final class InvalidateApiKeyResponse extends ActionResponse implements To
out.writeCollection(errors, StreamOutput::writeException);
}
static ConstructingObjectParser<InvalidateApiKeyResponse, Void> PARSER = new ConstructingObjectParser<>("invalidate_api_key_response",
args -> {
return new InvalidateApiKeyResponse((List<String>) args[0], (List<String>) args[1], (List<ElasticsearchException>) args[3]);
});
@SuppressWarnings("unchecked")
static final ConstructingObjectParser<InvalidateApiKeyResponse, Void> PARSER = new ConstructingObjectParser<>(
"invalidate_api_key_response",
args -> {
return new InvalidateApiKeyResponse((List<String>) args[0], (List<String>) args[1], (List<ElasticsearchException>) args[3]);
}
);
static {
PARSER.declareStringArray(constructorArg(), new ParseField("invalidated_api_keys"));
PARSER.declareStringArray(constructorArg(), new ParseField("previously_invalidated_api_keys"));

View File

@ -146,7 +146,7 @@ public class PreviewTransformAction extends ActionType<PreviewTransformAction.Re
public static ParseField PREVIEW = new ParseField("preview");
public static ParseField MAPPINGS = new ParseField("mappings");
static ObjectParser<Response, Void> PARSER = new ObjectParser<>("data_frame_transform_preview", Response::new);
static final ObjectParser<Response, Void> PARSER = new ObjectParser<>("data_frame_transform_preview", Response::new);
static {
PARSER.declareObjectArray(Response::setDocs, (p, c) -> p.mapOrdered(), PREVIEW);
PARSER.declareObject(Response::setMappings, (p, c) -> p.mapOrdered(), MAPPINGS);