Bug 87411 - [dom] API: ast.newName(String qualifiedName)
Summary: [dom] API: ast.newName(String qualifiedName)
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.0   Edit
Hardware: PC Windows XP
: P3 enhancement (vote)
Target Milestone: 3.1 M6   Edit
Assignee: Jim des Rivieres CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2005-03-08 12:26 EST by Martin Aeschlimann CLA
Modified: 2005-03-30 18:44 EST (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Martin Aeschlimann CLA 2005-03-08 12:26:01 EST
20050308

The existing API to construct a (qualified) name is
  public Name newName(String[] identifiers)

It is really nasty to use when an existing (possibly qualified) string is available.

AST#newName(String qualifiedName) would be nice to have
Comment 1 Jim des Rivieres CLA 2005-03-08 14:47:14 EST
Assuming that there are no whitespace or unicode issues to deal with, the 
following snippet does the trick. Is this a common enough issue for clients to 
warrant a convenience API?

public static void main(String[] args) {
	String qName = "org.eclipse.core.runtime.IWorkspace.IStatus";
	AST ast = AST.newAST(AST.JLS3);
	Name n = null;
	StringTokenizer t = new StringTokenizer(qName,".");
	while(t.hasMoreTokens()) {
		SimpleName s = ast.newSimpleName(t.nextToken());
		if (n == null) {
			n = s;
		} else {
			n = ast.newQualifiedName(n, s);
		}
	}
	System.out.println(n);
}
Comment 2 Martin Aeschlimann CLA 2005-03-09 10:37:41 EST
I think it would be better to use the Java scanner instead of the
StringTokenizer so that unicodes, whitespaces and comments are correctly handled.

It's better when not every client has to do this and even does it wrong. I think
it fits nicely in the other AST factory methods and the AST even has a scanner
instance readily available. 

Comment 3 Jim des Rivieres CLA 2005-03-09 11:03:06 EST
The AST methods are not generally in the business of parsing inputs; that's 
what the AST parser is for. This is why I'd like to understand where the 
concrete usecases are coming from. If unicodes, whitespaces and comments are 
involved, where on earth is the input string coming from? These kinds of 
features are normally found in snippets of source code, not in the string 
results returned from any of the JDT Core APIs.




Comment 4 Martin Aeschlimann CLA 2005-03-09 12:09:48 EST
The unicode idea was wrong, sorry, you are right. Even if taken from source we
should probably preserve the usage of unicodes, and not translate them.
Let's not focus on that, I'm fine if we use the string tokenizer. But I'm really
convinced that having AST.newName(String) would be a benefit. 

AST.newName(String[]) is just not very easy to use.

We have a util method ASTNodeFactory.newName() that does that, with 32 usages in
quick fix, refactoring and infrastructure

amongst them are:

- creating a Name node a reference to an exception (aquired from
binding.getQualifiedName())
- creating a name from referenced variables from source, (possibly) qualified
for extract method (to be used as arguments for calling the newly created method)
(Note that Dirk changed this code to use ast string placeholders because of it's
easier to use API)
- the import rewriter takes bindings or strings and returns type nodes (that may
or may not be qualified depending if an import could be added for the given
type). A type node is returned to be used with e.g. the ast rewriter
(Contructing a type is more complicated but name nodes are the base parts for
doing that)
Comment 5 Dirk Baeumer CLA 2005-03-10 07:18:04 EST
As Martin said I always started to use string place holders due to that fact
that there wasn't a convenient way to create name nodes from qualified names.

So +1 from my side.
Comment 6 Jim des Rivieres CLA 2005-03-11 21:13:10 EST
Added AT.newName(String) with following spec:

/**
 * Creates and returns a new unparented name node for the given name.
 * The name string must consist of 1 or more name segments separated 
 * by single dots '.'. Returns a {@link QualifiedName} if the name has
 * dots, and a {@link SimpleName} otherwise. Each of the name
 * segments should be legal Java identifiers (this constraint may or may 
 * not be enforced), and there must be at least one name segment.
 * The string must not contains white space, '<', '>',
 * '[', ']', or other any other characters that are not
 * part of the Java identifiers or separating '.'s.
 * 
 * @param qualifiedName string consisting of 1 or more name segments,
 * each of which is a legal Java identifier, separated  by single dots '.'
 * @return a new unparented name node
 * @exception IllegalArgumentException if:
 * <ul>
 * <li>the string is empty</li>
 * <li>the string begins or ends in a '.'</li>
 * <li>the string has adjacent '.'s</li>
 * <li>the segments between the '.'s are not valid Java identifiers</li>
 * </ul>
 * @since 3.1
 */
public Name newName(String qualifiedName) {

Updated tests and release notes.
Comment 7 Olivier Thomann CLA 2005-03-30 18:44:20 EST
Verified in 20050330-0500