2013-12-22 21 views
12

我需要提取所有Java文件中的所有hard coded Strings值在Java project
進入一個Java Constant file如何從Java項目中的所有Java文件中提取所有字符串?

例如

輸入

// Java file number 1 
public class A {  
    public static void main(String[] args) { 
     System.out.println("HardCoded String A"); 
    } 
} 

// Java file number 2 
public class B {  
    public static void main(String[] args) { 
     System.out.println("HardCoded String B"); 
    } 
} 

輸出

// a Java Constant file 
public class MyConstants { 
    public static final String HardCodedString_01 = "HardCoded String A"; 
    public static final String HardCodedString_02 = "HardCoded String B"; 
} 

// Java file number 1 
public class A {  
    public static void main(String[] args) { 
     System.out.println(MyConstants.HardCodedString_01); 
    } 
} 

// Java file number 2 
public class B {  
    public static void main(String[] args) { 
     System.out.println(MyConstants.HardCodedString_01); 
    } 
} 

我知道外部化字符串爲Eclipse
enter image description here 但它工作在一個文件沒有所有文件

當我檢查這個帖子
Extract all string from a java project
我找不到提供演示

而且我檢查這個帖子
Externalize strings for Android project
但是,這提供了Android項目不Java項目

+2

還是宣告您的變量不變時,確保他們都Capsed例如:公共靜態最後絃樂HARDCORESTRING_01 =「硬編碼字符串A」; ˚F –

+0

@яша不錯的評論,以滿足'Java'標準,但對核心問題的任何答案:) –

+1

嘗試[Lingobit提取](http://www.lingobit.com/extractor/java_extract_hardcoded_string.html)。雖然我沒有嘗試過,而且它是專有軟件,但免費試用版可能(或不可能)解決您的問題。 –

回答

7

首先從所有使用Eclipse
的核心理念檢查所有硬編碼的字符串值是
The Regular ExpressionJava的硬編碼字符串值"*"
所以
我們可以做到這一點通過本標準使用搜索
例如

Eclipse的搜索查詢
enter image description here

Eclipse的搜索結果

enter image description here

0

我猜你有一個大的項目,唐的鏈接不一定要尋找你的硬編碼的字符串。

雖然Eclipse的「外部化字符串」不會一次執行所有文件,但如果轉到項目上下文(通過右鍵單擊項目或源目錄),它應該找到所有候選類。但是,您仍然需要通過一個新窗口遍歷每個文件,但是這至少會爲您提供經過Kepler SR1驗證的文件列表。

+0

但是,您的解決方案在哪裏? –

0
We can use Java Code also.We need to pass the directory name as below 
and it will give the File name and matched Line which contains the Hard Code 
String (" <Hard Code String in Double Quotes> ") . 

目錄/路徑:C:\用戶\ XYZ的\ src \

Logs Sample

代碼:

   import java.io.BufferedReader; 
       import java.io.File; 
       import java.io.FileReader; 
       import java.io.IOException; 
       import java.util.List; 
       import org.apache.commons.io.FileUtils; 
       import org.apache.commons.io.filefilter.TrueFileFilter; 
       import org.apache.log4j.Logger; 

       public class ScanFiles { 

        private static final Logger logger = ScanFilesLogger.getLogger(ScanFiles.class); 

        private static void searchFiles(String folderPath, String searchString) throws IOException { 
         File folder = new File(folderPath); 
         List<File> files = (List<File>) FileUtils.listFiles(folder, TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE); 
         if (folder.isDirectory()) { 
          File[] fList = folder.listFiles(); 
          for (File file : files) { 
           if (!file.canRead()) { 
            file.setReadable(true); 
            } 
            String content = ""; 
            try(BufferedReader br = new BufferedReader(new FileReader(file));) { 
             StringBuilder sb = new StringBuilder(); 
             String line = br.readLine(); 
             logger.info("File ::::: " + file.getName()); 
             while ((line = br.readLine()) != null) 
             { 
             // Below condition we can omit in the search condition // 
             if (line.contains(searchString) && (!line.contains("System.out.println") && !line.contains("logger.info") && !line.contains("Logger.info") && !line.contains("logger.debug") && !line.contains("Logger.debug") && !line.contains("logger.error") && !line.contains("Logger.error") && !line.contains("Logger.info") && !line.contains("logger.debug") && !line.contains("Logger.debug") && !line.contains("logger.error") 
                && !line.contains("LOGGER.debug") && !line.contains("LOGGER.info") && !line.contains("LOGGER.error") && !line.contains("assertTrue") && !line.contains("//") && !line.contains("/*"))) 
              { 

               logger.info("Matched Line :::::"+line); 
              }       
             } 
            } 
          } 
         } else { 
          System.out.println("Not a Directory!"); 
         } 
        } 
        public static void main(String args[]) throws IOException{ 

         if(args.length == 0)    
         { 
          logger.info(" :::::::::: PATH cannot be empty ::::::::::"); 
          System.exit(0); 
         }   
          searchFiles(new File(args[0]).getAbsolutePath(),"\""); 

        } 
       } 
1

可能是過度設計的解決方案,但試試sonarqube,它可以給你靜態代碼分析,而不僅僅是硬編碼的字符串值。

如果你正在尋找一個代碼來做到這一點,你可以嘗試Linux的命令行(例如grep的-rnw「/路徑/要/地方/」 -e「模式」)的解決方案。

相關問題