I have one class that has number of a String variable, I want to count all String variables.
public class Account {
//objects
// private String pageAuthenticationNavBar = "xpath:=//div[@class='ng-binding' and contains(text(),'Account Authentication')]";
private String listAuthMethod = "id:=account-authentication-authentication-methods";
private String btnContinue = "xpath:=//button[@id='account-authentication-continue']";
private String btnSkip = "xpath:=//*[contains(text(), 'Send OTP')]/..//*[contains(text(),'Skip')]";
private String btnSkipNoSMS = "xpath:=//*[@class='skip-sms-auth-container']//*[contains(text(),'Skip Authentication')]";
private String formAccountAuthentication = "xpath:=//*[@class='global-container authentication-process']";
private String txtChooseSubscriber = "xpath:=//div[contains(text(), 'Choose Subscriber')]";
private String txtSubscriberByIndex = "xpath:=(//*[contains(@class,'flex subscriber-item-inside-list')]//*[@class='subscriber-icon'])[INDEX]";
private String btnSendOtp = "xpath:=//button[starts-with(@class,'auth-continue-button')]"; //id:=account-authentication-send-otp
private String txtOtp = "id:=smsCodeInput";
}
Is there any method, any logic for counting all the String objects?
CodePudding user response:
As mentionned by f1sh , You can use reflection to iterate (and filter) fields of a class. But it would be easier to put these Strings into a List or array which makes them countable.
import java.util.stream.Stream;
class Account {
// objects
// private String pageAuthenticationNavBar = "xpath:=//div[@class='ng-binding' and contains(text(),'Account Authentication')]";
private String listAuthMethod = "id:=account-authentication-authentication-methods";
private String btnContinue = "xpath:=//button[@id='account-authentication-continue']";
private String btnSkip = "xpath:=//*[contains(text(), 'Send OTP')]/..//*[contains(text(),'Skip')]";
private String btnSkipNoSMS = "xpath:=//*[@class='skip-sms-auth-container']//*[contains(text(),'Skip Authentication')]";
private String formAccountAuthentication = "xpath:=//*[@class='global-container authentication-process']";
private String txtChooseSubscriber = "xpath:=//div[contains(text(), 'Choose Subscriber')]";
private String txtSubscriberByIndex = "xpath:=(//*[contains(@class,'flex subscriber-item-inside-list')]//*[@class='subscriber-icon'])[INDEX]";
private String btnSendOtp = "xpath:=//button[starts-with(@class,'auth-continue-button')]"; // id:=account-authentication-send-otp
private String txtOtp = "id:=smsCodeInput";
}
public class Counter {
public static void main(String[] args) {
Long numberOfStringAttributes = Stream.of(Account.class.getDeclaredFields()).filter(f -> {
return f.getType().isAssignableFrom(String.class);
}).count();
System.out.println("The number of attributes of type String is : " numberOfStringAttributes);
}
}
CodePudding user response:
As f1sh mentioned in comment, you can use reflection something like below:
public class Test{
String a="";
String b="";
String c="";
public static void main(String[] args) {
int stringCount=0;
for(Field f : Test.class.getDeclaredFields()) {
if(f.getType().getName().equals("java.lang.String"))
stringCount ;
}
System.out.println(stringCount);
}
}
Output:
3