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

The following are Jave code examples for showing how to use getAdapter() of the org.eclipse.emf.ecore.util.EcoreUtil class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: n4js   File: N4JSGenerateImmediatelyBuilderState.java   View Source Code Vote up 6 votes
@Override
protected void updateMarkers(Delta delta, ResourceSet resourceSet, IProgressMonitor monitor) {
	Measurement mes = dcValidations.getMeasurement("validation");
	super.updateMarkers(delta, resourceSet, monitor);
	mes.end();

	if (resourceSet != null) { // resourceSet is null during clean build
		mes = dcTranspilation.getMeasurement("transpilation");
		IBuildParticipantInstruction instruction = (IBuildParticipantInstruction) EcoreUtil.getAdapter(
				resourceSet.eAdapters(), IBuildParticipantInstruction.class);
		if (instruction == null) {
			throw new IllegalStateException();
		}
		try {
			instruction.process(delta, resourceSet, monitor);
		} catch (CoreException e) {
			handleCoreException(e);
		}
		mes.end();
	}

}
 
Example 2
Project: n4js   File: N4JSCache.java   View Source Code Vote up 6 votes
@Override
public CacheAdapter getOrCreate(Resource resource) {
	// copied from super method, but we expect N4JSCacheAdapter here
	// note: if you got a cast exception here (because the adapter is not an N4JSCacheAdapter but a simple one),
	// you probably have loaded a resource via the wrong scope provider. That is, you probably tried to
	// load a non-N4JS resource with an n4js provider (maybe vice versa...).
	// In order to solve that problem, use the org.eclipse.xtext.resource.IResourceServiceProvider.Registry
	// to get the correct providers for the N4JS language.
	// a nice example is found in:
	// org.eclipse.n4js.validation.validators.N4JSProjectSetupValidator.getAllPolyfills(Resource)
	N4JSCacheAdapter adapter = (N4JSCacheAdapter) EcoreUtil.getAdapter(resource.eAdapters(), CacheAdapter.class);
	if (adapter == null) {
		adapter = new N4JSCacheAdapter(); // changed from CacheAdapter to N4JSCacheAdapter
		resource.eAdapters().add(adapter);
		adapter.setResource(resource);
	}
	return adapter;
}
 
Example 3
Project: n4js   File: XcoreReader.java   View Source Code Vote up 5 votes
@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 4
Project: n4js   File: BuiltInTypeScopeAccess.java   View Source Code Vote up 5 votes
/**
 * Assign the given scope to the given resource set by means of an Adapter.
 */
public static void registerBuiltInTypeScope(BuiltInTypeScope scope, ResourceSet context) {
	if (EcoreUtil.getAdapter(context.eAdapters(), BuiltInTypeScope.class) != null) {
		throw new IllegalStateException("Attempt to install adapter for BuiltInTypeScope twice");
	}
	BuiltInTypeScopeAccess adapter = new BuiltInTypeScopeAccess(scope);
	context.eAdapters().add(adapter);
}
 
Example 5
Project: n4js   File: BuiltInTypeScope.java   View Source Code Vote up 5 votes
/**
 * Obtains an instance in the context of the given resourceSet.
 * <p>
 * This is the preferred method of creating a BuiltInTypeScope if code needs to access built-in types. But note that
 * there are convenience methods available in RuleEnvironmentExtensions; so, if a RuleEnvironment is already
 * available those methods should be used.
 */
public static BuiltInTypeScope get(ResourceSet resourceSet) {
	BuiltInTypeScopeAccess result = (BuiltInTypeScopeAccess) EcoreUtil.getAdapter(resourceSet.eAdapters(),
			BuiltInTypeScope.class);
	if (result == null) {
		throw new IllegalStateException("Missing adapter for BuiltInTypeScope");
	}
	return result.getScope();
}
 
Example 6
Project: n4js   File: N4JSBuilderParticipant.java   View Source Code Vote up 5 votes
@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 7
Project: n4js   File: N4JSRuntimeCore.java   View Source Code Vote up 5 votes
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 8
Project: n4js   File: VirtualBaseTypeScope.java   View Source Code Vote up 5 votes
/**
 * 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 9
Project: n4js   File: VirtualBaseTypeScopeAccess.java   View Source Code Vote up 5 votes
/**
 * Registers an instance of the {@link VirtualBaseTypeScope} for the given context {@link ResourceSet}.
 */
public static void registerVirtualBaseTypeScope(VirtualBaseTypeScope scope, ResourceSet context) {
	if (EcoreUtil.getAdapter(context.eAdapters(), VirtualBaseTypeScope.class) != null) {
		throw new IllegalStateException("Attempt to install adapter for VirtualBaseTypeScope twice");
	}
	VirtualBaseTypeScopeAccess adapter = new VirtualBaseTypeScopeAccess(scope);
	context.eAdapters().add(adapter);
}
 
Example 10
Project: n4js   File: GlobalObjectScopeAccess.java   View Source Code Vote up 5 votes
/**
 * 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 11
Project: n4js   File: GlobalObjectScope.java   View Source Code Vote up 5 votes
/**
 * Obtains an instance in the context of the given resourceSet.
 */
public static GlobalObjectScope get(ResourceSet resourceSet) {
	GlobalObjectScopeAccess result = (GlobalObjectScopeAccess) EcoreUtil.getAdapter(resourceSet.eAdapters(),
			GlobalObjectScope.class);
	if (result == null) {
		throw new IllegalStateException("Missing adapter for GlobalObjectScope");
	}
	return result.getScope();
}