7 Replies Latest reply: May 28, 2010 8:31 AM by 843798 RSS

    Get declared methods

    843798
      I'm using the following :
      Method[] methods = c.getDeclaredMethods();
      , in order to get the declared methods but this doesn't work correctly if the return type is of type generic eg.
       public LinkedHashSet<String> returnHash (String fileName)
      I only get :
       public LinkedHashSet returnHash (String)
      Is there a way to solve this?? I want to know the exact return type.
        • 1. Re: Get declared methods
          EJP
          You can't. Generic types are erased. That's how it works.
          • 2. Re: Get declared methods
            843798
            Is there another way to do this??
            • 3. Re: Get declared methods
              EJP
              To do something you can't do? No.
              • 4. Re: Get declared methods
                jschellSomeoneStoleMyAlias
                Jigsaw23 wrote:
                Is there another way to do this??
                1. Add a step to the build process that parses Java files.
                2. Collect the return type in a file with appropriate information so you can tie it to the original class.
                3. Modify your code so it looks up the return type from the file rather than via reflection.

                Not sure how you would use it once you had it but there it is if you want it.
                • 5. Re: Get declared methods
                  843798
                  Jigsaw23 wrote:
                  Is there a way to solve this?? I want to know the exact return type.
                  Extend the collections you want this for;
                  class LinkedHashSet_String extends LinkedHashSet {} // so you know from the name
                  or
                  public LinkedHashSet.returnHash(String fileName, String dummyReturnTypeIndicator); // so you know from the second parameter
                  • 6. Re: Get declared methods
                    843798
                    Method.toString() only goes so far as to render the result of getReturnType() of the receiver. If the return type of a method might be a parameterized type, you can ask the Method getGenericReturnType(), decide whether the result is a Class or ParameterizedType, and if a ParameterizedType downcast the result and ask it getActualTypeArguments()...etc.

                    Depends on what you're looking to do.
                    • 7. Re: Get declared methods
                      843798
                      Advice for the OP: slow down. From your previous posts, you're trying to do things you're not ready to do. Reflection is a tricky sod, and I can see you don't yet understand fundamentals such as the difference between your filesystem and a view in an IDE, or why infinite recursion will cause a stack overflow. Seriously, don't try and do everything, you'll only ever end up not understanding anything. I'm not putting you down, or taking the mickey, seriously. Just do yourself a favour and don't overload yourself.