Skip to main content



      Home
Home » Modeling » TMF (Xtext) » Custom validation of Java Language
Custom validation of Java Language [message #709211] Wed, 03 August 2011 12:32 Go to next message
Eclipse UserFriend
Maybe it's an Eclipse beginner question (well, this is my first post)

Let's say I have a Java method that accept Strings but for some reason i want to check at design time that the argument is all lowercase, giving a warning to the user if not.

Looking around i found some examples of xtext
h**p://help.eclipse.org/galileo/index.jsp?topic=/org.eclipse.xtext.doc/help/validation.html

But seems to be oriented at Domain Specific Language.

Is there an easy way to do it for regular Java projects?
Thanks in advance

PS: I don't know if this is the correct forum for this question.
Re: Custom validation of Java Language [message #709213 is a reply to message #709211] Wed, 03 August 2011 12:34 Go to previous messageGo to next message
Eclipse UserFriend
Hi,

this is defintively the wrong forum. there are tools/plugins for this e.g. Checkstyle

~Christian
Re: Custom validation of Java Language [message #709274 is a reply to message #709211] Wed, 03 August 2011 13:56 Go to previous messageGo to next message
Eclipse UserFriend
What do you mean by "at design time"?

If you mean "at compile time" (so, as the programmer writes the code),
that is a almost impossible task.

Of course you might check that in a call like

myMethod("argument")

the argument is lowercase. This one is very straight forward and can be
done with xText. You "simply" have to write a grammar for Java and a
validator for it. But don't start working before you read the rest of my
post. There are some problems. Consider the following example:

String arg="argument";
//a lot of code
myMethod(arg);

How should that be checked? You need some kind of constant propagation
that finds the value of arg at compile time. You might also think about:

String arg=RandomStringGenerator.randomString();
myMethod(arg);

If, in that example, the RandomStringGenerator generates a random String
at runtime, you cannot check whether it consits of lower case letters
only at compile time. There is simply no way to do it, because you can
not get the value of arg at compile time. It's the same if your string
is a User input, read from a file or something like that.

In my eyes the most common way is to start "myMethod" with testing if
the string is lower case and throw an IllegalArgumentException
otherwise. Of course that's a runtime check.

Another possibility: Have a look at findBugs. It should be possible to
use a technic similiar to their NullPointer- Dereferencing check to find
things like that. But I don't think it is worth the time, a runtime
check will be okay.

On 03.08.2011 18:32, Gabriel wrote:
> Maybe it's an Eclipse beginner question (well, this is my first post)
>
> Let's say I have a Java method that accept Strings but for some reason i
> want to check at design time that the argument is all lowercase, giving
> a warning to the user if not.
>
> Looking around i found some examples of xtext
> h**p://help.eclipse.org/galileo/index.jsp?topic=/org.eclipse.xtext.doc/help/validation.html
>
>
> But seems to be oriented at Domain Specific Language.
>
> Is there an easy way to do it for regular Java projects?
> Thanks in advance
>
> PS: I don't know if this is the correct forum for this question.
Re: Custom validation of Java Language [message #709328 is a reply to message #709211] Wed, 03 August 2011 15:50 Go to previous message
Eclipse UserFriend
One way would be to write an interactive builder that makes use of JDT's
model. When executed, it looks things up in the model and checks if
literal strings are in lower case.

It is kind of meaningless though, as the user could use string
expressions, methods etc. so the builder would need to have extensive
knowledge of just about every string manipulation method to be able to
accurately have an opinion about a string.

Use runtime checking instead, or why not just do toLowerCase on the
arguments...

- henrik
On 8/3/11 6:32 PM, Gabriel wrote:
> Maybe it's an Eclipse beginner question (well, this is my first post)
>
> Let's say I have a Java method that accept Strings but for some reason i
> want to check at design time that the argument is all lowercase, giving
> a warning to the user if not.
>
> Looking around i found some examples of xtext
> h**p://help.eclipse.org/galileo/index.jsp?topic=/org.eclipse.xtext.doc/help/validation.html
>
>
> But seems to be oriented at Domain Specific Language.
>
> Is there an easy way to do it for regular Java projects?
> Thanks in advance
>
> PS: I don't know if this is the correct forum for this question.
Previous Topic:Syntactic sugar
Next Topic:using IXtextEObjectSearch.findMatches
Goto Forum:
  


Current Time: Wed Jul 02 17:14:14 EDT 2025

Powered by FUDForum. Page generated in 0.05994 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top