Skip to main content
summaryrefslogtreecommitdiffstats
blob: 821c4ed618a4372eadb850fd8104865c3289f9d4 (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
/*******************************************************************************
 * 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.common.core.internal.utility.jdt;

import java.util.List;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.Annotation;
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.common.core.utility.jdt.DeclarationAnnotationAdapter;
import org.eclipse.jpt.common.core.utility.jdt.ModifiedDeclaration;
import org.eclipse.jpt.common.utility.internal.StringTools;

/**
 * 
 */
public abstract class AbstractDeclarationAnnotationAdapter implements DeclarationAnnotationAdapter {
	private final String annotationName;


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

	protected AbstractDeclarationAnnotationAdapter(String annotationName) {
		super();
		this.annotationName = annotationName;
	}


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

	public MarkerAnnotation newMarkerAnnotation(ModifiedDeclaration declaration) {
		MarkerAnnotation annotation = this.newMarkerAnnotation(declaration.getAst());
		this.addAnnotationAndImport(declaration, annotation);
		return annotation;
	}

	public SingleMemberAnnotation newSingleMemberAnnotation(ModifiedDeclaration declaration) {
		SingleMemberAnnotation annotation = this.newSingleMemberAnnotation(declaration.getAst());
		this.addAnnotationAndImport(declaration, annotation);
		return annotation;
	}

	public NormalAnnotation newNormalAnnotation(ModifiedDeclaration declaration) {
		NormalAnnotation annotation = this.newNormalAnnotation(declaration.getAst());
		this.addAnnotationAndImport(declaration, annotation);
		return annotation;
	}

	/**
	 * Add the appropriate import statement, then shorten the annotation's
	 * name before adding it to the declaration.
	 */
	protected void addAnnotationAndImport(ModifiedDeclaration declaration, Annotation annotation) {
		annotation.setTypeName(declaration.getAst().newName(this.getSourceCodeAnnotationName(declaration)));
		this.addAnnotation(declaration, annotation);
	}

	/**
	 * Return the annotation's name as it can be used in source code;
	 * i.e. if we can add it to the compilation unit's imports, return the short
	 * name; if we can't (because of a collision), return the fully-qualified name.
	 * NB: an import may be added as a side-effect :-(
	 */
	protected String getSourceCodeAnnotationName(ModifiedDeclaration declaration) {
		return declaration.addImport(this.annotationName) ? this.getShortAnnotationName() : this.annotationName;
	}

	@Override
	public String toString() {
		return StringTools.buildToStringFor(this, this.annotationName);
	}

	/**
	 * Add the specified annotation to the specified declaration,
	 * replacing the original annotation if present.
	 */
	protected abstract void addAnnotation(ModifiedDeclaration declaration, Annotation annotation);


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

	/**
	 * This is 'public' because we use it in CombinationIndexedDeclarationAnnotationAdapter
	 * to get the annotation name from a NestedIndexedDeclarationAnnotationAdapter.
	 */
	public String getAnnotationName() {
		return this.annotationName;
	}


	// ********** helper methods **********

	protected boolean nameMatches(ModifiedDeclaration declaration, Annotation annotation) {
		return this.nameMatches(declaration, annotation, this.annotationName);
	}

	protected boolean nameMatches(ModifiedDeclaration declaration, Annotation annotation, String name) {
		return declaration.annotationIsNamed(annotation, name);
	}

	protected MarkerAnnotation newMarkerAnnotation(AST ast) {
		return this.newMarkerAnnotation(ast, this.annotationName);
	}

	protected MarkerAnnotation newMarkerAnnotation(AST ast, String name) {
		MarkerAnnotation annotation = ast.newMarkerAnnotation();
		annotation.setTypeName(ast.newName(name));
		return annotation;
	}

	protected SingleMemberAnnotation newSingleMemberAnnotation(AST ast) {
		return this.newSingleMemberAnnotation(ast, this.annotationName);
	}

	protected SingleMemberAnnotation newSingleMemberAnnotation(AST ast, String name) {
		SingleMemberAnnotation annotation = ast.newSingleMemberAnnotation();
		annotation.setTypeName(ast.newName(name));
		return annotation;
	}

	protected NormalAnnotation newNormalAnnotation(AST ast) {
		return this.newNormalAnnotation(ast, this.annotationName);
	}

	protected NormalAnnotation newNormalAnnotation(AST ast, String name) {
		NormalAnnotation annotation = ast.newNormalAnnotation();
		annotation.setTypeName(ast.newName(name));
		return annotation;
	}

	protected String getShortAnnotationName() {
		return convertToShortName(this.annotationName);
	}
	
	protected static String convertToShortName(String name) {
		return name.substring(name.lastIndexOf('.') + 1);
	}
	
	@SuppressWarnings("unchecked")
	protected List<MemberValuePair> values(NormalAnnotation na) {
		return na.values();
	}

}

Back to the top