简体   繁体   English

Java7 Diamond运算符转换编译错误

[英]java7 diamond operator convert compilation error

I wanted to convert the current code to Java7 diamond operator . 我想将当前代码转换为Java7 diamond operator I have the below regular expression for finding and replace. 我有下面的正则表达式,用于查找和替换。

Finding 寻找

new (\w+)<.+>

Replace: 更换:

new $1<> 

The problem is it is getting replaced for anonymous classes as well. 问题在于它也正被匿名类取代。 Becuase of this I get compilation error. 因为这个我得到编译错误。 So How do I write the regular expression which avoid inner classes? 那么,如何编写避免使用内部类的正则表达式呢?

Try this : (?s)new (\\w+)<.+>(\\(.*?\\);) 试试这个:( (?s)new (\\w+)<.+>(\\(.*?\\);)

String test = "List<String> t = new ArrayList<String>();";
System.out.println(test.replaceAll("(?s)new (\\w+)<.+>(\\(.*?\\);)", "new $1<>$2"));

String test2 = "List<String> t = new ArrayList<String>(getList());";
System.out.println(test2.replaceAll("(?s)new (\\w+)<.+>(\\(.*?\\);)", "new $1<>$2"));

String test3 = "List<String> t = new ArrayList<String>(\n\tgetList(\n\t\tanotherMethod()\n\t)\n);";
System.out.println(test3.replaceAll("(?s)new (\\w+)<.+>(\\(.*?\\);)", "new $1<>$2"));

String test4 = "List<String> t = new List<String>(){ /* implementation */ };";
System.out.println(test4.replaceAll("(?s)new (\\w+)<.+>(\\(.*?\\);)", "new $1<>$2"));

String test5 = "List<String> t = new List<String>()\n{\n\t/* implementation */\n};";
System.out.println(test5.replaceAll("(?s)new (\\w+)<.+>(\\(.*?\\);)", "new $1<>$2"));

It prints : 它打印:

List<String> t = new ArrayList<>();
List<String> t = new ArrayList<>(getList());
List<String> t = new ArrayList<>(
    getList(
        anotherMethod()
    )
);
List<String> t = new List<String>(){ /* implementation */ };
List<String> t = new List<String>()
{
    /* implementation */
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM