Search in sources :

Example 1 with N4JSValueConverterException

use of org.eclipse.n4js.conversion.N4JSValueConverterException in project n4js by eclipse.

the class PropertyNameAwareElementFactory method set.

/**
 * {@inheritDoc}
 *
 * <p>
 * If we parsed a {@link PropertyAssignment}, the set operation also initializes the {@link PropertyNameKind}.
 * </p>
 */
@Override
public void set(EObject object, String feature, Object value, String ruleName, INode node) throws N4JSValueConverterException {
    final EStructuralFeature structuralFeature = object.eClass().getEStructuralFeature(feature);
    if (structuralFeature == null)
        throw new IllegalArgumentException(object.eClass().getName() + "." + feature + " does not exist");
    try {
        final Object tokenValue = getTokenValue(value, ruleName, node);
        checkNullForPrimitiveFeatures(structuralFeature, tokenValue, node);
        object.eSet(structuralFeature, tokenValue);
        // this call is an extra to the super class method
        setPropertyNameKind(object, feature, ruleName);
        setRawStringValue(object, feature, value);
    } catch (ValueConverterWithValueException e) {
        final Object tokenValue = e.getValue();
        checkNullForPrimitiveFeatures(structuralFeature, tokenValue, node);
        object.eSet(structuralFeature, tokenValue);
        // this call is an extra to the super class method
        setPropertyNameKind(object, feature, ruleName);
        setRawStringValue(object, feature, value);
        throw e;
    } catch (ValueConverterException e) {
        throw e;
    } catch (NullPointerException e) {
        throw new N4JSValueConverterException(IssueCodes.getMessageForVCO_NPE(), IssueCodes.VCO_NPE, node, e);
    } catch (Exception e) {
        throw new ValueConverterException(null, node, e);
    }
}
Also used : ValueConverterWithValueException(org.eclipse.xtext.conversion.ValueConverterWithValueException) EStructuralFeature(org.eclipse.emf.ecore.EStructuralFeature) EObject(org.eclipse.emf.ecore.EObject) N4JSValueConverterException(org.eclipse.n4js.conversion.N4JSValueConverterException) ValueConverterException(org.eclipse.xtext.conversion.ValueConverterException) N4JSValueConverterException(org.eclipse.n4js.conversion.N4JSValueConverterException) N4JSValueConverterException(org.eclipse.n4js.conversion.N4JSValueConverterException) ValueConverterWithValueException(org.eclipse.xtext.conversion.ValueConverterWithValueException) ValueConverterException(org.eclipse.xtext.conversion.ValueConverterException)

Example 2 with N4JSValueConverterException

use of org.eclipse.n4js.conversion.N4JSValueConverterException in project n4js by eclipse.

the class N4JSLinker method createProxy.

/**
 * 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;
}
Also used : ParameterizedPropertyAccessExpression(org.eclipse.n4js.n4JS.ParameterizedPropertyAccessExpression) NamedImportSpecifier(org.eclipse.n4js.n4JS.NamedImportSpecifier) AbstractElement(org.eclipse.xtext.AbstractElement) JSXPropertyAttribute(org.eclipse.n4js.n4JS.JSXPropertyAttribute) URI(org.eclipse.emf.common.util.URI) LabelRef(org.eclipse.n4js.n4JS.LabelRef) N4JSValueConverterWithValueException(org.eclipse.n4js.conversion.N4JSValueConverterWithValueException) RuleCall(org.eclipse.xtext.RuleCall) EClass(org.eclipse.emf.ecore.EClass) DiagnosticMessage(org.eclipse.xtext.diagnostics.DiagnosticMessage) BadEscapementException(org.eclipse.n4js.conversion.AbstractN4JSStringValueConverter.BadEscapementException) EObject(org.eclipse.emf.ecore.EObject) InternalEObject(org.eclipse.emf.ecore.InternalEObject) EObject(org.eclipse.emf.ecore.EObject) InternalEObject(org.eclipse.emf.ecore.InternalEObject) N4JSValueConverterException(org.eclipse.n4js.conversion.N4JSValueConverterException) AbstractRule(org.eclipse.xtext.AbstractRule) InternalEObject(org.eclipse.emf.ecore.InternalEObject) IdentifierRef(org.eclipse.n4js.n4JS.IdentifierRef)

Aggregations

EObject (org.eclipse.emf.ecore.EObject)2 N4JSValueConverterException (org.eclipse.n4js.conversion.N4JSValueConverterException)2 URI (org.eclipse.emf.common.util.URI)1 EClass (org.eclipse.emf.ecore.EClass)1 EStructuralFeature (org.eclipse.emf.ecore.EStructuralFeature)1 InternalEObject (org.eclipse.emf.ecore.InternalEObject)1 BadEscapementException (org.eclipse.n4js.conversion.AbstractN4JSStringValueConverter.BadEscapementException)1 N4JSValueConverterWithValueException (org.eclipse.n4js.conversion.N4JSValueConverterWithValueException)1 IdentifierRef (org.eclipse.n4js.n4JS.IdentifierRef)1 JSXPropertyAttribute (org.eclipse.n4js.n4JS.JSXPropertyAttribute)1 LabelRef (org.eclipse.n4js.n4JS.LabelRef)1 NamedImportSpecifier (org.eclipse.n4js.n4JS.NamedImportSpecifier)1 ParameterizedPropertyAccessExpression (org.eclipse.n4js.n4JS.ParameterizedPropertyAccessExpression)1 AbstractElement (org.eclipse.xtext.AbstractElement)1 AbstractRule (org.eclipse.xtext.AbstractRule)1 RuleCall (org.eclipse.xtext.RuleCall)1 ValueConverterException (org.eclipse.xtext.conversion.ValueConverterException)1 ValueConverterWithValueException (org.eclipse.xtext.conversion.ValueConverterWithValueException)1 DiagnosticMessage (org.eclipse.xtext.diagnostics.DiagnosticMessage)1