2009-07-16 91 views
6

我知道这很愚蠢,但我无法克服我的好奇心。是否可以写一个shell脚本来格式化一段java代码?使用shell脚本进行Java代码格式化

例如,如果用户写在代码:

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"); 
    } 
    } 
} 

我想编写一个shell脚本,这将使这样的代码。

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"); 
    } 
} 

准确地说,我们应该改变花括号的格式。如果它是try/catch或控制结构,我们应该将它改为同一行,如果它是函数/方法/类,它应该进入下一行。我对sed和awk知之甚少,因此可以轻松完成此任务。另外我知道这可以使用eclipse完成。

+1

可能值得改变你的问题,你问的格式,而不是验证。 – 2009-07-16 14:23:49

+0

+1为“花托”8) – RichieHindle 2009-07-16 14:24:53

+0

改变标题谢谢:) – Harish 2009-07-16 14:36:23

回答

5

好修改压入深度,我有一些免费所以我决定重温我的好linux旧日:]

在阅读了一些关于awk和sed的信息后,我决定使用它们可能会更好,因为它更容易添加缩进在awk中解析sed中的字符串。

这里是〜/ sed_script该格式源文件:

 # 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 
    }

这里是〜/ awk_script,增加缩进:

 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 
      }

而且你使用他们喜欢的是:

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

它远不是正确的格式化工具,但我希望它可以作为示例脚本以供参考:] Go与你的学习运气不错。

0

看一看CLI for Jalopy。 Jalopy是一个非常强大的源格式化程序。

+0

我只是想知道是否可以通过shell脚本来完成。有很多软件可以对代码进行格式化,但我问的是awk还是sed – Harish 2009-07-16 14:28:06

+0

。虽然我会在问题的顶部清楚地说明你正在为sed/awk做一个学习练习,或者你会得到很多答案,比如我的 – 2009-07-16 14:48:40

1

这绝对是可能的......我只是不明白你为什么想花时间做这件事? :]有足够的工具可以做到这一点,任何像样的IDE都可以重新格式化源代码(包括Eclipse)。

例如,要在Eclipse 3.4中格式化(在其他版本中应该类似),只需右键单击您的项目或文件,然后从菜单中选择“源>格式”。

如果您需要更改其格式化代码的方式,请转至“首选项> Java>代码样式>格式化程序”并更改模板。据我所知,它在JDeveloper和NetBeans中非常相似。

1

我认为这可以通过一个简单的'sed'脚本完成。 使用1个变量(bCount)来存储'{'(开头括号) - (减去)'}'的数量(右括号) 然后我会浏览这个文件并根据实际情况插入'tabs'使用的支架数量。 所以

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 

所以instert在线0

1标签0的选项卡在第1行

2突出部在第3行和第4 ...等

2

快速,有缺陷的企图,而是一个对你的样品输入工作原理:

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

这是一个awk脚本:将其放入一个文件,然后执行

awk -f awk_script_file source_file 

与此明显的缺陷包括:

  • 它不会抓住你想要缩进的地方,如

    if (foo) 
        bar(); 
    
  • 这将基于注释括号和字符串

  • 它不会检测到大括号{之后评论
0

考虑使用Jindent,这是一个“使用Emacs的简单Java缩进工具”。这是一个免费的shell脚本,它是伯克利托勒密项目的一部分。