Download
Getting Started
Members
Projects
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
More
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
Toggle navigation
Bugzilla – Attachment 199278 Details for
Bug 351170
[1.7] ASTRewrite issues in Try with resources
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Requests
|
Help
|
Log In
[x]
|
Terms of Use
|
Copyright Agent
[patch]
Final patch
patch_351170.txt (text/plain), 30.82 KB, created by
Olivier Thomann
on 2011-07-07 13:30:25 EDT
(
hide
)
Description:
Final patch
Filename:
MIME Type:
Creator:
Olivier Thomann
Created:
2011-07-07 13:30:25 EDT
Size:
30.82 KB
patch
obsolete
>### Eclipse Workspace Patch 1.0 >#P org.eclipse.jdt.core >Index: compiler/org/eclipse/jdt/internal/compiler/parser/Parser.java >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/parser/Parser.java,v >retrieving revision 1.422.2.26 >diff -u -r1.422.2.26 Parser.java >--- compiler/org/eclipse/jdt/internal/compiler/parser/Parser.java 6 Jul 2011 18:46:36 -0000 1.422.2.26 >+++ compiler/org/eclipse/jdt/internal/compiler/parser/Parser.java 7 Jul 2011 17:29:56 -0000 >@@ -5299,7 +5299,7 @@ > // TrailingSemiColon ::= ';' > LocalDeclaration localDeclaration = (LocalDeclaration) this.astStack[this.astPtr]; > if (punctuated) { >- localDeclaration.declarationEnd = localDeclaration.declarationSourceEnd = this.endStatementPosition; >+ localDeclaration.declarationSourceEnd = this.endStatementPosition; > } > } > protected void consumeRestoreDiet() { >Index: dom/org/eclipse/jdt/core/dom/ASTConverter.java >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ASTConverter.java,v >retrieving revision 1.273.2.12 >diff -u -r1.273.2.12 ASTConverter.java >--- dom/org/eclipse/jdt/core/dom/ASTConverter.java 27 May 2011 19:18:41 -0000 1.273.2.12 >+++ dom/org/eclipse/jdt/core/dom/ASTConverter.java 7 Jul 2011 17:29:56 -0000 >@@ -2563,7 +2563,7 @@ > LocalDeclaration localDeclaration = localDeclarations[i]; > VariableDeclarationExpression variableDeclarationExpression = convertToVariableDeclarationExpression(localDeclaration); > int start = variableDeclarationExpression.getStartPosition(); >- int end = localDeclaration.declarationSourceEnd; >+ int end = localDeclaration.declarationEnd; > variableDeclarationExpression.setSourceRange(start, end - start + 1); > tryStatement.resources().add(variableDeclarationExpression); > } >Index: dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteAnalyzer.java >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteAnalyzer.java,v >retrieving revision 1.78.2.4 >diff -u -r1.78.2.4 ASTRewriteAnalyzer.java >--- dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteAnalyzer.java 18 May 2011 04:48:04 -0000 1.78.2.4 >+++ dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteAnalyzer.java 7 Jul 2011 17:29:56 -0000 >@@ -444,7 +444,7 @@ > > > class ListRewriter { >- protected String contantSeparator; >+ protected String constantSeparator; > protected int startPos; > > protected RewriteEvent[] list; >@@ -458,7 +458,7 @@ > } > > protected String getSeparatorString(int nodeIndex) { >- return this.contantSeparator; >+ return this.constantSeparator; > } > > protected int getInitialIndent() { >@@ -495,19 +495,24 @@ > } > > public final int rewriteList(ASTNode parent, StructuralPropertyDescriptor property, int offset, String keyword, String separator) { >- this.contantSeparator= separator; >- return rewriteList(parent, property, offset, keyword); >+ this.constantSeparator= separator; >+ return rewriteList(parent, property, keyword, null, offset); > } > > private boolean insertAfterSeparator(ASTNode node) { > return !isInsertBoundToPrevious(node); > } >- >+ > protected boolean mustRemoveSeparator(int originalOffset, int nodeIndex) { > return true; > } > >- public final int rewriteList(ASTNode parent, StructuralPropertyDescriptor property, int offset, String keyword) { >+ private int rewriteList( >+ ASTNode parent, >+ StructuralPropertyDescriptor property, >+ String keyword, >+ String endKeyword, >+ int offset) { > this.startPos= offset; > this.list= getEvent(parent, property).getChildren(); > >@@ -536,7 +541,8 @@ > } > } > >- if (currPos == -1) { // only inserts >+ boolean insertNew = currPos == -1; >+ if (insertNew) { // only inserts > if (keyword.length() > 0) { // creating a new list -> insert keyword first (e.g. " throws ") > TextEditGroup editGroup= getEditGroup(this.list[0]); // first node is insert > doTextInsert(offset, keyword, editGroup); >@@ -585,6 +591,11 @@ > doTextInsert(prevEnd, getSeparatorString(i - 1), editGroup); > doTextInsert(prevEnd, node, getNodeIndent(i), true, editGroup); > } >+ if (insertNew) { >+ if (endKeyword != null && endKeyword.length() > 0) { >+ doTextInsert(currPos, endKeyword, editGroup); >+ } >+ } > } else if (currMark == RewriteEvent.REMOVED) { > ASTNode node= (ASTNode) currEvent.getOriginalValue(); > TextEditGroup editGroup= getEditGroup(currEvent); >@@ -718,10 +729,18 @@ > } > return currPos; > } >+ public final int rewriteList(ASTNode parent, StructuralPropertyDescriptor property, int offset, String keyword) { >+ return rewriteList(parent, property, keyword, null, offset); >+ } > > protected void updateIndent(int prevMark, int originalOffset, int nodeIndex, TextEditGroup editGroup) { > // Do nothing. > } >+ >+ public final int rewriteList(ASTNode parent, StructuralPropertyDescriptor property, int offset, String keyword, String endKeyword, String separator) { >+ this.constantSeparator= separator; >+ return rewriteList(parent, property, keyword, endKeyword, offset); >+ } > } > > private int rewriteRequiredNode(ASTNode parent, StructuralPropertyDescriptor property) { >@@ -1141,6 +1160,14 @@ > return true; > } > >+ private int rewriteNodeList(ASTNode parent, StructuralPropertyDescriptor property, int pos, String keyword, String endKeyword, String separator) { >+ RewriteEvent event= getEvent(parent, property); >+ if (event != null && event.getChangeKind() != RewriteEvent.UNCHANGED) { >+ return new ListRewriter().rewriteList(parent, property, pos, keyword, endKeyword, separator); >+ } >+ return doVisit(parent, property, pos); >+ } >+ > private int rewriteNodeList(ASTNode parent, StructuralPropertyDescriptor property, int pos, String keyword, String separator) { > RewriteEvent event= getEvent(parent, property); > if (event != null && event.getChangeKind() != RewriteEvent.UNCHANGED) { >@@ -1233,6 +1260,21 @@ > return pos; > } > >+ /* >+ * Next token is try keyword. Returns the offset after 'try' keyword. For incomplete code, return the start offset. >+ */ >+ private int getPosAfterTry(int pos) { >+ try { >+ int nextToken= getScanner().readNext(pos, true); >+ if (nextToken == TerminalTokens.TokenNametry) { >+ return getScanner().getCurrentEndOffset(); >+ } >+ } catch (CoreException e) { >+ handleException(e); >+ } >+ return pos; >+ } >+ > final int getIndent(int offset) { > return this.formatter.computeIndentUnits(getIndentOfLine(offset)); > } >@@ -3050,7 +3092,10 @@ > int pos= node.getStartPosition(); > if (node.getAST().apiLevel() >= AST.JLS4) { > if (isChanged(node, TryStatement.RESOURCES_PROPERTY)) { >- pos= rewriteNodeList(node, TryStatement.RESOURCES_PROPERTY, pos, Util.EMPTY_STRING, "; "); //$NON-NLS-1$ >+ int indent= getIndent(node.getStartPosition()); >+ String prefix= this.formatter.TRY_RESOURCES.getPrefix(indent); >+ String newParen = this.formatter.TRY_RESOURCES_PAREN.getPrefix(indent) + "("; //$NON-NLS-1$ >+ pos= rewriteNodeList(node, TryStatement.RESOURCES_PROPERTY, getPosAfterTry(pos), newParen, ")", ";" + prefix); //$NON-NLS-1$ //$NON-NLS-2$ > } else { > pos= doVisit(node, TryStatement.RESOURCES_PROPERTY, pos); > } >Index: dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteFormatter.java >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteFormatter.java,v >retrieving revision 1.28 >diff -u -r1.28 ASTRewriteFormatter.java >--- dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteFormatter.java 17 Mar 2010 16:09:16 -0000 1.28 >+++ dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteFormatter.java 7 Jul 2011 17:29:56 -0000 >@@ -1,10 +1,14 @@ > /******************************************************************************* >- * Copyright (c) 2000, 2010 IBM Corporation and others. >+ * Copyright (c) 2000, 2011 IBM Corporation and others. > * All rights reserved. This program and the accompanying materials > * are made available under the terms of the Eclipse Public License v1.0 > * which accompanies this distribution, and is available at > * http://www.eclipse.org/legal/epl-v10.html > * >+ * This is an implementation of an early-draft specification developed under the Java >+ * Community Process (JCP) and is made available for testing and evaluation purposes >+ * only. The code is not compatible with any specification of the JCP. >+ * > * Contributors: > * IBM Corporation - initial API and implementation > *******************************************************************************/ >@@ -23,6 +27,7 @@ > import org.eclipse.jdt.core.dom.Expression; > import org.eclipse.jdt.core.dom.Statement; > import org.eclipse.jdt.core.formatter.CodeFormatter; >+import org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants; > import org.eclipse.jdt.core.formatter.IndentManipulation; > import org.eclipse.jface.text.BadLocationException; > import org.eclipse.jface.text.BadPositionCategoryException; >@@ -126,10 +131,15 @@ > public ASTRewriteFormatter(NodeInfoStore placeholders, RewriteEventStore eventStore, Map options, String lineDelimiter) { > this.placeholders= placeholders; > this.eventStore= eventStore; >- >+ > this.options= options; >+ if (options != null) { >+ this.options.put( >+ DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_RESOURCES_IN_TRY, >+ DefaultCodeFormatterConstants.createAlignmentValue(true, DefaultCodeFormatterConstants.WRAP_NEXT_PER_LINE, DefaultCodeFormatterConstants.INDENT_DEFAULT)); >+ } > this.lineDelimiter= lineDelimiter; >- >+ > this.tabWidth= IndentManipulation.getTabWidth(options); > this.indentWidth= IndentManipulation.getIndentWidth(options); > } >@@ -543,7 +553,9 @@ > public final Prefix FIRST_ENUM_CONST= new FormattingPrefix("enum E { X;}", "{ X" , CodeFormatter.K_COMPILATION_UNIT); //$NON-NLS-1$ //$NON-NLS-2$ > public final Prefix ANNOTATION_SEPARATION= new FormattingPrefix("@A @B class C {}", "A @" , CodeFormatter.K_COMPILATION_UNIT); //$NON-NLS-1$ //$NON-NLS-2$ > public final Prefix PARAM_ANNOTATION_SEPARATION= new FormattingPrefix("void foo(@A @B p) { }", "A @" , CodeFormatter.K_CLASS_BODY_DECLARATIONS); //$NON-NLS-1$ //$NON-NLS-2$ >- >+ public final Prefix TRY_RESOURCES = new FormattingPrefix("try (A a = new A(); B b = new B()) {}", "; B" , CodeFormatter.K_STATEMENTS); //$NON-NLS-1$ //$NON-NLS-2$ >+ public final Prefix TRY_RESOURCES_PAREN = new FormattingPrefix("try (A a = new A(); B b = new B()) {}", "y (" , CodeFormatter.K_STATEMENTS); //$NON-NLS-1$ //$NON-NLS-2$ >+ > public final BlockContext IF_BLOCK_WITH_ELSE= new BlockFormattingPrefixSuffix("if (true)", "else{}", 8); //$NON-NLS-1$ //$NON-NLS-2$ > public final BlockContext IF_BLOCK_NO_ELSE= new BlockFormattingPrefix("if (true)", 8); //$NON-NLS-1$ > public final BlockContext ELSE_AFTER_STATEMENT= new BlockFormattingPrefix("if (true) foo();else ", 15); //$NON-NLS-1$ >#P org.eclipse.jdt.core.tests.model >Index: src/org/eclipse/jdt/core/tests/dom/ASTConverter17Test.java >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/Attic/ASTConverter17Test.java,v >retrieving revision 1.1.2.14 >diff -u -r1.1.2.14 ASTConverter17Test.java >--- src/org/eclipse/jdt/core/tests/dom/ASTConverter17Test.java 28 Jun 2011 15:04:11 -0000 1.1.2.14 >+++ src/org/eclipse/jdt/core/tests/dom/ASTConverter17Test.java 7 Jul 2011 17:29:56 -0000 >@@ -317,7 +317,7 @@ > assertEquals("Not a simple type", ASTNode.SIMPLE_TYPE, type.getNodeType()); > checkSourceRange(type, "NumberFormatException", contents); > List resources = tryStatement.resources(); >- checkSourceRange((ASTNode) resources.get(0), "Reader r = new FileReader(s);", contents); >+ checkSourceRange((ASTNode) resources.get(0), "Reader r = new FileReader(s)", contents); > } > /* > * Check that catch type with union type as a simple type is converted to a simple type >@@ -350,8 +350,8 @@ > assertEquals("Not a simple type", ASTNode.SIMPLE_TYPE, type.getNodeType()); > checkSourceRange(type, "NumberFormatException", contents); > List resources = TryStatement.resources(); >- checkSourceRange((ASTNode) resources.get(0), "Reader r = new FileReader(s);", contents); >- checkSourceRange((ASTNode) resources.get(1), "Reader r2 = new FileReader(s);", contents); >+ checkSourceRange((ASTNode) resources.get(0), "Reader r = new FileReader(s)", contents); >+ checkSourceRange((ASTNode) resources.get(1), "Reader r2 = new FileReader(s)", contents); > } > /* > * Check that catch type with union type as a simple type is converted to a simple type >@@ -384,7 +384,7 @@ > assertEquals("Not a simple type", ASTNode.SIMPLE_TYPE, type.getNodeType()); > checkSourceRange(type, "NumberFormatException", contents); > List resources = tryStatement.resources(); >- checkSourceRange((ASTNode) resources.get(0), "Reader r = new FileReader(s);", contents); >+ checkSourceRange((ASTNode) resources.get(0), "Reader r = new FileReader(s)", contents); > checkSourceRange((ASTNode) resources.get(1), "Reader r2 = new FileReader(s)", contents); > } > /* >Index: src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingExpressionsTest.java >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingExpressionsTest.java,v >retrieving revision 1.19 >diff -u -r1.19 ASTRewritingExpressionsTest.java >--- src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingExpressionsTest.java 28 Apr 2009 17:49:28 -0000 1.19 >+++ src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingExpressionsTest.java 7 Jul 2011 17:29:56 -0000 >@@ -1,10 +1,14 @@ > /******************************************************************************* >- * Copyright (c) 2000, 2009 IBM Corporation and others. >+ * Copyright (c) 2000, 2011 IBM Corporation and others. > * All rights reserved. This program and the accompanying materials > * are made available under the terms of the Eclipse Public License v1.0 > * which accompanies this distribution, and is available at > * http://www.eclipse.org/legal/epl-v10.html > * >+ * This is an implementation of an early-draft specification developed under the Java >+ * Community Process (JCP) and is made available for testing and evaluation purposes >+ * only. The code is not compatible with any specification of the JCP. >+ * > * Contributors: > * IBM Corporation - initial API and implementation > *******************************************************************************/ >@@ -307,6 +311,84 @@ > > } > >+ /** @deprecated using deprecated code */ >+ public void testArrayInitializer2() throws Exception { >+ IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null); >+ StringBuffer buf= new StringBuffer(); >+ buf.append("package test1;\n"); >+ buf.append("public class E {\n"); >+ buf.append(" public void foo() {\n"); >+ buf.append(" goo(new int[] { 1, 2, 3, },\n"); >+ buf.append(" new int[] { 1, 2, 3, },\n"); >+ buf.append(" new int[] { 1, 2, 3 });\n"); >+ buf.append(" }\n"); >+ buf.append("}\n"); >+ ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null); >+ >+ CompilationUnit astRoot= createAST(cu); >+ ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST()); >+ >+ AST ast= astRoot.getAST(); >+ >+ assertTrue("Parse errors", (astRoot.getFlags() & ASTNode.MALFORMED) == 0); >+ TypeDeclaration type= findTypeDeclaration(astRoot, "E"); >+ MethodDeclaration methodDecl= findMethodDeclaration(type, "foo"); >+ Block block= methodDecl.getBody(); >+ List statements= block.statements(); >+ assertTrue("Number of statements not 1", statements.size() == 1); >+ ExpressionStatement statement= (ExpressionStatement) statements.get(0); >+ MethodInvocation invocation= (MethodInvocation) statement.getExpression(); >+ List args= invocation.arguments(); >+ >+ { // remove first and last initializer expression >+ ArrayCreation arrayCreation= (ArrayCreation) args.get(0); >+ ArrayInitializer initializer= arrayCreation.getInitializer(); >+ >+ List expressions= initializer.expressions(); >+ assertTrue("Number of initializer expressions not 3", expressions.size() == 3); >+ >+ rewrite.remove((ASTNode) expressions.get(0), null); >+ rewrite.remove((ASTNode) expressions.get(2), null); >+ } >+ { // insert at second and last position >+ ArrayCreation arrayCreation= (ArrayCreation) args.get(1); >+ ArrayInitializer initializer= arrayCreation.getInitializer(); >+ >+ List expressions= initializer.expressions(); >+ assertTrue("Number of initializer expressions not 3", expressions.size() == 3); >+ >+ NumberLiteral literal2= ast.newNumberLiteral("11"); >+ rewrite.getListRewrite(initializer, ArrayInitializer.EXPRESSIONS_PROPERTY).insertLast(literal2, null); >+ >+ } >+ { // replace first and last initializer expression >+ ArrayCreation arrayCreation= (ArrayCreation) args.get(2); >+ ArrayInitializer initializer= arrayCreation.getInitializer(); >+ >+ List expressions= initializer.expressions(); >+ assertTrue("Number of initializer expressions not 3", expressions.size() == 3); >+ >+ NumberLiteral literal1= ast.newNumberLiteral("10"); >+ NumberLiteral literal2= ast.newNumberLiteral("11"); >+ >+ rewrite.replace((ASTNode) expressions.get(0), literal1, null); >+ rewrite.replace((ASTNode) expressions.get(2), literal2, null); >+ } >+ >+ String preview= evaluateRewrite(cu, rewrite); >+ >+ buf= new StringBuffer(); >+ buf.append("package test1;\n"); >+ buf.append("public class E {\n"); >+ buf.append(" public void foo() {\n"); >+ buf.append(" goo(new int[] { 2, },\n"); >+ buf.append(" new int[] { 1, 2, 3, 11, },\n"); >+ buf.append(" new int[] { 10, 2, 11 });\n"); >+ buf.append(" }\n"); >+ buf.append("}\n"); >+ assertEqualString(preview, buf.toString()); >+ >+ } > > /** @deprecated using deprecated code */ > public void testAssignment() throws Exception { >Index: src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingStatementsTest.java >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingStatementsTest.java,v >retrieving revision 1.33.4.3 >diff -u -r1.33.4.3 ASTRewritingStatementsTest.java >--- src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingStatementsTest.java 18 May 2011 04:35:43 -0000 1.33.4.3 >+++ src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingStatementsTest.java 7 Jul 2011 17:29:56 -0000 >@@ -48,6 +48,11 @@ > > public static Test suite() { > return buildModelTestSuite(THIS); >+// TestSuite suite= new Suite(THIS.getClass().getName()); >+// suite.addTest(new ASTRewritingStatementsTest("testTryStatementWithResources3")); >+// suite.addTest(new ASTRewritingStatementsTest("testTryStatementWithResources4")); >+// suite.addTest(new ASTRewritingStatementsTest("testTryStatementWithResources5")); >+// return suite; > } > > public void testInsert1() throws Exception { >@@ -4970,7 +4975,8 @@ > buf.append("package test1;\n"); > buf.append("public class E {\n"); > buf.append(" public void foo(int i) {\n"); >- buf.append(" try (Reader reader = null; Reader reader2 = null) {\n"); >+ buf.append(" try (Reader reader = null;\n"); >+ buf.append(" Reader reader2 = null) {\n"); > buf.append(" } catch (IOException e) {\n"); > buf.append(" } finally {\n"); > buf.append(" return;\n"); >@@ -5042,6 +5048,235 @@ > } > } > >+ /** >+ * https://bugs.eclipse.org/bugs/show_bug.cgi?id=351170 >+ */ >+ public void testTryStatementWithResources3() throws Exception { >+ >+ createProject("P_17", JavaCore.VERSION_1_7); >+ IPackageFragmentRoot currentSourceFolder = getPackageFragmentRoot("P_17", "src"); >+ >+ try { >+ IPackageFragment pack1 = currentSourceFolder.createPackageFragment("test0017", false, null); >+ StringBuffer buf = new StringBuffer(); >+ buf.append("package test0017;\n"); >+ buf.append("\n"); >+ buf.append("public class X {\n"); >+ buf.append(" void foo() {\n"); >+ buf.append(" FileReader reader1 = new FileReader(\"file1\");\n"); >+ buf.append(" try {\n"); >+ buf.append(" int ch;\n"); >+ buf.append(" while ((ch = reader1.read()) != -1) {\n"); >+ buf.append(" System.out.println(ch);\n"); >+ buf.append(" }\n"); >+ buf.append(" } finally {\n"); >+ buf.append(" }\n"); >+ buf.append(" }\n"); >+ buf.append("}"); >+ >+ ICompilationUnit cu = pack1.createCompilationUnit("X.java", buf.toString(), false, null); >+ CompilationUnit astRoot= createAST(AST.JLS4, cu, true, true); >+ AST ast= astRoot.getAST(); >+ ASTRewrite rewrite= ASTRewrite.create(ast); >+ >+ Block block = ((MethodDeclaration) ((TypeDeclaration) astRoot.types().get(0)).bodyDeclarations().get(0)).getBody(); >+ List statements = block.statements(); >+ Statement statement = (Statement) statements.get(1); >+ assertTrue(statement instanceof TryStatement); >+ >+ TryStatement tryStatement = (TryStatement) statement; >+ >+ VariableDeclarationStatement variableDeclarationStatement = (VariableDeclarationStatement) statements.get(0); >+ VariableDeclarationFragment fragment = (VariableDeclarationFragment) variableDeclarationStatement.fragments().get(0); >+ VariableDeclarationExpression newVariableDeclarationExpression = ast.newVariableDeclarationExpression( >+ (VariableDeclarationFragment) rewrite.createCopyTarget(fragment)); >+ newVariableDeclarationExpression.setType((Type) rewrite.createCopyTarget(variableDeclarationStatement.getType())); >+ >+ ListRewrite listRewrite = rewrite.getListRewrite(tryStatement, TryStatement.RESOURCES_PROPERTY); >+ listRewrite.insertLast(newVariableDeclarationExpression, null); >+ rewrite.remove(variableDeclarationStatement, null); >+ >+ Document document1= new Document(cu.getSource()); >+ TextEdit res= rewrite.rewriteAST(document1, null); >+ res.apply(document1); >+ String preview = document1.get(); >+ >+ buf= new StringBuffer(); >+ buf.append("package test0017;\n"); >+ buf.append("\n"); >+ buf.append("public class X {\n"); >+ buf.append(" void foo() {\n"); >+ buf.append(" try (FileReader reader1 = new FileReader(\"file1\")) {\n"); >+ buf.append(" int ch;\n"); >+ buf.append(" while ((ch = reader1.read()) != -1) {\n"); >+ buf.append(" System.out.println(ch);\n"); >+ buf.append(" }\n"); >+ buf.append(" } finally {\n"); >+ buf.append(" }\n"); >+ buf.append(" }\n"); >+ buf.append("}"); >+ assertEqualString(preview, buf.toString()); >+ } finally { >+ deleteProject("P_17"); >+ } >+ } >+ >+ /** >+ * https://bugs.eclipse.org/bugs/show_bug.cgi?id=351170 >+ */ >+ public void testTryStatementWithResources4() throws Exception { >+ >+ createProject("P_17", JavaCore.VERSION_1_7); >+ IPackageFragmentRoot currentSourceFolder = getPackageFragmentRoot("P_17", "src"); >+ >+ try { >+ IPackageFragment pack1 = currentSourceFolder.createPackageFragment("test0017", false, null); >+ StringBuffer buf = new StringBuffer(); >+ buf.append("package test0017;\n"); >+ buf.append("\n"); >+ buf.append("public class X {\n"); >+ buf.append(" void foo() {\n"); >+ buf.append(" try (FileReader reader1 = new FileReader(\"file1\")) {\n"); >+ buf.append(" int ch;\n"); >+ buf.append(" while ((ch = reader1.read()) != -1) {\n"); >+ buf.append(" System.out.println(ch);\n"); >+ buf.append(" }\n"); >+ buf.append(" } finally {\n"); >+ buf.append(" }\n"); >+ buf.append(" }\n"); >+ buf.append("}"); >+ >+ ICompilationUnit cu = pack1.createCompilationUnit("X.java", buf.toString(), false, null); >+ CompilationUnit astRoot= createAST(AST.JLS4, cu, true, true); >+ AST ast= astRoot.getAST(); >+ ASTRewrite rewrite= ASTRewrite.create(ast); >+ >+ Block block = ((MethodDeclaration) ((TypeDeclaration) astRoot.types().get(0)).bodyDeclarations().get(0)).getBody(); >+ List statements = block.statements(); >+ Statement statement = (Statement) statements.get(0); >+ assertTrue(statement instanceof TryStatement); >+ >+ TryStatement tryStatement = (TryStatement) statement; >+ >+ VariableDeclarationFragment fragment = ast.newVariableDeclarationFragment(); >+ fragment.setExtraDimensions(0); >+ fragment.setName(ast.newSimpleName("reader2")); >+ ClassInstanceCreation classInstanceCreation = ast.newClassInstanceCreation(); >+ classInstanceCreation.setType(ast.newSimpleType(ast.newSimpleName("FileReader"))); >+ StringLiteral literal = ast.newStringLiteral(); >+ literal.setLiteralValue("file2"); >+ classInstanceCreation.arguments().add(literal); >+ fragment.setInitializer(classInstanceCreation); >+ VariableDeclarationExpression newVariableDeclarationExpression = ast.newVariableDeclarationExpression(fragment); >+ newVariableDeclarationExpression.setType(ast.newSimpleType(ast.newSimpleName("FileReader"))); >+ >+ ListRewrite listRewrite = rewrite.getListRewrite(tryStatement, TryStatement.RESOURCES_PROPERTY); >+ listRewrite.insertLast(newVariableDeclarationExpression, null); >+ >+ Document document1= new Document(cu.getSource()); >+ TextEdit res= rewrite.rewriteAST(document1, null); >+ res.apply(document1); >+ String preview = document1.get(); >+ >+ buf= new StringBuffer(); >+ buf.append("package test0017;\n"); >+ buf.append("\n"); >+ buf.append("public class X {\n"); >+ buf.append(" void foo() {\n"); >+ buf.append(" try (FileReader reader1 = new FileReader(\"file1\");\n"); >+ buf.append(" FileReader reader2 = new FileReader(\"file2\")) {\n"); >+ buf.append(" int ch;\n"); >+ buf.append(" while ((ch = reader1.read()) != -1) {\n"); >+ buf.append(" System.out.println(ch);\n"); >+ buf.append(" }\n"); >+ buf.append(" } finally {\n"); >+ buf.append(" }\n"); >+ buf.append(" }\n"); >+ buf.append("}"); >+ assertEqualString(preview, buf.toString()); >+ } finally { >+ deleteProject("P_17"); >+ } >+ } >+ >+ /** >+ * https://bugs.eclipse.org/bugs/show_bug.cgi?id=351170 >+ */ >+ public void testTryStatementWithResources5() throws Exception { >+ >+ createProject("P_17", JavaCore.VERSION_1_7); >+ IPackageFragmentRoot currentSourceFolder = getPackageFragmentRoot("P_17", "src"); >+ >+ try { >+ IPackageFragment pack1 = currentSourceFolder.createPackageFragment("test0017", false, null); >+ StringBuffer buf = new StringBuffer(); >+ buf.append("package test0017;\n"); >+ buf.append("\n"); >+ buf.append("public class X {\n"); >+ buf.append(" void foo() {\n"); >+ buf.append(" try (FileReader reader1 = new FileReader(\"file1\");) {\n"); >+ buf.append(" int ch;\n"); >+ buf.append(" while ((ch = reader1.read()) != -1) {\n"); >+ buf.append(" System.out.println(ch);\n"); >+ buf.append(" }\n"); >+ buf.append(" } finally {\n"); >+ buf.append(" }\n"); >+ buf.append(" }\n"); >+ buf.append("}"); >+ >+ ICompilationUnit cu = pack1.createCompilationUnit("X.java", buf.toString(), false, null); >+ CompilationUnit astRoot= createAST(AST.JLS4, cu, true, true); >+ AST ast= astRoot.getAST(); >+ ASTRewrite rewrite= ASTRewrite.create(ast); >+ >+ Block block = ((MethodDeclaration) ((TypeDeclaration) astRoot.types().get(0)).bodyDeclarations().get(0)).getBody(); >+ List statements = block.statements(); >+ Statement statement = (Statement) statements.get(0); >+ assertTrue(statement instanceof TryStatement); >+ >+ TryStatement tryStatement = (TryStatement) statement; >+ >+ VariableDeclarationFragment fragment = ast.newVariableDeclarationFragment(); >+ fragment.setExtraDimensions(0); >+ fragment.setName(ast.newSimpleName("reader2")); >+ ClassInstanceCreation classInstanceCreation = ast.newClassInstanceCreation(); >+ classInstanceCreation.setType(ast.newSimpleType(ast.newSimpleName("FileReader"))); >+ StringLiteral literal = ast.newStringLiteral(); >+ literal.setLiteralValue("file2"); >+ classInstanceCreation.arguments().add(literal); >+ fragment.setInitializer(classInstanceCreation); >+ VariableDeclarationExpression newVariableDeclarationExpression = ast.newVariableDeclarationExpression(fragment); >+ newVariableDeclarationExpression.setType(ast.newSimpleType(ast.newSimpleName("FileReader"))); >+ >+ ListRewrite listRewrite = rewrite.getListRewrite(tryStatement, TryStatement.RESOURCES_PROPERTY); >+ listRewrite.insertLast(newVariableDeclarationExpression, null); >+ >+ Document document1= new Document(cu.getSource()); >+ TextEdit res= rewrite.rewriteAST(document1, null); >+ res.apply(document1); >+ String preview = document1.get(); >+ >+ buf= new StringBuffer(); >+ buf.append("package test0017;\n"); >+ buf.append("\n"); >+ buf.append("public class X {\n"); >+ buf.append(" void foo() {\n"); >+ buf.append(" try (FileReader reader1 = new FileReader(\"file1\");\n"); >+ buf.append(" FileReader reader2 = new FileReader(\"file2\");) {\n"); >+ buf.append(" int ch;\n"); >+ buf.append(" while ((ch = reader1.read()) != -1) {\n"); >+ buf.append(" System.out.println(ch);\n"); >+ buf.append(" }\n"); >+ buf.append(" } finally {\n"); >+ buf.append(" }\n"); >+ buf.append(" }\n"); >+ buf.append("}"); >+ assertEqualString(preview, buf.toString()); >+ } finally { >+ deleteProject("P_17"); >+ } >+ } >+ > /** @deprecated using deprecated code */ > public void testTypeDeclarationStatement() throws Exception { > IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null); >Index: src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingTest.java >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingTest.java,v >retrieving revision 1.27.2.1 >diff -u -r1.27.2.1 ASTRewritingTest.java >--- src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingTest.java 19 Mar 2011 16:00:08 -0000 1.27.2.1 >+++ src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingTest.java 7 Jul 2011 17:29:56 -0000 >@@ -109,9 +109,13 @@ > return createAST(AST.JLS3, cu, statementsRecovery); > } > protected CompilationUnit createAST(int JLSLevel, ICompilationUnit cu, boolean statementsRecovery) { >+ return createAST(JLSLevel, cu, false, statementsRecovery); >+ } >+ >+ protected CompilationUnit createAST(int JLSLevel, ICompilationUnit cu, boolean resolveBindings, boolean statementsRecovery) { > ASTParser parser= ASTParser.newParser(JLSLevel); > parser.setSource(cu); >- parser.setResolveBindings(false); >+ parser.setResolveBindings(resolveBindings); > parser.setStatementsRecovery(statementsRecovery); > return (CompilationUnit) parser.createAST(null); > }
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
Attachments on
bug 351170
:
199130
|
199198
|
199200
| 199278 |
200297
|
200302