Java Code Examples for org.eclipse.xtext.resource.IEObjectDescription#getUserData()

The following examples show how to use org.eclipse.xtext.resource.IEObjectDescription#getUserData() . 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: ExternalIndexSynchronizer.java    From n4js with Eclipse Public License 1.0 6 votes vote down vote up
private String getVersion(IResourceDescription resourceDescription, SafeURI<?> nestedLocation, N4JSProjectName name,
		SafeURI<?> packageLocation) {

	if (!isProjectDescriptionFile(nestedLocation, packageLocation)) {
		return null;
	}

	Iterable<IEObjectDescription> pds = resourceDescription
			.getExportedObjectsByType(JSONPackage.eINSTANCE.getJSONDocument());
	Iterator<IEObjectDescription> pdsIter = pds.iterator();

	if (pdsIter.hasNext()) {
		IEObjectDescription pDescription = pdsIter.next();
		String nameFromPackageJSON = PackageJsonResourceDescriptionExtension.getProjectName(pDescription);
		if (nameFromPackageJSON == null || name.equals(new N4JSProjectName(nameFromPackageJSON))) {
			// consistency check
			String version = pDescription.getUserData(PackageJsonResourceDescriptionExtension.PROJECT_VERSION_KEY);
			return version;
		}
	}
	return null;
}
 
Example 2
Source File: N4JSResourceDescriptionDelta.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
@Override
protected boolean equals(IEObjectDescription oldObj, IEObjectDescription newObj) {
	if (oldObj == newObj)
		return true;
	if (oldObj.getEClass() != newObj.getEClass())
		return false;
	if (oldObj.getName() != null && !oldObj.getName().equals(newObj.getName()))
		return false;
	if (!oldObj.getEObjectURI().equals(newObj.getEObjectURI()))
		return false;
	String[] oldKeys = oldObj.getUserDataKeys();
	String[] newKeys = newObj.getUserDataKeys();
	if (oldKeys.length != newKeys.length)
		return false;
	for (String key : oldKeys) {
		// ------------------------------------------------ START of changes w.r.t. super class
		if (isIgnoredUserDataKey(key)) {
			continue;
		}
		// ------------------------------------------------ END of changes w.r.t. super class
		if (!Arrays.contains(newKeys, key))
			return false;
		String oldValue = oldObj.getUserData(key);
		String newValue = newObj.getUserData(key);
		if (oldValue == null) {
			if (newValue != null)
				return false;
		} else if (!oldValue.equals(newValue)) {
			return false;
		}
	}
	return true;
}
 
Example 3
Source File: XtextProposalProvider.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected StyledString getStyledDisplayString(IEObjectDescription description) {
	if (EcorePackage.Literals.EPACKAGE == description.getEClass()) {
		if ("true".equals(description.getUserData("nsURI"))) {
			String name = description.getUserData("name");
			if (name == null) {
				return new StyledString(description.getName().toString());
			}
			String string = name + " - " + description.getName();
			return new StyledString(string);
		}
	} else if(XtextPackage.Literals.GRAMMAR == description.getEClass()){
		QualifiedName qualifiedName = description.getQualifiedName();
		if(qualifiedName.getSegmentCount() >1) {
			return new StyledString(qualifiedName.getLastSegment() + " - " + qualifiedName.toString());
		}
		return new StyledString(qualifiedName.toString());
	} else if (XtextPackage.Literals.ABSTRACT_RULE.isSuperTypeOf(description.getEClass())) {
		EObject object = description.getEObjectOrProxy();
		if (!object.eIsProxy()) {
			AbstractRule rule = (AbstractRule) object;
			Grammar grammar = GrammarUtil.getGrammar(rule);
			if (description instanceof EnclosingGrammarAwareDescription) {
				if (grammar == ((EnclosingGrammarAwareDescription) description).getGrammar()) {
					return getStyledDisplayString(rule, null, rule.getName());
				}
			}
			return getStyledDisplayString(rule,
					grammar.getName() + "." + rule.getName(),
					description.getName().toString().replace(".", "::"));	
		}
		
	}
	return super.getStyledDisplayString(description);
}
 
Example 4
Source File: EObjectDescriptionBasedStubGenerator.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected void appendType(final IEObjectDescription description, Multimap<QualifiedName, IEObjectDescription> owner2nested,
		StringBuilder classSignatureBuilder) {
	classSignatureBuilder.append("\npublic ");
	if(isNestedType(description))
		classSignatureBuilder.append("static ");
	if (description.getEClass() == TypesPackage.Literals.JVM_GENERIC_TYPE) {
		if (description.getUserData(JvmTypesResourceDescriptionStrategy.IS_INTERFACE) != null) {
			classSignatureBuilder.append("interface ");
		} else {
			classSignatureBuilder.append("class ");
		}
	} else if (description.getEClass() == TypesPackage.Literals.JVM_ENUMERATION_TYPE) {
		classSignatureBuilder.append("enum ");
	} else if (description.getEClass() == TypesPackage.Literals.JVM_ANNOTATION_TYPE) {
		classSignatureBuilder.append("@interface ");
	}
	String lastSegment = description.getQualifiedName().getLastSegment();
	int trimIndex = lastSegment.lastIndexOf('$');
	String simpleName = lastSegment.substring(trimIndex + 1);
	classSignatureBuilder.append(simpleName);
	String typeParameters = description.getUserData(JvmTypesResourceDescriptionStrategy.TYPE_PARAMETERS);
	if (typeParameters != null) {
		classSignatureBuilder.append(typeParameters);
	}
	classSignatureBuilder.append("{");
	for(IEObjectDescription nested: owner2nested.get(description.getQualifiedName())) {
		appendType(nested, owner2nested, classSignatureBuilder);
	}
	classSignatureBuilder.append("\n}");
}
 
Example 5
Source File: N4JSResourceDescriptionStrategy.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
/** @return the version number of the given description. */
public static int getVersion(IEObjectDescription description) {
	try {
		String userData = description.getUserData(VERSION);
		if (userData == null) {
			return VERSION_DEFAULT;
		}
		return Integer.parseInt(userData);
	} catch (NumberFormatException e) {
		return VERSION_DEFAULT;
	}
}
 
Example 6
Source File: N4JSResourceDescriptionStrategy.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
/** @return the type access modifier of the given description. Default return is public. */
public static TypeAccessModifier getTypeAccessModifier(IEObjectDescription description) {
	try {
		String userData = description.getUserData(TYPE_ACCESS_MODIFIER_KEY);
		if (userData == null) {
			return TYPE_ACCESS_MODIFIER_DEFAULT;
		}
		return TypeAccessModifier.get(Integer.parseInt(userData));
	} catch (NumberFormatException e) {
		return TYPE_ACCESS_MODIFIER_DEFAULT;
	}
}
 
Example 7
Source File: N4JSResourceDescriptionStrategy.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
/** @return the export default modifier of the given description. Default return is public. */
public static boolean getExportDefault(IEObjectDescription description) {
	String userData = description.getUserData(EXPORT_DEFAULT_KEY);
	if (userData == null) {
		return EXPORT_DEFAULT_DEFAULT;
	}
	return Boolean.parseBoolean(userData);
}
 
Example 8
Source File: N4JSResourceDescriptionStrategy.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
/** @return the static polyfill modifier of the given description. */
public static boolean getStaticPolyfill(IEObjectDescription description) {
	String userData = description.getUserData(STATIC_POLYFILL_KEY);
	if (userData == null) {
		return STATIC_POLYFILL_DEFAULT;
	}
	return Boolean.parseBoolean(userData);
}
 
Example 9
Source File: AbstractScopingTest.java    From dsl-devkit with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Gets the fingerprint for a given resource description, returns <code>null</code> if resource description does not export any objects or if a non-existing
 * user data field was queried.
 *
 * @param description
 *          the description
 * @return the fingerprint or <code>null</code> if no fingerprint found
 */
protected String getFingerprint(final IResourceDescription description) {
  Iterable<IEObjectDescription> objects = description.getExportedObjects();
  if (!Iterables.isEmpty(objects)) {
    IEObjectDescription objectDescription = Iterables.get(objects, 0);
    return objectDescription.getUserData(IFingerprintComputer.RESOURCE_FINGERPRINT);
  }
  return null;
}
 
Example 10
Source File: DefaultResourceDescriptionDelta.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected boolean equals(IEObjectDescription oldObj, IEObjectDescription newObj) {
	if (oldObj == newObj)
		return true;
	if (oldObj.getEClass() != newObj.getEClass())
		return false;
	if (oldObj.getName() != null && !oldObj.getName().equals(newObj.getName()))
		return false;
	if (!oldObj.getEObjectURI().equals(newObj.getEObjectURI()))
		return false;
	String[] oldKeys = oldObj.getUserDataKeys();
	String[] newKeys = newObj.getUserDataKeys();
	if (oldKeys.length != newKeys.length)
		return false;
	for (String key : oldKeys) {
		if (!Arrays.contains(newKeys, key))
			return false;
		String oldValue = oldObj.getUserData(key);
		String newValue = newObj.getUserData(key);
		if (oldValue == null) {
			if (newValue != null)
				return false;
		} else if (!oldValue.equals(newValue)) {
			return false;
		}
	}
	return true;
}
 
Example 11
Source File: N4JSResourceDescriptionStrategy.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
/** @return the abstract modifier of the given description. */
public static boolean getAbstract(IEObjectDescription description) {
	String userData = description.getUserData(ABSTRACT_KEY);
	if (userData == null) {
		return ABSTRACT_DEFAULT;
	}
	return Boolean.parseBoolean(userData);
}
 
Example 12
Source File: N4JSResourceDescriptionStrategy.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
/** @return the final modifier of the given description. */
public static boolean getFinal(IEObjectDescription description) {
	String userData = description.getUserData(FINAL_KEY);
	if (userData == null) {
		return FINAL_DEFAULT;
	}
	return Boolean.parseBoolean(userData);
}
 
Example 13
Source File: N4JSResourceDescriptionStrategy.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
/** @return the whether the given description is the main module. */
public static boolean getMainModule(IEObjectDescription description) {
	String userData = description.getUserData(MAIN_MODULE_KEY);
	if (userData == null) {
		return MAIN_MODULE_DEFAULT;
	}
	return Boolean.parseBoolean(userData);
}
 
Example 14
Source File: ResourceDescriptionDelta.java    From dsl-devkit with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Checks if two IEObjectDescriptions are equal.
 * The implementation assumes the descriptions' fingerprints include all the necessary data, including name, EClass and relevant user data items.
 *
 * @param oldObj
 *          old object
 * @param newObj
 *          new object
 * @return true if both are equal
 */
protected boolean equals(final IEObjectDescription oldObj, final IEObjectDescription newObj) {
  if (oldObj != newObj) {
    String newFingerprint = newObj.getUserData(IFingerprintComputer.OBJECT_FINGERPRINT);
    if (newFingerprint != null && !newFingerprint.equals(oldObj.getUserData(IFingerprintComputer.OBJECT_FINGERPRINT))) {
      return false;
    }
    if (!oldObj.getEObjectURI().equals(newObj.getEObjectURI())) {
      return false;
    }
  }
  return true;
}
 
Example 15
Source File: UserDataMapper.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Reads the TModule stored in the given IEObjectDescription.
 */
public static TModule getDeserializedModuleFromDescription(IEObjectDescription eObjectDescription, URI uri) {
	final String serializedData = eObjectDescription.getUserData(USER_DATA_KEY_SERIALIZED_SCRIPT);
	if (Strings.isNullOrEmpty(serializedData)) {
		return null;
	}
	final XMIResource xres = new XMIResourceImpl(uri);
	try {
		final boolean binary = !serializedData.startsWith("<");
		final ByteArrayInputStream bais = new ByteArrayInputStream(
				binary ? Base64.getDecoder().decode(serializedData)
						: serializedData.getBytes(TRANSFORMATION_CHARSET_NAME));
		xres.load(bais, getOptions(uri, binary));
	} catch (Exception e) {
		LOGGER.warn("error deserializing module from IEObjectDescription: " + uri); //$NON-NLS-1$
		if (LOGGER.isTraceEnabled()) {
			LOGGER.trace("error deserializing module from IEObjectDescription=" + eObjectDescription
					+ ": " + uri, e); //$NON-NLS-1$
		}
		// fail safe, because not uncommon (serialized data might have been created with an old version of the N4JS
		// IDE, so the format could be out of date (after an update of the IDE))
		return null;
	}
	final List<EObject> contents = xres.getContents();
	if (contents.isEmpty() || !(contents.get(0) instanceof TModule)) {
		return null;
	}
	final TModule module = (TModule) contents.get(0);
	xres.getContents().clear();

	final String astMD5 = eObjectDescription.getUserData(USER_DATA_KEY_AST_MD5);
	module.setAstMD5(astMD5);

	return module;
}
 
Example 16
Source File: N4JSDirtyStateEditorSupport.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
private boolean equalDescriptions(IEObjectDescription newDescription,
		IEObjectDescription oldDescription, URI uri) {
	if (!newDescription.getQualifiedName().equals(oldDescription.getQualifiedName())) {
		return false;
	}
	if (!newDescription.getEClass().equals(oldDescription.getEClass())) {
		return false;
	}
	if (!newDescription.getEObjectURI().equals(oldDescription.getEObjectURI())) {
		return false;
	}
	if (TypesPackage.Literals.TMODULE == newDescription.getEClass()) {
		String newModule = newDescription.getUserData(UserDataMapper.USER_DATA_KEY_SERIALIZED_SCRIPT);
		String oldModule = oldDescription.getUserData(UserDataMapper.USER_DATA_KEY_SERIALIZED_SCRIPT);
		if (newModule == null || oldModule == null) {
			return true;
		}
		if (!newModule.equals(oldModule)) {
			TModule newModuleObj = UserDataMapper.getDeserializedModuleFromDescription(newDescription, uri);
			TModule oldModuleObj = UserDataMapper.getDeserializedModuleFromDescription(oldDescription, uri);
			// we deserialize the TModules and ignore the MD5 Hash
			newModuleObj.setAstMD5("");
			oldModuleObj.setAstMD5("");
			if (!EcoreUtilN4.equalsNonResolving(newModuleObj, oldModuleObj)) {
				return false;
			}
		}
	}
	// todo compare user data if module
	return true;
}
 
Example 17
Source File: ResourceDescriptionDelta.java    From dsl-devkit with Eclipse Public License 1.0 4 votes vote down vote up
/** {@inheritDoc} */
@SuppressWarnings({"PMD.CompareObjectsWithEquals", "PMD.NPathComplexity"})
protected boolean internalHasChanges() {
  if (getNew() == null || oldDesc == null) {
    return true; // Deleted or new resource
  }

  final Iterator<IEObjectDescription> oldObjectsIter = oldDesc.getExportedObjects().iterator();
  final Iterator<IEObjectDescription> newObjectsIter = getNew().getExportedObjects().iterator();

  final boolean empty = !oldObjectsIter.hasNext();
  if (empty != !newObjectsIter.hasNext()) {
    return true; // One is empty, but the other one is not
  } else if (empty) {
    return false; // Both are empty
  }

  final IEObjectDescription oldFirst = oldObjectsIter.next();
  final IEObjectDescription newFirst = newObjectsIter.next();

  objectFingerprints = newFirst.getUserData(IFingerprintComputer.OBJECT_FINGERPRINT) != null;

  final String oldHash = oldFirst.getUserData(IFingerprintComputer.RESOURCE_FINGERPRINT);
  final String newHash = newFirst.getUserData(IFingerprintComputer.RESOURCE_FINGERPRINT);
  if (oldHash != null && newHash != null) {
    // Both have fingerprints: it suffices to compare those!
    return !oldHash.equals(newHash);
  }

  // At least one has no fingerprint. If both have none, try the default method, otherwise, there is a change.
  if (oldHash != null || newHash != null) {
    if (LOGGER.isDebugEnabled()) {
      LOGGER.debug(getNew().getURI().toString() + ": fingerprint has changed from: " + oldHash + " to " + newHash); //$NON-NLS-1$ //$NON-NLS-2$
    }
    return true;
  }

  if (!equals(oldFirst, newFirst)) {
    return true;
  }

  while (oldObjectsIter.hasNext()) {
    if (!newObjectsIter.hasNext() || !equals(oldObjectsIter.next(), newObjectsIter.next())) {
      return true;
    }
  }

  return newObjectsIter.hasNext();
}
 
Example 18
Source File: DescriptionFlags.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
protected int getFlags(IEObjectDescription eObjectDescription) {
	String value = eObjectDescription.getUserData(KEY);
	return value == null ? 0 : Integer.parseInt(value);
}
 
Example 19
Source File: EObjectDescriptionBasedStubGenerator.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
protected boolean isNestedType(IEObjectDescription description) {
	return description.getUserData(IS_NESTED_TYPE) != null;
}
 
Example 20
Source File: UserDataMapper.java    From n4js with Eclipse Public License 1.0 2 votes vote down vote up
/**
 * Checks whether the given {@link IEObjectDescription} instance contains a serialized types module in its user
 * data.
 * <p>
 * Note that this method does not check whether the serialized types module is an empty string, or whether the
 * deserialized module is empty.
 * </p>
 *
 * @param eObjectDescription
 *            the object to check
 * @return <code>true</code> if the given object description contains a serialized types module and
 *         <code>false</code> otherwise
 */
public static boolean hasSerializedModule(IEObjectDescription eObjectDescription) {
	return eObjectDescription.getUserData(USER_DATA_KEY_SERIALIZED_SCRIPT) != null;
}