Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Building an extension to the java language - Steps?

Tags:

java

struct

I was wondering what are the general steps required in building some sort of java extension or plugin. More specifically, I'm looking to build something like a C++ struct in java that will allow me to declare the methods from a particular class so that I can indicate the order they should be executed by the JVM. This is just pie in the sky at the moment and I'm interested in how the likes of AspectJ or other java extensions work to allow you to declare syntax that is not native to java. I'm assuming this would require some sort of compiler plugin.

As an example, I envisage 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, meaning that the device they are using to invoke the service has low processing and poor bandwidth capabilities, then I would like to only deliver only streamDataViaGprs() and sendSimpleMap() functionality. However, if the client device has strong processing capabilities and excellent bandwidth connection, then I would like to streamDataVia3G(), sendComplexMap() and sendAudio(). This is my ultimate goal, however I'm not sure what would be involved in developing a structure as that above, let alone if it's even possible.

like image 910
Joeblackdev Avatar asked Jun 08 '11 19:06

Joeblackdev


2 Answers

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

1) Write your own compiler for your new language, which translates your code into Java code, then feeds it to the Java compiler (used to be popular, now rarely used). Your "compiler" might actually just do simple text transforms, passing most of the code through unchanged, so this is a reasonable way to do things if your changes are small and local.

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

3) Use a "bytecode engineering library" to take compiled .class files, analyze them, modify them, and either write them back to disk or dynamically load and execute them (this is the kind of thing AspectJ and most profilers do.)

4) Get the source for Sun^H^H^HOracle's actual Java compiler -- it's available in OpenJDK -- and modify it. This is hardcore and probably not the best plan.

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

like image 147
Ernest Friedman-Hill Avatar answered Oct 08 '22 22:10

Ernest Friedman-Hill


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

There is also JetBrains MPS, the Meta Programming System.

like image 28
Pescuma Avatar answered Oct 08 '22 23:10

Pescuma