IN:
In the visit(FieldDeclaration n, Object arg) method visit(FieldDeclaration n, Object arg) of the VoidVisitorAdapter class, you can use the following to retrieve the variables.
It will give the exact name of the variables, initialized or not.
String newstr; List<VariableDeclarator> list = n.getVariables(); //as getVariables() returns a list we need to implement that way for (VariableDeclarator var : list) { String item = var.toString(); if (item.contains("=")) { if (item != null && item.length() > 0) { int index = item.lastIndexOf("="); newstr = item.substring(0, index); newstr = newstr.trim(); variablename = newstr;`
source share