org.eclipse.xtext.util.Exceptions Java Examples

The following examples show how to use org.eclipse.xtext.util.Exceptions. 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: N4IDEXpectCompareEditorInput.java    From n4js with Eclipse Public License 1.0 6 votes vote down vote up
@Override
protected Object prepareInput(IProgressMonitor pm) {
	try {
		ResourceNode ancestor = new ResourceNode(file);
		String ancestorContent = getContent(ancestor);
		String leftContent, rightContent;

		leftContent = this.comparisonFailure.getExpected();
		rightContent = this.comparisonFailure.getActual();
		if (!leftContent.equals(ancestorContent))
			getCompareConfiguration().setProperty(ICompareUIConstants.PROP_ANCESTOR_VISIBLE, Boolean.TRUE);
		CompareItem left = new EditableCompareItem("Left", leftContent, file);
		CompareItem right = new CompareItem("Right", rightContent);
		return new DiffNode(null, Differencer.CHANGE | Differencer.DIRECTION_MASK, ancestor, left, right);
	} catch (Throwable t) {
		LOG.error(t.getMessage(), t);
		Exceptions.throwUncheckedException(t);
		return null;
	}
}
 
Example #2
Source File: FutureUtil.java    From n4js with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Obtains the given future's result via {@link Future#get()}, but converts <code>java.util.concurrent</code>'s
 * {@link CancellationException} to Xtext's {@link OperationCanceledException} and wraps checked exceptions in a
 * {@link RuntimeException}.
 */
public static <T> T getCancellableResult(Future<T> future) {
	try {
		return future.get();
	} catch (Throwable e) {
		Throwable cancellation = getCancellation(e);
		if (cancellation instanceof OperationCanceledError) {
			throw (OperationCanceledError) cancellation;
		} else if (cancellation instanceof OperationCanceledException) {
			throw (OperationCanceledException) cancellation;
		} else if (cancellation instanceof CancellationException) {
			String msg = e.getMessage();
			if (msg != null) {
				throw new OperationCanceledException(e.getMessage());
			}
			throw new OperationCanceledException();
		}
		return Exceptions.throwUncheckedException(e);
	}
}
 
Example #3
Source File: CompilationTestHelper.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Parses, validates and compiles the given source. Calls the given acceptor for each
 * resource which is generated from the source.
 *  
 * @param resourceSet - the {@link ResourceSet} to use
 * @param acceptor gets called once for each file generated in {@link IGenerator}
 */
public void compile(final ResourceSet resourceSet, IAcceptor<Result> acceptor) {
	try {
		List<Resource> resourcesToCheck = newArrayList(resourceSet.getResources());
		if (generatorConfigProvider instanceof GeneratorConfigProvider) {
			GeneratorConfigProvider configProvider = (GeneratorConfigProvider) generatorConfigProvider;
			GeneratorConfig config = generatorConfigProvider.get(null);
			config.setJavaSourceVersion(javaCompiler.getJavaVersion());
			GeneratorConfig existent = configProvider.install(resourceSet, config);
			if (existent != null) {
				existent.setJavaSourceVersion(javaCompiler.getJavaVersion());
				configProvider.install(resourceSet, existent);
			}
		}
		Result result = resultProvider.get();
		result.setJavaCompiler(javaCompiler);
		result.setCheckMode(getCheckMode());
		result.setResources(resourcesToCheck);
		result.setResourceSet(resourceSet);
		result.setOutputConfigurations(getOutputConfigurations());
		result.doGenerate();
		acceptor.accept(result);
	} catch (Exception e) {
		Exceptions.throwUncheckedException(e);
	}
}
 
Example #4
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 #5
Source File: CompilationTestHelper.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Parses, validates and compiles the given source. Calls the given acceptor for each
 * resource which is generated from the source.
 *  
 * @param resourceSet - the {@link ResourceSet} to use
 * @param acceptor gets called once for each file generated in {@link IGenerator}
 */
public void compile(final ResourceSet resourceSet, IAcceptor<Result> acceptor) {
	try {
		List<Resource> resourcesToCheck = newArrayList(resourceSet.getResources());
		Result result = resultProvider.get();
		result.setJavaCompiler(javaCompiler);
		result.setCheckMode(getCheckMode());
		result.setResources(resourcesToCheck);
		result.setResourceSet(resourceSet);
		result.setOutputConfigurations(getOutputConfigurations());
		result.doGenerate();
		acceptor.accept(result);
	} catch (Exception e) {
		Exceptions.throwUncheckedException(e);
	}
}
 
Example #6
Source File: DerivedResourceMarkers.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * @since 2.3
 */
@Override
public Iterable<IMarker> findDerivedResourceMarkers(IResource file, final String generatorId) throws CoreException {
	Iterable<IMarker> filtered = Iterables.filter(Arrays.asList(findDerivedResourceMarkers(file)), new Predicate<IMarker>() {
		@Override
		public boolean apply(IMarker input) {
			if (input.exists()) {
				try {
					if (generatorId != null && generatorId.equals(input.getAttribute(ATTR_GENERATOR))) {
						return true;
					}
				} catch (CoreException e) {
					return Exceptions.throwUncheckedException(e);
				}
			}
			return false;
		}
	});
	// filter only once
	return Lists.newArrayList(filtered);
}
 
Example #7
Source File: TestedWorkspace.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
public <Result> Result run(IWorkspaceModifyOperationWithResult<? extends Result> op) {
	try {
		return new WorkspaceModifyOperation(op.getRule()) {
			Result result;
			@Override
			protected void execute(IProgressMonitor monitor) throws InvocationTargetException, CoreException, InterruptedException {
				this.result = op.compute(monitor);
			}
			protected Result getResult() throws InvocationTargetException, InterruptedException {
				run(monitor());
				return result;
			}
		}.getResult();
	} catch (InvocationTargetException | InterruptedException e) {
		return Exceptions.throwUncheckedException(e);
	}
}
 
Example #8
Source File: TestedWorkspace.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
public void removeNature(IProject project, String natureId) {
	try {
		IResourcesSetupUtil.removeNature(project, natureId);
	} catch (Exception e) {
		Exceptions.throwUncheckedException(e);
	}
}
 
Example #9
Source File: TestedWorkspace.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
public String readFile(IFile file) {
	try {
		return IResourcesSetupUtil.fileToString(file);
	} catch (Exception e) {
		return Exceptions.throwUncheckedException(e);
	}
}
 
Example #10
Source File: TestedWorkspace.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
public void deleteAllProjects() {
	try {
		IResourcesSetupUtil.cleanWorkspace();
	} catch (Exception e) {
		Exceptions.throwUncheckedException(e);
	}
}
 
Example #11
Source File: TestedWorkspace.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
public void run(IWorkspaceModifyOperation op) {
	try {
		new WorkspaceModifyOperation(op.getRule()) {
			@Override
			protected void execute(IProgressMonitor monitor) throws InvocationTargetException, CoreException, InterruptedException {
				op.accept(monitor);
			}
		}.run(monitor());
	} catch (InvocationTargetException | InterruptedException e) {
		Exceptions.throwUncheckedException(e);
	}
}
 
Example #12
Source File: TestedWorkspace.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
public IProject createProject(String name) {
	try {
		return IResourcesSetupUtil.createProject(name);
	} catch (Exception e) {
		return Exceptions.throwUncheckedException(e);
	}
}
 
Example #13
Source File: TestedWorkspace.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
public void addNature(IProject project, String natureId) {
	try {
		IResourcesSetupUtil.addNature(project, natureId);
	} catch (Exception e) {
		Exceptions.throwUncheckedException(e);
	}
}
 
Example #14
Source File: TestedWorkspace.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
public void addBuilder(IProject project, String builderId) {
	try {
		IResourcesSetupUtil.addBuilder(project, builderId);
	} catch (Exception e) {
		Exceptions.throwUncheckedException(e);
	}
}
 
Example #15
Source File: TestedWorkspace.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
public void setReference(final IProject from, final IProject to) {
	try {
		IResourcesSetupUtil.setReference(from, to);
	} catch (Exception e) {
		Exceptions.throwUncheckedException(e);
	}
}
 
Example #16
Source File: TestedWorkspace.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
public void removeReference(final IProject from, final IProject to) {
	try {
		IResourcesSetupUtil.removeReference(from, to);
	} catch (Exception e) {
		Exceptions.throwUncheckedException(e);
	}
}
 
Example #17
Source File: TestedWorkspace.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
public void fullBuild() {
	try {
		joinJobsBeforeBuild();
		IResourcesSetupUtil.fullBuild();
	} catch (Exception e) {
		Exceptions.throwUncheckedException(e);
	}
}
 
Example #18
Source File: TestedWorkspace.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
public void removeBuilder(IProject project, String builderId) {
	try {
		IResourcesSetupUtil.removeBuilder(project, builderId);
	} catch (Exception e) {
		Exceptions.throwUncheckedException(e);
	}
}
 
Example #19
Source File: WorkspaceResourceAccess.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public <R> R readOnly(URI targetURI, IUnitOfWork<R, ResourceSet> work) {
	return workspaceManager.doRead(targetURI, (document, resource) -> {
		if (resource == null) {
			return null;
		}
		try {
			return work.exec(resource.getResourceSet());
		} catch (Exception e) {
			return Exceptions.throwUncheckedException(e);
		}
	});
}
 
Example #20
Source File: SARLLabelProvider.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** Invoked when an image descriptor cannot be found.
 *
 * @param params the parameters given to the method polymorphic dispatcher.
 * @param exception the cause of the error.
 * @return the image descriptor for the error.
 */
protected ImageDescriptor handleImageDescriptorError(Object[] params, Throwable exception) {
	if (exception instanceof NullPointerException) {
		final Object defaultImage = getDefaultImage();
		if (defaultImage instanceof ImageDescriptor) {
			return (ImageDescriptor) defaultImage;
		}
		if (defaultImage instanceof Image) {
			return ImageDescriptor.createFromImage((Image) defaultImage);
		}
		return super.imageDescriptor(params[0]);
	}
	return Exceptions.throwUncheckedException(exception);
}
 
Example #21
Source File: AbstractExtraLanguageValidator.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** Handle an exception.
 *
 * @param targetException the exception.
 * @param context the context.
 */
@SuppressWarnings("static-method")
protected void handleInvocationTargetException(Throwable targetException, Context context) {
	// ignore NullPointerException, as not having to check for NPEs all the time is a convenience feature
	if (!(targetException instanceof NullPointerException)) {
		Exceptions.throwUncheckedException(targetException);
	}
}
 
Example #22
Source File: TestedWorkspaceWithJDT.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
public void addToClasspath(IJavaProject project, IClasspathEntry newClassPathEntry) {
	try {
		JavaProjectSetupUtil.addToClasspath(project, newClassPathEntry, false);
		build();
	} catch(Exception e) {
		Exceptions.throwUncheckedException(e);
	}
}
 
Example #23
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 #24
Source File: Bug486584Test.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
private IFile copyAndGetXtendExampleJar(IJavaProject javaProject) throws CoreException {
	IFile file = javaProject.getProject().getFile(XTEND_EXAMPLE_JAR);
	try (InputStream inputStream = Bug486584Test.class.getResourceAsStream(XTEND_EXAMPLE_JAR)) {
		file.create(inputStream, IResource.FORCE, null);	
	} catch (IOException e) {
		Exceptions.throwUncheckedException(e);
	}
	return file;
}
 
Example #25
Source File: TestedWorkspaceWithJDT.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Join on the job that updates the classpath after a manifest change. 
 * The calling thread will effectively wait for that job to finish.
 */
protected void joinUpdateClasspathJob() {
	try {
		// Pseudo fence to make sure that we see what we want to see.
		synchronized (this) {
			wait(1);
		}
		Job joinMe = updateClasspathJob;
		if (joinMe != null) {
			joinMe.join();
		}
	} catch (Exception e) {
		Exceptions.throwUncheckedException(e);
	}
}
 
Example #26
Source File: TestedWorkspace.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
public IFile createFile(IPath wsRelativePath, final String content) {
	try {
		return IResourcesSetupUtil.createFile(wsRelativePath, content);
	} catch (Exception e) {
		return Exceptions.throwUncheckedException(e);
	}
}
 
Example #27
Source File: TestedWorkspaceWithJDT.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
public IJavaProject createJavaProject(String name) {
	try {
		IJavaProject result = JavaProjectSetupUtil.createJavaProject(name, false);
		build();
		return result;
	} catch(Exception e) {
		return Exceptions.throwUncheckedException(e);
	}
}
 
Example #28
Source File: TestedWorkspaceWithJDT.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
public IClasspathEntry addJarToClasspath(IJavaProject project, IFile jarFile) {
	try {
		IClasspathEntry result = JavaProjectSetupUtil.addJarToClasspath(project, jarFile);
		build();
		return result;
	} catch(Exception e) {
		return Exceptions.throwUncheckedException(e);
	}
}
 
Example #29
Source File: TestedWorkspaceWithJDT.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
public void addProjectReference(IJavaProject from, IJavaProject to) {
	try {
		JavaProjectSetupUtil.addProjectReference(from, to, false);
		addDynamicProjectReference(from.getProject(), to.getProject());
		build();
	} catch(Exception e) {
		Exceptions.throwUncheckedException(e);
	}
}
 
Example #30
Source File: TestedWorkspaceWithJDT.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
public void removeProjectReference(IJavaProject from, IJavaProject to) {
	try {
		JavaProjectSetupUtil.removeProjectReference(from, to);
		removeDynamicProjectReference(from.getProject(), to.getProject());
		build();
	} catch(Exception e) {
		Exceptions.throwUncheckedException(e);
	}
}