Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add or modify keywords in java language

Tags:

java

keyword

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 to user new to initiate certain objects and other keywords in java. My question is whether we have even the slightest ability to improve and this great language by way of defining new keywords and what they do or modifying the exisiting keyword to do the same thing. For example instead of writing:

import java.io.File;

What possibility is there to modify the import word to bosnian for example:

uvoziti java.io.File;

and it all works the same way. Please do not close before I get ideas.

like image 676
Stanley Mungai Avatar asked Sep 02 '14 12:09

Stanley Mungai


People also ask

Is add keyword in Java?

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.

How do you write keywords in Java?

else: Java else keyword is used to indicate the alternative branches in an if statement. enum: Java enum keyword is used to define a fixed set of constants. Enum constructors are always private or default. extends: Java extends keyword is used to indicate that a class is derived from another class or interface.


1 Answers

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:

  • Download ANTLR4 from http://www.antlr.org/download.html
  • Download the Java Grammar at https://github.com/antlr/grammars-v4/blob/master/java/Java.g4
  • Modify the Java Grammar according to your needs...
  • 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.

  • Create a Java Project that contains the ANTLR JAR and the 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...

like image 193
Marco13 Avatar answered Oct 07 '22 02:10

Marco13