How to invoke a string from a class using method.invoke(-,-) - java

Here i am trying to get uContainer object from another project. uContainer having all the setters and getters with return values set from properties file. Like a user properties for perticular user. I am using to get perticular method values from uContainer instance. But in the 4th line my application getting crashed.
uContainer is an instance of UserContainer class.
getSingleResultListing also a boolean variable in UserContainer class having with getters and setters methods.
The code is given below.
Method getUContainer = form.getClass().getMethod("getUserContainer", new Class[0]);
Object uContainerObj = (Object)getUContainer.invoke(form, new Object[0]);
Method getFlagValueMethod = uContainerObj.getClass().getMethod("getSingleResultListing", new Class[0]);
String flagValue = (String)getFlagValueMethod.invoke(uContainerObj, new Object[0]);
log.info(">>>flagValue: "+flagValue);
boolean singleListingFlag = Boolean.getBoolean(flagValue);
log.info(">>>singleListingFlag: "+singleListingFlag);
here in the fourth line while invoking the uContainer object i am getting error ..
Thanks..

You are casting the returned object to a String, but you are not getting a String from that method. You cannot convert objects to String via a cast operator. If you want the string representation, write
String flagValue = getFlagValueMethod.invoke(uContainerObj, new Object[0]).toString();

Related

How Can I get parameter's value in Object class?

public void findAllByMenuId() {
//given
boardRepository.save(FREE_BOARD);
boardRepository.save(NOTICE_BOARD);
boardRepository.save(NOTICE_BOARD_2);
Integer freeBoardId = FREE_BOARD.getMenu().getId();
Integer noticeBoardId = NOTICE_BOARD.getMenu().getId();
//when
Page<Object> freeBoards = boardRepository.findAllByMenuId(freeBoardId, Pageable.ofSize(5));
Page<Object> noticeBoards = boardRepository.findAllByMenuId(noticeBoardId, Pageable.ofSize(5));
//then
assertThat(freeBoards.getTotalElements()).isEqualTo(1);
freeBoards.forEach(
board->assertThat(board.getMenuId()).isEqualTo(freeBoardId)); // error in getMenuId()
assertThat(noticeBoards.getTotalElements()).isEqualTo(2);
noticeBoards.forEach(
board->assertThat(board.getMenuId()).isEqualTo(noticeBoardId)); // error in getMenuId()
}
hi. I wanna know how can I get MenuId in Object class.
In the service logic, it was not possible to use the separately designed Dto, but an object was created using the Object class. As a side effect during the test process, there was a problem that the menuId defined inside could not be obtained.
How can I solve this?

How To use Model OOP in Java

I have a Model Active Admin, I have created String ID setter and getter.
When I use setID in Login Form, I use this :
ActiveAdmin AA = new ActiveAdmin();
AA.setId(txtIdAdmin.getText());
When I test getter from login form, it works. When I test in another form, in another Java class, in different file, I can't get my string ID in Active Admin. I used:
AA.getId();
And the result is blank.
Build ActiveAdmin as a Singleton class, such that you have one instance for the entire application. Otherwise you will build a new object ActiveAdmin everytime when you use new ActiveAdmin().
Check link Java Singleton
Muhamad, setters and getters for properties typically take this form, based on what I think you're saying:
public class ActiveAdmin
{
public string Id { get; set; }
}
From another class, you would say "aA = new ActiveAdmin();"
Then aA.Id = "2"; and string aAId = aA.Id;

how to convert a String to Object in JAVA

I am retrieving a String variable from the Database and storing in a variable.
String name = "Peter" ---- >(retrieved from the database)
I already have a class called Peter. I need to initialize the class using the retrieved variable name.
Assume that ,whatever String is retrieved from the database , I have a class defined for it in my package...
Is there a way to create a Object for the String I retrieve from the Database?
Class.forName(name).newInstance()
Using Class.forName() - java.lang.reflect
If we know the name of the class & if it has a public default constructor we can create an object in this way.
Class myClass = Class.forName("MyClass");
Class[] types = {Double.TYPE, this.getClass()};
Constructor constructor = myClass.getConstructor(types);
Object[] parameters = {new Double(0), this};
Object instanceOfMyClass = constructor.newInstance(parameters);
See Java how to instantiate a class from string

Reading values assigned to annotated field

i am very new to JAVA 8 and SPRING MVC . I have a java bean which is a POJO with setter and getter. My Spring web service using reflection maps the request parameters to the POJO.
I want to do input validation using annotation. I have a requirement were i need to read all the values of the annotated field and check atleast one value is provided. I wrote a sample code.... BUT NOT SURE HOW TO GET THE VALUES THAT ARE ASSIGNED TO A FIELD. Please do share sample code if you have:
public boolean isValid(String object, ConstraintValidatorContext constraintContext) {
boolean canProceed = false;
for(Field field : DocumentSearchRequest_global.class.getDeclaredFields())
{
if (field.isAnnotationPresent(ValidDocumentModifiedDate.class))
{
String name = field.getName();
//IAM ABLE TO GET THE NAME OF THE FIELD
System.out.println("1.name : "+ name);
System.out.println("2. "+field.getType().getName());
}
}
// Method[] method = DocumentSearchRequest_global.class.getDeclaredMethods();
for (Method method :DocumentSearchRequest_global.class.getDeclaredMethods() )
{
System.out.println(method.getName() );
//ABLE TO GET NAME OF THE GETTER AND SETTER METHODS IN THE POJO
//CAN U SUGGEST HOW TO READ THE VALUE OF A PARTICULAR FIELD.. EITHER BY //GETTING THE VALUE FROM THE GET METHOD??? ...
}
You can get the values by calling method.invoke(Object, Object...) where first parameter is your class instance on which method is to be executed and second variable arguments are arguments of the method. In your case it'll be null or empty. Here is simple code snippet Object value = method.invoke(DocumentSearchRequest_global_instance);

how to get Store JAXBelement to String?

Hello I want to Store JAXBelement value to String variable how to do this kindly tell me please.
I have Following method .
public JAXBElement<String> getSessionId()
{
return sessionId;
}
above method is in sessiondata class. i have create object of sessiodata class like below.
sessiondata result=new sessiondata();
i have call getsession method of sessiondata class like below.
result.getssionId();
now i want to store sessionid in to string variable. but its type is JAXBelment now tell me how to store in to String.
now i want to store sessionId to One String variable. and pass in to another method. kindly help me.
String sID = (String)result.getSessionId().getValue();

Categories

Resources