Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract all String from all Java files in a Java project?

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

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
enter image description here BUT it works over one file not all files

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

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

like image 929
ahmednabil88 Avatar asked Dec 22 '13 19:12

ahmednabil88


2 Answers

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 Search Inquiry
enter image description here

Eclipse Search Result

enter image description here

like image 199
ahmednabil88 Avatar answered Nov 13 '22 01:11

ahmednabil88


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.

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.

like image 40
user1622369 Avatar answered Nov 13 '22 01:11

user1622369