|
|
|
Re: Using results from language interpreter in JavaValidator [message #697907 is a reply to message #697862] |
Mon, 18 July 2011 06:28  |
Eclipse User |
|
|
|
Hi Jon,
the resource scope cache uses the adapter approach internally. Sorry for
any confusion that I caused with the reference to EAdapter.
Regards,
Sebastian
--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com
On 18.07.11 10:26, forums-noreply@eclipse.org wrote:
> Hello Sebastian.
>
> Thanks for answering my questions.
> I can see how to add a second validator now (d) - I thought it would
> override the other one but the methods are named after the class not the
> interface so that is OK.
>
> (a + b) I had cobbled together the following, but your answer is much
> neater and I will try that.
>
>
> public class MyDslValidator extends AbstractDNAplJavaValidator {
>
> @Inject
> private IResourceScopeCache resourceScopeCache;
>
> /**
> * Used by getRegisteredAdapter to obtain the ResourceAdapter. */
> class Type {}
>
> /* * Sebastian Zarnekow wrote, in response to Jonathan Blakes:
> * You should not store the result in the validator as the instance will
> * be shared for different resources. Attach the result to the resource *
> instead (probably as an EAdapter). I'd recommend to use the *
> IResourceScopeCache. */
> /**
> * Used to add functionality to the current resource (like the result of the
> * interpretation) and cache it in such a way that it is discard as soon as
> * the model changes.
> */
> class ResourceAdapter extends AdapterImpl {
> private Root root;
> public ResourceAdapter(Root root) {
> this.root = root;
> }
> public boolean isAdapterForType(Object type) {
> if (type == Type.class) return true;
> return false;
> }
> public Result getResult() {
> return resourceScopeCache.get("Root", root.eResource(), new
> Provider<Result>() {
> public Result get() {
> Interpreter interpreter = new Interpreter(root);
> return interpreter.getResult();
> }
> );
> }
>
> /**
> * Add a ResourceAdapter to the resource being validated.
> * @param root The root EObject of the grammar.
> */
> @Check
> public void checkRoot(final Root root) {
> // Add a ResourceAdapter to the resource being validated
> EList<Adapter> adapters = root.eResource().eAdapters();
> if (Iterables.isEmpty((Iterables.filter(adapters,
> ResourceAdapter.class)))) {
> adapters.add(new ResourceAdapter(root));
> }
> }
>
> /**
> * Accessor that goes via a ResourceAdapter to get the cached Result.
> */
> private Result getResult() {
> return ((ResourceAdapter)
> EcoreUtil.getRegisteredAdapter(getCurrentObject().eResource(),
> Type.class)).getResult();
> }
>
> @Check
> public void checkSomething(Something something) {
> Result = getResult();
> ...
>
>
> How come you didn't use an eAdapter as you suggested before? I this
> there anything wrong with the approach that I used?
>
> Best regards,
>
> Jon
|
|
|
Powered by
FUDForum. Page generated in 0.04202 seconds