Author: scabanovich
Date: 2011-03-29 20:31:46 -0400 (Tue, 29 Mar 2011)
New Revision: 30132
Removed:
trunk/cdi/plugins/org.jboss.tools.cdi.solder.core/bin/org/
Modified:
trunk/cdi/plugins/org.jboss.tools.cdi.solder.core/src/org/jboss/tools/cdi/solder/core/BeanNameFeature.java
Log:
JBIDE-3120
https://issues.jboss.org/browse/JBIDE-3120
Modified:
trunk/cdi/plugins/org.jboss.tools.cdi.solder.core/src/org/jboss/tools/cdi/solder/core/BeanNameFeature.java
===================================================================
---
trunk/cdi/plugins/org.jboss.tools.cdi.solder.core/src/org/jboss/tools/cdi/solder/core/BeanNameFeature.java 2011-03-30
00:31:25 UTC (rev 30131)
+++
trunk/cdi/plugins/org.jboss.tools.cdi.solder.core/src/org/jboss/tools/cdi/solder/core/BeanNameFeature.java 2011-03-30
00:31:46 UTC (rev 30132)
@@ -10,23 +10,123 @@
******************************************************************************/
package org.jboss.tools.cdi.solder.core;
+import java.beans.Introspector;
+
+import org.eclipse.jdt.core.IAnnotation;
+import org.eclipse.jdt.core.IMemberValuePair;
+import org.eclipse.jdt.core.JavaModelException;
+import org.jboss.tools.cdi.core.CDIConstants;
+import org.jboss.tools.cdi.core.CDIUtil;
+import org.jboss.tools.cdi.core.IAnnotationDeclaration;
import org.jboss.tools.cdi.core.IBean;
+import org.jboss.tools.cdi.core.ICDIAnnotation;
+import org.jboss.tools.cdi.core.IClassBean;
+import org.jboss.tools.cdi.core.IProducerField;
+import org.jboss.tools.cdi.core.IProducerMethod;
import org.jboss.tools.cdi.core.extension.feature.IBeanNameFeature;
+import org.jboss.tools.cdi.internal.core.impl.AbstractBeanElement;
+import org.jboss.tools.cdi.internal.core.impl.AnnotationDeclaration;
+import org.jboss.tools.cdi.internal.core.impl.definition.AbstractMemberDefinition;
+import org.jboss.tools.cdi.internal.core.impl.definition.AbstractTypeDefinition;
+import org.jboss.tools.cdi.internal.core.impl.definition.PackageDefinition;
+import org.jboss.tools.common.util.BeanUtil;
+import org.jboss.tools.common.util.EclipseJavaUtil;
/**
*
* @author Viacheslav Kabanovich
- *
+ *
*/
public class BeanNameFeature implements IBeanNameFeature {
/**
- * The singleton instance that processes requests without building inner state.
+ * The singleton instance that processes requests without building inner
+ * state.
*/
public static final IBeanNameFeature instance = new BeanNameFeature();
public String computeBeanName(IBean bean) {
+ AbstractBeanElement abe = (AbstractBeanElement)bean;
+ AbstractMemberDefinition d = abe.getDefinition();
+ if(d == null) return null;
+ IAnnotationDeclaration named = CDIUtil.getNamedDeclaration(bean);
+ AbstractTypeDefinition t = d.getTypeDefinition();
+ PackageDefinition p = d.getPackageDefinition();
+ AnnotationDeclaration namedOnPackage = null;
+ AnnotationDeclaration fullyQualifiedOnPackage = null;
+ if(p != null) {
+ namedOnPackage = p.getAnnotation(CDIConstants.NAMED_QUALIFIER_TYPE_NAME);
+ fullyQualifiedOnPackage =
p.getAnnotation(CDISolderConstants.FULLY_QUALIFIED_ANNOTATION_TYPE_NAME);
+ }
+
+ AnnotationDeclaration fullyQualified =
d.getAnnotation(CDISolderConstants.FULLY_QUALIFIED_ANNOTATION_TYPE_NAME);
+
+ //@FullyQualified
+ if((fullyQualified != null || fullyQualifiedOnPackage != null) && (named !=
null || namedOnPackage != null)) {
+ if(named == null) named = namedOnPackage;
+ String pkg = resolvePackageName(fullyQualified, fullyQualifiedOnPackage, t, p);
+ String simpleName = getSimpleBeanName(bean, named);
+ return (simpleName == null) ? null : pkg.length() > 0 ? pkg + "." +
simpleName : simpleName;
+ }
+
+ // @Named on package only
+ if(named == null && namedOnPackage != null) {
+ return getSimpleBeanName(bean, namedOnPackage);
+ }
+
return null;
}
+ private String getStringValue(IAnnotation a) {
+ if(a == null) return null;
+ try {
+ IMemberValuePair[] ps = a.getMemberValuePairs();
+ if(ps != null && ps.length > 0 && ps[0].getValue() != null) {
+ return ps[0].getValue().toString();
+ }
+ } catch (JavaModelException e) {
+ CDISolderCorePlugin.getDefault().logError(e);
+ }
+ return null;
+ }
+
+ private String resolvePackageName(AnnotationDeclaration fullyQualified,
AnnotationDeclaration fullyQualifiedOnPackage, AbstractTypeDefinition t, PackageDefinition
p) {
+ String contextClass = null;
+ IAnnotation a = fullyQualified != null ? fullyQualified.getDeclaration() :
fullyQualifiedOnPackage.getDeclaration();
+ contextClass = getStringValue(a);
+ if(contextClass == null) {
+ contextClass = t == null ? "" : t.getQualifiedName();
+ } else if(fullyQualified != null && t != null) {
+ String resolved = EclipseJavaUtil.resolveType(t.getType(), contextClass);
+ if(resolved != null) contextClass = resolved;
+ } else if(fullyQualifiedOnPackage != null) {
+ contextClass = p.resolveType(contextClass);
+ }
+ int dot = contextClass.lastIndexOf('.');
+ return dot < 0 ? "" : contextClass.substring(0, dot);
+ }
+
+ private String getSimpleBeanName(IBean bean, IAnnotationDeclaration named) {
+ String simpleName = null;
+ if(named != null) {
+ simpleName = getStringValue(named.getDeclaration());
+ }
+ if(simpleName != null && simpleName.length() > 0) {
+ //do nothing
+ } else if(bean instanceof IClassBean) {
+ simpleName =
Introspector.decapitalize(((IClassBean)bean).getBeanClass().getElementName());
+ } else if(bean instanceof IProducerField) {
+ simpleName = ((IProducerField)bean).getField().getElementName();
+ } else if(bean instanceof IProducerMethod) {
+ IProducerMethod m = (IProducerMethod)bean;
+ String mn = m.getMethod().getElementName();
+ if(BeanUtil.isGetter(m.getMethod())) {
+ simpleName = BeanUtil.getPropertyName(mn);
+ } else {
+ simpleName = mn;
+ }
+ }
+
+ return simpleName;
+ }
}