-1

I want to iterate over Object[] within a class with Reflection

this is my class :

public class Lab  {
    public Browser[] browser;
}

class Browser {
    String url;
}

I want to reach browser[] from the Lab class at index 3 and check value of url

CodeMatrix
  • 2,124
  • 1
  • 18
  • 30
  • 2
    Can you elaborate on what do you aim to do and what have you tried ? Isn't there another, simpler way than reflection ? – Sébastien Palud Jul 25 '18 at 12:26
  • 1
    `myLabInstance.browser[3].url.equals("myValue")`? – Lino Jul 25 '18 at 12:28
  • my purpose it to reach it with reflection , im just testing my reflection skils and i found that i cant catch this case – Roman Glaz Jul 25 '18 at 12:31
  • Then I suggest you try it out yourself in a sandbox project, read the javadoc and discover for yourself what's available to you! – Sébastien Palud Jul 25 '18 at 12:50
  • Perfect answer : "try it for yourself" . i m already dit it , but can't found – Roman Glaz Jul 25 '18 at 12:57
  • I get your point. But if your goal is purely to test your skills, then you'll learn so much more if you struggle to find the answer yourself. That said, what you need is https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Field.html#get-java.lang.Object- with a proper cast. – Sébastien Palud Jul 25 '18 at 13:01
  • `browser` is *field* in `Lab` class. So first step is to "get value of field". When you already have value you can "test if it is array". When you know it is array "*access* value at specified *index* in array". Which of these steps you have problem with? – Pshemo Jul 25 '18 at 13:27
  • http://tutorials.jenkov.com/java-reflection/fields.html, http://tutorials.jenkov.com/java-reflection/arrays.html, [Unpacking an Array using reflection](https://stackoverflow.com/q/8095016) – Pshemo Jul 25 '18 at 13:33

2 Answers2

2

You can achieve that with something like the following snippet:

public boolean urlEquals(Lab lab, String other){
    try{
        Field browsersField = Lab.class.getDeclaredField("browsers");
        Object browsers = browsersField.get(lab);

        Object browser = Array.get(browsers, 3);

        Field urlField = Browser.class.getDeclaredField("url");
        urlField.setAccessible(true);
        Object url = urlField.get(browser);

        return url.equals(other);
    } catch(Exception e){ // probably catch specific exceptions than all
        throw new IllegalStateException(e);
    }
}
Lino
  • 19,604
  • 6
  • 47
  • 65
  • i lab.browser[3] its not replection , i try to get full reflection . to reach url from the lab after it reach browser and url – Roman Glaz Jul 25 '18 at 13:11
  • `browswers[3]` doesn't use reflection. I am guessing OP wants something like `Array.get(someArray, index)`. Java provides `java.lang.reflect.Array` for that purpose. – Pshemo Jul 25 '18 at 13:39
  • @Pshemo thanks for that Input, I've edited it into my answer – Lino Jul 25 '18 at 13:42
  • BTW `equals` method accept `Object` type parameter so there is no need to cast `url` to String. – Pshemo Jul 25 '18 at 14:01
  • Thank you , @Lino . using you solution i fixed my problem , i will put here my solution later – Roman Glaz Jul 26 '18 at 07:43
  • @RomanGlaz You're welcome. Note that there is no need to put your solution on here, it will almost always be enough to *accept* the answer which helped the most. Because for future readers of your question a general answer may help more than a specific solution which works exactly for your case – Lino Jul 26 '18 at 07:46
  • @Lino how can i accept your solution ? , im a rookie here – Roman Glaz Jul 26 '18 at 14:39
0

Maybe in this way:

public class Lab
{
public Browser[] browser;
public static void main(String[] args)
{
Lab myLabInstance = new Lab();
myLabInstance.browser = new Browser[] { new Browser(), new Browser(), new Browser(), new Browser(), };
for (Field field : Lab.class.getDeclaredFields())
{
  System.out.println(field.getName());
  if (field.getName().equalsIgnoreCase("browser"))
  {
    field.setAccessible(true);
    Browser[] browsers = (Browser[]) field.get(myLabInstance);
    System.out.println(browsers[3].url.equals("myvalue"));
  }
}}}

class Browser
{
 public String url = "hallo";
}