Skip to main content



      Home
Home » Language IDEs » Java Development Tools (JDT) » Getting the type/class of nested method/chained method invocations including static ones
Getting the type/class of nested method/chained method invocations including static ones [message #1751458] Tue, 10 January 2017 09:29 Go to next message
Eclipse UserFriend
So I'm new to Eclipse JDT and I'm developing an app where I need to get the types/classes of every method invocation in a given Java source file. I've developed my own ASTVisitor and I can traverse the AST and retrieve pretty much every type used in MethodInvocations. However, nested calls and static method calls do not seem to be retrieved:

	@Override
	public boolean visit(MethodInvocation node) {
	Expression exp           = node.getExpression();
	ITypeBinding typeBinding = null;
		
	if(exp != null) {
		typeBinding = exp.resolveTypeBinding();
	}

	if(typeBinding != null) {
		this.invocationTypes.add(typeBinding.getQualifiedName());
	}

	return true;
	}


For example, in this piece of code below, I believe I should retrieve System, PrintStream, IntStream, List and LinkedList<String>, but I only seem to get PrintStream, IntStream, List and LinkedList<String>, i.e., System does not seem to be retrieved.

	
        System.out.println("Hello");
	List<String> list = new LinkedList<String>();
	list.add("1");
	list.remove(0).codePoints().close();;


I believe the types from the nested list.remove(0).codePoints().close(); are retrieved all correctly, so I'm not sure if it's due to the System invocation being static? I pretty much just want to get all types in all method invocations in a source file, static or not, nested or not.

Thanks!
Re: Getting the type/class of nested method/chained method invocations including static ones [message #1751465 is a reply to message #1751458] Tue, 10 January 2017 09:51 Go to previous messageGo to next message
Eclipse UserFriend
It's simply because your example program is not invoking any method of System Smile

If you want to analyse also the receiver types of any field access, start by adding a visit(FieldAccess) method, and also have a look at the class javadoc of FieldAccess, to learn about similar constructs.
Re: Getting the type/class of nested method/chained method invocations including static ones [message #1751466 is a reply to message #1751465] Tue, 10 January 2017 09:54 Go to previous messageGo to next message
Eclipse UserFriend
You're right Stephan, now I feel dumb lol I've been away from Java for a long time. I actually have another visit method for FieldDeclaration.

Do you reckon my visit method for MethodInvocation will retrieve all the types I want?
Re: Getting the type/class of nested method/chained method invocations including static ones [message #1751467 is a reply to message #1751466] Tue, 10 January 2017 10:02 Go to previous messageGo to next message
Eclipse UserFriend
Please don't confuse FieldDeclaration vs. FieldAccess ;p

For method invocations your implementation looks solid to me, provided the input AST could be resolved without errors.
You might still be surprised by some Java corner cases like "myArray.clone()", where the type binding may represent an array type, but as said: corner cases.
Re: Getting the type/class of nested method/chained method invocations including static ones [message #1751470 is a reply to message #1751467] Tue, 10 January 2017 10:23 Go to previous messageGo to next message
Eclipse UserFriend
Thank you for clearing that up Stephan. How may I retrieve the type of System.out? visit(FieldAccess node) does not seem to be triggered?
Re: Getting the type/class of nested method/chained method invocations including static ones [message #1751471 is a reply to message #1751470] Tue, 10 January 2017 10:27 Go to previous messageGo to next message
Eclipse UserFriend
Quote:
... and also have a look at the class javadoc of FieldAccess, to learn about similar constructs.
Smile
Re: Getting the type/class of nested method/chained method invocations including static ones [message #1751473 is a reply to message #1751471] Tue, 10 January 2017 10:36 Go to previous message
Eclipse UserFriend
I see it now. Thank you for your time!
Previous Topic:Launch a Java appliation and detach it from Eclipse
Next Topic:Use
Goto Forum:
  


Current Time: Tue Jul 08 18:22:50 EDT 2025

Powered by FUDForum. Page generated in 0.03902 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top