This seems an interesting problem, and I thought why not ask ChatGPT and see how it responds to this problem. Surprisingly it has generated a code (first in Python, later in Java) that works (I just tested your two base codes by the way).
Maybe it can give you an idea of how to begin with.
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ParameterComparator {
public static boolean compareParameterNames(String code1, String code2) {
String pattern = "\\b(?:int|char|float|double|long|short)\\s+([a-zA-Z_][a-zA-Z0-9_]*)\\b";
Pattern paramPattern = Pattern.compile(pattern);
Matcher matcher1 = paramPattern.matcher(code1);
Matcher matcher2 = paramPattern.matcher(code2);
while (matcher1.find() && matcher2.find()) {
String param1 = matcher1.group(1);
String param2 = matcher2.group(1);
if (!param1.equals(param2)) {
return false;
}
}
return true;
}
public static void main(String[] args) {
String code1 = "old_function (x,y,z)\n"+
"int x;\n"+
"int y;\n"+
"int z;\n"+
"{\n"+
" // some code\n"+
" return (0);\n"+
"})\n";
String code2 = "old_function (int y, int x, int z)\n"+
"{\n"+
" // some code\n"+
" return (0);\n"+
"}\n";
boolean result = compareParameterNames(code1, code2);
System.out.println("Parameters swapped: " + !result);
}
}
Do I need to tell how to ask ChatGPT to modify the code in such a way that first, it needs two files (source and target) to extract functions and then apply comparison. No, I don't think so!
I would still think that for a large-scale project, above approach may not work and you should still go with OG Advice but then why not do a little bit of experiment (probably a day) and learn something new!
Thanks,
Asif