簡體   English   中英

如何使用 kotlin 知道 java 的評論前是否有代碼?

[英]How to know if there is some code before a comment in java using kotlin?

我有興趣編寫一些代碼來檢測 java 代碼中的特殊情況,我們在同一行上有一些注釋的代碼。

int i = 10 //here is the comment

我希望能夠使用 kotlin 的 File forEachLine 方法檢測此類行。 但是我不知道該怎么做。

我唯一能做的就是通過以下方式找到包含評論的行:

File(fileName).forEachLine {
    
   if(it.contains("//")){
      println("There is a comment!")
   }

}

我對檢測注釋不感興趣,只對那些在同一行有代碼和注釋的行感興趣。

注意:fileName 是一個 java 文件,例如:我們逐行讀取的 Test.java。

您可以在文件的每一行上組合使用splitfilter函數:

fun getCodeLinesWithComments(lines: ArrayList<String>): ArrayList<String> {
        val codeLinesWithComments = arrayListOf<String>()
        for (line in lines) {
            if (line.split("//").filter { it.isNotEmpty() }.count() == 2) {
                codeLinesWithComments.add(line)
            }
        }
        return codeLinesWithComments
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM