org.eclipse.xtext.generator.InMemoryFileSystemAccess Java Examples

The following examples show how to use org.eclipse.xtext.generator.InMemoryFileSystemAccess. 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: JvmModelGeneratorTest.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
public String generate(final Resource res, final JvmDeclaredType type) {
  String _xblockexpression = null;
  {
    res.eSetDeliver(false);
    EList<EObject> _contents = res.getContents();
    this.builder.<JvmDeclaredType>operator_add(_contents, type);
    res.eSetDeliver(true);
    final InMemoryFileSystemAccess fsa = new InMemoryFileSystemAccess();
    this.generator.doGenerate(res, fsa);
    Map<String, CharSequence> _textFiles = fsa.getTextFiles();
    String _replace = type.getIdentifier().replace(".", "/");
    String _plus = (IFileSystemAccess.DEFAULT_OUTPUT + _replace);
    String _plus_1 = (_plus + ".java");
    _xblockexpression = _textFiles.get(_plus_1).toString();
  }
  return _xblockexpression;
}
 
Example #2
Source File: GeneratorService.java    From xtext-web with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Generate artifacts for the given document. The result can be fetched with
 * {@link #getResult(XtextWebDocumentAccess)}.
 */
@Override
public GeneratedArtifacts compute(IXtextWebDocument doc, CancelIndicator cancelIndicator) {
	InMemoryFileSystemAccess fileSystemAccess = fileSystemAccessProvider.get();
	generator.generate(doc.getResource(), fileSystemAccess, () -> cancelIndicator);
	GeneratedArtifacts result = new GeneratedArtifacts();
	for (Entry<String, CharSequence> e : fileSystemAccess.getTextFiles().entrySet()) {
		String contentType = contentTypeProvider.getContentType(e.getKey());
		result.artifacts.add(new GeneratorResult(e.getKey(), contentType, e.getValue().toString()));
	}
	return result;
}
 
Example #3
Source File: XbaseIntegrationTest.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected Object invokeXbaseExpression(String expression) throws Exception {
	DomainModel model = parseHelper.parse("entity Foo { op doStuff() : Object { " + expression + " } } ");
	validationTestHelper.assertNoErrors(model);
	InMemoryFileSystemAccess fsa = new InMemoryFileSystemAccess();
	generator.doGenerate(model.eResource(), fsa);
	CharSequence concatenation = fsa.getTextFiles().values().iterator().next();
	Class<?> clazz = javaCompiler.compileToClass("Foo", concatenation.toString());
	Object foo = clazz.getDeclaredConstructor().newInstance();
	Method method = clazz.getDeclaredMethod("doStuff");
	return method.invoke(foo);
}
 
Example #4
Source File: JvmModelGeneratorTest.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
@Test
public void bug390290InnerClassMemberImport() {
  try {
    final XExpression expression = this.expression("null");
    final Procedure1<JvmGenericType> _function = (JvmGenericType it) -> {
      final JvmGenericType innerClass = this.builder.toClass(it, "InnerClass");
      final JvmGenericType innerClassString = this.builder.toClass(it, "String");
      EList<JvmMember> _members = it.getMembers();
      this.builder.<JvmGenericType>operator_add(_members, innerClass);
      EList<JvmMember> _members_1 = it.getMembers();
      this.builder.<JvmGenericType>operator_add(_members_1, innerClassString);
      EList<JvmMember> _members_2 = it.getMembers();
      final Procedure1<JvmOperation> _function_1 = (JvmOperation fooMethod) -> {
        EList<JvmFormalParameter> _parameters = fooMethod.getParameters();
        JvmFormalParameter _parameter = this.builder.toParameter(it, "p1", this.references.createTypeRef(innerClass));
        this.builder.<JvmFormalParameter>operator_add(_parameters, _parameter);
        EList<JvmFormalParameter> _parameters_1 = fooMethod.getParameters();
        JvmFormalParameter _parameter_1 = this.builder.toParameter(it, "p2", this.references.createTypeRef(innerClassString));
        this.builder.<JvmFormalParameter>operator_add(_parameters_1, _parameter_1);
        this.builder.setBody(fooMethod, expression);
      };
      JvmOperation _method = this.builder.toMethod(it, "foo", this.references.getTypeForName(String.class, expression), _function_1);
      this.builder.<JvmOperation>operator_add(_members_2, _method);
    };
    final JvmGenericType clazz = this.builder.toClass(expression, "my.test.Outer", _function);
    expression.eResource().eSetDeliver(false);
    EList<EObject> _contents = expression.eResource().getContents();
    this.builder.<JvmGenericType>operator_add(_contents, clazz);
    expression.eResource().eSetDeliver(true);
    final InMemoryFileSystemAccess fsa = new InMemoryFileSystemAccess();
    this.generator.doGenerate(expression.eResource(), fsa);
    Map<String, CharSequence> _textFiles = fsa.getTextFiles();
    String _replace = clazz.getIdentifier().replace(".", "/");
    String _plus = (IFileSystemAccess.DEFAULT_OUTPUT + _replace);
    String _plus_1 = (_plus + ".java");
    final String code = _textFiles.get(_plus_1).toString();
    Assert.assertFalse(code.contains("import"));
    Assert.assertTrue(code, code.contains("java.lang.String foo"));
    final Class<?> compiledClass = this.javaCompiler.compileToClass(clazz.getIdentifier(), code);
    this.helper.assertNoErrors(IterableExtensions.<EObject>head(expression.eResource().getContents()));
    Assert.assertEquals(2, ((List<Class<?>>)Conversions.doWrapArray(compiledClass.getDeclaredClasses())).size());
    Assert.assertNotNull(compiledClass.getMethod("foo", IterableExtensions.<Class<?>>head(((Iterable<Class<?>>)Conversions.doWrapArray(compiledClass.getDeclaredClasses()))), IterableExtensions.<Class<?>>last(((Iterable<Class<?>>)Conversions.doWrapArray(compiledClass.getDeclaredClasses())))));
  } catch (Throwable _e) {
    throw Exceptions.sneakyThrow(_e);
  }
}