Creating an Extension for the Java Language - Steps?

I was wondering what common steps are needed to create some kind of Java extension or plugin. In particular, I want to create something like a C ++ structure in java that will allow me to declare methods from a specific class so that I can specify the order that the JVM should execute them. Right now it's just a pie in the sky, and I wonder how similar AspectJ or other Java extensions work so that you can declare syntax that is not native to java. I assume that this will require some kind of compiler plugin.

As an example, I will introduce something like the following

public struct weakProfile { streamDataViaGprs(); sendSimpleMap(); } public struct strongProfile { streamDataVia3G(); sendComplexMap(); sendAudio(); } 

In the above example, if I have a web service and the client has a weak profile, which means that the device they use to call the service has poor performance and low bandwidth, then I would only want to provide only streamDataViaGprs() and sendSimpleMap() . However, if the client device has powerful processing capabilities and excellent bandwidth connectivity, I would like streamDataVia3G() , sendComplexMap() and sendAudio() . This is my final goal, but I'm not sure I will participate in the development of the structure, as above, not to mention if it is possible.

+7
source share
7 answers

If you use Eclipse, you can take a look at Xtext . It allows you to create a DSL and code editor (with syntax highlighting and code completion) inside Eclipse.

There is also a JetBrains MPS , a metaprogram system.

+3
source

There is no compiler plugin API. There are at least four things you can do:

1) Write your own compiler for a new language that translates your code into Java code and then passes it to the Java compiler (used for popularity, currently rarely used). Your “compiler” can simply just do simple text conversions, passing most of the code unchanged, so this is a smart way to do something if your changes are small and local.

2) Write your own compiler for a new language that emits Java.class files (now very often done: Groovy, Scala, Clojure all work this way.)

3) Use the "technical bytecode library" to compile .class files, parse them, modify them and either write them to disk or dynamically load and execute them (this is what AspectJ and most profilers are.)

4) Get the source for Sun ^ H ^ H ^ Native Java Java compiler - it is available in OpenJDK - and change it. This is hardcore and probably not the best plan.

Which way you choose, of course, depends on your skills and requirements.

+16
source

Take a look at JastAddJ .

+2
source

I suggest to look at ObjectTeams . This is an eclipse project that can improve a differently closed source, such as the Java Eclipse compiler, an editor for java files, etc. They also create sample Java extensions. Another option is to use Jetbrains MPS , although this is not real Java. MPS is a projection approach to extensible languages. They come with a Java view and a bunch of nice extensions. Their "base language" can be seen as something like "Java ++".

However, I am not familiar with C ++ structures, but is not a simple Java class with public fields almost the same as a structure?

+1
source

You really need to find some Java annotations. They allow you to mark fields, classes, and methods so that external objects can reason about code. One annotation, baked in the language, @Override , if the compiler sees this annotation, it checks whether the method really overrides something or whether it complains. However, you can write your own annotations so that you can create the @struct annotation (although I don't think this is particularly clear) ...

Annotations are compiled into a class file, so you can reason about code using reflection during a loop or one of the more complex bytecode manipulation libraries ( ASM is the best it has ever been).

+1
source

Thanks for all the guys suggestions. I ended up using Xtext, and this allowed me to create my own DSL (Domain-Specific Language). A very good tool!

0
source

First, let me say that I think it might be easier to solve the problem you were talking about besides the compiler plugin. At the top of my head, I would study the use of Java Enum types, which are complete objects and may contain behavior. However, the Java Annotation Processing Tool could help you compile the time code. This is the way Project Lombok does compilation magic, so learning its code can be useful.

0
source

All Articles