Community
Participate
Working Groups
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");
Created attachment 173932 [details] Testcase
Reproduced. The problem actually come from the cloned nodes recorded. I need more time to find out exactly how this can be fixed.
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()); }
Created attachment 192717 [details] TestCase for MethodDeclaration
I forgot to include this info: Version: 3.6.1 Build id: M20100909-0800
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.