Skip to main content
summaryrefslogtreecommitdiffstats
blob: 7f35d5e5f992c6ac799280653a43668aa1db3559 (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
/*******************************************************************************
 * Copyright (c) 2003, 2004 IBM Corporation and others.
 * 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:
 * IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.jst.common.internal.annotations.controller;

import java.util.List;

import org.eclipse.core.resources.IProject;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jem.util.emf.workbench.ProjectUtilities;
import org.eclipse.jem.util.logger.proxy.Logger;
import org.eclipse.jem.workbench.utility.JemProjectUtilities;
import org.eclipse.jst.common.internal.annotations.core.AnnotationsAdapter;
import org.eclipse.jst.common.internal.annotations.core.AnnotationsProviderManager;
import org.eclipse.jst.common.internal.annotations.core.IAnnotationsProvider;

/**
 * @author mdelder
 *  
 */
public class AnnotationsControllerHelper {
	public static final AnnotationsControllerHelper INSTANCE = new AnnotationsControllerHelper();

	protected AnnotationsControllerHelper() {
		super();
	}

	/**
	 * 
	 * @param eObject the annotated model object
	 * @return true only if the object has annotations
	 */
	public boolean isAnnotated(EObject eObject) {
		if (AnnotationsAdapter.getAnnotations(eObject, AnnotationsAdapter.GENERATED) != null)
			return true;
		List annotationsProviders = AnnotationsProviderManager.INSTANCE.getAnnotationsProviders();
		for (int i=0; i<annotationsProviders.size(); i++) {
			IAnnotationsProvider provider = (IAnnotationsProvider) annotationsProviders.get(i);
			if (provider!=null && provider.isAnnotated(eObject))
				return true;
		}
		return false;
	}

	/**
	 * A convenience method to tag a model object as annotated
	 * 
	 * @param eObject
	 * @param value
	 */
	public void setAnnotated(EObject eObject, String value) {
		AnnotationsAdapter.addAnnotations(eObject, AnnotationsAdapter.GENERATED, value);
	}

	/**
	 * A convenience method to tag a model object as annotated Annotations Adapters can hold extra
	 * information.
	 * 
	 * @param eObject
	 * @param name
	 *            A string key
	 * @param value
	 *            A String value
	 */
	public void addAnnotations(EObject eObject, String name, Object value) {
		AnnotationsAdapter.addAnnotations(eObject, name, value);
	}

	/**
	 * A convenience method to tag a model object as annotated Annotations Adapters can hold extra
	 * information.
	 * 
	 * @param eObject
	 * @param name
	 *            A string key
	 * @param value
	 *            A String value
	 */
	public Object getAnnotations(EObject eObject, String name) {
		return AnnotationsAdapter.getAnnotations(eObject, name);
	}

	/**
	 * Acquires the generated annotation comment and parses the Fragment URL of the following form
	 * to return the tagset name:
	 * 
	 * com.acme.ejbs.MyEJB# <tagset>/ <fragment>. <fragment-pointer>
	 * 
	 * @param eObject
	 *            The annotated object
	 * @return the value of <tagset>in the URL example
	 */
	public String getTagset(EObject eObject) {
		String tagset = getTagsetFromProviders(eObject);
		if (tagset == null) {
			tagset = getTagsetFromFragment(eObject);
		}
		return tagset;
	}
	
	/**
	 * Acquires the generated annotation comment and parses the Fragment URL of the following form
	 * to return the tagset name:
	 * 
	 * com.acme.ejbs.MyEJB# <tagset>/ <fragment>. <fragment-pointer>
	 * 
	 * @param eObject
	 *            The annotated object
	 * @return the value of <tagset>in the URL example
	 */
	private String getTagsetFromFragment(EObject eObject) {

		String generatedComment = (String) AnnotationsAdapter.getAnnotations(eObject, AnnotationsAdapter.GENERATED);
		if (generatedComment == null || generatedComment.length() == 0)
			return null;
		int poundit = generatedComment.indexOf('#');
		int slash = generatedComment.indexOf('/');
		if (poundit < 0 || slash < 0 || poundit >= slash)
			return null;
		return generatedComment.substring(poundit + 1, slash);

	}
	
	/**
	 * Detect the primary tagset used to create an eObject using the providers.
	 * 
	 * @since 1.0.2
	 * @param eObject - An {@link EObject} that may be annotated.
	 * @return a String array of the used tagset names.
	 */
	private String getTagsetFromProviders(EObject eObject) {
		String tagset = null;
		List annotationProviders = AnnotationsProviderManager.INSTANCE.getAnnotationsProviders();
		int size = annotationProviders.size();
		for (int i=0; i < size && tagset == null; i++) {
			IAnnotationsProvider provider = (IAnnotationsProvider) annotationProviders.get(i);
			tagset = provider != null ? provider.getPrimaryTagset(eObject) : null;
		}
		return tagset;
	}

	/**
	 * Returns the CompilationUnit associated with the given model object
	 * 
	 * @param eObject
	 *            an Annotated model Object
	 * @return The compilation unit which was responsible for the generation of the model object
	 */
	public ICompilationUnit getAnnotatedCU(EObject eObject) {
		ICompilationUnit cu = getAnnotatedCUFromProvider(eObject);
		if (cu == null) {
			cu = getAnnotatedCUFromFragment(eObject);
		}
		return cu;
	}
	
	/**
	 * Returns the CompilationUnit associated with the given model object
	 * 
	 * @param eObject
	 *            an Annotated model Object
	 * @return The compilation unit which was responsible for the generation of the model object
	 */
	private ICompilationUnit getAnnotatedCUFromFragment(EObject eObject) {
		String fragmentString = (String) AnnotationsAdapter.getAnnotations(eObject, AnnotationsAdapter.GENERATED);
		if (fragmentString == null)
			return null;

		String typeString = fragmentString;
		if (fragmentString.indexOf('#')>0)
			typeString = fragmentString.substring(0, fragmentString.indexOf('#'));
		IType itype;

		if (typeString != null && (itype = findType(typeString, eObject)) != null) {
			return itype.getCompilationUnit();
		}
		return null;
	}
	
	/**
	 * Need to delegate the retrieval of the annotated {@link ICompilationUnit} for
	 * the passed eObject.  There could be multiple but in this case the first will be returned.
	 * 
	 * <p>
	 * This API would need to be revisited in the future if there is a requirement to show
	 * all {@link ICompilationUnit} elements that contribute to the eObject via annotations.
	 * </p>
	 * 
	 * @param eObject - an instance of an {@link EObject} that may be annotated.
	 * @since 1.0.2
	 */
	private ICompilationUnit getAnnotatedCUFromProvider(EObject eObject) {
		ICompilationUnit primaryCU = null;
		List annotationProviders = AnnotationsProviderManager.INSTANCE.getAnnotationsProviders();
		int size = annotationProviders.size();
		for (int i=0; i < size && primaryCU == null; i++) {
			IAnnotationsProvider provider = (IAnnotationsProvider) annotationProviders.get(i);
			primaryCU = provider != null ? provider.getPrimaryAnnotatedCompilationUnit(eObject) : null;
		}
		return primaryCU;
	}

	protected IType findType(String type, EObject eObject) {
		IType result = null;
		IProject project = ProjectUtilities.getProject(eObject);
		IJavaProject javaProject = JemProjectUtilities.getJavaProject(project);
		if (javaProject != null)
			try {
				result = javaProject.findType(type);
			} catch (JavaModelException e) {
				Logger.getLogger().logError(e);
			}
		return result;
	}

	/**
	 * Return true if <code>project</code> has annotation support enabled on it.
	 * 
	 * @return
	 */
	public boolean hasAnnotationSupport(IProject project) {
		return AnnotationsControllerManager.INSTANCE.hasAnnotationsBuilder(project);
	}
}

Back to the top