Community
Participate
Working Groups
- start with CompilationUnit ( ... package foo; ...") - create a new MarkerAnnotation using ast.newMarkerAnnotation() - set the type name( "javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter" in this case ) - add the annotation using PackageDeclaration.getAnnotations().add( <the marker annotation> ) - then generate TextEdits using astRoot.rewrite(doc, compilationUnit.getJavaProject().getOptions(true)); When applying the TextEdits using edits.apply(doc, TextEdit.UPDATE_REGIONS) the resulting document is written ( ... @javax.xml.bind.annotation.adapters.XmlJavaTypeAdapterpackage foo; ...) That is, there is no space inserted between the annotation and the package declaration in the way that it is for classes, fields, or methods.
I'm bumping the severity of this. It prevents our ability to add annotations programmaticaly to package declarations, as the package declaration is unparseable afterward.
Could you please provide a small test case that shows the problem so I can see how you are using the ASTRewrite? Thanks.
Ok, I reproduced the issue. Investigating.
Created attachment 181528 [details] Proposed fix + regression tests
Released in HEAD. Added regression tests in: org.eclipse.jdt.core.tests.rewrite.modifying.ASTRewritingModifyingInsertTest#test0016 org.eclipse.jdt.core.tests.rewrite.describing.ASTRewritingPackageDeclTest
Daniel, I think this should be backported to 3.6.2.
Fantastic! Thanks for the quick turnaround!
Could you please check that the fix is working for you ?
Paul, please confirm that the fix works for you. Olivier, shouldn't we add an else branch where we call rewriteModifiers(...)? At least the other *Declaration visit methods do so.
(In reply to comment #9) > Olivier, shouldn't we add an else branch where we call rewriteModifiers(...)? > At least the other *Declaration visit methods do so. I don't think so as the other calls also needs to handle the old getModifiers() call. Package declaration's annotations are only available in JLS3 so there is no need to try to handle the call if the JLS level < JLS3.
(In reply to comment #8) > Could you please check that the fix is working for you ? Yes it is. In fact, there is also a return/new line after the new annotation, which is a very nice plus. Thanks.
> Package declaration's annotations are only available in JLS3 so there is no > need to try to handle the call if the JLS level < JLS3. True. +1 for 3.6.2.
Verified for 3.7M3 using build I20101025-0901 Not marking it verified as this has to be fixed for 3.6.2
Reopening to target 3.6.2.
(In reply to comment #11) > Yes it is. In fact, there is also a return/new line after the new annotation, > which is a very nice plus. Thanks. It should respect the formatter settings which is to add a new line by default.
Released for 3.6.2. Same patch was applied on 3.6 maintenance stream.
Verified for 3.6.2 using build M20110119-0834