Bug 231122 - ASTNode.copySubtree(): NullPointerException if name or type is not set
Summary: ASTNode.copySubtree(): NullPointerException if name or type is not set
Status: RESOLVED FIXED
Alias: None
Product: JSDT
Classification: WebTools
Component: General (show other bugs)
Version: unspecified   Edit
Hardware: All All
: P3 major (vote)
Target Milestone: 3.0 RC1   Edit
Assignee: Phil Berkland CLA
QA Contact: Phil Berkland CLA
URL:
Whiteboard: PMC_approved
Keywords: contributed
Depends on:
Blocks:
 
Reported: 2008-05-08 09:09 EDT by Etienne Pfister CLA
Modified: 2010-03-03 11:18 EST (History)
3 users (show)

See Also:
berkland: pmc_approved? (david_williams)
berkland: pmc_approved? (raghunathan.srinivasan)
berkland: pmc_approved? (naci.dai)
berkland: pmc_approved? (deboer)
berkland: pmc_approved? (neil.hauge)
kaloyan: pmc_approved+


Attachments
Patch to solve copySubtree issue (1.62 KB, patch)
2008-05-08 09:09 EDT, Etienne Pfister CLA
no flags Details | Diff
New Patch to solve copySubtree issue (1.68 KB, patch)
2008-05-10 05:55 EDT, Etienne Pfister CLA
bjorn.freeman-benson: iplog+
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Etienne Pfister CLA 2008-05-08 09:09:47 EDT
Created attachment 99271 [details]
Patch to solve copySubtree issue

In the method ASTNode.copySubtree() if the tree to copy contains one of the following node with the given condition a NullPointerException will be thrown in the clone0() method of these classes.

FunctionDeclaration: If attribute methodName (getName()) is not set
ClassInstanceCreation: If attribute type (getType()) is not set

In both cases the default value is null.

The problem occurs because JavaScript allows function declarations without a name and JavaScript has no types.

The included patch fixes these two issues to make ASTNode.copySubtree() work.
Comment 1 Phil Berkland CLA 2008-05-09 16:23:10 EDT
This patch looks good.
Comment 2 Kaloyan Raev CLA 2008-05-10 03:36:14 EDT
Patch looks trivial, just a null check added. 
But, I have a comment on the patch implementation. In my opinion the pattern:

  if (getSomething() != null) {
      doSomethingWith(getSomething());
  }

is a bad practice. Especially, if getSomething() does something more than just returning an object reference. This is a precondition for possible performance and synchronization issues. And, as I look in the code, the getName() and getType() methods looks they were quite complex in the past (the commented code). Nothing can guarantee that their implementation may become complex again in the future. Therefore, I propose to use the following safer pattern:

  Something something = getSomething();
  if (something != null) {
    doSomethingWith(something);
  }

Please, also add "PMC" in Whiteboard and Helen in CC as specified in the defect review guidelines:
http://wiki.eclipse.org/WTP_PMC_Defect_Review

The Target Milestone field should be set appropriately, too. 
Comment 3 Kaloyan Raev CLA 2008-05-10 03:46:51 EDT
Another thing... Is Etienne a committer?
If not, you should add the "contribution" keyword in the Keywords field:
http://dev.eclipse.org/mhonarc/lists/wtp-dev/msg06019.html
Comment 4 Etienne Pfister CLA 2008-05-10 05:55:47 EDT
Created attachment 99603 [details]
New Patch to solve copySubtree issue 

adapted patch according to Kaloyan Raev
Comment 5 Kaloyan Raev CLA 2008-05-13 06:11:21 EDT
Approved
Comment 6 Phil Berkland CLA 2008-05-13 12:29:05 EDT
Applied patch, thank you for your contribution.