Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable InstantiatingObjectParser to pass context as a first argument #79206

Merged
merged 3 commits into from
Oct 18, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Improve the error message for annotated constructor
imotov committed Oct 14, 2021

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 88ec32cecbe73d2017828e204d55be9afeeca198
Original file line number Diff line number Diff line change
@@ -112,9 +112,15 @@ public InstantiatingObjectParser<Value, Context> build() {
throw new IllegalArgumentException("More then one public constructor with @ParserConstructor annotation exist in " +
"the class " + valueClass.getName());
}
if (c.getParameterCount() < neededArguments || c.getParameterCount() > neededArguments + 1 ) {
throw new IllegalArgumentException("Annotated constructor doesn't have " + neededArguments +
" arguments in the class " + valueClass.getName());
if (c.getParameterCount() < neededArguments || c.getParameterCount() > neededArguments + 1) {
throw new IllegalArgumentException(
"Annotated constructor doesn't have "
+ neededArguments
+ " or "
+ (neededArguments + 1)
+ " arguments in the class "
+ valueClass.getName()
);
}
constructor = c;
}
Original file line number Diff line number Diff line change
@@ -214,8 +214,10 @@ public void testAnnotationWrongArgumentNumber() {
InstantiatingObjectParser.Builder<Annotations, Void> builder = InstantiatingObjectParser.builder("foo", Annotations.class);
builder.declareInt(constructorArg(), new ParseField("a"));
builder.declareString(constructorArg(), new ParseField("b"));
builder.declareInt(constructorArg(), new ParseField("c"));
builder.declareString(constructorArg(), new ParseField("d"));
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, builder::build);
assertThat(e.getMessage(), containsString("Annotated constructor doesn't have 2 arguments in the class"));
assertThat(e.getMessage(), containsString("Annotated constructor doesn't have 4 or 5 arguments in the class"));
}

public void testDoubleDeclarationThrowsException() throws IOException {
@@ -309,9 +311,8 @@ public void testContextAsArgumentWrongArgumentNumber() {
);
builder.declareInt(constructorArg(), new ParseField("a"));
builder.declareString(constructorArg(), new ParseField("b"));
builder.declareString(constructorArg(), new ParseField("c"));
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, builder::build);
assertThat(e.getMessage(), containsString("Annotated constructor doesn't have 2 arguments in the class"));
assertThat(e.getMessage(), containsString("Annotated constructor doesn't have 2 or 3 arguments in the class"));
}

}