All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.eclipse.xtext.xbase.validation.ExceptionInExpressionFinder Maven / Gradle / Ivy

There is a newer version: 2.4.3
Show newest version
/*******************************************************************************
 * Copyright (c) 2011 itemis AG (http://www.itemis.eu) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/
package org.eclipse.xtext.xbase.validation;

import static com.google.common.collect.Iterables.*;
import static com.google.common.collect.Lists.*;

import java.util.List;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.xtext.common.types.JvmIdentifiableElement;
import org.eclipse.xtext.common.types.JvmTypeReference;
import org.eclipse.xtext.util.IAcceptor;
import org.eclipse.xtext.xbase.XExpression;
import org.eclipse.xtext.xbase.typing.ITypeProvider;

import com.google.inject.Inject;

/**
 * @author Jan Koehnlein - Initial contribution and API
 * 
 * TODO this should be a service of the type provider
 */
public class ExceptionInExpressionFinder {

	@Inject
	private ITypeProvider typeProvider;
	
	public List findChildrenThrowingException(EObject rootExpression, JvmTypeReference thrownException) {
		final List result = newArrayList();
		findChildrenThrowingException(rootExpression, thrownException, new IAcceptor() {
			public void accept(EObject t) {
				result.add(t);
			}
		});
		return result;
	}

	protected void findChildrenThrowingException(EObject rootExpression, JvmTypeReference thrownException,
			IAcceptor acceptor) {
		boolean exceptionSeenInChildren = false;
		for(EObject element: rootExpression.eContents()) {
			if (element instanceof XExpression) {
				if(contains(typeProvider.getThrownExceptionTypes((XExpression)element),thrownException)) {
					exceptionSeenInChildren = true;
					findChildrenThrowingException(element, thrownException, acceptor);
				}
			} else if(element instanceof JvmIdentifiableElement) {
				if(contains(typeProvider.getThrownExceptionForIdentifiable((JvmIdentifiableElement)element),thrownException)) {
					exceptionSeenInChildren = true;
					findChildrenThrowingException(element, thrownException, acceptor);
				}
			}
		}
		if(!exceptionSeenInChildren)
			acceptor.accept(rootExpression);
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy