如何从Java项目中的所有Java文件中提取所有字符串?

时间:2023-02-05 15:30:54

I need to extract all hard coded Strings values from all Java files in a Java project
Into a Java Constant file

我需要将Java项目中所有Java文件中所有硬编码的字符串值提取到Java常量文件中。

for example

例如

Input

输入

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

Output

输出

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

I am aware of Externalize Strings for Eclipse
如何从Java项目中的所有Java文件中提取所有字符串? BUT it works over one file not all files

我知道Eclipse的外部化字符串,但它在一个文件上工作,而不是所有文件。

And when i check this post
Extract all string from a java project
I could not find the link of provided presentation

当我检查这篇文章从java项目中提取所有字符串时,我找不到提供的表示的链接

Also i check this post
Externalize strings for Android project
But that is provided for Android projects not Java projects

我也检查了这篇文章,外化字符串为Android项目,但那是为Android项目提供的,不是Java项目

4 个解决方案

#1


7  

First to inspect all hard coded Strings values from all using Eclipse
The core idea is that
The Regular Expression of Java hard coded String value is "*"
So
We can do that Using search by this criteria
For example

首先,要使用Eclipse检查所有硬编码字符串值,核心思想是Java硬编码字符串值的正则表达式是“*”,因此我们可以使用这个标准进行搜索

Eclipse Search Inquiry
如何从Java项目中的所有Java文件中提取所有字符串?

Eclipse的搜索查询

Eclipse Search Result

Eclipse搜索结果

如何从Java项目中的所有Java文件中提取所有字符串?

#2


1  

Might be a over-engineered solution, but give a try to sonarqube which can give you static analysis of code and much more than just hard-coded string values.

可能是一个过度设计的解决方案,但是尝试一下sonarqube,它可以提供代码的静态分析,而不仅仅是硬编码的字符串值。

If you are looking for a code to do that, you can try linux commandline (e.g. grep -rnw '/path/to/somewhere/' -e "pattern") solutions.

如果您正在寻找这样做的代码,您可以尝试linux命令行(例如grep -rnw '/path/to/somewhere/' -e“pattern”)解决方案。

#3


0  

I'm guessing you have a large project, and don't necessarily want to hunt your hard-coded strings.

我猜你有一个大的项目,并不一定想要寻找硬编码的字符串。

While Eclipse's "Externalize String" won't do all files in one go, if you go to context of your project (by right-clicking the project or your source directory), it should find all of your candidate classes. However, you'll still need to iterate through each file via a new window, but this at least gives you the list of files as verified with Kepler SR1.

虽然Eclipse的“Externalize String”不能一次完成所有文件,但是如果您访问项目的上下文(通过右键单击项目或源目录),它应该会找到所有的候选类。但是,您仍然需要通过一个新的窗口遍历每个文件,但这至少提供了经过开普勒SR1验证的文件列表。

#4


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> " ) .

Directory/Path : c:\User\XYZ\src\

用户目录/路径:c:\ \ XYZ \ src \

如何从Java项目中的所有Java文件中提取所有字符串?

Code :

代码:

                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


7  

First to inspect all hard coded Strings values from all using Eclipse
The core idea is that
The Regular Expression of Java hard coded String value is "*"
So
We can do that Using search by this criteria
For example

首先,要使用Eclipse检查所有硬编码字符串值,核心思想是Java硬编码字符串值的正则表达式是“*”,因此我们可以使用这个标准进行搜索

Eclipse Search Inquiry
如何从Java项目中的所有Java文件中提取所有字符串?

Eclipse的搜索查询

Eclipse Search Result

Eclipse搜索结果

如何从Java项目中的所有Java文件中提取所有字符串?

#2


1  

Might be a over-engineered solution, but give a try to sonarqube which can give you static analysis of code and much more than just hard-coded string values.

可能是一个过度设计的解决方案,但是尝试一下sonarqube,它可以提供代码的静态分析,而不仅仅是硬编码的字符串值。

If you are looking for a code to do that, you can try linux commandline (e.g. grep -rnw '/path/to/somewhere/' -e "pattern") solutions.

如果您正在寻找这样做的代码,您可以尝试linux命令行(例如grep -rnw '/path/to/somewhere/' -e“pattern”)解决方案。

#3


0  

I'm guessing you have a large project, and don't necessarily want to hunt your hard-coded strings.

我猜你有一个大的项目,并不一定想要寻找硬编码的字符串。

While Eclipse's "Externalize String" won't do all files in one go, if you go to context of your project (by right-clicking the project or your source directory), it should find all of your candidate classes. However, you'll still need to iterate through each file via a new window, but this at least gives you the list of files as verified with Kepler SR1.

虽然Eclipse的“Externalize String”不能一次完成所有文件,但是如果您访问项目的上下文(通过右键单击项目或源目录),它应该会找到所有的候选类。但是,您仍然需要通过一个新的窗口遍历每个文件,但这至少提供了经过开普勒SR1验证的文件列表。

#4


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> " ) .

Directory/Path : c:\User\XYZ\src\

用户目录/路径:c:\ \ XYZ \ src \

如何从Java项目中的所有Java文件中提取所有字符串?

Code :

代码:

                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(),"\"");

                    }
                }