| /** |
| * |
| * Copyright (c) 2011, 2016 - Loetz GmbH&Co.KG (69115 Heidelberg, Germany) |
| * |
| * 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 |
| * |
| * Contributors: |
| * Christophe Loetz (Loetz GmbH&Co.KG) - initial implementation |
| */ |
| package org.eclipse.osbp.xtext.basic.validation; |
| |
| import java.util.Arrays; |
| import java.util.List; |
| |
| import org.eclipse.emf.ecore.EClass; |
| import org.eclipse.emf.ecore.EObject; |
| import org.eclipse.emf.ecore.resource.Resource; |
| import org.eclipse.xtext.resource.IEObjectDescription; |
| import org.eclipse.xtext.resource.IResourceDescription; |
| import org.eclipse.xtext.resource.IResourceServiceProvider; |
| import org.eclipse.xtext.util.CancelIndicator; |
| |
| import com.google.inject.Inject; |
| |
| import org.eclipse.xtext.common.types.JvmAnnotationTarget; |
| import org.eclipse.xtext.common.types.JvmExecutable; |
| import org.eclipse.xtext.common.types.JvmIdentifiableElement; |
| import org.eclipse.xtext.common.types.JvmMember; |
| import org.eclipse.xtext.common.types.JvmTypeParameterDeclarator; |
| |
| public class NamesAreUniqueValidator extends org.eclipse.xtext.validation.NamesAreUniqueValidator implements INamesAreUniqueValidationClassComparator { |
| |
| @Inject |
| private IResourceServiceProvider.Registry resourceServiceProviderRegistry = IResourceServiceProvider.Registry.INSTANCE; |
| |
| // !!! vvv |
| /** use our own helper */ |
| private INamesAreUniqueValidationHelper helper = new NamesAreUniqueValidationHelper(this); |
| // ^^^ !!! |
| |
| @Override |
| public void doCheckUniqueNames(Resource resource, CancelIndicator cancelIndicator) { |
| final IResourceServiceProvider resourceServiceProvider = resourceServiceProviderRegistry.getResourceServiceProvider(resource.getURI()); |
| if (resourceServiceProvider==null) |
| return; |
| IResourceDescription.Manager manager = resourceServiceProvider.getResourceDescriptionManager(); |
| if (manager != null) { |
| IResourceDescription description = manager.getResourceDescription(resource); |
| if (description != null) { |
| Iterable<IEObjectDescription> descriptions = description.getExportedObjects(); |
| helper.checkUniqueNames(descriptions, cancelIndicator, this); |
| } |
| } |
| } |
| |
| // private final static List<Class<?>> ingoreClasses = Arrays.asList( |
| // JvmIdentifiableElement.class, |
| // JvmAnnotationTarget.class, |
| // JvmMember.class, |
| // JvmExecutable.class, |
| // JvmTypeParameterDeclarator.class, |
| // EObject.class |
| // ); |
| |
| protected boolean ignoreClass(EClass eclass) { |
| // List<Class<?>> ingoreClassess = Arrays.asList( |
| // JvmIdentifiableElement.class, |
| // JvmAnnotationTarget.class, |
| // JvmMember.class, |
| // JvmExecutable.class, |
| // JvmTypeParameterDeclarator.class, |
| // EObject.class |
| // ); |
| // |
| // Class<?> tClass = eclass.getInstanceClass(); |
| // if (ingoreClassess.contains(tClass)) { |
| // return true; |
| // } |
| return false; |
| } |
| |
| @Override |
| public boolean areOfEqualClass(EObject object1, EObject object2) { |
| EClass class1 = object1.eClass(); |
| EClass class2 = object2.eClass(); |
| EObject container1 = object1.eContainer(); |
| EObject container2 = object2.eContainer(); |
| if ((container1 != null) && (container2 != null) && !container1.equals(container2)) { |
| return false; |
| } |
| if (class1.isInstance(object2)) { |
| return true; |
| } |
| if (class2.isInstance(object1)) { |
| return true; |
| } |
| for (EClass super1 : class1.getEAllSuperTypes()) { |
| if (!ignoreClass(super1) && super1.isInstance(object2)) { |
| return true; |
| } |
| } |
| for (EClass super2 : class2.getEAllSuperTypes()) { |
| if (!ignoreClass(super2) && super2.isInstance(object1)) { |
| return true; |
| } |
| } |
| return false; |
| } |
| } |