Bug 426967 - [1.8][quick assist] Multiple quick assists produce incorrect result with UnionType having type annotations in CatchClause
Summary: [1.8][quick assist] Multiple quick assists produce incorrect result with Unio...
Status: ASSIGNED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 4.4   Edit
Hardware: All All
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: Noopur Gupta CLA
QA Contact:
URL:
Whiteboard: stalebug
Keywords:
Depends on:
Blocks:
 
Reported: 2014-01-30 02:06 EST by Noopur Gupta CLA
Modified: 2023-07-28 14:20 EDT (History)
4 users (show)

See Also:


Attachments
AST View screenshot of catch clause (34.10 KB, image/png)
2014-01-30 02:06 EST, Noopur Gupta CLA
no flags Details
Patch + Tests (25.81 KB, patch)
2014-03-03 12:01 EST, Noopur Gupta CLA
no flags Details | Diff
Patch + Tests (59.33 KB, patch)
2014-03-03 12:29 EST, Noopur Gupta CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Noopur Gupta CLA 2014-01-30 02:06:59 EST
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.
Comment 1 Markus Keller CLA 2014-02-04 06:11:04 EST
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
Comment 2 Noopur Gupta CLA 2014-02-28 04:55:42 EST
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)
...
Comment 3 Noopur Gupta CLA 2014-03-03 12:01:28 EST
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.
Comment 4 Noopur Gupta CLA 2014-03-03 12:29:15 EST
Created attachment 240477 [details]
Patch + Tests

I had created the previous patch via Team -> Create Patch...
It did not include the newly added files.
Comment 5 Noopur Gupta CLA 2014-03-03 12:35:18 EST
(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.
Comment 6 Noopur Gupta CLA 2014-04-29 08:42:00 EDT
(In reply to Noopur Gupta from comment #2)
> Found one more bug while working on this..
Fixed with bug 433754.
Comment 7 Eclipse Genie CLA 2021-08-06 07:55:31 EDT
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.
Comment 8 Eclipse Genie CLA 2023-07-28 14:20:01 EDT
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.