开发者

Building an extension to the java language - Steps?

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 someth开发者_开发知识库ing 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.


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.


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.


Take a look at JastAddJ.


I suggest to look at ObjectTeams. It's an eclipse project that allows to enhance otherwise closed source like Eclipse's Java compiler, the editor for java files etc. They build some exemplary Java extensions, too. Another option is to use Jetbrains MPS although that's not actual Java. MPS is a projectional approach to extensible languages. They ship with a Java representation and with a bunch of nice extensions. Their 'base language' can be seen as something like "Java++".

However, I'm not familiar with C++ structs but isn't a plain Java class with public fields pretty much the same as a struct?


You really should get into some Java annotations. They allow you to mark fields, classes and methods so that external entities can reason about the code. One annotation that is baked into the language is @Override, if the compiler sees this annotation it checks that the method actually does override something or else it complains. You are, however, free to write your own annotations, so you could create an @struct annotation (though I don't think that is particularly clear myself...).

The annotations are compiled into the class file, so you can then reason about the code using your run-of-the-mill reflection or one of the more sophisticated bytecode manipulation libraries (ASM being the best thing ever).


Thanks for all the suggestions guys. I ended up using Xtext and this allowed me to build my own DSL (Domain-Specific Language). Very nice tool!


First let me say I believe there may easier means to solve the problem you mentioned other than a compiler plugin. Off the top of my head I would explore the use of Java Enum types, which are full fledged objects and may contain behavior. However, the Java Annotation Processing Tool could be a way for you to accomplish compile time code generation. This is the means by which Project Lombok accomplishes it's compilation magic, so exploring its code may be useful.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜