javaparser generic method

604 views Asked by At

Can I parse generic method with javaparser ?? If its possible so how? For example this is my method:

public static < E > void printArray( E[] inputArray )
   {
      // Display array elements              
         for ( E element : inputArray ){        
            System.out.printf( "%s ", element );
         }
         System.out.println();
    }

When I parse with MethodVisitor and use MethodVisitor.getParameters().getType I get E[] but I need to get that it is Object. It is posible?

2

There are 2 answers

0
user1951618 On BEST ANSWER

The only way is create own parser. JavaParser return BeginLine methods and the only way is parse this line.

public static < E > void printArray( E[] inputArray )

When we remove all modificators (public and static) and next value not equal to return type(void), we get and we know E is Object.

0
Federico Tomassetti On

If I understand correctly your problem, you would like JavaParser to tell you that E does not extends anything, so it could be an Object. This is something JavaParser cannot do, because it is just a parser: it builds the AST but it does not try to resolve symbols, analyze types etc. therefore it cannot be used for your goal. You may want to try something like Eclipse JDT (which is usable also outside Eclipse with some effort...)

Disclaimer: I am a contributor to JavaParser