Community
Participate
Working Groups
Created attachment 239462 [details] AST View screenshot of catch clause package test.pack1; import java.io.FileNotFoundException; import java.lang.annotation.ElementType; import java.lang.annotation.Target; import javax.security.auth.login.AccountException; public class Snippet { void test(int a) { try { if (a==1) throw new @Marker1 AccountException(); else throw new @Marker2 FileNotFoundException(); } catch (@Marker1 AccountException | @Marker2 FileNotFoundException e) { } } } @Target(ElementType.TYPE_USE) @interface Marker1 { } @Target(ElementType.TYPE_USE) @interface Marker2 { } ---------------------------------------------------------------------- In the above example, place the caret at 'AccountException' in the catch clause and press Ctrl+1. See the results with proposed quick assists: "Move exception to separate catch block", "Remove exception", "Replace exception with throws". The type annotation on 'AccountException' is not moved along with the exception. Example - result of "Move exception to separate catch block" : try { if (a==1) throw new @Marker1 AccountException(); else throw new @Marker2 FileNotFoundException(); } catch (@Marker1 @Marker2 FileNotFoundException e) { } catch (AccountException e) { } See bug 404719 and bug 418347. Attached the screenshot of the AST View of catch clause.
That's the AST structure we have to live with, see bug 404719 comment 1. The best solution I see is to create a helper method that takes care of such problems and then use that everywhere. E.g. for code like this (from QuickAssistProcessor#getPickoutTypeFromMulticatchProposals(..)): types.add((Type) rewrite.createCopyTarget(typeNode)); rewrite.remove(typeNode, null); ... the solution could look something like this: types.add(createCopyAndMoveTypeAnnotations( rewrite, (Type) typeNode)); rewrite.remove(typeNode, null); ... with a new helper method: Type createCopyAndMoveTypeAnnotations(ASTRewrite rewrite, Type typeNode) The helper method could go to StubUtility2, or to a new helper class org.eclipse.jdt.internal.corext.dom.DeclarationRewrite. The implementation can inspect typeNode.getLocationInParent() and see if the typeNode is the type of a declaration. In that case, type annotations that belong to the given typeNode can also be part of the declaration's modifiers list. If type annotations exist, then they are removed from their old location and instead added to the copied typeNode. Note that you can't just add annotations to the result of ASTRewrite#createCopyTarget(..). Possible solutions (these lose the original formatting and interspersed comments, so don't do this unless necessary): - use ASTNode#copySubtree(AST, ASTNode) - use ImportRewrite with an ITypeBinding to create a new type node that already contains the type annotations
Found one more bug while working on this which will be fixed along with this bug: class E { private void foo() { for (String str : new String[1]) { } } } Press Ctrl+1 at "str", we get the exception: java.lang.ClassCastException: org.eclipse.jdt.core.dom.SimpleType cannot be cast to org.eclipse.jdt.core.dom.ArrayType at org.eclipse.jdt.internal.ui.text.correction.QuickAssistProcessor.getConvertEnhancedForLoopProposal(QuickAssistProcessor.java:2588) at org.eclipse.jdt.internal.ui.text.correction.QuickAssistProcessor.getAssists(QuickAssistProcessor.java:281) at org.eclipse.jdt.internal.ui.text.correction.JavaCorrectionProcessor$SafeAssistCollector.safeRun(JavaCorrectionProcessor.java:403) at org.eclipse.jdt.internal.ui.text.correction.JavaCorrectionProcessor$SafeCorrectionProcessorAccess.run(JavaCorrectionProcessor.java:339) ...
Created attachment 240476 [details] Patch + Tests Added helper methods to a new class DeclarationRewrite and updated the copy creation of Type with the helper methods at multiple places, wherever seemed appropriate (ignored the places where all the declaration's modifiers were being copied to the new location). Also, added tests for the same. Some of the newly added tests in the patch currently fail due to some formatting issues (newlines and spaces with annotations). Markus, please run the tests and have a look to see if we need to fix the formatting issues or the tests.
Created attachment 240477 [details] Patch + Tests I had created the previous patch via Team -> Create Patch... It did not include the newly added files.
(In reply to Noopur Gupta from comment #4) > I had created the previous patch via Team -> Create Patch... > It did not include the newly added files. It missed all the test files as I selected "org.eclipse.jdt.ui" and invoked Team -> Create Patch.
(In reply to Noopur Gupta from comment #2) > Found one more bug while working on this.. Fixed with bug 433754.
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.