A data class uses properties defined in the primary constructor to generate its equals, hashCode, toString and other methods. A data class declared in the way that you suggest would have meaningless implementations of those methods, which is very unlikely to be what you actually want.
Alright, I checked them with JD-GUI, here what I found out for the generated name field:
plain class:
public final class PersonForm
{
@Size(min=2, max=30)
@NotBlank
@NotNull
private String name = "";
...
}
data class:
public final class PersonForm
{
@NotNull
private String name;
public PersonForm(@NotNull @Size(min=2, max=30) @NotBlank String name, ...)
{
this.name = name;
...
}
...
}
So on the data class, the @Size and @NotBlank annotation aren’t put correctly on the private String name field declaration, instead they are put on the constructor’s parameters.
Not related with the above issue, just curious that there are these methods generated for each constructor fields on data class:
@NotNull
public final String component1()
{
return this.name;
}
@NotNull
public final Integer component2()
{
return this.age;
}
I’ve tried adding @field:Name and then @field:name, but they gave compile error. I think you meant @field:<annotation_name>, thus:
data class PersonForm (
@field:Size(min=2, max=30)
@field:NotBlank
var name: String = "",
It took me awhile to figure out that. The documentation is a bit confusing too, it mentioned @field:Ann but don’t describe what is Ann at that section. It is mentioned at above which requires some reading. I was thinking I have to add new annotation @field:??, but turns out it’s just prefixing @field to existing annotation.
Suggestion:
I think it’s better if kotlin compiler put the annotation into field location by default if the constructor param is val or var in order to avoid the overhead of @field prefix, as most people would expect they are annotated on field location.