I'm working on a personal project just for fun, a new programming languages (just because there are not enough). I m going to make it run on JVM but I need to store some metadata in the compiled file. So I have two alternatives, create two separated file(one with metadata and one with bytecode), or put metadata and bytecode in the same file. I would like to choose the second one, my only problem is how do I tell the jvm how to extract the bytecode from the compiled file? I thought to create a specific version of an already existent jvm changing the source code but maybe there is a easier way.
Thank you in advance
EDIT
The metadata I need to store are useful only during compilation and not during runtime
AnyClassYouWant.class.getResourceAsStream("AnyClassYouWant.class")
works - at least, for any non-inner class. For inner classes you'd have to ask for"OuterClassName$InnerClass.class"
instead. This gets you the raw bytes. For anything, and for classes whose files can be anywhere (in a jar, on disk, fetched from the network, loaded by some module loading system, generated on the fly, you name it).The class file format is not the best place to attempt to store random data and
javac
cannot be coerced to do it, you'd have to write a tool that takes an existing class file and adds metadata to it, which is complicated, but, possible. It's not how java tools tend to work: Instead they pack such things into a separate file, use the same.getResourceAsStream
mechanism to load it, and distribute as jar files. There are no real downsides to shipping your data in a separate 'file' if all files just end up as entries in a jar, after all.Thus, this answer explains how to do it, but comes with the rider of: But, really, don't.