I am attempting to add a feature to Java. You can find a description of it here. I was wondering if there is a way to write a custom Java compiler (or an extension for the compiler). The code would still compile into standard Java code, so I wouldn't need a custom JVM (or JRE, whichever it is). If anyone knows of how to do this, let me know. If you know it's not possible, could you link me to proof (maybe a statement from Oracle)? Thanks!
-
scala-lang.orguniverio– univerio2014-08-06 00:30:06 +00:00Commented Aug 6, 2014 at 0:30
-
@univerio Scala would allow me to do this? Can you provide a link to a tutorial or sample class or anything at all?nrubin29– nrubin292014-08-06 00:32:42 +00:00Commented Aug 6, 2014 at 0:32
-
openjdk.java.net/groups/compilerGreg Kopff– Greg Kopff2014-08-06 00:33:22 +00:00Commented Aug 6, 2014 at 0:33
-
Sorry, it was a tongue-in-cheek observation that it's much easier to use a mature language that somebody else has already written with the features you want than to fork javac.univerio– univerio2014-08-06 00:34:13 +00:00Commented Aug 6, 2014 at 0:34
-
@univerio My idea is not implemented in any language that currently exists.nrubin29– nrubin292014-08-06 00:34:56 +00:00Commented Aug 6, 2014 at 0:34
|
Show 8 more comments
2 Answers
Scala targets the JVM and its Structural Typing does what you want. Example:
def generateNickname(name: {def getName(): String; def setName(name: String): Unit}): String {
// ...
}
Comments
The Eclipse Compiler for Java (ECJ) is open source, so it's freely modifiable. See the JDT Core documentation. However, @univerio's suggestion to look at Scala before taking this on is a good idea. Scala fixes many of the shortcomings of Java but still compiles to JVM code, so existing Java libraries work seamlessly.