0

I am trying to pull a field via reflection that is an array of the below class.

package com.api.Person
public class Person {

    private String name;
    private int age;

    public Person() {

    }

    public void setName(String name) {
    this.name = name;
    }

    public String getName() {
    return name;
    }
    
    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
    
}
package com.client.Client
public class Client{
    ...
    People[] peoples;
    ...
}

I know how to get the field I am looking for and declare my methods. So below, obj would be my People[] and methodgetAge and methodsetAge are two methods I have defined that act on a Person class. How do I take my obj and loop through it to get individual People and call methodgetAge on each person?

Class<?> mainClass = cl.loadClass("com.client.Client");
Class<?> peopleClass = cl.loadClass("com.api.Person");
Field allPersons = mainClass.getDeclaredField("peoples");
allPersons.setAccessible(true);
Object obj = allPersons.get(mainClass);

Method methodgetAge = peopleClass .getDeclaredMethod("getAge");
Method methodsetAge = peopleClass .getDeclaredMethod("setAge", int.class);
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
asdasd31
  • 95
  • 7
  • I am still a little unsure where some of the variables from your code sniped come from but. You could cast it is my guess. – Alex Sep 24 '21 at 16:45
  • 1
    `Object obj = s.get(applet);` if you are sure that `obj` will hold `People[] peoples` then you can cast it to `People[]` type like `People[] peopleArray = (People[])obj;`. This way you can write rest of code for `People[] peopleArray` type instead of having to use reflection. – Pshemo Sep 24 '21 at 16:46
  • So what is "s" ? – Alex Sep 24 '21 at 16:47
  • @Pshemo So the jar that is executing this code doesn't have `People` defined and I intend to leave it that way, needs to be done all in reflection. – asdasd31 Sep 24 '21 at 16:53
  • @Alex Sorry, I updated the variables named incorrectly. – asdasd31 Sep 24 '21 at 16:53
  • Then for "How do I take my obj and loop through it to get individial People" you are probably looking for: [Iterating over arrays by reflection](https://stackoverflow.com/q/2200399). "and call `methodgetAge` on each person" -> [How do I invoke a Java method when given the method name as a string?](https://stackoverflow.com/q/160970) – Pshemo Sep 24 '21 at 16:56
  • Cast it to Object[] – Alex Sep 24 '21 at 16:58
  • @Pshemo Thanks! So the first link helped, I'm able to iterate over the array.. but invoking the method, still not sure there, this is not as standard as that question, tried that and it didn't work. The problem is, normally you would refer your `Class` object from `ClassLoader.loadClass` and then do `.getDeclaredMethod("methodName")` but I need to somehow pass my iterated object as the class (I tried just casting it as a class or doing `getClass()` but it didn't work – asdasd31 Sep 24 '21 at 17:19
  • Assuming you have some `valueFromArray` you can also use `valueFromArray.getClass()` to get `Class` corresponding to its type. On it you can call `getDeclaredMethod`. – Pshemo Sep 24 '21 at 17:25
  • Assuming your code snippet is right, your Classes full quallified names are `com.api.Person.Person` and `com.client.Client.Client`. to get a full qualified class name as in `cl.loadClass("com.client.Client")` your package should be `com.client` – Michael Katt Sep 24 '21 at 17:45
  • `allPersons.get(mainClass);` << This would not work, as `mainClass` is a `Class` and not an instance of `com.client.Client`. – Johannes Kuhn Sep 25 '21 at 00:07
  • 1
    It seems you generally like to mix up classes and objects. It’s not only that `allPersons.get(mainClass);` is wrong because your passing a class where an object is required, your statement “I need to somehow pass my iterated object as the class” makes no sense either. – Holger Sep 27 '21 at 11:51

0 Answers0