Skip to main content
summaryrefslogtreecommitdiffstats
blob: 9606e659261e519242c6046943ca190d2a15f85a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/*******************************************************************************
 * Copyright (c) 2006, 2008 Oracle. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0, which accompanies this distribution
 * and is available at http://www.eclipse.org/legal/epl-v10.html.
 * 
 * Contributors:
 *     Oracle - initial API and implementation
 ******************************************************************************/
package org.eclipse.jpt.core.internal.utility.jdt;

import java.util.Iterator;
import java.util.List;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.Annotation;
import org.eclipse.jdt.core.dom.Expression;
import org.eclipse.jdt.core.dom.MarkerAnnotation;
import org.eclipse.jdt.core.dom.MemberValuePair;
import org.eclipse.jdt.core.dom.NormalAnnotation;
import org.eclipse.jdt.core.dom.SingleMemberAnnotation;
import org.eclipse.jpt.core.utility.jdt.DeclarationAnnotationAdapter;
import org.eclipse.jpt.core.utility.jdt.ModifiedDeclaration;

/**
 * Pull together some of the behavior common to NestedDeclarationAnnotationAdapter
 * and IndexedNestedDeclarationAnnotationAdapter
 */
public abstract class AbstractNestedDeclarationAnnotationAdapter extends AbstractDeclarationAnnotationAdapter {
	private final DeclarationAnnotationAdapter outerAnnotationAdapter;
	private final String elementName;
	private final boolean removeOuterAnnotationWhenEmpty;

	// reduce NLS checks
	protected static final String VALUE = "value"; //$NON-NLS-1$


	// ********** constructors **********

	/**
	 * default element name is "value";
	 * default behavior is to remove the outer annotation when it is empty
	 */
	protected AbstractNestedDeclarationAnnotationAdapter(DeclarationAnnotationAdapter outerAnnotationAdapter, String annotationName) {
		this(outerAnnotationAdapter, VALUE, annotationName);
	}

	/**
	 * default behavior is to remove the outer annotation when it is empty
	 */
	protected AbstractNestedDeclarationAnnotationAdapter(DeclarationAnnotationAdapter outerAnnotationAdapter, String elementName, String annotationName) {
		this(outerAnnotationAdapter, elementName, annotationName, true);
	}

	protected AbstractNestedDeclarationAnnotationAdapter(DeclarationAnnotationAdapter outerAnnotationAdapter, String elementName, String annotationName, boolean removeOuterAnnotationWhenEmpty) {
		super(annotationName);
		this.outerAnnotationAdapter = outerAnnotationAdapter;
		this.elementName = elementName;
		this.removeOuterAnnotationWhenEmpty = removeOuterAnnotationWhenEmpty;
	}


	// ********** DeclarationAnnotationAdapter implementation **********

	public Annotation getAnnotation(ModifiedDeclaration declaration) {
		Annotation outer = this.outerAnnotationAdapter.getAnnotation(declaration);
		if (outer == null) {
			return null;
		}
		Expression value = this.elementValue(outer);
		if (value == null) {
			return null;
		}
		Annotation inner = this.getAnnotation(value);
		if (inner == null) {
			return null;
		}
		// return the annotation only if it has a matching name(?)
		return this.nameMatches(declaration, inner) ? inner : null;
	}

	public void removeAnnotation(ModifiedDeclaration declaration) {
		Annotation outer = this.outerAnnotationAdapter.getAnnotation(declaration);
		if (outer == null) {
			return;
		}
		Expression value = this.elementValue(outer);
		if (value == null) {
			return;
		}
		// hack to allow short-circuit when the value is an array initializer
		if (this.removeAnnotation(declaration, outer, value)) {
			return;
		}
		Annotation inner = this.annotationValue(value);
		if (inner == null) {
			return;
		}
		// remove the annotation only if it has a matching name(?)
		if (this.nameMatches(declaration, inner)) {
			this.removeElementAndNormalize(declaration, outer);
		}
	}

	public ASTNode getAstNode(ModifiedDeclaration declaration) {
		// if the annotation is missing, return the outer annotation's node
		Annotation annotation = this.getAnnotation(declaration);
		return (annotation != null) ? annotation : this.outerAnnotationAdapter.getAstNode(declaration);
	}

	@Override
	protected void addAnnotation(ModifiedDeclaration declaration, Annotation inner) {
		Annotation outer = this.outerAnnotationAdapter.getAnnotation(declaration);
		if (outer == null) {
			this.buildNewOuterAnnotation(declaration, inner);
		} else if (outer.isMarkerAnnotation()) {
			this.modifyAnnotation(declaration, (MarkerAnnotation) outer, inner);
		} else if (outer.isSingleMemberAnnotation()) {
			this.modifyAnnotation(declaration, (SingleMemberAnnotation) outer, inner);
		} else if (outer.isNormalAnnotation()) {
			this.modifyAnnotation(declaration, (NormalAnnotation) outer, inner);
		} else {
			throw new IllegalStateException("unknown annotation type: " + outer); //$NON-NLS-1$
		}
	}


	// ********** abstract methods **********

	/**
	 * Return an annotation extracted from the specified expression,
	 * which is the value of the adapter's element.
	 */
	protected abstract Annotation getAnnotation(Expression value);

	/**
	 * Remove the annotation from the specified expression,
	 * which is the value of the adapter's element.
	 * Return whether the removal was successful.
	 */
	protected abstract boolean removeAnnotation(ModifiedDeclaration declaration, Annotation outer, Expression value);

	/**
	 * Set the value of the specified outer annotation to the
	 * specified inner annotation.
	 */
	protected abstract void modifyAnnotationValue(SingleMemberAnnotation outer, Annotation inner);

	/**
	 * Set the value of the specified member value pair to the
	 * specified inner annotation.
	 */
	protected abstract void modifyMemberValuePair(MemberValuePair pair, Annotation inner);


	// ********** public methods **********

	public DeclarationAnnotationAdapter getOuterAnnotationAdapter() {
		return this.outerAnnotationAdapter;
	}

	public String getElementName() {
		return this.elementName;
	}


	// ********** internal methods **********

	/**
	 * If the specified expression is an annotation, cast it to an annotation;
	 * otherwise return null.
	 */
	protected Annotation annotationValue(Expression expression) {
		switch (expression.getNodeType()) {
			case ASTNode.NORMAL_ANNOTATION:
			case ASTNode.SINGLE_MEMBER_ANNOTATION:
			case ASTNode.MARKER_ANNOTATION:
				return (Annotation) expression;
			default:
				return null;
		}
	}

	/**
	 * Remove the *first* annotation element with the specified name
	 * from the specified annotation, converting the annotation as appropriate.
	 */
	protected void removeElementAndNormalize(ModifiedDeclaration declaration, Annotation outer) {
		if (outer.isNormalAnnotation()) {
			this.removeElementAndNormalize(declaration, (NormalAnnotation) outer);
		} else if (outer.isSingleMemberAnnotation()) {
			this.removeElementAndNormalize(declaration, (SingleMemberAnnotation) outer);
		} else if (outer.isMarkerAnnotation()) {
			this.removeElementAndNormalize(declaration, (MarkerAnnotation) outer);
		} else {
			throw new IllegalArgumentException("unknown annotation type: " + outer); //$NON-NLS-1$
		}
	}

	/**
	 * Remove the *first* annotation element with the adapter's element name
	 * from the specified annotation. Convert the annotation to
	 * a marker annotation or single member annotation if appropriate.
	 * <pre>
	 * &#64;Outer(name="Fred", foo=&#64;Inner) => &#64;Outer(name="Fred")
	 * &#64;Outer(foo=&#64;Inner) => &#64;Outer
	 * </pre>
	 */
	protected void removeElementAndNormalize(ModifiedDeclaration declaration, NormalAnnotation outer) {
		this.removeElement(outer);
		this.normalizeAnnotation(declaration, outer);
	}

	/**
	 * Remove from the specified annotation the element with
	 * the adapter's element name.
	 */
	protected void removeElement(NormalAnnotation annotation) {
		for (Iterator<MemberValuePair> stream = this.values(annotation).iterator(); stream.hasNext(); ) {
			MemberValuePair pair = stream.next();
			if (pair.getName().getFullyQualifiedName().equals(this.elementName)) {
				stream.remove();
				break;
			}
		}
	}

	/**
	 * Convert the specified normal annotation to a marker annotation or
	 * single member annotation if appropriate.
	 */
	protected void normalizeAnnotation(ModifiedDeclaration declaration, NormalAnnotation outer) {
		List<MemberValuePair> values = this.values(outer);
		switch (values.size()) {
			case 0:
				// if the elements are all gone, remove the annotation or convert it to a marker annotation
				if (this.removeOuterAnnotationWhenEmpty) {
					this.outerAnnotationAdapter.removeAnnotation(declaration);
				} else {
					// convert the annotation to a marker annotation
					this.outerAnnotationAdapter.newMarkerAnnotation(declaration);
				}
				break;
			case 1:
				MemberValuePair pair = values.get(0);
				if (pair.getName().getFullyQualifiedName().equals(VALUE)) {
					// if the last remaining element is 'value', convert the annotation to a single member annotation
					Expression vv = pair.getValue();
					vv = (Expression) ASTNode.copySubtree(vv.getAST(), vv);
					this.outerAnnotationAdapter.newSingleMemberAnnotation(declaration).setValue(vv);
				}
				break;
			default:
				// do nothing
				break;
		}
	}

	/**
	 * Convert the specified single member annotation to a marker annotation
	 * if the adapter's element name is "value".
	 */
	protected void removeElementAndNormalize(ModifiedDeclaration declaration, @SuppressWarnings("unused") SingleMemberAnnotation outer) {
		if (this.elementName.equals(VALUE)) {
			if (this.removeOuterAnnotationWhenEmpty) {
				this.outerAnnotationAdapter.removeAnnotation(declaration);
			} else {
				// convert the annotation to a marker annotation
				this.outerAnnotationAdapter.newMarkerAnnotation(declaration);
			}
		}
	}

	protected void removeElementAndNormalize(ModifiedDeclaration declaration, @SuppressWarnings("unused") MarkerAnnotation outer) {
		if (this.removeOuterAnnotationWhenEmpty) {
			this.outerAnnotationAdapter.removeAnnotation(declaration);
		}
	}

	/**
	 * Return the value of the *first* annotation element
	 * with the adapter's element name.
	 * Return null if the annotation has no such element.
	 * (An element name of "value" will return the value of a single
	 * member annotation.)
	 */
	protected Expression elementValue(Annotation annotation) {
		if (annotation.isNormalAnnotation()) {
			return this.elementValue((NormalAnnotation) annotation);
		}
		if (annotation.isSingleMemberAnnotation()) {
			return this.elementValue((SingleMemberAnnotation) annotation);
		}
		return null;
	}

	protected Expression elementValue(NormalAnnotation annotation) {
		MemberValuePair pair = this.memberValuePair(annotation);
		return (pair == null) ? null : pair.getValue();
	}

	/**
	 * If the adapter's element name is "value", return the value of the
	 * annotation, otherwise return null.
	 */
	protected Expression elementValue(SingleMemberAnnotation annotation) {
		return this.elementName.equals(VALUE) ? annotation.getValue() : null;
	}

	/**
	 * Return the *first* member value pair for the adapter's element name.
	 * Return null if the specified annotation has no such element.
	 */
	protected MemberValuePair memberValuePair(NormalAnnotation annotation) {
		for (MemberValuePair pair : this.values(annotation)) {
			if (pair.getName().getFullyQualifiedName().equals(this.elementName)) {
				return pair;
			}
		}
		return null;
	}

	/**
	 * Build a new outer annotation and add the specified
	 * inner annotation to it:
	 * <pre>
	 *     &#64;Outer(&#64;Inner)
	 * </pre>
	 * or
	 * <pre>
	 *     &#64;Outer(foo=&#64;Inner)
	 * </pre>
	 */
	protected void buildNewOuterAnnotation(ModifiedDeclaration declaration, Annotation inner) {
		if (this.elementName.equals(VALUE)) {
			this.outerAnnotationAdapter.newSingleMemberAnnotation(declaration).setValue(this.buildNewInnerExpression(inner));
		} else {
			List<MemberValuePair> values = this.values(this.outerAnnotationAdapter.newNormalAnnotation(declaration));
			values.add(this.newMemberValuePair(this.buildNewInnerExpression(inner)));
		}
	}

	/**
	 * Build an expression to be added to a new outer annotation
	 * for the specified inner annotation.
	 */
	protected abstract Expression buildNewInnerExpression(Annotation inner);

	/**
	 * Build a new member value pair with the specified name and value.
	 */
	protected MemberValuePair newMemberValuePair(String name, Expression value) {
		AST ast = value.getAST();
		MemberValuePair pair = ast.newMemberValuePair();
		pair.setName(ast.newSimpleName(name));
		pair.setValue(value);
		return pair;
	}

	/**
	 * Build a new member value pair with the adapter's element name
	 * and the specified inner annotation.
	 */
	protected MemberValuePair newMemberValuePair(Expression value) {
		return this.newMemberValuePair(this.elementName, value);
	}

	/**
	 * Add the specified inner annotation to the marker annotation.
	 */
	protected void modifyAnnotation(ModifiedDeclaration declaration, @SuppressWarnings("unused") MarkerAnnotation outer, Annotation inner) {
		this.buildNewOuterAnnotation(declaration, inner);
	}

	/**
	 * Add the specified inner annotation to the single member annotation.
	 */
	protected void modifyAnnotation(ModifiedDeclaration declaration, SingleMemberAnnotation outer, Annotation inner) {
		if (this.elementName.equals(VALUE)) {
			this.modifyAnnotationValue(outer, inner);
		} else {
			this.modifyAnnotationNonValue(declaration, outer, inner);
		}
	}

	/**
	 * Add the specified inner annotation to the single member annotation,
	 * converting it to a normal annotation:
	 * <pre>
	 *     &#64;Outer("lorem ipsum") => &#64;Outer(value="lorem ipsum", foo=&#64;Inner)
	 * </pre>
	 */
	protected void modifyAnnotationNonValue(ModifiedDeclaration declaration, SingleMemberAnnotation outer, Annotation inner) {
		Expression vv = outer.getValue();
		vv = (Expression) ASTNode.copySubtree(vv.getAST(), vv);
		NormalAnnotation newOuter = this.outerAnnotationAdapter.newNormalAnnotation(declaration);
		List<MemberValuePair> values = this.values(newOuter);
		values.add(this.newMemberValuePair(VALUE, vv));
		values.add(this.newMemberValuePair(this.buildNewInnerExpression(inner)));
	}

	/**
	 * Add the specified inner annotation to the normal annotation:
	 * <pre>
	 *     &#64;Outer(bar="lorem ipsum") => &#64;Outer(bar="lorem ipsum", foo=&#64;Inner)
	 * </pre>
	 * or
	 * <pre>
	 *     &#64;Outer(foo=&#64;Inner("lorem ipsum")) => &#64;Outer(foo=&#64;Inner)
	 * </pre>
	 */
	protected void modifyAnnotation(@SuppressWarnings("unused") ModifiedDeclaration declaration, NormalAnnotation outer, Annotation inner) {
		MemberValuePair pair = this.memberValuePair(outer);
		if (pair == null) {
			List<MemberValuePair> values = this.values(outer);
			values.add(this.newMemberValuePair(inner));
		} else {
			this.modifyMemberValuePair(pair, inner);
		}
	}

}

Back to the top