private static ISerializer getSerializer(final String delimiter) { ISerializer result = xtextSerializerByLineDelimiter.get(delimiter); if (result != null) { return result; } final ILineSeparatorInformation lineSeparatorInformation = new ILineSeparatorInformation() { @Override public String getLineSeparator() { return delimiter; } }; Injector injector = Guice.createInjector(new LineSeparatorModule(lineSeparatorInformation)); result = injector.getInstance(ISerializer.class); xtextSerializerByLineDelimiter.put(delimiter, result); return result; }
@Test public void testSerialize_01() throws Exception { Resource resource = newResource("'foo' as String"); XCastedExpression casted = (XCastedExpression) resource.getContents().get(0); XbaseFactory factory = XbaseFactory.eINSTANCE; XClosure closure = factory.createXClosure(); XStringLiteral stringLiteral = factory.createXStringLiteral(); stringLiteral.setValue("value"); XBlockExpression blockExpression = factory.createXBlockExpression(); blockExpression.getExpressions().add(stringLiteral); closure.setExpression(blockExpression); closure.setExplicitSyntax(true); XInstanceOfExpression instanceOfExpression = factory.createXInstanceOfExpression(); instanceOfExpression.setExpression(closure); instanceOfExpression.setType(EcoreUtil.copy(casted.getType())); resource.getContents().clear(); resource.getContents().add(instanceOfExpression); ISerializer serializer = get(ISerializer.class); String string = serializer.serialize(instanceOfExpression); assertEquals("[|\"value\"] instanceof String", string); XInstanceOfExpression parsedExpression = parseHelper.parse(string); assertTrue(EcoreUtil.equals(instanceOfExpression, parsedExpression)); }
/** * @noreference */ public static String grammarFragmentToString(final ISerializer serializer, final EObject object, final String prefix) { String s = null; try { final SaveOptions options = SaveOptions.newBuilder().format().getOptions(); s = serializer.serialize(object, options); } catch (final Throwable _t) { if (_t instanceof Exception) { final Exception e = (Exception)_t; s = e.toString(); } else { throw Exceptions.sneakyThrow(_t); } } String _replace = s.trim().replaceAll("(\\r?\\n)", ("$1" + prefix)).replace("\\u", "\\\\u"); String _plus = (prefix + _replace); s = _plus; return s; }
@Test public void testSerializeAndReparse() throws Exception { int intValue=34786482; double doubleValue=2.32E-17; Date dateValue = new SimpleDateFormat("YYYY-MM-DD").parse("2010-05-25"); Model model = EcoreTerminalsTestLanguageFactory.eINSTANCE.createModel(); model.getIntValues().add(intValue); model.getDoubleValues().add(doubleValue); model.getDateValues().add(dateValue); ISerializer serializer = get(ISerializer.class); String modelAsString = serializer.serialize(model); Model reparsedModel = (Model) getModel(modelAsString); assertEquals(intValue, (int) reparsedModel.getIntValues().get(0)); assertEquals(doubleValue, reparsedModel.getDoubleValues().get(0), 0.00001); assertEquals(dateValue, reparsedModel.getDateValues().get(0)); try { model.getDoubleValues().add(-doubleValue); modelAsString = serializer.serialize(model); fail("Serialization should fail"); } catch (Exception exc) { // normal case } }
@Override public Set<Binding> getGuiceBindingsRt(final Grammar grammar) { final BindFactory bf = new BindFactory(); bf.addTypeToType(ISemanticSequencer.class.getName(), this.names.getSemanticSequencer().getQualifiedName()); bf.addTypeToType(ISyntacticSequencer.class.getName(), this.names.getSyntacticSequencer().getQualifiedName()); bf.addTypeToType(ISerializer.class.getName(), Serializer.class.getName()); return bf.getBindings(); }
@Test public void testSerialize_ExtrasIssue164() throws Exception { DerivedStateAwareResource resource = (DerivedStateAwareResource) newResource("org.eclipse.xtext.xbase.tests.serializer.SerializerTest.Demo.demo"); XMemberFeatureCall call = (XMemberFeatureCall) resource.getContents().get(0); ISerializer serializer = get(ISerializer.class); call.eAdapters().clear(); String string = serializer.serialize(call); assertEquals("org.eclipse.xtext.xbase.tests.serializer.SerializerTest.Demo.demo", string); }
@Test public void testSerialize_ExtrasIssue164_02() throws Exception { DerivedStateAwareResource resource = (DerivedStateAwareResource) newResource("org.eclipse.xtext.xbase.tests.serializer.SerializerTest.Demo.getDemo2(1)"); XMemberFeatureCall call = (XMemberFeatureCall) resource.getContents().get(0); ISerializer serializer = get(ISerializer.class); call.eAdapters().clear(); String string = serializer.serialize(call); assertEquals("org.eclipse.xtext.xbase.tests.serializer.SerializerTest.Demo.getDemo2(1)", string); }
private ISerializer getSerializer() { final String delimiter = this.codeConfig.getLineDelimiter(); ISerializer result = this.xtextSerializerByLineDelimiter.get(delimiter); if ((result != null)) { return result; } final ILineSeparatorInformation _function = () -> { return delimiter; }; GrammarAccessExtensions.LineSeparatorModule _lineSeparatorModule = new GrammarAccessExtensions.LineSeparatorModule(_function); final Injector injector = Guice.createInjector(_lineSeparatorModule); result = injector.<ISerializer>getInstance(ISerializer.class); this.xtextSerializerByLineDelimiter.put(delimiter, result); return result; }
public Class<? extends ISerializer> bindISerializer() { return Serializer.class; }
/** * A custom serializer class that allows to access otherwise protected methods. */ @Override public Class<? extends ISerializer> bindISerializer() { return AccessibleSerializer.class; }