Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    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 14:29 Go to next message
Sasha Fonseca is currently offline Sasha FonsecaFriend
Messages: 4
Registered: January 2017
Junior Member
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 14:51 Go to previous messageGo to next message
Stephan Herrmann is currently offline Stephan HerrmannFriend
Messages: 1527
Registered: July 2009
Senior Member
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 14:54 Go to previous messageGo to next message
Sasha Fonseca is currently offline Sasha FonsecaFriend
Messages: 4
Registered: January 2017
Junior Member
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 15:02 Go to previous messageGo to next message
Stephan Herrmann is currently offline Stephan HerrmannFriend
Messages: 1527
Registered: July 2009
Senior Member
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 15:23 Go to previous messageGo to next message
Sasha Fonseca is currently offline Sasha FonsecaFriend
Messages: 4
Registered: January 2017
Junior Member
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 15:27 Go to previous messageGo to next message
Stephan Herrmann is currently offline Stephan HerrmannFriend
Messages: 1527
Registered: July 2009
Senior Member
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 15:36 Go to previous message
Sasha Fonseca is currently offline Sasha FonsecaFriend
Messages: 4
Registered: January 2017
Junior Member
I see it now. Thank you for your time!
Previous Topic:Launch a Java appliation and detach it from Eclipse
Next Topic:RCP Framework can't list serial port
Goto Forum:
  


Current Time: Mon Nov 20 11:59:37 GMT 2017

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

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