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

The following are Jave code examples for showing how to use create() 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: N4JSResource.java   View Source Code Vote up 5 votes
/**
 * Discard the AST and proxify all referenced nodes. Does nothing if the AST is already unloaded.
 */
private void discardAST() {
	EObject script = getScript();
	if (script != null && !script.eIsProxy()) {

		// Create a proxy for the AST.
		InternalEObject scriptProxy = (InternalEObject) EcoreUtil.create(script.eClass());
		scriptProxy.eSetProxyURI(EcoreUtil.getURI(script));

		TModule module = null;
		ModuleAwareContentsList theContents = (ModuleAwareContentsList) contents;
		if (isFullyInitialized()) {
			module = getModule();
			if (module != null && !module.eIsProxy()) {
				proxifyASTReferences(module);
				module.setAstElement(scriptProxy);
			}
		}

		// Unload the AST.
		unloadElements(theContents.subList(0, 1));

		theContents.sneakyClear();

		if (module != null) {
			theContents.sneakyAdd(scriptProxy);
			theContents.sneakyAdd(module);
		} else {
			// there was no module (not even a proxy)
			// -> don't add the script proxy
			// (i.e. transition from resource load state "Loaded" to "Created", not to "Loaded from Description")
		}

		// Clear AST meta cache and Xtext cache
		this.setASTMetaInfoCache(null);
		getCache().clear(this);
	}
}
 
Example 2
Project: n4js   File: N4JSResource.java   View Source Code Vote up 5 votes
private void proxifyASTReferences(EObject object) {
	if (object instanceof SyntaxRelatedTElement) {
		SyntaxRelatedTElement element = (SyntaxRelatedTElement) object;
		EObject astElement = element.getAstElement();
		if (astElement != null && !astElement.eIsProxy()) {
			InternalEObject proxy = (InternalEObject) EcoreUtil.create(astElement.eClass());
			proxy.eSetProxyURI(EcoreUtil.getURI(astElement));
			element.setAstElement(proxy);
		}
	}

	for (EObject child : object.eContents()) {
		proxifyASTReferences(child);
	}
}
 
Example 3
Project: neoscada   File: WorldGenerator.java   View Source Code Vote up 5 votes
private Endpoint createExporter ( final EClass exporterClass, final Node node, final EquinoxApplication application, final int port )
{
    final Exporter exporter = (Exporter)EcoreUtil.create ( exporterClass );

    final Endpoint ep = Endpoints.registerEndpoint ( node, port, Endpoints.reference ( exporter ), String.format ( "Exporter Endpoint: %s - %s", exporter.getTypeTag (), exporter.getName () ) );
    node.getEndpoints ().add ( ep );

    exporter.setName ( application.getName () + "/exporter" );
    exporter.getEndpoints ().add ( ep );
    application.getExporter ().add ( exporter );
    return ep;
}
 
Example 4
Project: rest-modeling-framework   File: Scope.java   View Source Code Vote up 5 votes
private EObject getEObjectByName(final Resource resource, final String name) {
    final EClass type = (EClass) feature.getEType();
    final String uriFragment = getUriFragment(name);

    final EObject resolvedType;
    final String[] segments = name.split("\\.");
    if (segments.length == 1) {
        final EObject eObject = getEObject(uriFragment);
        if (eObject != null) {
            resolvedType = eObject;
        } else {
            final InternalEObject internalEObject = (InternalEObject) EcoreUtil.create(type);
            internalEObject.eSetProxyURI(resource.getURI().appendFragment(uriFragment));
            resolvedType = internalEObject;
        }
    } else if (segments.length == 2) {
        final String libraryName = segments[0];
        final Library usedLibrary = getUsedLibrary(libraryName);
        if (usedLibrary == null) {
            addError("Library use {0} doesn't exist in {1}", libraryName, resource.getURI());
            resolvedType = null;
        } else {
            final String resolvedId = segments[1];
            final Scope usedLibraryScope = with(usedLibrary.eResource());
            resolvedType = usedLibraryScope.getEObjectByName(resolvedId);
        }
    } else {
        addError("Uses has invalid format {0}", name);
        resolvedType = null;
    }
    return resolvedType;
}
 
Example 5
Project: rest-modeling-framework   File: TypeDeclarationResolver.java   View Source Code Vote up 5 votes
private EObject constructType(final ParserRuleContext context, final EObject superType) {
    final EObject declaredType;
    final Token nameToken = context.getStart();
    final Optional<BuiltinType> optionalBuiltinType = BuiltinType.of(nameToken.getText());
    if (optionalBuiltinType.isPresent() || superType == null || !superType.eIsProxy()) {
        final EClass eClass = optionalBuiltinType
                .map(builtinType -> builtinType.getScopedMetaType(scope))
                .orElseGet(() -> superType == null ? BuiltinType.STRING.getScopedMetaType(scope) : superType.eClass());
        declaredType = create(eClass, context);
        final Scope typeScope = scope.with(declaredType);

        final String name = nameToken.getText();
        typeScope.with(declaredType.eClass().getEStructuralFeature("name"))
                .setValue(name, nameToken);
        if (!optionalBuiltinType.isPresent()) {
            typeScope.with(declaredType.eClass().getEStructuralFeature("type"))
                    .setValue(superType, nameToken);
        }
    } else {
        final InternalEObject proxy = (InternalEObject) EcoreUtil.create(superType.eClass());
        final String uriFragment = scope.getUriFragment(nameToken.getText());
        proxy.eSetProxyURI(scope.getResource().getURI().appendFragment(uriFragment));
        declaredType = proxy;
    }
    scope.setValue(declaredType, nameToken);
    return declaredType;
}
 
Example 6
Project: rest-modeling-framework   File: TypeExpressionConstructor.java   View Source Code Vote up 5 votes
@Override
public EObject visitArrayType(final TypeExpressionParser.ArrayTypeContext ctx) {
    final EObject arrayType = EcoreUtil.create(arrayTypeDeclarationType);
    scope.addValue(INLINE_TYPE_CONTAINER__INLINE_TYPES, arrayType);
    final EObject itemsType = visit(ctx.primary_type_expr());
    arrayType.eSet(itemsFeature, itemsType);

    return arrayType;
}
 
Example 7
Project: rest-modeling-framework   File: TypeExpressionConstructor.java   View Source Code Vote up 5 votes
@Override
public EObject visitUnionType(final TypeExpressionParser.UnionTypeContext ctx) {
    final UnionType unionType = (UnionType) EcoreUtil.create(UNION_TYPE);
    scope.addValue(INLINE_TYPE_CONTAINER__INLINE_TYPES, unionType);
    final EList<AnyType> oneOfType = ECollections.asEList(ctx.primary_type_expr().stream()
            .map(this::visit)
            .filter(AnyType.class::isInstance) // TODO report errors
            .map(AnyType.class::cast)
            .collect(Collectors.toList()));
    unionType.getOneOf().addAll(oneOfType);

    return unionType;
}
 
Example 8
Project: Tarski   File: TraceManager.java   View Source Code Vote up 5 votes
public EObject createEObject(String sigTypeName, String atomName, IMarker source)
    throws TraceException {
  SigTrace trace = getSigTraceByType(sigTypeName);
  EClass eClass = trace.getEClass();
  EObject eObject = EcoreUtil.create(eClass);
  EcoreUtilities.eSetAttributeByName(eObject, "name", atomName);
  EcoreUtilities.putIntoContainer(
      source == null ? trace.getLoad().getInstanceRoot() : findEObject(source), eObject);
  EcoreUtilities.saveResource(trace.getLoad().getInstanceRoot());
  return eObject;
}
 
Example 9
Project: OCCI-Studio   File: OCCILinker.java   View Source Code Vote up 5 votes
@Override
protected EObject createProxy(EObject obj, INode node, EReference eRef) {
	if (eRef.equals(OCCIPackage.eINSTANCE.getExtension_Import())
			|| eRef.equals(OCCIPackage.eINSTANCE.getConfiguration_Use())) {
		EClass referenceType = getProxyType(obj, eRef);
		final EObject proxy = EcoreUtil.create(referenceType);
		((InternalEObject) proxy)
				.eSetProxyURI(URI.createURI(node.getText().replaceAll("'", "").replaceAll("\"", "")));
		return proxy;
	}
	return super.createProxy(obj, node, eRef);
}
 
Example 10
Project: n4js   File: N4JSLinker.java   View Source Code Vote up 4 votes
/**
 * Creates the proxy with a custom encoded URI format (starting with "|"). The object used to produce the encoded
 * URI are collected as tuple inside {@link N4JSResource}. Then the node text is checked if it is convertible to a
 * valid value. If there is a {@link BadEscapementException} is thrown then there is either a warning or an error
 * produced via the diagnosticProducer.
 *
 *
 * @param resource
 *            the N4JSResource
 * @param obj
 *            the EObject containing the cross reference
 * @param node
 *            the node representing the EObject
 * @param eRef
 *            the cross reference in the domain model
 * @param xref
 *            the cross reference in the node model
 * @param diagnosticProducer
 *            to produce errors or warnings
 * @return the created proxy
 */
private EObject createProxy(N4JSResource resource, EObject obj, INode node, EReference eRef, CrossReference xref,
		IDiagnosticProducer diagnosticProducer) {
	final URI uri = resource.getURI();
	/*
	 * as otherwise with 0 the EObjectDescription created for Script would be fetched
	 */
	final int fragmentNumber = resource.addLazyProxyInformation(obj, eRef, node);
	final URI encodedLink = uri.appendFragment("|" + fragmentNumber);
	EClass referenceType = findInstantiableCompatible(eRef.getEReferenceType());
	final EObject proxy = EcoreUtil.create(referenceType);
	((InternalEObject) proxy).eSetProxyURI(encodedLink);
	AbstractElement terminal = xref.getTerminal();
	if (!(terminal instanceof RuleCall)) {
		throw new IllegalArgumentException(String.valueOf(xref));
	}
	AbstractRule rule = ((RuleCall) terminal).getRule();
	try {
		String tokenText = NodeModelUtils.getTokenText(node);
		Object value = valueConverterService.toValue(tokenText, rule.getName(), node);
		if (obj instanceof IdentifierRef && value instanceof String) {
			((IdentifierRef) obj).setIdAsText((String) value);
		} else if (obj instanceof LabelRef && value instanceof String) {
			((LabelRef) obj).setLabelAsText((String) value);
		} else if (obj instanceof ParameterizedPropertyAccessExpression && value instanceof String) {
			((ParameterizedPropertyAccessExpression) obj).setPropertyAsText((String) value);
		} else if (obj instanceof NamedImportSpecifier && value instanceof String) {
			((NamedImportSpecifier) obj).setImportedElementAsText((String) value);
		} else if ((obj instanceof JSXPropertyAttribute) && (value instanceof String)) {
			((JSXPropertyAttribute) obj).setPropertyAsText((String) value);
		} else {
			setOtherElementAsText(tokenText, obj, value);
		}
	} catch (BadEscapementException e) {
		diagnosticProducer.addDiagnostic(new DiagnosticMessage(e.getMessage(), e.getSeverity(), e.getIssueCode(),
				Strings.EMPTY_ARRAY));
	} catch (N4JSValueConverterException vce) {
		diagnosticProducer.addDiagnostic(new DiagnosticMessage(vce.getMessage(), vce.getSeverity(),
				vce.getIssueCode(), Strings.EMPTY_ARRAY));
	} catch (N4JSValueConverterWithValueException vcwve) {
		diagnosticProducer.addDiagnostic(new DiagnosticMessage(vcwve.getMessage(), vcwve.getSeverity(),
				vcwve.getIssueCode(), Strings.EMPTY_ARRAY));
	}
	return proxy;
}
 
Example 11
Project: rest-modeling-framework   File: AbstractScopedVisitor.java   View Source Code Vote up 4 votes
protected <T extends EObject> T create(final EClass eClass, final ParserRuleContext ruleContext) {
    final T newEObject = (T) EcoreUtil.create(eClass);
    final RAMLTokenProviderAdapter adapter = RAMLTokenProviderAdapter.of((RamlToken) ruleContext.getStart());
    newEObject.eAdapters().add(adapter);
    return newEObject;
}
 
Example 12
Project: Saturn   File: SaturnDocLoadSaveUtil.java   View Source Code Vote up 4 votes
private final static List<BasicEMap.Entry<String, String>> getXmlNsList()
{
  List<EPackage> packages = new ArrayList<EPackage>();

  // resource packages
  packages.add(Db2ResourcePackage.eINSTANCE);
  packages.add(DirectoryResourcePackage.eINSTANCE);
  packages.add(FileResourcePackage.eINSTANCE);
  packages.add(InformixResourcePackage.eINSTANCE);
  packages.add(MqResourcePackage.eINSTANCE);
  packages.add(OracleResourcePackage.eINSTANCE);
  packages.add(QueryResourcePackage.eINSTANCE);
  packages.add(ReferenceResourcePackage.eINSTANCE);
  packages.add(ShadowDirectResourcePackage.eINSTANCE);
  packages.add(SoapIOResourcePackage.eINSTANCE);
  packages.add(SybaseResourcePackage.eINSTANCE);
  packages.add(TeradataResourcePackage.eINSTANCE);
  packages.add(UdbResourcePackage.eINSTANCE);
  packages.add(NullResourcePackage.eINSTANCE);
  packages.add(TransactionResourcePackage.eINSTANCE);
  packages.add(SemaphoreResourcePackage.eINSTANCE);
  packages.add(LdapResourcePackage.eINSTANCE);
  packages.add(LdapQueryResourcePackage.eINSTANCE);
  packages.add(SqliteResourcePackage.eINSTANCE);
  packages.add(MssqlResourcePackage.eINSTANCE);

  // global packages
  packages.add(ValuesPackage.eINSTANCE);
  packages.add(TypesPackage.eINSTANCE);
  packages.add(ListenersPackage.eINSTANCE);
  packages.add(RepetitionPackage.eINSTANCE);
  packages.add(SaturnWebPackage.eINSTANCE);
  packages.add(SaturnReportPackage.eINSTANCE);

  // module packages
  packages.add(CpsModulePackage.eINSTANCE);
  packages.add(DatacompareModulePackage.eINSTANCE);
  packages.add(DsConvertModulePackage.eINSTANCE);
  packages.add(DsValidatorModulePackage.eINSTANCE);
  packages.add(EnvValidatorModulePackage.eINSTANCE);
  packages.add(ExtractDDLModulePackage.eINSTANCE);
  packages.add(MailModulePackage.eINSTANCE);
  packages.add(MqModulePackage.eINSTANCE);
  packages.add(ProcedureCallModulePackage.eINSTANCE);
  packages.add(PsManagerModulePackage.eINSTANCE);
  packages.add(RuncmdsModulePackage.eINSTANCE);
  packages.add(SingularityModulePackage.eINSTANCE);
  packages.add(SoapIOModulePackage.eINSTANCE);
  packages.add(SqlModulePackage.eINSTANCE);
  packages.add(Xml2csvModulePackage.eINSTANCE);
  packages.add(XmlDiffModulePackage.eINSTANCE);
  packages.add(XmlGenModulePackage.eINSTANCE);
  packages.add(XmlManipModulePackage.eINSTANCE);
  packages.add(XmlValidatorModulePackage.eINSTANCE);
  packages.add(CometModulePackage.eINSTANCE);

  packages.add(XSplitModulePackage.eINSTANCE);
  packages.add(ReferenceModulePackage.eINSTANCE);
  packages.add(DocumentumModulePackage.eINSTANCE);

  List<BasicEMap.Entry<String, String>> xmlNsList = new ArrayList<BasicEMap.Entry<String, String>>();
  for (EPackage ePackage : packages)
  {
    ((EcoreFactoryImpl) EcoreFactory.eINSTANCE).createEStringToStringMapEntry();
    BasicEMap.Entry<String, String> newEntry = (BasicEMap.Entry<String, String>) EcoreUtil
        .create((EClass) SaturnPackage.eINSTANCE.getDocumentRoot_XMLNSPrefixMap().getEType());
    // new EStringToStringMapEntryImpl();
    // //((EcoreFactoryImpl)EcoreFactory.eINSTANCE).createEStringToStringMapEntry();
    xmlNsList.add(newEntry);

    newEntry.setKey(ePackage.getNsPrefix());
    newEntry.setValue(ePackage.getNsURI());
  }
  return xmlNsList;
}