Tricky situation on Bean and Reflection - java

I want to read a flat file with contents like "Deepak,25,Singhal" and then populate the POJO dynamically. I got the method object using reflection. I also created an instance using newInstance().
Problem I am stuck is method.invoke( obj, Object ..) ..Now how do I determine the Type of argument to be passed ! From Method object how do I know that argument to be passed is int; so that I could convert String 25 to corresponding type so that method.invoke could be invoked !

Use a java class with the help of Regular expression to identify Strings and number sand convert to respective type generically

Related

How to convert the string to methods (like .Sendkeys) in Java?

Need to convert variable contains String to a method call.
Example:
Variable:
//Enter the name and value of the locator
public String[] LoginID_Button = {"name","Log in"};
In my another class:
driver.findElement(By.name(loc1.LoginID_Button[1])).isDisplayed();
But I need to write as:
driver.findElement(By.loc1.LoginID_Button[0](loc1.LoginID_Button[1])).isDisplayed();
The name is the variable string but should be changed as Method. How to do this?
This is called Reflection. Which, is the ability to change structure/behavior in runtime.
This is a nice question about it.
For your problem, you can do the same as in that question's accepted answer.
driver.findElement(By.class.getMethod(loc1.LoginID_Button[0],String.class).invoke(null,loc1.LoginID_Button[1])).isDisplayed();
In the above code, the method getMethod() is used to dynamically find a method by its name which in this case LoginId_Butt[0]. It is required also to specify the type of the parameters that target method is accepting, in our case, it is String.
The found method is then invoked using the invoke() method. The invoke() method takes two arguments, the first one is the instance that the dynamic method is executed against. In our case, the instance is null because the dynamic method is static.
The second argument is a params of the arguments passed to the dynamic method. In this case, we have only one parameter to pass, that is LoginId_Butt[1].
Notes
Please don't forget to wrap this code with ty/catch against many exceptions can be thrown.
Please use the java naming convention for variable names i.e. loginID_Button

Create an object from the contents of a variable java

How do I Create an object from the contents of a variable in Java?
For example: if the string variable "name" has a value of "Margaret"
and I apply this constructor
Name nx= new Name();
I want Java to know that I´m refering to the content of name. Therefore Java will know that I'm refering to Margaret
What I want is to create Dynamic objects without the name fixed.
I can do this in PHP but I'm new in Java and don't know if is it possible.
Thanks
Dynamic instantiation of Java classes is far more complicated than doing the same thing in PHP. In PHP you'd just use eval(), i guess.
You can get an instance of the Class object by using Class.forName. Afterwards, you would have to instantiate the object by using the Class.newInstance:
Class cls = Class.forName(name);
Object obj = cls.newInstance();
This is the simplest case. If the constructor needs parameters, you would have to get the constructor method and call it.

Java get() functionality in C#

I am trying to convert some Java to C# and I have a line as follows:
int[][] variableName = get();
What my question is is what does "get();" actually do? There is no function or method in the Java code I am converting called "get()" so I am assuming "get();" simply creates an empty object of the required type, in this case, an empty int[][]. Would I be correct in this assumption or does "get()" in Java have some other meaning?
I have searched for "get()" within stackoverflow but the () are ignored and as a result I get masses of information about HTTP GET which is not what I'm after so excuse me if this is duplicated anywhere else.
All help appreciated.
There is no function or method in the Java code I am converting called "get()"
There must be, either in that class or one of its superclasses, or as a static import although that's not very likely. (Nice one, Jesper!) My guess is that you haven't checked all of the superclasses.
...so I am assuming "get();" simply creates an empty object of the required type, in this case, an empty int[][]. Would I be correct in this assumption or does "get()" in Java have some other meaning?
No, unlike C#, get is not a keyword and has no special meaning in Java. That line of code calls a method called get (it could just as easily be called foo) which is declared in the class or one of its superclasses. It may be a static or instance method, but it will be defined by the class or one of its superclasses, or as a static import.

How to invoke method with generics args in reflect way in Java?

I'd like to build a dynamic method invoker in Java7.
I can use following code to realize it.
Method method = klazz.getMethod(methodName, argsType);
method.invoke(klazz.getConstructors()[0].newInstance(), args);
However, argsType only supports Class[] type, which means generics cannot work well here. Is there any other reflect methods to make this come true?
Thank you.
for args you could try new Object[}{} notation, i can't try it right now but using an object array you could pass all of your parameters.
Generic information is erased by the compiler. It means that during run time, all Generic types are considered to be Object (for instance, all List<String> become List<Object>) so what you're asking is impossible.
The way they circumvent this in the Java std lib is to supply a Class arg that can specify type during run time. For instance, Collections.checkedList() receives a Class arg that specifies the Generic type of the list to be created

How to share an object state between java and prolog with JPL?

I want to create an object in java:
MyObject obj = new MyObject ();
and I want to pass it to prolog with a jpl query.
How can I accomplish java to prolog object passing?
I know that I could use jpl_new in a prolog file like this:
execMethod :-
jpl_new('my_package.MyObject', [], Object),
jpl_call(Object, myMethod, [], _ ).
But, I want to avoid the jpl_new call and just use the jpl_call with the java object obj.
And converserly,
How can I accomplish prolog to java object passing?
I mean passing to java, objects created with a jpl_new call.
In other words, I want to share an object state between java and prolog.
To access a Prolog knowledge base from within Java, you can use JPL Queries. Let's look at a simple, trivial example below:
% Knowledge base (Prolog)
foo(x,bar).
all_foo(X,Y) :- foo(X,Y).
In java, we could then write:
String query = "all_foo(x,Y)";
System.out.println("First solution: " + Query.oneSolution(query).get("Y"));
which would return 'bar' as answer in Y.
Vice versa -as you showed in your question- JPL can be used when we want to access Java functionality from within a Prolog file.
Firstly, looking at the docs of jpl_call/4, we see that its first arguments can be:
a type, class object or classname (for static methods of the denoted class, or for static or instance methods of java.lang.Class)
a class instance or array (for static or instance methods)
So you are free in how to pass your class information to jpl_call/4 to execute certain methods.
Subsequently, you can access your Java model rather than executing logic by using jpl_get/3. An example below is shown where we bind the Prolog variable Colour to a reference of a field of a Java car object held in the static final .colour field of the example.class.car class.
jpl_get('example.class.car', colour, Colour)
More generally:
jpl_get(+Class_or_Object, +Field, -Datum)
Hope this helped.
Good luck!

Categories

Resources