Formatting Java code using a shell script

I know this is stupid, but I can’t overcome my curiosity. Is it possible to write a shell script to format a piece of Java code?

For example, if the user writes in code:

public class Super{ public static void main(String[] args){ System.out.println("Hello world"); int a=0; if(a==100) { System.out.println("Hello world"); } else { System.out.println("Hello world with else"); } } } 

I would like to write a shell script that will make the code this way.

  public class Super { public static void main(String[] args) { System.out.println("Hello world"); int a=0; if(a==100){ System.out.println("Hello world"); } else{ System.out.println("Hello world with else"); } } 

To be precise, we have to change the formatting of the flower brackets. If it is a try / catch or control structure, we must change it to the same line, and if it is a function / method / class, it should appear on the next line. I know little about sed and awk, which can easily handle this task. I also know that this can be done using eclipse.

+6
java scripting unix shell
source share
6 answers

Well, I had free time on hand, so I decided to relive my good old linux days:]

After reading a bit about awk and sed, I decided that it would be better to use both, as it is easier to add indentation to awk and syntax lines to sed.

Here is the ~ / sed_script that formats the source file:

  # delete indentation
     s / ^ \ + // g

     # format lines with class
     s /^\(.\+ class. \ + \) * \ ({. * \) $ / \ 1 \ n \ 2 / g

     # format lines with methods
     s / ^ \ (public \ | private \) \ (\ + static \) \? \ (\ + void \) \?  \ + \ (. \ + (. *) \) * \ ({. * \) $ / \ 1 \ 2 \ 3 \ 4 \ n \ 5 / g

     # format lines with other structures
     / ^ \ (if \ | else \ | for \ | while \ | case \ | do \ | try \) \ ([^ {] * \) $ /, + 1 {# get lines not containing '{'
                                                             # along with the next line
       /.*{.*/ d # delete the next line with '{'
       s / \ ([^ {] * \) / \ 1 {/ g # and add '{' to the first line
     } 

And here is ~ / awk_script that adds padding:

  BEGIN {depth = 0}
     /} / {depth = depth - 1}
           {
               getPrefix (depth)
               print prefix $ 0
           }
     / {/ {depth = depth + 1}

           function getPrefix (depth) {
               prefix = ""
               for (i = 0; i <depth; i ++) {prefix = prefix ""}
               return prefix
           } 

And you use them like this:

  > sed -f ~ / sed_script ~ / file_to_format> ~ / .tmp_sed
     > awk -f ~ / awk_script ~ / .tmp_sed 

This is far from the right formatting tool, but I hope it will do OK as a sample script for reference:] Good luck with your training.

+5
source share

A quick, incorrect attempt, but working on your input example:

 BEGIN {depth = 0;} /{$/ {depth = depth + 1} /^}/ {depth = depth - 1} {prefix = ""; for (i = 0; i < depth; i++) { prefix = prefix " "} print prefix $0 ; } 

This is awk script: put it in a file and do

 awk -f awk_script_file source_file 

Obvious disadvantages with this include:

  • It doesn’t capture aimless places where you want to retreat like

     if (foo) bar(); 
  • It will change the indentation depth based on curly brackets in comments and string literals

  • It will not detect {curly brackets followed by comments
+2
source share

It is definitely possible ... I just don’t understand why you would like to spend your time on this ?:] There are enough tools for this, and any decent IDE provides a way to reformat the source code (including Eclipse).

For example, to format in Eclipse 3.4 (should be the same in other versions), simply right-click on your project or file and select "Source> Format" from the menu.

And if you need to change the way the code is formatted, just go to "Settings> Java> Code Style> Formatter" and change the template. As far as I know, it is very similar to JDeveloper and NetBeans.

+1
source share

I think this can be done with a simple "sed" script. Use 1 variable (bCount), which stores the number "{" (opening brackets) - (minus) the number "}" (closing brackets) Then I look at the file and insert the “tabs” according to the actual number of bracelets used. So,

 public class Super{ //bCount=0 public static void main(String[] args){ //bCount=1 System.out.println("Hello world"); //bCount=2 int a=0; //bCount=2 ....and so on 

so set 0 tabs in line 0

1 tab in line 1

2 tabs in lines 3 and 4, etc.

+1
source share

Take a look at the CLI for Jalopy . Jalopy is a pretty powerful source for formatting.

0
source share

Consider using Jindent , which is a "simple Java indentation tool using Emacs." This is a free shell script that is part of the Ptolemy Berkeley project.

0
source share

All Articles