Java-action

2
Im not realy sure if the following is possible since i’m not a backend developer… Let’s say I have a Person class (this java file is automatically created when you create the Person entity with it’s attributes) with a few getters and setters: Person: getName{...} getAddress{...} getAge{...} setName{...} setAddress{...} setAge{...} ... In my MxApplication I created a custom Java-action, which has a type parameter “SomeMxObject”, an entity “SomeEntity” and string “SomeString” as input parameters. The return value is the type parameter “SomeMxObject”. What I now want to do is the following: Retrieve a list of objects based on the provided “SomeEntity” and then call a specific method within that SomeEntity-class. After that, I want to do some magic and finaly return a object of SomeEntity. To make it less abstract: @runtime the java-action gets Person as type parameter, Person as entity and getName as string value. Next I want to retrieve All objects of Person in the java-action based on the provided entity. Next I want to call a specific method (getter) within that class based on the provided string “getName”. Next I do some magic and return a Person-object.
asked
3 answers
0

Hi Jannes, 

You could use the reflection concepts in Java to achieve something like that. But, you must also pass the method name of the class you want to execute for each object.

I have done similar stuff like this for a different case. I will try to give some steps as pointers, but please be informed that I did not tried this yet. With little bit of changes to the code below, it should be working

  1. Write a java action that accepts object name and method name to execute. Method name param is only needed if it is different per object
  2. Then you can do the following to get the list of objects
  3. For executing the methods: refer https://www.baeldung.com/java-method-reflection

 

Class<?> objectClass = Class.forName(this.ClassName); // This returns the class that is needed

XPath<IMendixObject> xpath = XPath.create(this.getContext(), objectClass.getName()); //Verify if the objectClass,getName() gives you the required value. It must be Module.Entity name. 

java.util.List<T> resultList = xpath.all();

Method instanceMethod = objectClass.getMethod("methodname"); // here you can define parameters for the method 

for (int i = 0; i < resultList.size(); i++){

instanceMethod.invoke(result.get(i)); // I dont think casting might be needed. But you can check this while programming

}

Word of caution: java reflection must be handled properly.

answered
0

Not directly the solution for the way you are trying to implement this currently, but have a look at https://www.mendix.com/blog/easy-xpath-retrieval-in-java/ to see an example of how this can be done differently and probably a bit easier than you are trying now.

answered
0

Based on the TIOBE Index for June 2016 Java is now more well-known than a variety of widely-used programming languages, including C++, Python, C#, PHP and JavaScript. The statistics on usage posted on various websites illustrate the immense popularity and importance of Java in the present day world. Java is an all-purpose programming language. Certain developers prefer to use Java to build desktop GUI applications as well as other developers create numerous web-based applications using Java technologies. In addition, Java is also used extensively for the development of mobile applications and games for Android which is the mobile platform with the biggest user base. There are plenty of reasons that Java is going to be a major factor over an extended period of time.

 Java Course in Pune

answered