Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support Spring annotations for JSF and CDI managed beans #489

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/*******************************************************************************
* Copyright (c) 2009 Red Hat, Inc.
* Distributed under license by Red Hat, Inc. All rights reserved.
* This program is 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:
* Red Hat, Inc. - initial API and implementation
******************************************************************************/
/*******************************************************************************
* Copyright (c) 2009 Red Hat, Inc.
* Distributed under license by Red Hat, Inc. All rights reserved.
* This program is 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:
* Red Hat, Inc. - initial API and implementation
******************************************************************************/
package org.jboss.tools.cdi.internal.core.impl.definition;

import java.util.ArrayList;
Expand Down Expand Up @@ -41,19 +41,24 @@
import org.jboss.tools.common.text.ITextSourceReference;

/**
*
*
* @author Viacheslav Kabanovich
*
*/
public abstract class AbstractMemberDefinition implements IAnnotated {
public static int FLAG_NO_ANNOTATIONS = 1;
public static int FLAG_ALL_MEMBERS = 2;

private static final String SPRING_CONTROLLER = "org.springframework.stereotype.Controller";
private static final String SPRING_REPOSITORY = "org.springframework.stereotype.Repository";
private static final String SPRING_SERVICE = "org.springframework.stereotype.Service";
private static final String SPRING_COMPONENT = "org.springframework.stereotype.Component";

CDICoreNature project;
protected List<IAnnotationDeclaration> annotations = new ArrayList<IAnnotationDeclaration>(2);
protected List<IAnnotationDeclaration> annotations = new ArrayList<>(2);
protected IAnnotatable member;
private IAnnotationMap annotationsByType = EmptyMap.instance;

protected ITextSourceReference originalDefinition = null;

public AbstractMemberDefinition() {}
Expand Down Expand Up @@ -149,7 +154,7 @@ private void addAnnotation(AnnotationDeclaration a, IRootDefinitionContext conte
} else {
a = b;
}

if(a.getTypeName() != null) {
annotationsByType = annotationsByType.put(a.getTypeName(), a);
}
Expand All @@ -165,7 +170,7 @@ public void annotationKindChanged(String typeName, IRootDefinitionContext contex
}
//Make sure that a is non-specific annotation.
addAnnotation(new AnnotationDeclaration(a), context);

}

public void removeAnnotation(IAnnotationDeclaration a) {
Expand All @@ -181,6 +186,7 @@ public void removeAnnotation(IAnnotationDeclaration a) {
* (non-Javadoc)
* @see org.jboss.tools.cdi.core.IAnnotated#getAnnotations()
*/
@Override
public List<IAnnotationDeclaration> getAnnotations() {
return annotations;
}
Expand All @@ -189,6 +195,7 @@ public List<IAnnotationDeclaration> getAnnotations() {
* (non-Javadoc)
* @see org.jboss.tools.cdi.core.IAnnotated#getAnnotation(java.lang.String)
*/
@Override
public AnnotationDeclaration getAnnotation(String typeName) {
return annotationsByType.get(typeName);
}
Expand All @@ -197,6 +204,7 @@ public AnnotationDeclaration getAnnotation(String typeName) {
* (non-Javadoc)
* @see org.jboss.tools.common.java.IAnnotated#getAnnotationPosition(java.lang.String)
*/
@Override
public IJavaSourceReference getAnnotationPosition(String annotationTypeName) {
return getAnnotation(annotationTypeName);
}
Expand All @@ -205,12 +213,24 @@ public IJavaSourceReference getAnnotationPosition(String annotationTypeName) {
* (non-Javadoc)
* @see org.jboss.tools.cdi.core.IAnnotated#isAnnotationPresent(java.lang.String)
*/
@Override
public boolean isAnnotationPresent(String annotationTypeName) {
return getAnnotation(annotationTypeName)!=null;
}

public AnnotationDeclaration getNamedAnnotation() {
return getAnnotation(CDIConstants.NAMED_QUALIFIER_TYPE_NAME);
AnnotationDeclaration ad = getAnnotation(CDIConstants.NAMED_QUALIFIER_TYPE_NAME);
// also support all Spring dependencies (note: these are not real CDI "beans" but often
// can be used in the same way, so offering support for this widely used alternative is useful)
if (ad != null) return ad;
ad = getAnnotation(SPRING_CONTROLLER);
if (ad != null) return ad;
ad = getAnnotation(SPRING_SERVICE);
if (ad != null) return ad;
ad = getAnnotation(SPRING_REPOSITORY);
if (ad != null) return ad;
ad = getAnnotation(SPRING_COMPONENT);
return ad;
}

public AnnotationDeclaration getTypedAnnotation() {
Expand Down Expand Up @@ -246,56 +266,62 @@ interface IAnnotationMap {

class EmptyMap implements IAnnotationMap {
static EmptyMap instance = new EmptyMap();

private EmptyMap() {}

@Override
public IAnnotationMap put(String type, AnnotationDeclaration d) {
return new OneEntryMap(d);
}

@Override
public AnnotationDeclaration get(String type) {
return null;
}

@Override
public IAnnotationMap remove(String type) {
return this;
}
}

class OneEntryMap implements IAnnotationMap {
AnnotationDeclaration d;

public OneEntryMap(AnnotationDeclaration d) {
this.d = d;
}

@Override
public IAnnotationMap put(String type, AnnotationDeclaration d) {
public IAnnotationMap put(String type, AnnotationDeclaration ad) {
if(this.d.getTypeName().equals(type)) {
this.d = d;
this.d = ad;
return this;
}
return new TwoEntryMap(this.d, d);
return new TwoEntryMap(this.d, ad);
}

@Override
public AnnotationDeclaration get(String type) {
return (d.getTypeName().equals(type)) ? d : null;
}

@Override
public IAnnotationMap remove(String type) {
return (get(type) != null) ? EmptyMap.instance : this;
}
}
}

class TwoEntryMap implements IAnnotationMap {
AnnotationDeclaration d1;
AnnotationDeclaration d2;

public TwoEntryMap(AnnotationDeclaration d1,AnnotationDeclaration d2) {
this.d1 = d1;
this.d2 = d2;
}

@Override
public IAnnotationMap put(String type, AnnotationDeclaration d) {
AnnotationDeclaration dc = get(type);
if(dc == d1) {
Expand All @@ -312,30 +338,36 @@ public IAnnotationMap put(String type, AnnotationDeclaration d) {
return map;
}

@Override
public AnnotationDeclaration get(String type) {
return (d1.getTypeName().equals(type)) ? d1 : (d2.getTypeName().equals(type)) ? d2 : null;
}

@Override
public IAnnotationMap remove(String type) {
AnnotationDeclaration d = get(type);
return (d == d1) ? new OneEntryMap(d2) : (d == d2) ? new OneEntryMap(d1) : this;
}
}
}

class AnnotationMap implements IAnnotationMap {
Map<String, AnnotationDeclaration> annotationsByType = new HashMap<String, AnnotationDeclaration>(8);
Map<String, AnnotationDeclaration> annotationsByType = new HashMap<>(8);

AnnotationMap() {}


@Override
public IAnnotationMap put(String type, AnnotationDeclaration d) {
annotationsByType.put(type, d);
annotationsByType.put(type, d);

return this;
}

@Override
public AnnotationDeclaration get(String type) {
return annotationsByType.get(type);
}

@Override
public IAnnotationMap remove(String type) {
annotationsByType.remove(type);
return this;
Expand Down
Loading