Bug 319448 - copySubtree(StringLiteral) doesn't record edits
Summary: copySubtree(StringLiteral) doesn't record edits
Status: NEW
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.6   Edit
Hardware: PC Windows Vista
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: JDT-Core-Inbox CLA
QA Contact:
URL:
Whiteboard: stalebug
Keywords:
Depends on:
Blocks:
 
Reported: 2010-07-09 19:28 EDT by Chris West (Faux) CLA
Modified: 2023-01-31 07:49 EST (History)
3 users (show)

See Also:


Attachments
Testcase (2.67 KB, text/plain)
2010-07-09 19:29 EDT, Chris West (Faux) CLA
no flags Details
TestCase for MethodDeclaration (5.77 KB, application/octet-stream)
2011-04-07 06:31 EDT, Keith Power CLA
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
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.