Java Code Examples for org.eclipse.emf.ecore.util.EcoreUtil#getAdapter()

The following examples show how to use org.eclipse.emf.ecore.util.EcoreUtil#getAdapter() . 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: JvmModelAssociator.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected Adapter getOrInstall(Resource resource) {
	if (!(resource instanceof XtextResource)) {
		return new Adapter();
	}
	if (resource instanceof JvmMemberInitializableResource) {
		((JvmMemberInitializableResource) resource).ensureJvmMembersInitialized();
	}
	String resourceLanguageName = ((XtextResource) resource).getLanguageName();
	if (!languageName.equals(resourceLanguageName)){
		return new Adapter();
	}
	Adapter adapter = (Adapter) EcoreUtil.getAdapter(resource.eAdapters(), Adapter.class);
	if (adapter == null) {
		adapter = new Adapter();
		resource.eAdapters().add(adapter);
	}
	return adapter;
}
 
Example 2
Source File: GlobalObjectScopeAccess.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Registers an instance of the {@link GlobalObjectScope} for the given context {@link ResourceSet}.
 */
public static void registerGlobalObjectScope(GlobalObjectScope scope, ResourceSet context) {
	if (EcoreUtil.getAdapter(context.eAdapters(), GlobalObjectScope.class) != null) {
		throw new IllegalStateException("Attempt to install adapter for GlobalObjectScope twice");
	}
	GlobalObjectScopeAccess adapter = new GlobalObjectScopeAccess(scope);
	context.eAdapters().add(adapter);
}
 
Example 3
Source File: JvmTypeChangeDispatcher.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
public static JvmTypeChangeDispatcher findResourceChangeDispatcher(Notifier notifier) {
	JvmTypeChangeDispatcher result = (JvmTypeChangeDispatcher) EcoreUtil.getAdapter(
			notifier.eAdapters(), JvmTypeChangeDispatcher.class);
	if (result == null) {
		result = new JvmTypeChangeDispatcher();
		notifier.eAdapters().add(result);
	}
	return result;
}
 
Example 4
Source File: ProjectAdapter.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** Get the project associated to the resource.
 *
 * @param resource the resource
 * @return the project.
 */
public static IProject getProject(Resource resource) {
	ProjectAdapter adapter = (ProjectAdapter) EcoreUtil.getAdapter(resource.getResourceSet().eAdapters(), ProjectAdapter.class);
	if (adapter == null) {
		final String platformString = resource.getURI().toPlatformString(true);
		final IProject project = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(platformString)).getProject();
		adapter = new ProjectAdapter(project);
		resource.getResourceSet().eAdapters().add(adapter);
	}
	return adapter.getProject();
}
 
Example 5
Source File: SARLOperationHelper.java    From sarl with Apache License 2.0 5 votes vote down vote up
@Override
public void attachPureAnnotationAdapter(JvmOperation operation,
		Function2<? super JvmOperation, ? super IOperationHelper, ? extends Boolean> dynamicCallback) {
	if (operation != null && dynamicCallback != null) {
		AnnotationJavaGenerationAdapter adapter = (AnnotationJavaGenerationAdapter) EcoreUtil.getAdapter(
				operation.eAdapters(), AnnotationJavaGenerationAdapter.class);
		if (adapter == null) {
			adapter = new AnnotationJavaGenerationAdapter();
			operation.eAdapters().add(adapter);
		}
		adapter.addPredicate(dynamicCallback);
	}
}
 
Example 6
Source File: AbstractPolymorphicScopeProvider.java    From dsl-devkit with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Gets the scope given a context object and an expected type, using the given scope name.
 *
 * @param context
 *          the context, must not be {@code null}
 * @param namedElement
 *          the named element (either EClass or EReference), must not be {@code null}
 * @param scopeName
 *          the scope name, must not be {@code null}
 * @param originalResource
 *          the original resource, must not be {@code null}
 * @return the scope, or {@code null} if none
 */
@SuppressWarnings({"PMD.CompareObjectsWithEquals", "PMD.NPathComplexity"})
protected IScope internalGetScope(final EObject context, final ENamedElement namedElement, final String scopeName, final Resource originalResource) {
  IScope result = null;
  IResourceSetScopeCache resourceSetScopeCache = null;
  ResourceCache<Object, IScope> resourceScopeCache = null;
  Object cacheKey = null;
  Object globalCacheKey = null;

  if (originalResource != null) {
    final ResourceSet resourceSet = originalResource.getResourceSet();
    if (resourceSet != null) {
      final Adapter scopeCacheAdapter = EcoreUtil.getAdapter(resourceSet.eAdapters(), IResourceSetScopeCache.class);
      if (scopeCacheAdapter instanceof IResourceSetScopeCache && doGlobalCache(context, namedElement, scopeName, originalResource)) {
        resourceSetScopeCache = (IResourceSetScopeCache) scopeCacheAdapter;
        globalCacheKey = getGlobalCacheKey(context, namedElement, scopeName);
        result = resourceSetScopeCache.get(globalCacheKey);
      }
    }
    if (result == null && doCache(context, namedElement, scopeName, originalResource)) {
      resourceScopeCache = CacheManager.getInstance().getOrCreateResourceCache("AbstractPolymorphicScopeProvider#resourceScopeCache", originalResource); //$NON-NLS-1$
      cacheKey = getCacheKey(context, namedElement, scopeName);
      result = resourceScopeCache.get(cacheKey);
    }
    if (result != null) {
      return result == NO_SCOPE ? null : result;
    }
  }

  result = doGetScope(context, namedElement, scopeName, originalResource);

  if (resourceSetScopeCache != null) {
    resourceSetScopeCache.put(globalCacheKey, result == null ? NO_SCOPE : result);
  } else if (resourceScopeCache != null) {
    resourceScopeCache.set(cacheKey, result == null ? NO_SCOPE : result);
  }
  return result;
}
 
Example 7
Source File: JvmTypesBuilder.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Retrieves the attached documentation for the given source element.
 * By default this implementation provides the text of a multi line comment preceding the definition of the given source element.
 * 
 * @return the documentation of the given source, <code>null</code> if source is <code>null</code> or doesn't have any documentation.
 */
/* @Nullable */
public String getDocumentation(/* @Nullable */ EObject source) {
	if (source == null)
		return null;
	if (source instanceof JvmIdentifiableElement) {
		DocumentationAdapter adapter = (DocumentationAdapter) EcoreUtil.getAdapter(source.eAdapters(), DocumentationAdapter.class);
		if (adapter != null)
			return adapter.getDocumentation();
	}
	String documentation = documentationProvider.getDocumentation(source);
	return documentation;
}
 
Example 8
Source File: JvmTypeChangeDispatcher.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
public void requestNotificationOnChange(Notifier notifier, Runnable runnable) {
	NotificationDispatcher result = (NotificationDispatcher) EcoreUtil.getAdapter(
			notifier.eAdapters(), NotificationDispatcher.class);
	if (result == null) {
		result = new NotificationDispatcher(notifier);
		notifier.eAdapters().add(result);
	}
	synchronized (listeners) {
		listeners.add(runnable);
	}
}
 
Example 9
Source File: BonitaEditingDomainUtil.java    From bonita-studio with GNU General Public License v2.0 5 votes vote down vote up
private static void addResourceTracking(final TransactionalEditingDomain editingDomain) {
	final EList<Adapter> adapters = editingDomain.getResourceSet().eAdapters();
	final Adapter adapter = EcoreUtil.getAdapter(adapters, ModificationAdapter.class);
	if (adapter == null) {
		final NotificationFilter diagramResourceModifiedFilter = NotificationFilter.createNotifierFilter(editingDomain.getResourceSet())
				.and(NotificationFilter.createEventTypeFilter(Notification.ADD))
				.and(NotificationFilter.createFeatureFilter(ResourceSet.class, ResourceSet.RESOURCE_SET__RESOURCES));
		adapters.add(new ModificationAdapter(diagramResourceModifiedFilter));
	}
}
 
Example 10
Source File: XcoreReader.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
@Override
protected void invokeInternal(WorkflowContext ctx, ProgressMonitor monitor,
		Issues issues) {
	ResourceSet resourceSet = getResourceSet();

	// due to some Xcore peculiarity we have to access the IAllContainerState here
	// to trigger some lazy init logic
	IAllContainersState allContainerState = (IAllContainersState) EcoreUtil.getAdapter(resourceSet.eAdapters(),
			IAllContainersState.class);
	allContainerState.isEmpty("");

	Multimap<String, URI> uris = getPathTraverser().resolvePathes(pathes,
			new Predicate<URI>() {
		@Override
		public boolean apply(URI input) {
			return input.fileExtension().equals(XCORE_FILE_EXT);
		}
	});
	List<Resource> resources = new ArrayList<>();
	for (URI uri : uris.values()) {
		LOGGER.info(uri);
		try {
			resources.add(parse(uri, resourceSet));
		} catch (Exception e) {
			LOGGER.error("Problem during loading of resource @ " + uri, e);
		}
	}
	installIndex(resourceSet);
	for (Resource r : resources) {
		EcoreUtil.resolveAll(r);
		for (Diagnostic x : r.getErrors()) {
			issues.addError(x.getMessage(), x);
		}

	}
	ctx.set(slot, resources);
}
 
Example 11
Source File: CompilerPhases.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
public boolean isIndexing(Notifier ctx) {
	ResourceSet set = EcoreUtil2.getResourceSet(ctx);
	if (set != null) {
		return EcoreUtil.getAdapter(set.eAdapters(), IndexingAdapter.INSTANCE) != null;
	}
	return false;
}
 
Example 12
Source File: VirtualBaseTypeScope.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Obtains an instance in the context of the given resourceSet.
 */
public static VirtualBaseTypeScope get(ResourceSet resourceSet) {
	VirtualBaseTypeScopeAccess result = (VirtualBaseTypeScopeAccess) EcoreUtil.getAdapter(resourceSet.eAdapters(),
			VirtualBaseTypeScope.class);
	if (result == null) {
		throw new IllegalStateException("Missing adapter for VirtualBaseTypeScope");
	}
	return result.getScope();
}
 
Example 13
Source File: N4JSRuntimeCore.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
private void installIndex(ResourceSet resourceSet) {
	// Fill index
	ResourceDescriptionsData index = new OrderedResourceDescriptionsData(
			Collections.<IResourceDescription> emptyList());
	List<Resource> resources = Lists.newArrayList(resourceSet.getResources());
	for (Resource resource : resources) {
		index(resource, resource.getURI(), index);
	}
	Adapter existing = EcoreUtil.getAdapter(resourceSet.eAdapters(), ResourceDescriptionsData.class);
	if (existing != null) {
		resourceSet.eAdapters().remove(existing);
	}
	ResourceDescriptionsData.ResourceSetAdapter.installResourceDescriptionsData(resourceSet, index);
}
 
Example 14
Source File: OutputConfigurationProvider.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * @since 2.9
 */
@Override
public Set<OutputConfiguration> getOutputConfigurations(ResourceSet context) {
	EList<Adapter> adapters = context.eAdapters();
	OutputConfigurationAdapter adapter = (OutputConfigurationAdapter) EcoreUtil.getAdapter(adapters, OutputConfigurationAdapter.class);
	if (adapter == null) {
		return getOutputConfigurations();
	} else {
		Set<OutputConfiguration> outputConfigurations = adapter.getOutputConfigurationsPerLanguage().get(languageName);
		return outputConfigurations == null ? getOutputConfigurations() : outputConfigurations;
	}
}
 
Example 15
Source File: ProxyModelAssociationsAdapter.java    From dsl-devkit with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Installs a {@link ProxyModelAssociationsAdapter} unless the given resource already has an {@link InferredModelAssociator.Adapter}.
 * 
 * @param resource
 *          resource to add adapter to, must not be {@code null}
 */
static void install(final StorageAwareResource resource) {
  InferredModelAssociator.Adapter adapter = (InferredModelAssociator.Adapter) EcoreUtil.getAdapter(resource.eAdapters(), InferredModelAssociator.Adapter.class);
  if (adapter == null) {
    adapter = new ProxyModelAssociationsAdapter(resource);
    resource.eAdapters().add(adapter);
  }
}
 
Example 16
Source File: N4JSBuilderParticipant.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void build(IBuildContext context, IProgressMonitor monitor) throws CoreException {
	IBuildParticipantInstruction delegate = (IBuildParticipantInstruction) EcoreUtil.getAdapter(context
			.getResourceSet().eAdapters(), IBuildParticipantInstruction.class);
	if (delegate == null) {
		if (context.getBuildType() == BuildType.CLEAN) {
			super.build(context, monitor);
		}
	} else {
		delegate.finish(context.getDeltas(), monitor);
		if (delegate.isRebuild()) {
			context.needRebuild();
		}
	}
}
 
Example 17
Source File: JvmMemberDeclarationImpl.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
public void setDocComment(final String docComment) {
  this.checkMutable();
  Adapter _adapter = EcoreUtil.getAdapter(this.getDelegate().eAdapters(), DocumentationAdapter.class);
  DocumentationAdapter adapter = ((DocumentationAdapter) _adapter);
  if ((adapter == null)) {
    DocumentationAdapter _documentationAdapter = new DocumentationAdapter();
    adapter = _documentationAdapter;
    EList<Adapter> _eAdapters = this.getDelegate().eAdapters();
    _eAdapters.add(adapter);
  }
  adapter.setDocumentation(docComment);
}
 
Example 18
Source File: SARLEcoreDocumentationSyntacticSequencer.java    From sarl with Apache License 2.0 5 votes vote down vote up
protected void emitDocumentation(EObject semanticObject) {
	if (this.documentedSemanticObjects.add(semanticObject)) {
		DocumentationAdapter documentationAdapter = (DocumentationAdapter) EcoreUtil.getAdapter(semanticObject.eAdapters(), DocumentationAdapter.class);
		if (documentationAdapter != null) {
			emitDocumentation(semanticObject.getClass(), documentationAdapter.getDocumentation());
		}
	}
}
 
Example 19
Source File: ResourceSetBasedAllContainersStateProvider.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public IAllContainersState get(IResourceDescriptions context) {
	ResourceSet resourceSet = getResourceSet(context);
	IAllContainersState adapter = (IAllContainersState) EcoreUtil.getAdapter(resourceSet.eAdapters(),
			IAllContainersState.class);
	if (adapter == null) {
		return handleAdapterNotFound(resourceSet);
	}
	return adapter;
}
 
Example 20
Source File: DirectLinkingSourceLevelURIsAdapter.java    From dsl-devkit with Eclipse Public License 1.0 2 votes vote down vote up
/**
 * Finds an installed {@link DirectLinkingSourceLevelURIsAdapter} adapter for a given resource set.
 *
 * @param resourceSet
 *          resource set, must not be {@code null}
 * @return installed adapter or {@code null} if none
 */
public static DirectLinkingSourceLevelURIsAdapter findInstalledAdapter(final ResourceSet resourceSet) {
  return (DirectLinkingSourceLevelURIsAdapter) EcoreUtil.getAdapter(resourceSet.eAdapters(), DirectLinkingSourceLevelURIsAdapter.class);
}