EDIT: Found the solution –
/**
* Returns the arguments of the method. Ensures inner methods are intact.
*
* @param fullMethod full method string
* @return arguments of the method
*/
public static String[] getArguments(String fullMethod) {
String innerFirstBrackets = fullMethod.substring(fullMethod.indexOf("(") + 1, fullMethod.lastIndexOf(")"));
if (innerFirstBrackets.contains("(") && innerFirstBrackets.contains(")")) {
List list = new List();
int count = 0;
int lastComma = 0;
for (int x = 0; x < innerFirstBrackets.length(); x++) {
if (innerFirstBrackets.charAt(x) == '(') {
count ++;
} else if (innerFirstBrackets.charAt(x) == ')') {
count --;
}
if (innerFirstBrackets.charAt(x) == ',' || x == innerFirstBrackets.length() - 1) {
if (count == 0) {
list.add(innerFirstBrackets.substring((lastComma == 0 ? -1 : lastComma) + 1,
(x == innerFirstBrackets.length() - 1 ? x + 1 : x)).trim());
lastComma = x;
}
}
}
return list.getItems();
} else {
// No inner methods
return innerFirstBrackets.split(",");
}
}
I am trying to get the arguments inside of a String representation of a method. So far I have succeeded in doing it in most cases, but it does not work for certain cases.
Here is the code I currently have :
/**
* Returns the arguments of the method. Ensures inner methods are intact.
*
* @param fullMethod full method string
* @return arguments of the method
*/
public static String[] getArguments(String fullMethod) {
String innerFirstBrackets = fullMethod.substring(fullMethod.indexOf("(") + 1, fullMethod.lastIndexOf(")"));
if (innerFirstBrackets.contains("(") && innerFirstBrackets.contains(")")) {
List list = new List();
boolean first = false, second = false;
int lastComma = 0;
for (int x = 0; x < innerFirstBrackets.length(); x++) {
if (innerFirstBrackets.charAt(x) == '(') {
first = !second;
} else if (innerFirstBrackets.charAt(x) == ')') {
second = true;
}
if (first && second) {
first = second = false;
}
if (innerFirstBrackets.charAt(x) == ',' || x == innerFirstBrackets.length() - 1) {
if (!first) {
list.add(innerFirstBrackets.substring((lastComma == 0 ? -1 : lastComma) + 1,
(x == innerFirstBrackets.length() - 1 ? x + 1 : x)).trim());
lastComma = x;
}
}
}
return list.getItems();
} else {
// No inner methods
return innerFirstBrackets.split(",");
}
}
This works when there is a method as an argument, but does not work when multiple arguments with methods as their arguments are there. This isn’t a common occurrence, but I don’t like having a vulnerability in my code.
Example of method that works
get(get(1,2));
or
get(get(get(get(1,2))));
or
get(get(1),get(1));
But it will not work when something like this is given
get(get(get(1)),get(1));
I’m not sure how to find the sister parenthesis instead of just finding the next parenthesis. (If you don’t know what I mean by sister parenthesis, think about how on most IDEs, when you highlight one parenthesis, the other is automatically highlighted. EX. 
I’m not sure I understand why commas are a problem for your algorithm. What you can do is start scanning characters to the right (assuming that you’re starting at a
(left parenthesis), initialise a counter to 0, and:()When the counter reaches zero again, you’ve found the matching pair. The only thing you need to take care of is quoted strings where the code has random parentheses inside single or double quotes (you won’t want to apply those to your counter).