The Javac compiler for OPENJDK extended a syntax sugar

Source: Internet
Author: User

My extension features are described below:

Add Var to the existing syntax in Java to declare variables, and you can automatically type derivation based on the initialization data.

Give two examples:

Example one:

The following Java code (note that var here is the new syntax):

import java.util.*;  Public class Testhello {        privatenew arraylist<list<?>>();}

After compiling with the compiler I modified, you can see the following form of code when you decompile it with the Anti-compilation tool:

Import java.util.ArrayList; Import java.util.List;  Public class testhello{  privatenew  ArrayList ();}

Example two:

The following Java code (note that var here is the new syntax):

 Public class Testinit {    public var v = 1.0;} 

After compiling with the compiler I modified, you can see the following form of code when you decompile it with the Anti-compilation tool:

 Public class testinit{  publicdouble v = 1.0D;}

The idea itself is very simple:

1. Use the type of the right expression of the initialization statement as the type of the variable.

2. If there is no initialization value, or is empty, it cannot be deduced, then use object directly.

But:

Because I have just studied the source code of Javac a day, just a rough look at the "Lexical analysis" and "syntax analysis", so I do this syntax extension is in the process of parsing the code added, this approach is limited, that is, only if there is a constant in the initialization expression, Or you can automatically type recognition with a declared type after the new operator, but not for a function call (or property Reference) to initialize a variable (this is because during parsing, when parsing the statement initialized by the variable, it is usually not analyzed to other dependent resources), So if you want to do it all, you can't type-identify it in the parsing process, but instead scan the syntax tree for the second time after the entire parsing is complete, so you can do it.

Temporarily do not intend to continue to improve this expansion, just a moment to rise, play only.

My code can be downloaded here: Https://github.com/naturemickey/valjavac after downloading the code to search "ADD by zhouyou" Can find My code (only dozens of lines), the other code is OPENJDK.

Note: I'm using Jdk8u40 's Javac code to modify it, so if you want to do it yourself, you need to install JDK8 (the Java compiler is bootstrap, written entirely in Java).

Because a lot of people on the Javac code is not familiar (actually I just touch the next day only, also not familiar), so a little more:

The main class of the OpenJDK Javac Run is Com.sun.tools.javac.Main, which runs the class directly and takes the file name to be compiled as the parameter of the main function.

The Javac compiler for OPENJDK extended a syntax sugar

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.