View | Details | Raw Unified | Return to bug 426967 | Differences between
and this patch

Collapse All | Expand All

(-)a/org.eclipse.jdt.ui/core (+170 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2014 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * This is an implementation of an early-draft specification developed under the Java
9
 * Community Process (JCP) and is made available for testing and evaluation purposes
10
 * only. The code is not compatible with any specification of the JCP.
11
 *
12
 * Contributors:
13
 *     IBM Corporation - initial API and implementation
14
 *******************************************************************************/
15
package org.eclipse.jdt.internal.corext.dom;
16
17
import java.util.ArrayList;
18
import java.util.Arrays;
19
import java.util.Collections;
20
import java.util.List;
21
22
import org.eclipse.text.edits.TextEditGroup;
23
24
import org.eclipse.jdt.core.dom.ASTNode;
25
import org.eclipse.jdt.core.dom.AnnotatableType;
26
import org.eclipse.jdt.core.dom.Annotation;
27
import org.eclipse.jdt.core.dom.AnnotationTypeMemberDeclaration;
28
import org.eclipse.jdt.core.dom.ArrayType;
29
import org.eclipse.jdt.core.dom.FieldDeclaration;
30
import org.eclipse.jdt.core.dom.IAnnotationBinding;
31
import org.eclipse.jdt.core.dom.IExtendedModifier;
32
import org.eclipse.jdt.core.dom.ITypeBinding;
33
import org.eclipse.jdt.core.dom.MethodDeclaration;
34
import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
35
import org.eclipse.jdt.core.dom.StructuralPropertyDescriptor;
36
import org.eclipse.jdt.core.dom.Type;
37
import org.eclipse.jdt.core.dom.UnionType;
38
import org.eclipse.jdt.core.dom.VariableDeclarationExpression;
39
import org.eclipse.jdt.core.dom.VariableDeclarationStatement;
40
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
41
42
import org.eclipse.jdt.internal.corext.util.JDTUIHelperClasses;
43
44
/**
45
 * Rewrite helper for type annotations part of the declaration's modifiers of a type reference.
46
 * 
47
 * @see JDTUIHelperClasses
48
 * @since 3.9 BETA_JAVA8
49
 */
50
public class DeclarationRewrite {
51
52
	/**
53
	 * Creates a copy of the given <code>typeNode</code> and adds its type annotations to the copied
54
	 * type by removing them from its declaration's modifiers.
55
	 * 
56
	 * @param typeNode the Type node
57
	 * @param rewrite the ASTRewrite with which to create new nodes
58
	 * @param editGroup the edit group in which to collect the corresponding text edits, or null if
59
	 *            ungrouped
60
	 * @return copy of the type node with added type annotations
61
	 */
62
	public static Type copyTypeAndMoveTypeAnnotations(Type typeNode, ASTRewrite rewrite, TextEditGroup editGroup) {
63
		Type copiedtypeNode= copyTypeAndAddTypeAnnotations(typeNode, rewrite);
64
		removeTypeAnnotationsFromDeclaration(typeNode, rewrite, editGroup);
65
		return copiedtypeNode;
66
	}
67
68
	/**
69
	 * Creates a copy of the given <code>typeNode</code> and adds its type annotations to the copied
70
	 * type by copying them from its declaration's modifiers. The type annotations are not removed
71
	 * from the declaration node.
72
	 * 
73
	 * @param typeNode the Type node
74
	 * @param rewrite the ASTRewrite with which to create new nodes
75
	 * @return copy of the type node with added type annotations
76
	 */
77
	public static Type copyTypeAndAddTypeAnnotations(Type typeNode, ASTRewrite rewrite) {
78
		if (!(typeNode instanceof AnnotatableType || (typeNode instanceof ArrayType && ((ArrayType) typeNode).getElementType() instanceof AnnotatableType)))
79
			return (Type) rewrite.createCopyTarget(typeNode);
80
81
		List<Annotation> typeAnnotations= DeclarationRewrite.getTypeAnnotationsOnDeclaration(typeNode);
82
		if (typeAnnotations.isEmpty())
83
			return (Type) rewrite.createCopyTarget(typeNode);
84
85
		Type copiedTypeNode= (Type) ASTNode.copySubtree(rewrite.getAST(), typeNode);
86
		AnnotatableType copiedAnnotatableType;
87
		if (copiedTypeNode instanceof ArrayType) {
88
			copiedAnnotatableType= (AnnotatableType) ((ArrayType) copiedTypeNode).getElementType();
89
		} else {
90
			copiedAnnotatableType= (AnnotatableType) copiedTypeNode;
91
		}
92
		for (Annotation annotation : typeAnnotations) {
93
			copiedAnnotatableType.annotations().add(rewrite.createCopyTarget(annotation));
94
		}
95
		return copiedTypeNode;
96
	}
97
98
	/**
99
	 * Removes type annotations of the given <code>typeNode</code> from its declaration's modifiers.
100
	 * 
101
	 * @param typeNode the Type node
102
	 * @param rewrite rewrite that removes the nodes
103
	 * @param editGroup the edit group in which to collect the corresponding text edits, or null if
104
	 *            ungrouped
105
	 */
106
	public static void removeTypeAnnotationsFromDeclaration(Type typeNode, ASTRewrite rewrite, TextEditGroup editGroup) {
107
		List<Annotation> typeAnnotations= DeclarationRewrite.getTypeAnnotationsOnDeclaration(typeNode);
108
		for (Annotation annotation : typeAnnotations) {
109
			rewrite.remove(annotation, editGroup);
110
		}
111
	}
112
113
	private static List<Annotation> getTypeAnnotationsOnDeclaration(Type typeNode) {
114
		ITypeBinding typeBinding= typeNode.resolveBinding();
115
		if (typeBinding == null)
116
			return Collections.emptyList();
117
118
		List<IAnnotationBinding> typeAnnotationBindings;
119
		if (typeBinding.isArray()) {
120
			typeAnnotationBindings= Arrays.asList(typeBinding.getElementType().getTypeAnnotations());
121
		} else {
122
			typeAnnotationBindings= Arrays.asList(typeBinding.getTypeAnnotations());
123
		}
124
125
		if (typeAnnotationBindings.size() == 0)
126
			return Collections.emptyList();
127
128
		List<IExtendedModifier> declarationModifiers= new ArrayList<IExtendedModifier>();
129
		StructuralPropertyDescriptor locationInParent= typeNode.getLocationInParent();
130
131
		if (locationInParent == UnionType.TYPES_PROPERTY) {
132
			typeNode= (UnionType) typeNode.getParent();
133
			locationInParent= typeNode.getLocationInParent();
134
		}
135
136
		if (locationInParent == ArrayType.ELEMENT_TYPE_PROPERTY) {
137
			typeNode= (ArrayType) typeNode.getParent();
138
			locationInParent= typeNode.getLocationInParent();
139
		}
140
141
		if (locationInParent == MethodDeclaration.RETURN_TYPE2_PROPERTY) {
142
			declarationModifiers= ((MethodDeclaration) typeNode.getParent()).modifiers();
143
		} else if (locationInParent == FieldDeclaration.TYPE_PROPERTY) {
144
			declarationModifiers= ((FieldDeclaration) typeNode.getParent()).modifiers();
145
		} else if (locationInParent == VariableDeclarationExpression.TYPE_PROPERTY) {
146
			declarationModifiers= ((VariableDeclarationExpression) typeNode.getParent()).modifiers();
147
		} else if (locationInParent == VariableDeclarationStatement.TYPE_PROPERTY) {
148
			declarationModifiers= ((VariableDeclarationStatement) typeNode.getParent()).modifiers();
149
		} else if (locationInParent == SingleVariableDeclaration.TYPE_PROPERTY) {
150
			declarationModifiers= ((SingleVariableDeclaration) typeNode.getParent()).modifiers();
151
		} else if (locationInParent == AnnotationTypeMemberDeclaration.TYPE_PROPERTY) {
152
			declarationModifiers= ((AnnotationTypeMemberDeclaration) typeNode.getParent()).modifiers();
153
		}
154
155
		return getTypeAnnotationsFromModifiers(declarationModifiers, typeAnnotationBindings);
156
	}
157
158
	private static List<Annotation> getTypeAnnotationsFromModifiers(List<IExtendedModifier> modifiers, List<IAnnotationBinding> typeAnnotationBindings) {
159
		List<Annotation> typeAnnotations= new ArrayList<Annotation>();
160
		for (IExtendedModifier modifier : modifiers) {
161
			if (modifier.isAnnotation()) {
162
				IAnnotationBinding modifierAnnotationBinding= ((Annotation) modifier).resolveAnnotationBinding();
163
				if (typeAnnotationBindings.contains(modifierAnnotationBinding)) {
164
					typeAnnotations.add((Annotation) modifier);
165
				}
166
			}
167
		}
168
		return typeAnnotations;
169
	}
170
}
(-)a/org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/dom/DimensionRewrite.java (-5 / +5 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2013 IBM Corporation and others.
2
 * Copyright (c) 2013, 2014 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 39-45 Link Here
39
public class DimensionRewrite {
39
public class DimensionRewrite {
40
40
41
	/**
41
	/**
42
	 * Creates a {@link ASTRewrite#createCopyTarget(ASTNode) copy} of <code>type</code>
42
	 * Creates a {@link DeclarationRewrite#copyTypeAndAddTypeAnnotations(Type, ASTRewrite) copy} of <code>type</code>
43
	 * and adds <code>extraDimensions</code> to it.
43
	 * and adds <code>extraDimensions</code> to it.
44
	 * 
44
	 * 
45
	 * @param type the type to copy
45
	 * @param type the type to copy
Lines 50-67 Link Here
50
	public static Type copyTypeAndAddDimensions(Type type, List<Dimension> extraDimensions, ASTRewrite rewrite) {
50
	public static Type copyTypeAndAddDimensions(Type type, List<Dimension> extraDimensions, ASTRewrite rewrite) {
51
		AST ast= rewrite.getAST();
51
		AST ast= rewrite.getAST();
52
		if (extraDimensions.isEmpty()) {
52
		if (extraDimensions.isEmpty()) {
53
			return (Type) rewrite.createCopyTarget(type);
53
			return DeclarationRewrite.copyTypeAndAddTypeAnnotations(type, rewrite);
54
		}
54
		}
55
		
55
		
56
		ArrayType result;
56
		ArrayType result;
57
		if (type instanceof ArrayType) {
57
		if (type instanceof ArrayType) {
58
			ArrayType arrayType= (ArrayType) type;
58
			ArrayType arrayType= (ArrayType) type;
59
			Type varElementType= (Type) rewrite.createCopyTarget(arrayType.getElementType());
59
			Type varElementType= DeclarationRewrite.copyTypeAndAddTypeAnnotations(arrayType.getElementType(), rewrite);
60
			result= ast.newArrayType(varElementType, 0);
60
			result= ast.newArrayType(varElementType, 0);
61
			result.dimensions().addAll(copyDimensions(extraDimensions, rewrite));
61
			result.dimensions().addAll(copyDimensions(extraDimensions, rewrite));
62
			result.dimensions().addAll(copyDimensions(arrayType.dimensions(), rewrite));
62
			result.dimensions().addAll(copyDimensions(arrayType.dimensions(), rewrite));
63
		} else {
63
		} else {
64
			Type elementType= (Type) rewrite.createCopyTarget(type);
64
			Type elementType= DeclarationRewrite.copyTypeAndAddTypeAnnotations(type, rewrite);
65
			result= ast.newArrayType(elementType, 0);
65
			result= ast.newArrayType(elementType, 0);
66
			result.dimensions().addAll(copyDimensions(extraDimensions, rewrite));
66
			result.dimensions().addAll(copyDimensions(extraDimensions, rewrite));
67
		}
67
		}
(-)a/org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/fix/UnusedCodeFix.java (-2 / +7 lines)
Lines 1-9 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2013 IBM Corporation and others.
2
 * Copyright (c) 2000, 2014 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * This is an implementation of an early-draft specification developed under the Java
9
 * Community Process (JCP) and is made available for testing and evaluation purposes
10
 * only. The code is not compatible with any specification of the JCP.
7
 *
11
 *
8
 * Contributors:
12
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
13
 *     IBM Corporation - initial API and implementation
Lines 63-68 Link Here
63
import org.eclipse.jdt.core.dom.rewrite.ListRewrite;
67
import org.eclipse.jdt.core.dom.rewrite.ListRewrite;
64
68
65
import org.eclipse.jdt.internal.corext.dom.ASTNodes;
69
import org.eclipse.jdt.internal.corext.dom.ASTNodes;
70
import org.eclipse.jdt.internal.corext.dom.DeclarationRewrite;
66
import org.eclipse.jdt.internal.corext.dom.LinkedNodeFinder;
71
import org.eclipse.jdt.internal.corext.dom.LinkedNodeFinder;
67
import org.eclipse.jdt.internal.corext.dom.NecessaryParenthesesChecker;
72
import org.eclipse.jdt.internal.corext.dom.NecessaryParenthesesChecker;
68
import org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite;
73
import org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite;
Lines 392-398 Link Here
392
					VariableDeclarationFragment movedFragment= (VariableDeclarationFragment) rewrite.createMoveTarget(currentFragment);
397
					VariableDeclarationFragment movedFragment= (VariableDeclarationFragment) rewrite.createMoveTarget(currentFragment);
393
					if (newDeclaration == null) {
398
					if (newDeclaration == null) {
394
						newDeclaration= rewrite.getAST().newVariableDeclarationStatement(movedFragment);
399
						newDeclaration= rewrite.getAST().newVariableDeclarationStatement(movedFragment);
395
						Type copiedType= (Type) rewrite.createCopyTarget(originalStatement.getType());
400
						Type copiedType= DeclarationRewrite.copyTypeAndAddTypeAnnotations(originalStatement.getType(), rewrite);
396
						newDeclaration.setType(copiedType);
401
						newDeclaration.setType(copiedType);
397
					} else
402
					} else
398
						newDeclaration.fragments().add(movedFragment);
403
						newDeclaration.fragments().add(movedFragment);
(-)a/org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/util/JDTUIHelperClasses.java (-1 / +7 lines)
Lines 1-9 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2013 IBM Corporation and others.
2
 * Copyright (c) 2013, 2014 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * This is an implementation of an early-draft specification developed under the Java
9
 * Community Process (JCP) and is made available for testing and evaluation purposes
10
 * only. The code is not compatible with any specification of the JCP.
7
 *
11
 *
8
 * Contributors:
12
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
13
 *     IBM Corporation - initial API and implementation
Lines 21-26 Link Here
21
import org.eclipse.jdt.internal.corext.dom.ASTNodes;
25
import org.eclipse.jdt.internal.corext.dom.ASTNodes;
22
import org.eclipse.jdt.internal.corext.dom.Bindings;
26
import org.eclipse.jdt.internal.corext.dom.Bindings;
23
import org.eclipse.jdt.internal.corext.dom.BodyDeclarationRewrite;
27
import org.eclipse.jdt.internal.corext.dom.BodyDeclarationRewrite;
28
import org.eclipse.jdt.internal.corext.dom.DeclarationRewrite;
24
import org.eclipse.jdt.internal.corext.dom.DimensionRewrite;
29
import org.eclipse.jdt.internal.corext.dom.DimensionRewrite;
25
import org.eclipse.jdt.internal.corext.dom.GenericVisitor;
30
import org.eclipse.jdt.internal.corext.dom.GenericVisitor;
26
import org.eclipse.jdt.internal.corext.dom.HierarchicalASTVisitor;
31
import org.eclipse.jdt.internal.corext.dom.HierarchicalASTVisitor;
Lines 106-111 Link Here
106
 * <li>{@link CompilationUnitRewrite}</li>
111
 * <li>{@link CompilationUnitRewrite}</li>
107
 * <li>{@link BodyDeclarationRewrite}</li>
112
 * <li>{@link BodyDeclarationRewrite}</li>
108
 * <li>{@link DimensionRewrite}</li>
113
 * <li>{@link DimensionRewrite}</li>
114
 * <li>{@link DeclarationRewrite}</li>
109
 * <li>{@link ModifierRewrite}</li>
115
 * <li>{@link ModifierRewrite}</li>
110
 * <li>{@link ReplaceRewrite}</li>
116
 * <li>{@link ReplaceRewrite}</li>
111
 * <li>{@link StatementRewrite}</li>
117
 * <li>{@link StatementRewrite}</li>
(-)a/org.eclipse.jdt.ui/core refactoring/org/eclipse/jdt/internal/corext/refactoring/code/PromoteTempToFieldRefactoring.java (-1 / +2 lines)
Lines 81-86 Link Here
81
import org.eclipse.jdt.internal.corext.codemanipulation.StubUtility;
81
import org.eclipse.jdt.internal.corext.codemanipulation.StubUtility;
82
import org.eclipse.jdt.internal.corext.dom.ASTNodeFactory;
82
import org.eclipse.jdt.internal.corext.dom.ASTNodeFactory;
83
import org.eclipse.jdt.internal.corext.dom.ASTNodes;
83
import org.eclipse.jdt.internal.corext.dom.ASTNodes;
84
import org.eclipse.jdt.internal.corext.dom.DeclarationRewrite;
84
import org.eclipse.jdt.internal.corext.dom.DimensionRewrite;
85
import org.eclipse.jdt.internal.corext.dom.DimensionRewrite;
85
import org.eclipse.jdt.internal.corext.dom.HierarchicalASTVisitor;
86
import org.eclipse.jdt.internal.corext.dom.HierarchicalASTVisitor;
86
import org.eclipse.jdt.internal.corext.dom.ModifierRewrite;
87
import org.eclipse.jdt.internal.corext.dom.ModifierRewrite;
Lines 862-868 Link Here
862
		FieldDeclaration fieldDeclaration= ast.newFieldDeclaration(fragment);
863
		FieldDeclaration fieldDeclaration= ast.newFieldDeclaration(fragment);
863
864
864
		VariableDeclarationStatement vds= getTempDeclarationStatement();
865
		VariableDeclarationStatement vds= getTempDeclarationStatement();
865
		Type type= (Type)rewrite.createCopyTarget(vds.getType());
866
		Type type= DeclarationRewrite.copyTypeAndAddTypeAnnotations(vds.getType(), rewrite);
866
		fieldDeclaration.setType(type);
867
		fieldDeclaration.setType(type);
867
		fieldDeclaration.modifiers().addAll(ASTNodeFactory.newModifiers(ast, getModifiers()));
868
		fieldDeclaration.modifiers().addAll(ASTNodeFactory.newModifiers(ast, getModifiers()));
868
		return fieldDeclaration;
869
		return fieldDeclaration;
(-)a/org.eclipse.jdt.ui/core refactoring/org/eclipse/jdt/internal/corext/refactoring/sef/SelfEncapsulateFieldRefactoring.java (-3 / +6 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2013 IBM Corporation and others.
2
 * Copyright (c) 2000, 2014 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 93-98 Link Here
93
import org.eclipse.jdt.internal.corext.dom.ASTNodeFactory;
93
import org.eclipse.jdt.internal.corext.dom.ASTNodeFactory;
94
import org.eclipse.jdt.internal.corext.dom.ASTNodes;
94
import org.eclipse.jdt.internal.corext.dom.ASTNodes;
95
import org.eclipse.jdt.internal.corext.dom.Bindings;
95
import org.eclipse.jdt.internal.corext.dom.Bindings;
96
import org.eclipse.jdt.internal.corext.dom.DeclarationRewrite;
96
import org.eclipse.jdt.internal.corext.dom.DimensionRewrite;
97
import org.eclipse.jdt.internal.corext.dom.DimensionRewrite;
97
import org.eclipse.jdt.internal.corext.dom.VariableDeclarationRewrite;
98
import org.eclipse.jdt.internal.corext.dom.VariableDeclarationRewrite;
98
import org.eclipse.jdt.internal.corext.refactoring.Checks;
99
import org.eclipse.jdt.internal.corext.refactoring.Checks;
Lines 625-636 Link Here
625
		result.setName(ast.newSimpleName(fSetterName));
626
		result.setName(ast.newSimpleName(fSetterName));
626
		result.modifiers().addAll(ASTNodeFactory.newModifiers(ast, createModifiers()));
627
		result.modifiers().addAll(ASTNodeFactory.newModifiers(ast, createModifiers()));
627
		if (fSetterMustReturnValue) {
628
		if (fSetterMustReturnValue) {
628
			result.setReturnType2((Type)rewriter.createCopyTarget(type));
629
			Type copiedType= DimensionRewrite.copyTypeAndAddDimensions(type, fFieldDeclaration.extraDimensions(), rewriter);
630
			result.setReturnType2(copiedType);
629
		}
631
		}
630
		SingleVariableDeclaration param= ast.newSingleVariableDeclaration();
632
		SingleVariableDeclaration param= ast.newSingleVariableDeclaration();
631
		result.parameters().add(param);
633
		result.parameters().add(param);
632
		param.setName(ast.newSimpleName(fArgName));
634
		param.setName(ast.newSimpleName(fArgName));
633
		param.setType((Type)rewriter.createCopyTarget(type));
635
		Type copiedType= DeclarationRewrite.copyTypeAndAddTypeAnnotations(type, rewriter);
636
		param.setType(copiedType);
634
		List<Dimension> extraDimensions= DimensionRewrite.copyDimensions(fFieldDeclaration.extraDimensions(), rewriter);
637
		List<Dimension> extraDimensions= DimensionRewrite.copyDimensions(fFieldDeclaration.extraDimensions(), rewriter);
635
		param.extraDimensions().addAll(extraDimensions);
638
		param.extraDimensions().addAll(extraDimensions);
636
639
(-)a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/LocalCorrectionsSubProcessor.java (-1 / +3 lines)
Lines 116-121 Link Here
116
import org.eclipse.jdt.internal.corext.dom.Bindings;
116
import org.eclipse.jdt.internal.corext.dom.Bindings;
117
import org.eclipse.jdt.internal.corext.dom.BodyDeclarationRewrite;
117
import org.eclipse.jdt.internal.corext.dom.BodyDeclarationRewrite;
118
import org.eclipse.jdt.internal.corext.dom.CodeScopeBuilder;
118
import org.eclipse.jdt.internal.corext.dom.CodeScopeBuilder;
119
import org.eclipse.jdt.internal.corext.dom.DeclarationRewrite;
119
import org.eclipse.jdt.internal.corext.dom.NecessaryParenthesesChecker;
120
import org.eclipse.jdt.internal.corext.dom.NecessaryParenthesesChecker;
120
import org.eclipse.jdt.internal.corext.dom.Selection;
121
import org.eclipse.jdt.internal.corext.dom.Selection;
121
import org.eclipse.jdt.internal.corext.dom.TypeRules;
122
import org.eclipse.jdt.internal.corext.dom.TypeRules;
Lines 318-324 Link Here
318
						UnionType newUnionType= ast.newUnionType();
319
						UnionType newUnionType= ast.newUnionType();
319
						List<Type> types= newUnionType.types();
320
						List<Type> types= newUnionType.types();
320
321
321
						types.add((Type) rewrite.createCopyTarget(type));
322
						Type copiedType= DeclarationRewrite.copyTypeAndMoveTypeAnnotations(type, rewrite, null);
323
						types.add(copiedType);
322
						for (int i= 0; i < uncaughtExceptions.length; i++) {
324
						for (int i= 0; i < uncaughtExceptions.length; i++) {
323
							ITypeBinding excBinding= uncaughtExceptions[i];
325
							ITypeBinding excBinding= uncaughtExceptions[i];
324
							Type type2= imports.addImport(excBinding, ast, importRewriteContext);
326
							Type type2= imports.addImport(excBinding, ast, importRewriteContext);
(-)a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/QuickAssistProcessor.java (-14 / +22 lines)
Lines 121-126 Link Here
121
import org.eclipse.jdt.internal.corext.dom.ASTNodeFactory;
121
import org.eclipse.jdt.internal.corext.dom.ASTNodeFactory;
122
import org.eclipse.jdt.internal.corext.dom.ASTNodes;
122
import org.eclipse.jdt.internal.corext.dom.ASTNodes;
123
import org.eclipse.jdt.internal.corext.dom.Bindings;
123
import org.eclipse.jdt.internal.corext.dom.Bindings;
124
import org.eclipse.jdt.internal.corext.dom.DeclarationRewrite;
124
import org.eclipse.jdt.internal.corext.dom.DimensionRewrite;
125
import org.eclipse.jdt.internal.corext.dom.DimensionRewrite;
125
import org.eclipse.jdt.internal.corext.dom.LinkedNodeFinder;
126
import org.eclipse.jdt.internal.corext.dom.LinkedNodeFinder;
126
import org.eclipse.jdt.internal.corext.dom.ModifierRewrite;
127
import org.eclipse.jdt.internal.corext.dom.ModifierRewrite;
Lines 951-957 Link Here
951
			VariableDeclarationExpression oldVarDecl= (VariableDeclarationExpression) fragParent;
952
			VariableDeclarationExpression oldVarDecl= (VariableDeclarationExpression) fragParent;
952
953
953
			VariableDeclarationStatement newVarDec= ast.newVariableDeclarationStatement(newFrag);
954
			VariableDeclarationStatement newVarDec= ast.newVariableDeclarationStatement(newFrag);
954
			newVarDec.setType((Type) rewrite.createCopyTarget(oldVarDecl.getType()));
955
			Type copiedTypeNode= DeclarationRewrite.copyTypeAndAddTypeAnnotations(oldVarDecl.getType(), rewrite);
956
			newVarDec.setType(copiedTypeNode);
955
			newVarDec.modifiers().addAll(ASTNodeFactory.newModifiers(ast, oldVarDecl.getModifiers()));
957
			newVarDec.modifiers().addAll(ASTNodeFactory.newModifiers(ast, oldVarDecl.getModifiers()));
956
			newStatement= newVarDec;
958
			newStatement= newVarDec;
957
		}
959
		}
Lines 1503-1509 Link Here
1503
			ASTRewrite rewrite= ASTRewrite.create(ast);
1505
			ASTRewrite rewrite= ASTRewrite.create(ast);
1504
			if (selectedMultiCatchType != null) {
1506
			if (selectedMultiCatchType != null) {
1505
				removeException(rewrite, (UnionType) type, selectedMultiCatchType);
1507
				removeException(rewrite, (UnionType) type, selectedMultiCatchType);
1506
				addExceptionToThrows(ast, methodDeclaration, rewrite, selectedMultiCatchType);
1508
				addExceptionToThrows(methodDeclaration, rewrite, selectedMultiCatchType);
1507
				String label= CorrectionMessages.QuickAssistProcessor_exceptiontothrows_description;
1509
				String label= CorrectionMessages.QuickAssistProcessor_exceptiontothrows_description;
1508
				ASTRewriteCorrectionProposal proposal= new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.REPLACE_EXCEPTION_WITH_THROWS, image);
1510
				ASTRewriteCorrectionProposal proposal= new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.REPLACE_EXCEPTION_WITH_THROWS, image);
1509
				resultingCollections.add(proposal);
1511
				resultingCollections.add(proposal);
Lines 1515-1524 Link Here
1515
					for (Type elementType : types) {
1517
					for (Type elementType : types) {
1516
						if (!(elementType instanceof SimpleType || elementType instanceof NameQualifiedType))
1518
						if (!(elementType instanceof SimpleType || elementType instanceof NameQualifiedType))
1517
							return false;
1519
							return false;
1518
						addExceptionToThrows(ast, methodDeclaration, rewrite, elementType);
1520
						addExceptionToThrows(methodDeclaration, rewrite, elementType);
1519
					}
1521
					}
1520
				} else {
1522
				} else {
1521
					addExceptionToThrows(ast, methodDeclaration, rewrite, type);
1523
					addExceptionToThrows(methodDeclaration, rewrite, type);
1522
				}
1524
				}
1523
				String label= CorrectionMessages.QuickAssistProcessor_catchclausetothrows_description;
1525
				String label= CorrectionMessages.QuickAssistProcessor_catchclausetothrows_description;
1524
				ASTRewriteCorrectionProposal proposal= new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.REPLACE_CATCH_CLAUSE_WITH_THROWS, image);
1526
				ASTRewriteCorrectionProposal proposal= new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.REPLACE_CATCH_CLAUSE_WITH_THROWS, image);
Lines 1544-1549 Link Here
1544
	}
1546
	}
1545
1547
1546
	private static void removeException(ASTRewrite rewrite, UnionType unionType, Type exception) {
1548
	private static void removeException(ASTRewrite rewrite, UnionType unionType, Type exception) {
1549
		DeclarationRewrite.removeTypeAnnotationsFromDeclaration(exception, rewrite, null);
1547
		ListRewrite listRewrite= rewrite.getListRewrite(unionType, UnionType.TYPES_PROPERTY);
1550
		ListRewrite listRewrite= rewrite.getListRewrite(unionType, UnionType.TYPES_PROPERTY);
1548
		List<Type> types= unionType.types();
1551
		List<Type> types= unionType.types();
1549
		for (Iterator<Type> iterator= types.iterator(); iterator.hasNext();) {
1552
		for (Iterator<Type> iterator= types.iterator(); iterator.hasNext();) {
Lines 1554-1563 Link Here
1554
		}
1557
		}
1555
	}
1558
	}
1556
1559
1557
	private static void addExceptionToThrows(AST ast, MethodDeclaration methodDeclaration, ASTRewrite rewrite, Type type2) {
1560
	private static void addExceptionToThrows(MethodDeclaration methodDeclaration, ASTRewrite rewrite, Type type2) {
1558
		ITypeBinding binding= type2.resolveBinding();
1561
		ITypeBinding binding= type2.resolveBinding();
1559
		if (binding == null || isNotYetThrown(binding, methodDeclaration.thrownExceptionTypes())) {
1562
		if (binding == null || isNotYetThrown(binding, methodDeclaration.thrownExceptionTypes())) {
1560
			Type newType= (Type) ASTNode.copySubtree(ast, type2);
1563
			Type newType= DeclarationRewrite.copyTypeAndAddTypeAnnotations(type2, rewrite);
1561
1564
1562
			ListRewrite listRewriter= rewrite.getListRewrite(methodDeclaration, MethodDeclaration.THROWN_EXCEPTION_TYPES_PROPERTY);
1565
			ListRewrite listRewriter= rewrite.getListRewrite(methodDeclaration, MethodDeclaration.THROWN_EXCEPTION_TYPES_PROPERTY);
1563
			listRewriter.insertLast(newType, null);
1566
			listRewriter.insertLast(newType, null);
Lines 1657-1664 Link Here
1657
		List<Type> types= newUnionType.types();
1660
		List<Type> types= newUnionType.types();
1658
		for (int i= 0; i < coveredNodes.size(); i++) {
1661
		for (int i= 0; i < coveredNodes.size(); i++) {
1659
			ASTNode typeNode= coveredNodes.get(i);
1662
			ASTNode typeNode= coveredNodes.get(i);
1660
			types.add((Type) rewrite.createCopyTarget(typeNode));
1663
			if (typeNode instanceof Type) {
1661
			rewrite.remove(typeNode, null);
1664
				Type copiedTypeNode= DeclarationRewrite.copyTypeAndMoveTypeAnnotations((Type) typeNode, rewrite, null);				
1665
				types.add(copiedTypeNode);
1666
				rewrite.remove(typeNode, null);
1667
			}
1662
		}
1668
		}
1663
		newSingleVariableDeclaration.setType(newUnionType);
1669
		newSingleVariableDeclaration.setType(newUnionType);
1664
		newSingleVariableDeclaration.setName((SimpleName) rewrite.createCopyTarget(catchClause.getException().getName()));
1670
		newSingleVariableDeclaration.setName((SimpleName) rewrite.createCopyTarget(catchClause.getException().getName()));
Lines 1747-1756 Link Here
1747
			if (type instanceof UnionType) {
1753
			if (type instanceof UnionType) {
1748
				List<Type> types2= ((UnionType) type).types();
1754
				List<Type> types2= ((UnionType) type).types();
1749
				for (Iterator<Type> iterator2= types2.iterator(); iterator2.hasNext();) {
1755
				for (Iterator<Type> iterator2= types2.iterator(); iterator2.hasNext();) {
1750
					types.add((Type) rewrite.createCopyTarget(iterator2.next()));
1756
					Type copiedTypeNode= DeclarationRewrite.copyTypeAndMoveTypeAnnotations(iterator2.next(), rewrite, null);
1757
					types.add(copiedTypeNode);
1751
				}
1758
				}
1752
			} else {
1759
			} else {
1753
				types.add((Type) rewrite.createCopyTarget(type));
1760
				Type copiedTypeNode= DeclarationRewrite.copyTypeAndMoveTypeAnnotations(type, rewrite, null);
1761
				types.add(copiedTypeNode);
1754
			}
1762
			}
1755
		}
1763
		}
1756
1764
Lines 1817-1823 Link Here
1817
			CatchClause newCatchClause= ast.newCatchClause();
1825
			CatchClause newCatchClause= ast.newCatchClause();
1818
1826
1819
			SingleVariableDeclaration newSingleVariableDeclaration= ast.newSingleVariableDeclaration();
1827
			SingleVariableDeclaration newSingleVariableDeclaration= ast.newSingleVariableDeclaration();
1820
			newSingleVariableDeclaration.setType((Type) rewrite.createCopyTarget(type2));
1828
			Type copiedTypeNode= DeclarationRewrite.copyTypeAndMoveTypeAnnotations(type2, rewrite, null);				
1829
			newSingleVariableDeclaration.setType(copiedTypeNode);
1821
			newSingleVariableDeclaration.setName((SimpleName) rewrite.createCopyTarget(singleVariableDeclaration.getName()));
1830
			newSingleVariableDeclaration.setName((SimpleName) rewrite.createCopyTarget(singleVariableDeclaration.getName()));
1822
			newCatchClause.setException(newSingleVariableDeclaration);
1831
			newCatchClause.setException(newSingleVariableDeclaration);
1823
			setCatchClauseBody(newCatchClause, rewrite, catchClause);
1832
			setCatchClauseBody(newCatchClause, rewrite, catchClause);
Lines 2585-2593 Link Here
2585
				VariableDeclarationStatement varDeclaration= ast.newVariableDeclarationStatement(varFragment);
2594
				VariableDeclarationStatement varDeclaration= ast.newVariableDeclarationStatement(varFragment);
2586
				Type varType;
2595
				Type varType;
2587
				if (initializerIsArray) {
2596
				if (initializerIsArray) {
2588
					ArrayType varArrayType= (ArrayType) DimensionRewrite.copyTypeAndAddDimensions(parameter.getType(), parameter.extraDimensions(), rewrite);
2597
					Type copiedType= DimensionRewrite.copyTypeAndAddDimensions(parameter.getType(), parameter.extraDimensions(), rewrite);
2589
					varArrayType.dimensions().add(0, ast.newDimension());
2598
					varType= ASTNodeFactory.newArrayType(copiedType);
2590
					varType= varArrayType;
2591
				} else {
2599
				} else {
2592
					ImportRewrite imports= proposal.createImportRewrite(context.getASTRoot());
2600
					ImportRewrite imports= proposal.createImportRewrite(context.getASTRoot());
2593
					ImportRewriteContext importRewriteContext= new ContextSensitiveImportRewriteContext(node, imports);
2601
					ImportRewriteContext importRewriteContext= new ContextSensitiveImportRewriteContext(node, imports);

Return to bug 426967