How can I get all the string in a java file.
Example:
we have a java class file like this:
-test.java -------------------------------------------------------------------------------------------------------------------
public class test{
public static void correctString(String sourceFile, String desFile){
String str1 = "Dang Nhon Hoa";
String str2 = "Nguyen Quang Minh";
String str3 = "Test1" + "test2" + "test3";
System.out.print(str3 + str1 + str2 + "test");
}
}
--------------------------------------------------------------------------------------------------------------------------
We have to write a method that read the java file above then return the expected String array content values like this:
returnArrayValues = {"Dang Nhon Hoa", "Nguyen Quang Minh", "Test1", "test2", "test3", "test"};
This mean that all values that considered as String in java will be cacth to this list.
Thanks for your help.
Instead of reading the java source file per se, if the class file is availeble then the following utility method would be of help
....
YourCustomClass yourCustClass = new YourCustomClass ();
String str = describe(yourCustClass);
if(str != null && str.length > 0){
for(int i=0; i < str.length; i++){
System.out.println("String
}
}
...
public static String[] describe(Object obj) {
String[] sb = null;
if (obj != null) {
Field[] fields = obj.getClass().getFields()
int length = fields.length;
sb = new String[length];
for (int i = 0; i < length; i++) {
try {
if (fields[i].getClass().getN
"java.lang.String")) {
sb[i] = (String) fields[i].get(obj);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
return sb;
}