| /** |
| * Copyright (c) 2011, 2015 - Lunifera GmbH (Gross Enzersdorf, Austria), 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 2.0 |
| * which accompanies this distribution, and is available at |
| * https://www.eclipse.org/legal/epl-2.0/ |
| * |
| * SPDX-License-Identifier: EPL-2.0 |
| * |
| * Contributors: |
| * Florian Pirchner - Initial implementation |
| * |
| */ |
| package org.eclipse.osbp.xtext.oxtype.linking; |
| |
| import org.eclipse.emf.ecore.EObject; |
| import org.eclipse.emf.ecore.EReference; |
| import org.eclipse.osbp.xtext.oxtype.scoping.OXDelegatingNamespaceScopeProvider; |
| import org.eclipse.xtext.linking.impl.ImportedNamesAdapter; |
| import org.eclipse.xtext.resource.IEObjectDescription; |
| import org.eclipse.xtext.scoping.IScope; |
| import org.eclipse.xtext.scoping.IScopeProvider; |
| import org.eclipse.xtext.scoping.impl.AbstractGlobalScopeDelegatingScopeProvider; |
| import org.eclipse.xtext.scoping.impl.IDelegatingScopeProvider; |
| import org.slf4j.Logger; |
| import org.slf4j.LoggerFactory; |
| |
| /** |
| * This linking service supports the OXDelegatingNamespaceScopeProvider. Since |
| * OXDelegatingNamespaceScopeProvider delegates between JvmTypes and EObjects, |
| * we need to pass the EReference. |
| */ |
| public class OXTypeLinkingService extends JvmTypeEnhancingLinkingService { |
| |
| private static final Logger LOGGER = LoggerFactory |
| .getLogger(OXTypeLinkingService.class); |
| |
| protected IScope getScope(EObject context, EReference reference) { |
| if (getScopeProvider() == null) |
| throw new IllegalStateException("scopeProvider must not be null."); |
| try { |
| registerImportedNamesAdapter(context, reference); |
| return getScopeProvider().getScope(context, reference); |
| } finally { |
| unRegisterImportedNamesAdapter(reference); |
| } |
| } |
| |
| protected void doCheckAndLog(EObject context, EReference ref, |
| IEObjectDescription eObjectDescription) { |
| } |
| |
| protected void unRegisterImportedNamesAdapter() { |
| throw new UnsupportedOperationException("Use method with EReference!"); |
| } |
| |
| protected void unRegisterImportedNamesAdapter(IScopeProvider scopeProvider) { |
| throw new UnsupportedOperationException("Use method with EReference!"); |
| } |
| |
| protected void registerImportedNamesAdapter(EObject context) { |
| throw new UnsupportedOperationException("Use method with EReference!"); |
| } |
| |
| protected void registerImportedNamesAdapter(IScopeProvider scopeProvider, |
| EObject context) { |
| throw new UnsupportedOperationException("Use method with EReference!"); |
| } |
| |
| protected void unRegisterImportedNamesAdapter(EReference eReference) { |
| unRegisterImportedNamesAdapter(getScopeProvider(), eReference); |
| } |
| |
| protected void unRegisterImportedNamesAdapter(IScopeProvider scopeProvider, |
| EReference eReference) { |
| if (scopeProvider instanceof OXDelegatingNamespaceScopeProvider) { |
| // this scope provider just delegates between JvmTypes and EObjects |
| OXDelegatingNamespaceScopeProvider provider = (OXDelegatingNamespaceScopeProvider) scopeProvider; |
| provider.setWrapper(null, eReference); |
| } else if (scopeProvider instanceof AbstractGlobalScopeDelegatingScopeProvider) { |
| AbstractGlobalScopeDelegatingScopeProvider provider = (AbstractGlobalScopeDelegatingScopeProvider) scopeProvider; |
| provider.setWrapper(null); |
| } else if (scopeProvider instanceof IDelegatingScopeProvider) { |
| IDelegatingScopeProvider delegatingScopeProvider = (IDelegatingScopeProvider) scopeProvider; |
| unRegisterImportedNamesAdapter( |
| delegatingScopeProvider.getDelegate(), eReference); |
| } |
| } |
| |
| protected void registerImportedNamesAdapter(EObject context, |
| EReference eReference) { |
| registerImportedNamesAdapter(getScopeProvider(), context, eReference); |
| } |
| |
| protected void registerImportedNamesAdapter(IScopeProvider scopeProvider, |
| EObject context, EReference eReference) { |
| if (scopeProvider instanceof OXDelegatingNamespaceScopeProvider) { |
| // this scope provider just delegates between JvmTypes and EObjects |
| try { |
| OXDelegatingNamespaceScopeProvider provider = (OXDelegatingNamespaceScopeProvider) scopeProvider; |
| ImportedNamesAdapter adapter = getImportedNamesAdapter(context); |
| provider.setWrapper(adapter, eReference); |
| } catch (Exception e) { |
| LOGGER.error("{}", e); |
| } |
| } else if (scopeProvider instanceof AbstractGlobalScopeDelegatingScopeProvider) { |
| AbstractGlobalScopeDelegatingScopeProvider provider = (AbstractGlobalScopeDelegatingScopeProvider) scopeProvider; |
| ImportedNamesAdapter adapter = getImportedNamesAdapter(context); |
| provider.setWrapper(adapter); |
| } else if (scopeProvider instanceof IDelegatingScopeProvider) { |
| IDelegatingScopeProvider delegatingScopeProvider = (IDelegatingScopeProvider) scopeProvider; |
| registerImportedNamesAdapter(delegatingScopeProvider.getDelegate(), |
| context, eReference); |
| } |
| } |
| |
| } |