Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Standalone XBase DSl with Compile Time Java Source (.java) and Jar (.class file) Dependencies
Standalone XBase DSl with Compile Time Java Source (.java) and Jar (.class file) Dependencies [message #1830498] Mon, 27 July 2020 20:51 Go to next message
Ben Holland is currently offline Ben HollandFriend
Messages: 34
Registered: April 2019
Member
Hello my apologies for asking what is probably a duplicate question but after lots of searching I have not found the answer myself.

I have a DSL that leverages XBase for code generation to Java source. In Eclipse I setup an XText project that has additional a) Java source and b) Jar library dependencies. I am making a standalone compiler now and I know I need to populate the resource set myself because I will not be using Eclipse to do it. How can I create the resources for a) everything in Jar libraries and b) additional Java source files (originally provided in the project)?

Here is what I have tried so far, with TODOs where I am unsure what to do next. Any advice is very welcome!

       // dslFile is a .dsl file (loading this resource works as expected)
	public static void generateCode(File dslFile) throws IOException {
		try {
			Injector injector = new MyDslStandaloneSetup().createInjectorAndDoEMFRegistration();
			
			XtextResourceSet resourceSet = injector.getInstance(XtextResourceSet.class);
			resourceSet.addLoadOption(XtextResource.OPTION_RESOLVE_ALL, Boolean.TRUE);
			
			File myDslRuntime = new File("MyDslRuntime.jar");
			// TODO: how to load resources from jar file???? a jar handler is not defined
			resourceSet.getResource(URI.createFileURI(myDslRuntime.getAbsolutePath()), true);
			
			
			File javaSourceFiles = new File("input-path/src");
			Arrays.stream(javaSourceFiles.listFiles()).filter(file -> file.isFile() && file.getName().endsWith(".java")).forEach(file -> {
				// TODO: how to load resources from java source file???? a .java handler is not defined
				resourceSet.getResource(URI.createFileURI(file.getAbsolutePath()), true);
			});
			
			XtextResource resource = (XtextResource) resourceSet.getResource(URI.createFileURI(source.getAbsolutePath()), true);
			
			// TODO: is there a better way to create a simple FSA?
			class FSA implements IFileSystemAccess {
				private File root;
				
				public FSA(File root) {
					this.root = root;
					this.root.mkdirs();
				}
				
				@Override
				public void generateFile(String fileName, CharSequence contents) {
					this.generateFile(fileName, null, contents);
				}

				@Override
				public void generateFile(String fileName, String outputConfigurationName, CharSequence contents) {
					File file = new File(root.getAbsolutePath() + File.separator + fileName);
					file.getParentFile().mkdirs();
					try {
						FileWriter fw = new FileWriter(file);
						fw.write(contents.toString());
						fw.close();
					} catch (Exception e) {
						throw new RuntimeException(e);
					}
				}

				@Override
				public void deleteFile(String fileName) {
					File file = new File(root.getAbsolutePath() + File.separator + fileName);
					if(file.exists()) {
						file.delete();
					}
				}
			}
			
			// this works but fails when it cannot find the types in the runtime/Java source resources
			MyDslGenerators generators = injector.getInstance(MyDslGenerators.class);
			generators.doGenerate(resource, new FSA(new File("output-path/src-gen/")));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

[Updated on: Mon, 27 July 2020 21:49]

Report message to a moderator

Re: Standalone XBase DSl with Compile Time Java Source (.java) and Jar (.class file) Dependencies [message #1830500 is a reply to message #1830498] Mon, 27 July 2020 21:24 Go to previous messageGo to next message
Ben Holland is currently offline Ben HollandFriend
Messages: 34
Registered: April 2019
Member
In case my question wasn't clear I want to implement the following TODOs.

    private static void loadFiles(File directory, XtextResourceSet resourceSet) {
		Arrays.stream(directory.listFiles()).forEach(file -> {
			if(file.isDirectory()) {
				loadFiles(file, resourceSet);
			} else if (file.getName().endsWith(".dsl")){
				loadDSLFile(resourceSet, file);
			} else if (file.getName().endsWith(".java")){
				// TODO: how to load java source resources?
			} else if (file.getName().endsWith(".jar")){
				// TODO: how to load jar resources?
			} else {
				throw new IllegalArgumentException("Unhandled resource type");
			}
		});
	}

	private static void loadDSLFile(XtextResourceSet resourceSet, File file) {
		System.out.println("Loading: " + file.getAbsolutePath());
		resourceSet.getResource(URI.createFileURI(file.getAbsolutePath()), true);
	}


P.S. Right after I posted I noticed another example used the JavaIoFileSystemAccess which works beautifully, so now I just to figure out how to load .java and .jar resources properly.

JavaIoFileSystemAccess fsa = injector.getInstance(JavaIoFileSystemAccess.class);
fsa.setOutputPath("output-path/src-gen/");

[Updated on: Mon, 27 July 2020 21:48]

Report message to a moderator

Re: Standalone XBase DSl with Compile Time Java Source (.java) and Jar (.class file) Dependencies [message #1830509 is a reply to message #1830500] Tue, 28 July 2020 05:17 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
I assume https://github.com/eclipse/xtext-core/blob/master/org.eclipse.xtext/src/org/eclipse/xtext/mwe/PathTraverser.java
And the reader class in the same package are good starting points


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Standalone XBase DSl with Compile Time Java Source (.java) and Jar (.class file) Dependencies [message #1830927 is a reply to message #1830509] Thu, 06 August 2020 21:49 Go to previous messageGo to next message
Ben Holland is currently offline Ben HollandFriend
Messages: 34
Registered: April 2019
Member
The PathTraverser answers part of my question, but I am still very confused about how I should be loading Java class resources that are referenced in the XBase language. Is there an equivalent helper in the xtext-extras repo I should looking at? I cannot find what I'm looking for. There must be someway to load a .class or .jar resource into the resource set. If I do a resourceSet.getResource(uri) where resourceSet = injector.getInstance(XtextResourceSet.class) then I get the following runtime exception.

Cannot create a resource for 'file:...MyClass.class'; a registered resource factory is needed


To me this indicates there must be a resource set or some similar helper that is capable of loading class file resources, but I cannot find it.
Re: Standalone XBase DSl with Compile Time Java Source (.java) and Jar (.class file) Dependencies [message #1830929 is a reply to message #1830927] Thu, 06 August 2020 22:17 Go to previous messageGo to next message
Ben Holland is currently offline Ben HollandFriend
Messages: 34
Registered: April 2019
Member
org.eclipse.xtext.xbase.compiler.CompilationTestHelper seems closer to what I am looking for, but its using a lot of internal test APIs.
Re: Standalone XBase DSl with Compile Time Java Source (.java) and Jar (.class file) Dependencies [message #1830937 is a reply to message #1830929] Fri, 07 August 2020 05:52 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
i dont understand: do you want to makes classes available in xbase or do you want to to add model files in jars to the resourceset.



Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Standalone XBase DSl with Compile Time Java Source (.java) and Jar (.class file) Dependencies [message #1830971 is a reply to message #1830937] Fri, 07 August 2020 13:50 Go to previous messageGo to next message
Ben Holland is currently offline Ben HollandFriend
Messages: 34
Registered: April 2019
Member
I want to make a set of Java classes available. My XBase language links against some Java classes as .java source files and compiled .class files (which may be in jars). Right now when I run the generator it fails when it reaches a model source that is referencing these Java classes and that makes sense to me because I do not know how to add them to the classpath for the XBase resource set.

[Updated on: Fri, 07 August 2020 14:29]

Report message to a moderator

Re: Standalone XBase DSl with Compile Time Java Source (.java) and Jar (.class file) Dependencies [message #1830977 is a reply to message #1830971] Fri, 07 August 2020 15:32 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
You can call XtextResourceSet setClasspathuricontext
With an URlclassLoader (or something like that )
Populated with the jars

e.g. like
https://github.com/eclipse/xtext-xtend/blob/f3c0231cce718cde9fb37cec9e86569e7977668b/org.eclipse.xtend.core/src/org/eclipse/xtend/core/compiler/batch/XtendBatchCompiler.java#L821


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de

[Updated on: Fri, 07 August 2020 17:04]

Report message to a moderator

Re: Standalone XBase DSl with Compile Time Java Source (.java) and Jar (.class file) Dependencies [message #1830983 is a reply to message #1830977] Fri, 07 August 2020 20:57 Go to previous message
Ben Holland is currently offline Ben HollandFriend
Messages: 34
Registered: April 2019
Member
Thank you!
Previous Topic:Support of OSGI DS 1.3 annotations in Xtend?
Next Topic:Filter the terminals list proposed in content assistant
Goto Forum:
  


Current Time: Thu Apr 18 02:03:31 GMT 2024

Powered by FUDForum. Page generated in 0.02185 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top