8 Replies Latest reply: Apr 11, 2012 11:45 AM by 914159 RSS

    How to iterate through List returned by method using reflection?

    914159
      Hi,

      How do I iterate through the List<Address> and call a method within Address class?

                Method m = someObj.getClass().getMethod("listOfIPv4Addresses");
      Object aList = m.invoke(someObj); // returns a List<Address>
                          
      ParameterizedType returnType = (ParameterizedType) m.getGenericReturnType();
      Type[] typeArguments = returnType.getActualTypeArguments();
      Class returnTypeParamClass = (Class) typeArguments [0]; // gets the 'Address' Class type

      for (Object aAddress : aList) {
      // what do i do here?
      }

      Thanks.
        • 1. Re: How to iterate through List returned by method using reflection?
          EJP
          Object aList = m.invoke(someObj); // returns a List<Address>
          List<Address> aList = List<Address>m.invoke(someObj); // returns a List<Address>
          ParameterizedType returnType = (ParameterizedType) m.getGenericReturnType();
          Type[] typeArguments = returnType.getActualTypeArguments();
          Class returnTypeParamClass = (Class) typeArguments [0]; // gets the 'Address' Class type
          If you already know it returns a List<Address> I don't see what those three lines of code are for.
          for (Object aAddress : aList) {
          for (Address aAddress : aList) {
          // what do i do here?
          I give up. What do you want to do here?
          • 2. Re: How to iterate through List returned by method using reflection?
            914159
            I have a JAR package from a third party that may or may not be included in my current application build. Therefore I need to use reflection to discover if this JAR exists and if it does, run the third party code where one of the methods returns a List of Address type. From my point of view I know exactly what is returned, but from the software side, it has no way of knowing what exists on the JAR without reflection which exposes the classes and methods. So, I just want to be able to iterate through the List of Address types and call some methods within there. The three lines I wasn't sure would help me in solving this issue, but I included it anyways.

            I apologize if this is confusing, hopefully this makes it a little more clear. Thanks.
            • 3. Re: How to iterate through List returned by method using reflection?
              EJP
              Well in that case:
              List<?> alist = (List<?>)m.invoke(someObj); // returns a List<Address>
              // Then your next 3 lines ...
              Now iterate over the List<?>
              for (Object aAddress : aList)
              {
                Object result = m.invoke(aAddress);
                // etc, whatever you want to do with that
              } 
              • 4. Re: How to iterate through List returned by method using reflection?
                DrClap
                Once you have something which might be a List<Address>, why not just assume it's a List<Address> and use ordinary Java code (i.e. not reflection) to iterate through it? If it turns out to not be that, then you're going to get a ClassCastException, which tells you that the JAR wasn't there, right? You could catch that exception and carry on doing whatever you were going to do in the absence of the JAR.
                • 5. Re: How to iterate through List returned by method using reflection?
                  EJP
                  Because the Address class mightn't be there ...
                  • 6. Re: How to iterate through List returned by method using reflection?
                    800268
                    You can compile your code with the third party library on the class path and have a single detection method to see if it is on the class path at runtime. Then make sure the actual code which needs the third party classes is conditional on that. And you probably want you tests to run once with the third party library on the class path and once without.
                    private static final boolean thirdPartyAvailable;
                    static {
                      try {
                        Class.forName("com.third.party.SomeClass");
                        thirdPartyAvailable = true;
                      }
                      catch(Throwable t) {
                        thirdPartyAvailable = false;
                      }
                    }
                    
                    public String getSomething() {
                      if(thirdPartyAvailable) {
                        for(Address address : loadAddresses()) {
                           // ...
                        }
                        return // ...
                      }
                      else {
                        return // ...
                      }
                    }
                    • 7. Re: How to iterate through List returned by method using reflection?
                      TPD-Opitz
                      This looks like a poor approach to a plugin design...

                      What's the reason why you don't kow if the third party jar is present or not?

                      Don't you need this jar to make your program compile? If so, why can't you ship it with your program?

                      Does the third party jar provide something in its <tt>META-INF/services/</tt> folder (preparing it for use with ServiceRegistry)?

                      bye
                      TPD
                      • 8. Re: How to iterate through List returned by method using reflection?
                        914159
                        I have the exact code you have there, the only issue is that Address can only be exposed through reflection. Therefore, the application will not compile when the JAR is not available, the application does not know about the existence of Address.

                        Thanks, EJP, works wonderfully.