Skip to content

Commit fcba4d8

Browse files
author
Thies Wellpott
committed
support Spring annotations for JSF and CDI managed beans
Signed-off-by: Thies Wellpott <twforen@online.de>
1 parent 16c4408 commit fcba4d8

2 files changed

Lines changed: 218 additions & 45 deletions

File tree

cdi/plugins/org.jboss.tools.cdi.core/src/org/jboss/tools/cdi/internal/core/impl/definition/AbstractMemberDefinition.java

Lines changed: 60 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
/*******************************************************************************
2-
* Copyright (c) 2009 Red Hat, Inc.
3-
* Distributed under license by Red Hat, Inc. All rights reserved.
4-
* This program is made available under the terms of the
5-
* Eclipse Public License v1.0 which accompanies this distribution,
6-
* and is available at http://www.eclipse.org/legal/epl-v10.html
7-
*
8-
* Contributors:
9-
* Red Hat, Inc. - initial API and implementation
10-
******************************************************************************/
1+
/*******************************************************************************
2+
* Copyright (c) 2009 Red Hat, Inc.
3+
* Distributed under license by Red Hat, Inc. All rights reserved.
4+
* This program is made available under the terms of the
5+
* Eclipse Public License v1.0 which accompanies this distribution,
6+
* and is available at http://www.eclipse.org/legal/epl-v10.html
7+
*
8+
* Contributors:
9+
* Red Hat, Inc. - initial API and implementation
10+
******************************************************************************/
1111
package org.jboss.tools.cdi.internal.core.impl.definition;
1212

1313
import java.util.ArrayList;
@@ -41,19 +41,24 @@
4141
import org.jboss.tools.common.text.ITextSourceReference;
4242

4343
/**
44-
*
44+
*
4545
* @author Viacheslav Kabanovich
4646
*
4747
*/
4848
public abstract class AbstractMemberDefinition implements IAnnotated {
4949
public static int FLAG_NO_ANNOTATIONS = 1;
5050
public static int FLAG_ALL_MEMBERS = 2;
5151

52+
private static final String SPRING_CONTROLLER = "org.springframework.stereotype.Controller";
53+
private static final String SPRING_REPOSITORY = "org.springframework.stereotype.Repository";
54+
private static final String SPRING_SERVICE = "org.springframework.stereotype.Service";
55+
private static final String SPRING_COMPONENT = "org.springframework.stereotype.Component";
56+
5257
CDICoreNature project;
53-
protected List<IAnnotationDeclaration> annotations = new ArrayList<IAnnotationDeclaration>(2);
58+
protected List<IAnnotationDeclaration> annotations = new ArrayList<>(2);
5459
protected IAnnotatable member;
5560
private IAnnotationMap annotationsByType = EmptyMap.instance;
56-
61+
5762
protected ITextSourceReference originalDefinition = null;
5863

5964
public AbstractMemberDefinition() {}
@@ -149,7 +154,7 @@ private void addAnnotation(AnnotationDeclaration a, IRootDefinitionContext conte
149154
} else {
150155
a = b;
151156
}
152-
157+
153158
if(a.getTypeName() != null) {
154159
annotationsByType = annotationsByType.put(a.getTypeName(), a);
155160
}
@@ -165,7 +170,7 @@ public void annotationKindChanged(String typeName, IRootDefinitionContext contex
165170
}
166171
//Make sure that a is non-specific annotation.
167172
addAnnotation(new AnnotationDeclaration(a), context);
168-
173+
169174
}
170175

171176
public void removeAnnotation(IAnnotationDeclaration a) {
@@ -181,6 +186,7 @@ public void removeAnnotation(IAnnotationDeclaration a) {
181186
* (non-Javadoc)
182187
* @see org.jboss.tools.cdi.core.IAnnotated#getAnnotations()
183188
*/
189+
@Override
184190
public List<IAnnotationDeclaration> getAnnotations() {
185191
return annotations;
186192
}
@@ -189,6 +195,7 @@ public List<IAnnotationDeclaration> getAnnotations() {
189195
* (non-Javadoc)
190196
* @see org.jboss.tools.cdi.core.IAnnotated#getAnnotation(java.lang.String)
191197
*/
198+
@Override
192199
public AnnotationDeclaration getAnnotation(String typeName) {
193200
return annotationsByType.get(typeName);
194201
}
@@ -197,6 +204,7 @@ public AnnotationDeclaration getAnnotation(String typeName) {
197204
* (non-Javadoc)
198205
* @see org.jboss.tools.common.java.IAnnotated#getAnnotationPosition(java.lang.String)
199206
*/
207+
@Override
200208
public IJavaSourceReference getAnnotationPosition(String annotationTypeName) {
201209
return getAnnotation(annotationTypeName);
202210
}
@@ -205,12 +213,24 @@ public IJavaSourceReference getAnnotationPosition(String annotationTypeName) {
205213
* (non-Javadoc)
206214
* @see org.jboss.tools.cdi.core.IAnnotated#isAnnotationPresent(java.lang.String)
207215
*/
216+
@Override
208217
public boolean isAnnotationPresent(String annotationTypeName) {
209218
return getAnnotation(annotationTypeName)!=null;
210219
}
211220

212221
public AnnotationDeclaration getNamedAnnotation() {
213-
return getAnnotation(CDIConstants.NAMED_QUALIFIER_TYPE_NAME);
222+
AnnotationDeclaration ad = getAnnotation(CDIConstants.NAMED_QUALIFIER_TYPE_NAME);
223+
// also support all Spring dependencies (note: these are not real CDI "beans" but often
224+
// can be used in the same way, so offering support for this widely used alternative is useful)
225+
if (ad != null) return ad;
226+
ad = getAnnotation(SPRING_CONTROLLER);
227+
if (ad != null) return ad;
228+
ad = getAnnotation(SPRING_SERVICE);
229+
if (ad != null) return ad;
230+
ad = getAnnotation(SPRING_REPOSITORY);
231+
if (ad != null) return ad;
232+
ad = getAnnotation(SPRING_COMPONENT);
233+
return ad;
214234
}
215235

216236
public AnnotationDeclaration getTypedAnnotation() {
@@ -246,56 +266,62 @@ interface IAnnotationMap {
246266

247267
class EmptyMap implements IAnnotationMap {
248268
static EmptyMap instance = new EmptyMap();
249-
269+
250270
private EmptyMap() {}
251271

272+
@Override
252273
public IAnnotationMap put(String type, AnnotationDeclaration d) {
253274
return new OneEntryMap(d);
254275
}
255276

277+
@Override
256278
public AnnotationDeclaration get(String type) {
257279
return null;
258280
}
259281

282+
@Override
260283
public IAnnotationMap remove(String type) {
261284
return this;
262285
}
263286
}
264287

265288
class OneEntryMap implements IAnnotationMap {
266289
AnnotationDeclaration d;
267-
290+
268291
public OneEntryMap(AnnotationDeclaration d) {
269292
this.d = d;
270293
}
271294

272295
@Override
273-
public IAnnotationMap put(String type, AnnotationDeclaration d) {
296+
public IAnnotationMap put(String type, AnnotationDeclaration ad) {
274297
if(this.d.getTypeName().equals(type)) {
275-
this.d = d;
298+
this.d = ad;
276299
return this;
277300
}
278-
return new TwoEntryMap(this.d, d);
301+
return new TwoEntryMap(this.d, ad);
279302
}
280303

304+
@Override
281305
public AnnotationDeclaration get(String type) {
282306
return (d.getTypeName().equals(type)) ? d : null;
283307
}
284308

309+
@Override
285310
public IAnnotationMap remove(String type) {
286311
return (get(type) != null) ? EmptyMap.instance : this;
287-
}
312+
}
288313
}
289314

290315
class TwoEntryMap implements IAnnotationMap {
291316
AnnotationDeclaration d1;
292317
AnnotationDeclaration d2;
293-
318+
294319
public TwoEntryMap(AnnotationDeclaration d1,AnnotationDeclaration d2) {
295320
this.d1 = d1;
296321
this.d2 = d2;
297322
}
298323

324+
@Override
299325
public IAnnotationMap put(String type, AnnotationDeclaration d) {
300326
AnnotationDeclaration dc = get(type);
301327
if(dc == d1) {
@@ -312,30 +338,36 @@ public IAnnotationMap put(String type, AnnotationDeclaration d) {
312338
return map;
313339
}
314340

341+
@Override
315342
public AnnotationDeclaration get(String type) {
316343
return (d1.getTypeName().equals(type)) ? d1 : (d2.getTypeName().equals(type)) ? d2 : null;
317344
}
318345

346+
@Override
319347
public IAnnotationMap remove(String type) {
320348
AnnotationDeclaration d = get(type);
321349
return (d == d1) ? new OneEntryMap(d2) : (d == d2) ? new OneEntryMap(d1) : this;
322-
}
350+
}
323351
}
324352

325353
class AnnotationMap implements IAnnotationMap {
326-
Map<String, AnnotationDeclaration> annotationsByType = new HashMap<String, AnnotationDeclaration>(8);
327-
354+
Map<String, AnnotationDeclaration> annotationsByType = new HashMap<>(8);
355+
328356
AnnotationMap() {}
329-
357+
358+
@Override
330359
public IAnnotationMap put(String type, AnnotationDeclaration d) {
331-
annotationsByType.put(type, d);
360+
annotationsByType.put(type, d);
361+
332362
return this;
333363
}
334364

365+
@Override
335366
public AnnotationDeclaration get(String type) {
336367
return annotationsByType.get(type);
337368
}
338369

370+
@Override
339371
public IAnnotationMap remove(String type) {
340372
annotationsByType.remove(type);
341373
return this;

0 commit comments

Comments
 (0)