4

I have a small problem here . Even though it is related to Xtext i think the solution may be more java related .

I am using Xtext to create a grammar which works fine . There was an existing validation called BufferValidator for the grammar . Its defined correctly as

public class BufferValidator extends AbstractSPJavaValidator {

// Validation methods which work fine 

}

I added another .

public class FreqValueValidator extends AbstractSPJavaValidator{

    // Some new validation methods

}

Now when i run the project the folder src-gen/ is automatically created and contains the validation information in a file called AbstractSPJavaValidator .

**Snip**
@ComposedChecks(validators= {gem.demo.validation.BufferValidator.class})
public class AbstractSPJavaValidator extends AbstractDeclarativeValidator {

          // Methods here 
        }

}

Clearly my new validator is not added to the list of @ComposedChecks validators . Now if i add my validator in the file myself ( which ideally i should not as this file is automatically generated ) it works as expected .

Any idea what i could be missing here . For starters i did re-create / re-generate the grammar symbols after adding the new validation class .

1 Answer 1

3

I hope I'm understanding your situation correctly:

BufferValidator is the automatically generated Validator, and by default only those validations work. You want to add another Validator called FreqValueValidator and that only works when you manipulate the abstract class in the src-gen folder, which would be overwritten with the next built.

You can split your Validators and define them in the "main" Validator, BufferValidator in your case, with the @ComposedChecks annotation. In the sub validators you have to overwrite the register method. it not required and should not call the super method, just leave it empty with a comment.

BufferValidator:

@ComposedChecks(validators = { FreqValueValidator.class })
public class BufferValidator extends AbstractSPJavaValidator {

// Validation methods which work fine 

}

FreqValueValidator:

public class FreqValueValidator extends AbstractDeclarativeValidator {

    @Override
    @Inject
    public void register(EValidatorRegistrar registrar) {
        // nothing to do
    }

    // Some new validation methods
}

I got this solution from the official xtext support forum, but unfortunately didn't find the thread again. I'm using it and it works like a charm. I hope I got anything right in the above code, it's untested. But it should get you going.

1
  • I couldnt have hoped for a better written answer . Well formatted , concepts clearly explained . Thanks a bunch . Works like a charm .
    – rockstar
    Jun 27, 2013 at 1:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.