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 54367 Details for
Bug 165525
[comments] ASTParser excludes trailing line comments from extended range of fields in enums
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Requests
|
Help
|
Log In
[x]
|
Terms of Use
|
Copyright Agent
Test case illustrating the problem
ASTEnumFieldsProblem.java (text/plain), 4.44 KB, created by
Dmitry Denisov
on 2006-11-22 16:18:48 EST
(
hide
)
Description:
Test case illustrating the problem
Filename:
MIME Type:
Creator:
Dmitry Denisov
Created:
2006-11-22 16:18:48 EST
Size:
4.44 KB
patch
obsolete
>package org.eclipse.emf.test.tools.merger; > >import java.util.Hashtable; > >import junit.framework.TestCase; > >import org.eclipse.jdt.core.JavaCore; >import org.eclipse.jdt.core.dom.AST; >import org.eclipse.jdt.core.dom.ASTParser; >import org.eclipse.jdt.core.dom.AbstractTypeDeclaration; >import org.eclipse.jdt.core.dom.CompilationUnit; >import org.eclipse.jdt.core.dom.FieldDeclaration; >import org.eclipse.jdt.core.dom.VariableDeclarationFragment; >import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; >import org.eclipse.jdt.core.dom.rewrite.ListRewrite; >import org.eclipse.jface.text.Document; >import org.eclipse.text.edits.TextEdit; > >/** > * Test that shows the difference in assigning line comments in enum and > * class declarations. > */ >public class ASTEnumFieldsProblem extends TestCase >{ > protected String enumSource = > "enum X\n" + > "{" + > "\tENUM_CONST_1; // line comment after ENUM_CONST_1\n" + > "\tString a; //$NON-NLS-1$\n" + > "\tString b; //$NON-NLS-2$\n" + > "}\n"; > > protected String classSource = > "class X\n" + > "{" + > "\tString a; //$NON-NLS-1$\n" + > "\tString b; //$NON-NLS-2$\n" + > "}\n"; > > protected String expectedEnumSource = > "enum X\n" + > "{" + > "\tENUM_CONST_1; // line comment after ENUM_CONST_1\n" + > "\tString a; //$NON-NLS-1$\n" + > "\tString b; //$NON-NLS-2$\n" + > "\tint MISSING;\n" + > "}\n"; > > protected String expectedClassSource = > "class X\n" + > "{" + > "\tString a; //$NON-NLS-1$\n" + > "\tString b; //$NON-NLS-2$\n" + > "\tint MISSING;\n" + > "}\n"; > > protected CompilationUnit enumCompilationUnit; > protected CompilationUnit classCompilationUnit; > > @Override > protected void setUp() throws Exception > { > adjustCompilerOptions(); > enumCompilationUnit = parse(enumSource); > classCompilationUnit = parse(classSource); > } > > public void testReadEnumFieldDeclarations() > { > testReadFields(enumCompilationUnit, enumSource); > } > > public void testReadClassFieldDeclarations() > { > testReadFields(classCompilationUnit, classSource); > } > > public void testAddEnumField() throws Exception > { > assertEquals(expectedEnumSource, testAddField(enumCompilationUnit, enumSource)); > } > > public void testAddClassField() throws Exception > { > assertEquals(expectedClassSource, testAddField(classCompilationUnit, classSource)); > } > > protected void testReadFields(CompilationUnit cu, String source) > { > AbstractTypeDeclaration td = (AbstractTypeDeclaration) cu.types().get(0); > > FieldDeclaration field1 = (FieldDeclaration) td.bodyDeclarations().get(0); > FieldDeclaration field2 = (FieldDeclaration) td.bodyDeclarations().get(1); > > String field1Source = source.substring(cu.getExtendedStartPosition(field1), cu.getExtendedStartPosition(field1) + cu.getExtendedLength(field1)); > String field2Source = source.substring(cu.getExtendedStartPosition(field2), cu.getExtendedStartPosition(field2) + cu.getExtendedLength(field2)); > > assertEquals("String a; //$NON-NLS-1$", field1Source); > // this fails for enum since line comment is not a part of the last field > assertEquals("String b; //$NON-NLS-2$", field2Source); > } > > protected String testAddField(CompilationUnit cu, String source) throws Exception > { > ASTRewrite rewriter = ASTRewrite.create(cu.getAST()); > > AbstractTypeDeclaration td = (AbstractTypeDeclaration) cu.types().get(0); > > ListRewrite lrw = rewriter.getListRewrite(td, td.getBodyDeclarationsProperty()); > > VariableDeclarationFragment newFragment = cu.getAST().newVariableDeclarationFragment(); > FieldDeclaration newField = cu.getAST().newFieldDeclaration(newFragment); > > // for enum, new field is inserted before the line comment of the last field > lrw.insertLast(newField, null); > > Document doc = new Document(source); > TextEdit edit = rewriter.rewriteAST(doc, null); > edit.apply(doc); > > return doc.get(); > } > > protected CompilationUnit parse(String source) > { > ASTParser parser = ASTParser.newParser(AST.JLS3); > parser.setSource(source.toCharArray()); > return (CompilationUnit) parser.createAST(null); > } > > protected void adjustCompilerOptions() > { > Hashtable options = JavaCore.getOptions(); > options.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_5); > JavaCore.setOptions(options); > } >}
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 Raw
Actions:
View
Attachments on
bug 165525
:
54367
|
54427
|
54468