Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » How to resolve bindings for statically imported methods
How to resolve bindings for statically imported methods [message #1439333] Tue, 07 October 2014 04:12 Go to next message
Tai Tashiro is currently offline Tai TashiroFriend
Messages: 5
Registered: October 2014
Junior Member
I'm using ASTParser to analyze source code.

When I call resolveMethodBinding() in MethodInvocation class, it returns null when the MethodInvocation instance is for statically imported method invocations. It works fine for all the other invocations. What am I missing?

import static foo.bar.Other.methodA;

public class Sample {
    public void invoke() {
        Other.methodA(); // Binding is resolved for this one ...
        methodA(); // but not for this one.
    }
}


I've tried with following versions of org.eclipse.jdt.core.
- 3.10.0
- 3.6.51

Re: How to resolve bindings for statically imported methods [message #1440416 is a reply to message #1439333] Wed, 08 October 2014 10:38 Go to previous messageGo to next message
shankha banerjee is currently offline shankha banerjeeFriend
Messages: 40
Registered: February 2013
Member
Hi Tai,
Please see:

http://docs.oracle.com/javase/1.5.0/docs/guide/language/static-import.html

"The static import declaration is analogous to the normal import declaration. Where the normal import declaration imports classes from packages, allowing them to be used without package qualification, the static import declaration imports static members from classes, allowing them to be used without class qualification".

Thanks
Re: How to resolve bindings for statically imported methods [message #1440445 is a reply to message #1440416] Wed, 08 October 2014 11:27 Go to previous messageGo to next message
Tai Tashiro is currently offline Tai TashiroFriend
Messages: 5
Registered: October 2014
Junior Member
Hi, shankha. Thanks for your reply.

But I don't get the relation between my question and this article. Could you give a brief explanation for me?

Thanks.
Re: How to resolve bindings for statically imported methods [message #1440491 is a reply to message #1440445] Wed, 08 October 2014 12:40 Go to previous messageGo to next message
shankha banerjee is currently offline shankha banerjeeFriend
Messages: 40
Registered: February 2013
Member
Sorry. I misunderstood the question. I did not read it properly. I will come up with a test case and check if I can reproduce your issue. If you have code which shows the issue please post it.
Re: How to resolve bindings for statically imported methods [message #1440935 is a reply to message #1440491] Thu, 09 October 2014 03:27 Go to previous messageGo to next message
Tai Tashiro is currently offline Tai TashiroFriend
Messages: 5
Registered: October 2014
Junior Member
Following is the parsing code.
package ast.sample;

import java.io.*;
import java.nio.charset.*;
import java.nio.file.*;

import org.eclipse.jdt.core.dom.*;

public class TestASTParser {

	private static final String PROJECT_ROOT = System.getProperty("user.dir");
	private static final Path SOURCE_PATH = Paths.get(PROJECT_ROOT, "/src/foo/bar/Sample.java");
	private static final String[] CLASS_PATH_ENTRIES = new String[] {PROJECT_ROOT + "/bin"};

	public static void main(String... args) {

		ASTParser parser = ASTParser.newParser(AST.JLS8);
		parser.setKind(ASTParser.K_COMPILATION_UNIT);
		parser.setEnvironment(CLASS_PATH_ENTRIES, null, null, true);
		parser.setResolveBindings(true);
		parser.setSource(readAsCharArray(SOURCE_PATH));
		parser.setUnitName(SOURCE_PATH.toString());

		ASTNode ast = parser.createAST(null);
		CompilationUnit unit = (CompilationUnit) ast;

		unit.accept(new ASTVisitor() {
			@Override
			public boolean visit(MethodInvocation node) {
				int lineNumber = unit.getLineNumber(node.getStartPosition());
				System.out.println(lineNumber + ": "  + node + " -> " + node.resolveMethodBinding());
				return true;
			};
		});
	}

	private static char[] readAsCharArray(Path path) {
		try (BufferedReader reader = Files.newBufferedReader(path, StandardCharsets.UTF_8)) {
			StringBuilder buffer = new StringBuilder();
			int c = -1;
			while ((c = reader.read()) != -1) {
				buffer.append((char) c);
			}
			return buffer.toString().toCharArray();
		} catch (IOException e) {
			throw new UncheckedIOException(e);
		}
	}
}


And following is the target source file.
package foo.bar;

import static foo.bar.Other.methodA;

public class Sample {
    public void invoke() {
        Other.methodA(); // Binding is resolved for this one ...
        methodA(); // but not for this one.
    }
}


When I execute the first program, I get the following result which indicates that it had failed to resolve binding for the second invocation of methodA.
7: Other.methodA() -> public static void methodA() 
8: methodA() -> null
Re: How to resolve bindings for statically imported methods [message #1441881 is a reply to message #1440935] Fri, 10 October 2014 10:00 Go to previous message
shankha banerjee is currently offline shankha banerjeeFriend
Messages: 40
Registered: February 2013
Member
Hi Tai,
I am able to reproduce your problem. Thanks for the test case. The bug is https://bugs.eclipse.org/bugs/show_bug.cgi?id=446600.
You can add yourself to the CC list to receive notifications regarding the bug.

Thanks
Previous Topic:Eclipse Hovers Window does not show buttons
Next Topic:Eclipse EE Luna + Tomcat 8
Goto Forum:
  


Current Time: Fri Nov 27 18:38:19 GMT 2020

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

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

Back to the top