Search in sources :

Example 26 with ValueConverterException

use of org.eclipse.xtext.conversion.ValueConverterException in project xtext-core by eclipse.

the class EFactoryValueConverterTest method testEmptyString.

@Test
public void testEmptyString() {
    EFactoryValueConverter eIntConverter = new EFactoryValueConverter(EcorePackage.Literals.EINT);
    try {
        eIntConverter.toValue("", null);
        fail("EInt converter should not convert empty string");
    } catch (ValueConverterException e) {
    // expected result
    }
    try {
        EFactoryValueConverter eIntegerConverter = new EFactoryValueConverter(EcorePackage.Literals.EINTEGER_OBJECT);
        eIntegerConverter.toValue("", null);
        fail("EInteger converter should not convert empty string");
    } catch (ValueConverterException e) {
        // expected result
        assertTrue(e.getCause() instanceof NumberFormatException);
    }
}
Also used : EFactoryValueConverter(org.eclipse.xtext.conversion.impl.EFactoryValueConverter) ValueConverterException(org.eclipse.xtext.conversion.ValueConverterException) Test(org.junit.Test)

Example 27 with ValueConverterException

use of org.eclipse.xtext.conversion.ValueConverterException in project xtext-core by eclipse.

the class LinkingHelper method getCrossRefNodeAsString.

public String getCrossRefNodeAsString(INode node, boolean convert) {
    String convertMe = NodeModelUtils.getTokenText(node);
    if (!convert)
        return convertMe;
    try {
        String ruleName = getRuleNameFrom(node.getGrammarElement());
        if (ruleName == null)
            return convertMe;
        Object result = valueConverter.toValue(convertMe, ruleName, node);
        return result != null ? result.toString() : null;
    } catch (ValueConverterWithValueException ex) {
        Object result = ex.getValue();
        return result != null ? result.toString() : null;
    } catch (ValueConverterException ex) {
        throw new IllegalNodeException(node, ex);
    }
}
Also used : ValueConverterWithValueException(org.eclipse.xtext.conversion.ValueConverterWithValueException) EObject(org.eclipse.emf.ecore.EObject) ValueConverterException(org.eclipse.xtext.conversion.ValueConverterException)

Example 28 with ValueConverterException

use of org.eclipse.xtext.conversion.ValueConverterException in project xtext-core by eclipse.

the class CrossReferenceSerializer method getCrossReferenceNameFromScope.

protected String getCrossReferenceNameFromScope(EObject semanticObject, CrossReference crossref, EObject target, final IScope scope, Acceptor errors) {
    String ruleName = linkingHelper.getRuleNameFrom(crossref);
    boolean foundOne = false;
    List<ISerializationDiagnostic> recordedErrors = null;
    for (IEObjectDescription desc : scope.getElements(target)) {
        foundOne = true;
        String unconverted = qualifiedNameConverter.toString(desc.getName());
        try {
            return valueConverter.toString(unconverted, ruleName);
        } catch (ValueConverterException e) {
            if (errors != null) {
                if (recordedErrors == null)
                    recordedErrors = Lists.newArrayList();
                recordedErrors.add(diagnostics.getValueConversionExceptionDiagnostic(semanticObject, crossref, unconverted, e));
            }
        }
    }
    if (errors != null) {
        if (recordedErrors != null)
            for (ISerializationDiagnostic diag : recordedErrors) errors.accept(diag);
        if (!foundOne)
            errors.accept(diagnostics.getNoEObjectDescriptionFoundDiagnostic(semanticObject, crossref, target, scope));
    }
    return null;
}
Also used : ISerializationDiagnostic(org.eclipse.xtext.serializer.diagnostic.ISerializationDiagnostic) ValueConverterException(org.eclipse.xtext.conversion.ValueConverterException) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription)

Example 29 with ValueConverterException

use of org.eclipse.xtext.conversion.ValueConverterException in project xtext-eclipse by eclipse.

the class JdtTypesProposalProvider method createTypeProposal.

protected void createTypeProposal(final String typeName, int modifiers, boolean isInnerType, ICompletionProposalFactory proposalFactory, ContentAssistContext context, ICompletionProposalAcceptor acceptor, final IJvmTypeProvider jvmTypeProvider, IValueConverter<String> valueConverter) {
    if (acceptor.canAcceptMoreProposals()) {
        int lastDot = typeName.lastIndexOf('.');
        final StyledString displayString;
        if (lastDot != -1) {
            if (isInnerType) {
                displayString = new StyledString(typeName.substring(lastDot + 1).replace('$', '.')).append(" - " + typeName.substring(0, lastDot), StyledString.QUALIFIER_STYLER);
            } else {
                displayString = new StyledString(typeName.substring(lastDot + 1)).append(" - " + typeName.substring(0, lastDot), StyledString.QUALIFIER_STYLER);
            }
        } else {
            displayString = new StyledString(isInnerType ? typeName.replace('$', '.') : typeName);
        }
        Image img = computeImage(typeName, isInnerType, modifiers);
        String proposalAsString = typeName;
        if (valueConverter != null) {
            try {
                proposalAsString = valueConverter.toString(isInnerType ? proposalAsString.replace('$', '.') : proposalAsString);
            } catch (ValueConverterException vce) {
                return;
            }
        }
        ICompletionProposal proposal = proposalFactory.createCompletionProposal(proposalAsString, displayString, img, context);
        if (proposal instanceof ConfigurableCompletionProposal) {
            ConfigurableCompletionProposal theProposal = (ConfigurableCompletionProposal) proposal;
            // calculate the type lazy, as this require a lot of time for large completion lists
            theProposal.setAdditionalProposalInfo(new Provider<EObject>() {

                @Override
                public EObject get() {
                    return jvmTypeProvider.findTypeByName(typeName);
                }
            });
            theProposal.setHover(hover);
            priorities.adjustCrossReferencePriority(theProposal, context.getPrefix());
            theProposal.setPriority(theProposal.getPriority() + jdtTypeRelevance.getRelevance(typeName, context.getPrefix()));
        }
        acceptor.accept(proposal);
    }
}
Also used : ConfigurableCompletionProposal(org.eclipse.xtext.ui.editor.contentassist.ConfigurableCompletionProposal) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) EObject(org.eclipse.emf.ecore.EObject) StyledString(org.eclipse.jface.viewers.StyledString) StyledString(org.eclipse.jface.viewers.StyledString) ValueConverterException(org.eclipse.xtext.conversion.ValueConverterException) Image(org.eclipse.swt.graphics.Image)

Example 30 with ValueConverterException

use of org.eclipse.xtext.conversion.ValueConverterException 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)

Aggregations

ValueConverterException (org.eclipse.xtext.conversion.ValueConverterException)54 EObject (org.eclipse.emf.ecore.EObject)37 AntlrDatatypeRuleToken (org.eclipse.xtext.parser.antlr.AntlrDatatypeRuleToken)13 ValueConverterWithValueException (org.eclipse.xtext.conversion.ValueConverterWithValueException)4 Test (org.junit.Test)4 URI (org.eclipse.emf.common.util.URI)3 EStructuralFeature (org.eclipse.emf.ecore.EStructuralFeature)3 Resource (org.eclipse.emf.ecore.resource.Resource)3 ClasspathUriResolutionException (org.eclipse.xtext.resource.ClasspathUriResolutionException)3 IEObjectDescription (org.eclipse.xtext.resource.IEObjectDescription)3 XtextResource (org.eclipse.xtext.resource.XtextResource)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 FailedPredicateException (org.antlr.runtime.FailedPredicateException)2 MismatchedTokenException (org.antlr.runtime.MismatchedTokenException)2 MissingTokenException (org.antlr.runtime.MissingTokenException)2 RecognitionException (org.antlr.runtime.RecognitionException)2 UnwantedTokenException (org.antlr.runtime.UnwantedTokenException)2 WrappedException (org.eclipse.emf.common.util.WrappedException)2 EClass (org.eclipse.emf.ecore.EClass)2 EReference (org.eclipse.emf.ecore.EReference)2