Bug 319448

Summary: copySubtree(StringLiteral) doesn't record edits
Product: [Eclipse Project] JDT Reporter: Chris West (Faux) <eclipse>
Component: CoreAssignee: JDT-Core-Inbox <jdt-core-inbox>
Status: NEW --- QA Contact:
Severity: normal    
Priority: P3 CC: amj87.iitr, daniel_megert, keith.power
Version: 3.6   
Target Milestone: ---   
Hardware: PC   
OS: Windows Vista   
Whiteboard: stalebug
Attachments:
Description Flags
Testcase
none
TestCase for MethodDeclaration none

Description Chris West (Faux) CLA 2010-07-09 19:28:32 EDT
Build Identifier: N20100703-2000

When cu.recordModifications()ing,

sl = someStringLiteralLocation.get();
newsl = ASTNode.copySubtree(sl);
newsl.setLiteralValue("pony");
someStringLiteralLocation.set(newsl);

...results in the correct value being in the CU, but the original value coming out during cu.rewrite(doc,null).apply(doc, ..).


Reproducible: Always

Steps to Reproduce:
1. Run the attached file, which edits bar("a","b");; the first parameter via copySubtree and the second via. ast.newStringLitral();
2. Observe how printing the CU results in both edited: bar("a LOOK AT ME LOOK AT ME","b LOOK AT ME LOOK AT ME");
3. Observe how rewriting the CU results in only the newStringLiteral way causing an edit: bar("a","b LOOK AT ME LOOK AT ME");
Comment 1 Chris West (Faux) CLA 2010-07-09 19:29:21 EDT
Created attachment 173932 [details]
Testcase
Comment 2 Olivier Thomann CLA 2010-07-13 12:03:13 EDT
Reproduced. The problem actually come from the cloned nodes recorded. I need more time to find out exactly how this can be fixed.
Comment 3 Keith Power CLA 2011-04-07 06:30:03 EDT
I'm seeing a similar issue with a copied MethodDeclaration. Below is a test that I've been using. I try to set the name on the copied method two different ways and both fail. When I modify the name of the original using either of two ways the names of the original and the copy both change. methodDeclarationCopy.getName()==methodDeclaration.getName() is false. I'll upload the a test file as ASTParserTest.java

	       public void testRewrite1() throws MalformedTreeException, BadLocationException {
	            String contents =
	                    "package p;\n" + 
	                    "import org.eclipse.jdt.core.dom.ASTParser;\n" +
	                    "public class X {\n" + 
	                    "   public static void main(String[] args) {\n" +
	                    "       System.out.println();\n" +
	                    "   }\n" + 
	                    "}";
	            ASTNode node = runConversion(AST.JLS3, contents, true, true, true, "p/X.java");
	            CompilationUnit unit = (CompilationUnit) node;
	            unit.recordModifications(); // turn on recording
	            TypeDeclaration typeDeclaration  = (TypeDeclaration) unit.types().get(0);
	            MethodDeclaration methodDeclaration = (MethodDeclaration) typeDeclaration.bodyDeclarations().get(0);
	            
	            // copy and add as new body declaration
	            MethodDeclaration methodDeclarationCopy = (MethodDeclaration)ASTNode.copySubtree(unit.getAST(), methodDeclaration);
	            typeDeclaration.bodyDeclarations().add(methodDeclarationCopy);
	            
	            System.out.println(methodDeclarationCopy.getName()==methodDeclaration.getName()); // false
	            
	            methodDeclarationCopy.getName().setIdentifier("copyOfMain"); // no effect 
	            methodDeclarationCopy.setName(unit.getAST().newSimpleName("copyOfMain")); // no effect
	            methodDeclaration.getName().setIdentifier("copyOfMain"); // changes both names
	            methodDeclaration.setName(unit.getAST().newSimpleName("copyOfMain")); // changes both names                

	            IDocument doc = new Document(contents);
	            TextEdit edits = unit.rewrite(doc, null);
	            edits.apply(doc);           
	            System.out.println(doc.get());
	        }
Comment 4 Keith Power CLA 2011-04-07 06:31:01 EDT
Created attachment 192717 [details]
TestCase for MethodDeclaration
Comment 5 Keith Power CLA 2011-04-07 06:32:11 EDT
I forgot to include this info:

Version: 3.6.1
Build id: M20100909-0800
Comment 6 Eclipse Genie CLA 2019-01-01 15:17:43 EST
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet.

If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.
Comment 7 Eclipse Genie CLA 2020-12-23 05:41:36 EST
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet.

If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.
Comment 8 Eclipse Genie CLA 2023-01-31 07:49:07 EST
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet.

If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.