PROPOSAL: String parameters compile time validation (early beta)
Joe Darcy
Joe.Darcy at Sun.COM
Fri May 8 15:21:41 PDT 2009
On 03/27/09 12:00 AM, Artur Biesiadowski wrote:
> Reinier Zwitserloot wrote:
>
>> The idea is simply to have a literal syntax for regexps, where the
>> compiler will actually put the binary representation of a compiled
>> pattern in the .class source. As I mentioned, this has very
>> significant speed savings, because compiling a regexp is more
>> expensive than applying one, especially for complex regexps that are
>> performance sensitive.
>>
>> I don't understand your argument about casting. Right now, you get
>> compile-time checking on e.g. the bounds of an integer, but if you
>> cast, you get that check at runtime (or, actually, the bits are just
>> cut off, but you get the point).
>>
>> The regexp literal notation would literally have the type 'Pattern'.
>> You can try to cast a string to this, but that would obviously fail,
>> and the compiler will in fact error when you try, because String is
>> not a subtype of Pattern. If you want to turn a string into a pattern,
>> you'd use Pattern.compile. So:
>>
>> Pattern x = /a*a+a*a+/;
>> Pattern y = Pattern.compile("a*a+a*a+");
>>
>
>
> I think that we are talking about different things. As far as I
> understood OP, he wants to have kind of typedef String which accepts
> only certain values. It is not about compile-time Patterns, which are
> just a side requirement. Main topic is to have something like
>
> @Regexp("JustDigits","[0-9]+");
>
> void setPhoneNumber(@Regexp("JustDigits") String phoneNumber)
>
>
> Now, you can call it from the code like
>
> obj.setPhoneNumber("12345")
>
> and compile would do a static check if 12345 is fitting "[0-9]+" regexp,
> but you probably would also like to do
>
> String number = someField.getText();
> obj.setPhoneNumber(number);
>
> which is not safe. We would need to have something like
>
> String number = someField.getText();
> @Regexp("JustDigits") String reallyNumber =
> @Regexp("JustDigits").check(number);
> obj.setPhoneNumber(reallyNumber);
>
> Actually, when I think about static code checks like in the first case,
> I don't think it could be pulled out if this crosses class boundary -
> you could recompile receiver class, changing the regexp contents and the
> safety would fail (unless regexp would be included into method signature
> by contents).
>
> Regards,
> Artur Biesiadowski
>
>
Catching up on email, one can imagine schemes to validate
extra-linguistic aspects of annotation values like "this string must be
a regular expression" or "this integer must be in the range 0 to 10,
inclusive" and JSR 308 is working on enabling a new class of
compile-time checkers; however, I don't think any further language
support for such idea, including AOP style injection of additional
runtime checks, is appropriate for Project Coin.
-Joe
-Joe
More information about the coin-dev
mailing list