Java Code Examples for org.eclipse.xtext.util.concurrent.IUnitOfWork#exec()

The following examples show how to use org.eclipse.xtext.util.concurrent.IUnitOfWork#exec() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OutdatedStateManager.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
public <R extends Object, P extends Resource> R exec(IUnitOfWork<R, P> work, P param) {
	Boolean wasCancelationAllowed = cancelationAllowed.get();
	try {
		if (work instanceof CancelableUnitOfWork) {
			((CancelableUnitOfWork<?, ?>) work)
					.setCancelIndicator((param == null) ? () -> true : newCancelIndicator(param.getResourceSet()));
		} else {
			cancelationAllowed.set(false);
		}
		return work.exec(param);
	} catch (Throwable e) {
		return Exceptions.throwUncheckedException(e);
	} finally {
		cancelationAllowed.set(wasCancelationAllowed);
	}
}
 
Example 2
Source File: ResourceAccess.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public <R> R readOnly(URI targetURI, IUnitOfWork<R, ResourceSet> work) {
	URI resourceURI = targetURI.trimFragment();
	Iterable<Pair<IStorage, IProject>> storages = storage2UriMapper.getStorages(resourceURI);
	Iterator<Pair<IStorage, IProject>> iterator = storages.iterator();
	ResourceSet resourceSet = fallBackResourceSet; 
	while(iterator.hasNext()) {
		Pair<IStorage, IProject> pair = iterator.next();
		IProject project = pair.getSecond();
		if (project != null) {
			resourceSet = getResourceSet(project);
			break;
		}
	}
	if(resourceSet != null) {
		resourceSet.getResource(resourceURI, true);
		try {
			return work.exec(resourceSet);
		} catch (Exception e) {
			throw new WrappedException(e);
		}
	}
	return null;
}
 
Example 3
Source File: LoadingResourceAccess.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public <R> R readOnly(URI targetURI, IUnitOfWork<R, ResourceSet> work) {
	URI resourceURI = targetURI.trimFragment();
	Iterable<Pair<IStorage, IProject>> storages = storage2UriMapper.getStorages(resourceURI);
	Iterator<Pair<IStorage, IProject>> iterator = storages.iterator();
	while(iterator.hasNext()) {
		Pair<IStorage, IProject> pair = iterator.next();
		IProject project = pair.getSecond();
		if (project != null) {
			ResourceSet resourceSet = resourceSetProvider.get(project);
			if(resourceSet != null)
				resourceSet.getResource(resourceURI, true);
				try {
					return work.exec(resourceSet);
				} catch (Exception e) {
					throw new WrappedException(e);
				}
		}
	}
	return null;
}
 
Example 4
Source File: XtextDocumentModifyTest.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
private IXtextDocument createDocument(String model) throws Exception {
	resource = getResource(new StringInputStream(model));
	DocumentTokenSource tokenSource = new DocumentTokenSource();
	tokenSource.setLexer(new Provider<Lexer>(){
		@Override
		public Lexer get() {
			return new InternalXtextLexer();
		}});
	
	final XtextDocument document = new XtextDocument(tokenSource, get(ITextEditComposer.class), new OutdatedStateManager(), new OperationCanceledManager()) {
		@Override
		public <T> T internalModify(IUnitOfWork<T, XtextResource> work) {
			try {
				return work.exec((XtextResource) resource);
			}
			catch (Exception e) {
				throw new RuntimeException(e);
			}
		}
	};
	document.set(model);
	return document;
}
 
Example 5
Source File: TemporaryResourceProvider.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
public <Result> Result useTemporaryResource(
		ResourceSet resourceSet, Grammar language, 
		AbstractRule rootRule, String content, IUnitOfWork<Result, XtextResource> runnable) {
	XtextResource resource = languageRegistry.createTemporaryResourceIn(language, resourceSet);
	if (rootRule != null)
		PartialParser.assignRootRule(resource, (ParserRule) rootRule);
	try {
		resource.load(new StringInputStream(content, resource.getEncoding()), null);
		return runnable.exec(resource);
	} catch(Exception e) {
		throw new RuntimeException(e);
	} finally {
		if (resource != null) {
			if (resource.isLoaded())
				resource.unload();
			resourceSet.getResources().remove(resource);
		}
	}
}
 
Example 6
Source File: DirtyStateEditorSupportTest.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public <T> T readOnly(IUnitOfWork<T, XtextResource> work) {
	try {
		return work.exec(resource);
	} catch (Exception e) {
		throw new RuntimeException(e);
	}
}
 
Example 7
Source File: AbstractSARLQuickfixTest.java    From sarl with Apache License 2.0 5 votes vote down vote up
@Override
public <T> T modify(IUnitOfWork<T, XtextResource> work) {
	try {
		return work.exec(this.resource);
	} catch (Exception e) {
		throw new RuntimeException(e);
	}
}
 
Example 8
Source File: OnChangeEvictingCache.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * The transaction will be executed with caching enabled. However, all newly cached values will be discarded as soon
 * as the transaction is over.
 * @since 2.1
 */
public <Result, Param extends Resource> Result execWithTemporaryCaching(Param resource, IUnitOfWork<Result, Param> transaction) throws WrappedException {
	CacheAdapter cacheAdapter = getOrCreate(resource);
	IgnoreValuesMemento memento = cacheAdapter.ignoreNewValues();
	try {
		return transaction.exec(resource);
	} catch (Exception e) {
		throw new WrappedException(e);
	} finally {
		memento.done();
	}
}
 
Example 9
Source File: SynchronizedXtextResourceSet.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 * 
 * @since 2.4
 */
/* @Nullable */
@Override
public <Result> Result execute(/* @NonNull */ IUnitOfWork<Result, ? super SynchronizedXtextResourceSet> unit) throws Exception {
	synchronized (getLock()) {
		return unit.exec(this);
	}
}
 
Example 10
Source File: SimpleLocalResourceAccess.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public <R> R readOnly(URI targetURI, IUnitOfWork<R, ResourceSet> work) {
	try {
		return work.exec(resourceSet);
	} catch(OperationCanceledException e) {
		throw e;
	} catch(Exception exc) {
		throw new WrappedException(exc);
	}
}
 
Example 11
Source File: SimpleResourceAccess.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public <R> R readOnly(URI resourceURI, IUnitOfWork<R, ResourceSet> work) {
	try {
		return work.exec(resourceSet);
	} catch (Exception e) {
		throw new RuntimeException(e);
	}
}
 
Example 12
Source File: DocumentBasedDirtyResourceTest.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public <T> T readOnly(IUnitOfWork<T, XtextResource> work) {
	try {
		return work.exec(resource);
	} catch (Exception e) {
		throw new RuntimeException(e);
	}
}
 
Example 13
Source File: TypeResource.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 * 
 * @since 2.4
 */
/* @Nullable */
@Override
public <Result> Result execute(/* @NonNull */ IUnitOfWork<Result, ? super TypeResource> unit) throws Exception {
	synchronized (getLock()) {
		return unit.exec(this);
	}
}
 
Example 14
Source File: BatchLinkableResource.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 * 
 * @since 2.4
 */
/* @Nullable */
@Override
public <Result> Result execute(/* @NonNull */ IUnitOfWork<Result, ? super BatchLinkableResource> unit) throws Exception {
	synchronized (getLock()) {
		return unit.exec(this);
	}
}
 
Example 15
Source File: ForwardingResourceAccess.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public <R> R readOnly(URI targetURI, IUnitOfWork<R, ResourceSet> work) {
	if (N4Scheme.isN4Scheme(targetURI)) {
		if (resourceSet == null) {
			resourceSet = resourceSetProvider.get(null);
		}
		resourceSet.getResource(targetURI, true);
		try {
			return work.exec(resourceSet);
		} catch (Exception e) {
			throw new WrappedException(e);
		}
	}
	return delegate.readOnly(targetURI, work);
}
 
Example 16
Source File: XWorkspaceResourceAccess.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
/** Actually do the work in the context of the given resource set. */
protected <R> R doWork(ResourceSet resourceSet, IUnitOfWork<R, ResourceSet> work) {
	try {
		return work.exec(resourceSet);
	} catch (Exception e) {
		return Exceptions.throwUncheckedException(e);
	}
}
 
Example 17
Source File: ImportURINavigationTest.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
protected void doTestNavigation(IUnitOfWork<URI, IFile> uriComputation, boolean expectFQN) throws Exception {
	IJavaProject project = JavaProjectSetupUtil.createJavaProject("importuriuitestlanguage.project");
	try {
		IFile first = project.getProject().getFile("src/first.importuriuitestlanguage");
		first.create(new StringInputStream("type ASimpleType"), true, null);
		
		ResourceSet resourceSet = resourceSetProvider.get(project.getProject());
		
		Resource resource = resourceFactory.createResource(URI.createURI("synthetic://second.importuriuitestlanguage"));
		resourceSet.getResources().add(resource);
		String model = "import '" + uriComputation.exec(first) + "' type MyType extends ASimpleType";
		resource.load(new StringInputStream(model), null);
		EcoreUtil.resolveAll(resource);
		Assert.assertTrue(resource.getErrors().isEmpty());
		
		IHyperlink[] hyperlinks = helper.createHyperlinksByOffset((XtextResource) resource, model.indexOf("SimpleType"), false);
		Assert.assertEquals(1, hyperlinks.length);
		IWorkbenchPage activePage = workbench.getActiveWorkbenchWindow().getActivePage();
		Assert.assertNull(activePage.getActiveEditor());
		if (expectFQN) {
			Assert.assertEquals(URI.createURI(first.getLocationURI().toString()), ((XtextHyperlink)hyperlinks[0]).getURI().trimFragment());
		} else {
			Assert.assertEquals(URI.createPlatformResourceURI(first.getFullPath().toString(), true), ((XtextHyperlink)hyperlinks[0]).getURI().trimFragment());
		}
		hyperlinks[0].open();
		IEditorPart editor = activePage.getActiveEditor();
		Assert.assertNotNull(editor);
		IXtextDocument document = xtextDocumentUtil.getXtextDocument(editor);
		document.readOnly(new IUnitOfWork.Void<XtextResource>() {
			@Override
			public void process(XtextResource state) throws Exception {
				Assert.assertEquals("platform:/resource/importuriuitestlanguage.project/src/first.importuriuitestlanguage", state.getURI().toString());
			}
		});
		Assert.assertEquals("type ASimpleType", document.get());
		IEditorPart newPart = IDE.openEditor(activePage, first);
		Assert.assertEquals(1, activePage.getEditorReferences().length);
		Assert.assertEquals(editor, newPart);
	} finally {
		project.getProject().delete(true, null);
	}
}
 
Example 18
Source File: JavaResource.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public <Result extends Object> Result execute(IUnitOfWork<Result, ? super JavaResource> unit) throws Exception {
	synchronized (getLock()) {
		return unit.exec(this);
	}
}