Automatically add comments to Java source code
This program can not understand your code. What it is doing is to add some comments based on specific rule and make your program full of comments to meet the requirement of dummy comments required in some courses.
And actually, this program can only add comments to some specific area. For some other areas, it could detect it and send a window to ask you for comments and provide some suggestion. This app is done solely by me.
Github Page: https://github.com/ShaokangJiang/AutoCommenter
Compiled version: https://github.com/ShaokangJiang/AutoCommenter/releases
Code version: https://github.com/ShaokangJiang/AutoCommenter/archive/0.1.zip
Sample result:
Before adding:
After adding
Usage
Creating configuration file and put it to the running directory
A model of configuration file as shown below:
1 | -----special:----- |
-----special:-----
and -----conditions:-----
are field tags, These field tags should matches exactly as what described above.
Special field:
- BAIDU_APP_ID/BAIDU_SECURITY_KEY: Need register Baidu translation API at here. No need to register if you don’t plan to use translation function.
- IFLY_APP_ID: Need register with Ifly at here. No need to register if you don’t plan to use voice dedication function
- lineLimit: Words will be cut of at the exact limitation position if it is out of limits.
Conditions:
The conditions used to detect on each line of code and make comments at the end of each line. It uses the regular expression of java to detect words. The format of writing condition is [Expression] <Words>
. They are separated using space.
[Expression]
: The java regular expression. If space is needed, use \s instead of an empty place.
<Words>
: The words to comment at the end of sentences that contains this expression. Use ###.*###
to place contents matches .*
on the left hand side. Only the first one on the left hand side will be placed on the right side. And code only support ###.*###
syntax right now.
For example, If there is a line in java code: System.out.println("Hello World!");
. This meet the requirement of System\.out\.println\(.*\)
defined above. So, the comments to be added to the end of this line is Print information of "Hello World!" to the user screen.
In most case, this will make sense.
Other places:
For the other place, including method header, method implementation method, return value description, pass in parameter and some other things, program will promote a window like this to let you input word you want. Default in graph below will be replaced with some guidance words.
For some method, such as getter, setter, constructor, program will place some predefined words at here. You could edit it as you need. For the edit method, you could use start listen button to speech language you choose in the first step and use voice dedicator.
Compile & run
Download from release page or compile from source.
Download from release page:
- Choose correct library to download and download AutoCommenter from release page
java -jar AutoCommenter.jar [-d] < Directory or files path >
-d
is optional, It means a directory. If -d exist, program will loop through all files in the path and make comments on them. Only support for one directory. Like,java -jar AutoCommenter.jar E:\test
<File path>
could be a list of files separated with space. Like,java -jar AutoCommenter.jar file1.java file2.java
.
Download from release page:
git clone https://github.com/sjiang97/AutoCommenter.git
cd AutoCommenter
find -name "*.java" > sources.txt
: Find all java files to compilemkdir bin
javac -d ./bin -cp ./\* @sources.txt
cd bin
java -classpath ./:../json-jena-1.0.jar:../Msc.jar Test [-d] < Directory or files path >
-d
is optional, It means a directory. If -d exist, program will loop through all files in the path and make comments on them. Only support for one directory. Like,java -jar AutoCommenter.jar E:\test
<File path>
could be a list of files separated with space. Like,java -jar AutoCommenter.jar file1.java file2.java
.
Special notes:
It requires oracle java 1.8(openjdk on linux) or above
It requires a well formated java code:, In eclipse, kick Ctrl+Shift+f
to make code well formated. And define property of class, fields specifically, i.e. include public
instead of omit it.
It works perfectly on windows machine. It might have problem on mac/linux.
Further
- This is not perfect. Lots of places need to improve
- Didn’t add class header automatically
- Program should able to do multiple replacement
- Program should allow user to define mapping relationship between name of method and javadoc sample.