Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » How do I resolve binding for instance creation with diamond operator?
How do I resolve binding for instance creation with diamond operator? [message #1442079] Fri, 10 October 2014 15:32 Go to next message
Tai Tashiro is currently offline Tai TashiroFriend
Messages: 5
Registered: October 2014
Junior Member
With ASTParser, how can I resolve bindings for instance creation with diamond operator? ClassInstanceCreation#resolveConstructorBinding method returns null when diamond operator is used.

Following is the target source to parse.
package foo.bar;

import java.util.*;

public class SampleDiamondOperator {

    public void invoke() {

        Map<String, String> map1 = new HashMap<String, String>(); // Binding is resolved for this one ...
        Map<String, String> map2 = new HashMap<>(); // but not for this one
    }
}


When the above source is parsed with the following code, second instance creation cannot be binded.
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/SampleDiamondOperator.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);
		final CompilationUnit unit = (CompilationUnit) ast;

		unit.accept(new ASTVisitor() {
			@Override
			public boolean visit(ClassInstanceCreation node) {
				System.out.println(lineNumberOf(node) + ": " + node + " -> " + node.resolveConstructorBinding());
				return true;
			}
			private int lineNumberOf(ASTNode node) {
				return unit.getLineNumber(node.getStartPosition());
			};
		});
	}

	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);
		}
	}
}


Here is the result of executing the above.
9: new HashMap<String,String>() -> public void <init>() 
10: new HashMap<>() -> null

Re: How do I resolve binding for instance creation with diamond operator? [message #1442429 is a reply to message #1442079] Sat, 11 October 2014 04:10 Go to previous messageGo to next message
shankha banerjee is currently offline shankha banerjeeFriend
Messages: 40
Registered: February 2013
Member
Looks like a bug. Can you please file a bug report.
Will take a look.
Re: How do I resolve binding for instance creation with diamond operator? [message #1444347 is a reply to message #1442429] Tue, 14 October 2014 01:10 Go to previous message
Tai Tashiro is currently offline Tai TashiroFriend
Messages: 5
Registered: October 2014
Junior Member
I have filed a bug report.
https://bugs.eclipse.org/bugs/show_bug.cgi?id=447003
Previous Topic:Eclipse Kepler in Ubuntu/Debian ==> CONTROL + C (copy) don't work
Next Topic:Reading resources from a Eclipse plugin via command line options
Goto Forum:
  


Current Time: Wed Apr 24 23:51:25 GMT 2024

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

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

Back to the top