Skip to content

Instantly share code, notes, and snippets.

View nbhusare's full-sized avatar
🏠
Working from home

Neeraj Bhusare nbhusare

🏠
Working from home
View GitHub Profile
java.lang.NullPointerException
at org.eclipse.emf.ecore.impl.BasicEObjectImpl.eSettingDelegate(BasicEObjectImpl.java:1565)
at org.eclipse.emf.ecore.impl.BasicEObjectImpl.eGet(BasicEObjectImpl.java:1027)
at org.neclipse.xtext.example.smalljava.smallJavaDsl.impl.ClassTypeImpl.eGet(ClassTypeImpl.java:120)
at org.eclipse.emf.ecore.impl.BasicEObjectImpl.eGet(BasicEObjectImpl.java:1011)
at org.eclipse.emf.ecore.impl.BasicEObjectImpl.eGet(BasicEObjectImpl.java:1003)
at org.eclipse.emf.compare.utils.ReferenceUtil.safeEGet(ReferenceUtil.java:94)
at org.eclipse.emf.compare.diff.DefaultDiffEngine.computeSingleValuedAttributeDifferences(DefaultDiffEngine.java:1024)
at org.eclipse.emf.compare.diff.DefaultDiffEngine.computeDifferences(DefaultDiffEngine.java:575)
at org.eclipse.emf.compare.diff.DefaultDiffEngine.checkForDifferences(DefaultDiffEngine.java:151)
@nbhusare
nbhusare / Injection.xtend
Created July 26, 2016 10:23
TIP : Member injection
val resourceServiceProvider = IResourceServiceProvider.Registry.INSTANCE.getResourceServiceProvider(URI.createURI("dummy.mydsl"))
resourceServiceProvider.get(Injector).injectMembers(this)
val resolver = new ClassloaderClasspathUriResolver()
val classpathURI = URI.createURI('''«ClasspathUriUtil.CLASSPATH_SCHEME»:/«packagePath»/«fileName»''')
val normalizedURI = resolver.resolve(this, classpathURI) /*resolver.resolve(null, classpathURI)*/
val resource = new ResourceSetImpl().getResource(normalizedUri.trimFragment(), true)
if (resource.diagnostic != Diagnostic.OK_INSTANCE) {
throw new IllegalStateException("Problem parsing '" + uri + "':\n" +
Joiner.on('\n').join(resource.getErrors()));
}
resource
val resolver = new BundleClasspathUriResolver()
val classpathUri = URI.createURI('''«ClasspathUriUtil.CLASSPATH_SCHEME»:/models/yourmodel.xcore''')
val normalizedUri = resolver.resolve(Platform.getBundle("org.ne.emf.xcore.models"), classpathUri) /*resolver.resolve(null, classpathURI)*/
val resource = new ResourceSetImpl().getResource(normalizedUri.trimFragment(), true)
if (resource.diagnostic != Diagnostic.OK_INSTANCE) {
throw new IllegalStateException("Problem parsing '" + uri + "':\n" +
Joiner.on('\n').join(resource.getErrors()));
}
resource
/*TIP : Getting handle of services across DSL's using IGlobalServiceProvider*/
// DSL Project
@Inject private IGlobalServiceProvider globalServiceProvider
val myLanguageIndex = globalServiceProvider.findService(URI.createURI("dummy.mylang"), MyLanguageIndex)
val myAnotherLanguageIndex = globalServiceProvider.findService(URI.createURI("dummy.anotherlang"), MyAnotherLanguageIndex)
// Non-DSL Project
val registry = IResourceServiceProvider.Registry.INSTANCE;
public class MyDslDocumentProvider extends XtextDocumentProvider {
@Override
protected void doSaveDocument(IProgressMonitor monitor, Object element, IDocument document, boolean overwrite)
throws CoreException {
try {
final IHandlerService service = (IHandlerService) PlatformUI.getWorkbench().getService(IHandlerService.class);
service.executeCommand("org.eclipse.xtext.ui.FormatAction", null);
} catch (Exception e) {
e.printStackTrace();
}
@nbhusare
nbhusare / ResourceDescriptionsData Handle
Last active May 31, 2017 08:40
Getting handle of ResourceDescriptionsData for indexed access to the resource descriptions
final XtextResourceSet xtextResourceSet = ...
final ResourceDescriptionsData resourceDescriptionsData = ResourceDescriptionsData.ResourceSetAdapter.findResourceDescriptionsData(xtextResourceSet);
if (resourceDescriptionsData != null) {
//resourceDescriptionsData.getAllResourceDescriptions()
//resourceDescriptionsData.getResourceDescription(uri)
//resourceDescriptionsData.getExportedObjectsByType(type)
}
@nbhusare
nbhusare / DisableResourceSetNotification.java
Last active June 1, 2017 18:31
Disable notification delivery to the adapters
boolean deliver = resourceSet.eDeliver();
try {
// Disable notification delivery to the adapters
resourceSet.eSetDeliver(false);
// Make change to the resource set
resourceSet.getResources().clear();
} finally {
// Reset the deliver flag to its previous state
resourceSet.eSetDeliver(deliver);
}
@nbhusare
nbhusare / AddingResourceToReasourceset.java
Last active August 16, 2017 09:58
Safe way of adding resource to the resource set
// Get the URI to the resource
final URI uri = resource.getURI();
// Get the resource resolved by the URI. Do not load the resource, only get one if exists
final Resource r = resourceSet.getResource(uri, false);
if (r != null) {
// Add the resource to the resourceset
resourceSet.getResources().add(resource);
}
eClass.getEPackage().getEFactoryInstance().create(eClass);