I know my question does not seem valid, but it is genuine. When writing java I must use the word import
so as to import classes from classpath. It is required t
You can't really solve this to work generically; java strictly defines the keywords in its language specification and there is no mechanism in the java language to add keywords (e.g. macros).
A partial solution would be to create a preprocessor that translates your new keywords into plain java. Needless to say that this is a pain to integrate into common tool chains and you won't get useful compiler error messages any more for constructs created by the preprocessor.
One step further would be to write your own compiler; again this integrates poorly with existing toolchains. You still don't get proper support from your IDE.
As fascinating as the idea is; the obstacles make it highly impractical for generic use.
The situation is different in languages that come with a compile time macro language (most assembler langauges have this). C's define is another example. They still all have the problem that they are preprocessor based, so added constructs are more complicated (only basic syntax checking etc.)
Java doesn't provide any way to redefine keywords.
If you add or remove keywords to the Java language, then it isn't Java anymore.
You could write your own language that compiles to Java. This could be as simple as writing a program that does a string replace of uvoziti
for import
and then runs it through the javac
compiler.
One approach that uses a rather sophisticated toolchain and could be considered as an "overkill", but is not as much effort as writing an own compiler or so:
Run
java -classpath "antlr-4.4-complete.jar" org.antlr.v4.Tool Java.g4
This will generate some files, one of them being JavaLexer.java
.
JavaLexer.java
Create a class like the following, which does the translation:
import java.io.IOException;
import org.antlr.v4.runtime.ANTLRFileStream;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.TokenStream;
public class Main {
public static void main(String[] args) throws IOException {
CharStream s = new ANTLRFileStream("Input.javaX");
JavaLexer lexer = new JavaLexer(s);
TokenStream t = new CommonTokenStream(lexer);
int i = 1;
while (true) {
if (t.LA(i) == -1) {
break;
}
if (t.LA(i) == JavaLexer.IMPORT) {
System.out.print("import ");
} else {
System.out.print(t.LT(i).getText() + " ");
}
i++;
}
}
}
(of course, this is only an example that only translates the IMPORT
token, which was defined in the grammar file to be "uvoziti"
. For a more general and flexible translation, one would define the translation in an external file, and probably read this file to create a map Map<Integer, String>
that maps JavaLexer.IMPORT
to "import"
etc...)
Create the input file from the example: Input.javaX
:
uvoziti java.io.File;
public class Input
{
public static void main(String args[])
{
File file = null;
System.out.println("done");
}
}
When you then run the Main
, it will read this input file, eventually find the IMPORT
token, and instead of the original text (uvoziti
) it will print import
.
The result will be the contents of a Java file, with an awful formatting...
import java . io . File ; public class Input { public static void main ( String args [ ] ) { File file = null ; System . out . println ( "done" ) ; } }
but fortuntately, the compiler does not care about the formatting: You may write this output directly to a .java
file, and the compiler will swallow it.
As it is described here, it is only a proof of concept. For a flexible and generic translation of many (all) keywords, one would have to build some infrastructure around all that. The input files should be read automatically (File.listFiles()
, recursively for packages). Each of them would have to be translated (using the Map<Integer, String>
that I mentioned earlier). Then the output files would have to be written and compiled, either with the runtime JavaCompiler, or by manually invoking the javac
with Runtime#exec.
But in general, I think that this should be doable within a few hours in the best case, and within one week when considering that everything takes longer than you think.
Writing an own Java compiler, on the other hand, might take a bit longer, even when you consider that everything takes longer than you think...
As an option, use something like preprocessor or write your own one, to process java code via replacement of bosnian words to english ones before passing this code to the javac compiler.
I think this approach should work for your case.
Java by itself doesn't help you in this.
You might want to pass your code through a pre-processor, but things start to look a bit crazy: Can I have macros in Java source files. I've never done something like this, so I'm not sure it will work as intended.
Also, consider that after this change your code is only readable by people understanding bosnian.
JVM and JDK understands Java key words. If you want to change into you language, then you have to change JDK and JVM also.