Permalink
Browse files

[WELD-966]; fix default @Named to use full name binding.

  • Loading branch information...
1 parent 60a848d commit fb404984b44d50186b8f2d8125e26d3a1cd04010 @alesj alesj committed Oct 19, 2011
View
33 impl/src/main/java/org/jboss/weld/bean/AbstractBean.java
@@ -24,6 +24,7 @@
import org.jboss.weld.introspector.WeldAnnotated;
import org.jboss.weld.literal.AnyLiteral;
import org.jboss.weld.literal.DefaultLiteral;
+import org.jboss.weld.literal.NamedLiteral;
import org.jboss.weld.manager.BeanManagerImpl;
import org.jboss.weld.metadata.cache.MergedStereotypes;
import org.jboss.weld.metadata.cache.MetaAnnotationStore;
@@ -44,6 +45,7 @@
import java.lang.reflect.Type;
import java.util.Collections;
import java.util.HashSet;
+import java.util.Iterator;
import java.util.Map;
import java.util.Set;
@@ -66,6 +68,7 @@
* @param <T> the type of bean
* @param <S> the Class<?> of the bean type
* @author Pete Muir
+ * @author Ales Justin
*/
public abstract class AbstractBean<T, S> extends RIBean<T> {
@@ -213,6 +216,25 @@ protected void initDefaultQualifiers() {
}
}
this.qualifiers.add(AnyLiteral.INSTANCE);
+
+ // fix found Named, to have full name binding value
+ boolean foundRemoved = false;
+ Iterator<Annotation> qIter = qualifiers.iterator();
+ while (qIter.hasNext()) {
+ Annotation next = qIter.next();
+ if (next.annotationType().equals(Named.class)) {
+ Named named = (Named) next;
+ if (named.value().length() == 0) {
+ qIter.remove();
+ foundRemoved = true;
+ }
+ break;
+ }
+ }
+ if (foundRemoved) {
+ Named named = new NamedLiteral(getDefaultName());
+ qualifiers.add(named);
+ }
}
protected void initAlternative() {
@@ -238,7 +260,6 @@ protected void initName() {
if (beanNameDefaulted || getMergedStereotypes().isBeanNameDefaulted()) {
this.name = getDefaultName();
log.trace(USING_DEFAULT_NAME, name, this);
- return;
}
}
@@ -287,7 +308,7 @@ protected void specialize(BeanDeployerEnvironment environment) {
* Gets the binding types
*
* @return The set of binding types
- * @see org.jboss.weld.bean.BaseBean#getQualifiers()
+ * @see org.jboss.weld.bean.RIBean#getQualifiers()
*/
public Set<Annotation> getQualifiers() {
return qualifiers;
@@ -325,7 +346,7 @@ protected void specialize(BeanDeployerEnvironment environment) {
* Gets the name of the bean
*
* @return The name
- * @see org.jboss.weld.bean.BaseBean#getName()
+ * @see org.jboss.weld.bean.RIBean#getName()
*/
public String getName() {
return name;
@@ -335,7 +356,7 @@ public String getName() {
* Gets the scope type of the bean
*
* @return The scope type
- * @see org.jboss.weld.bean.BaseBean#getScope()
+ * @see org.jboss.weld.bean.RIBean#getScope()
*/
public Class<? extends Annotation> getScope() {
return scope;
@@ -355,7 +376,7 @@ public String getName() {
* Gets the API types of the bean
*
* @return The set of API types
- * @see org.jboss.weld.bean.BaseBean#getTypes()
+ * @see org.jboss.weld.bean.RIBean#getTypes()
*/
public Set<Type> getTypes() {
return types;
@@ -365,7 +386,7 @@ public String getName() {
* Indicates if bean is nullable
*
* @return True if nullable, false otherwise
- * @see org.jboss.weld.bean.BaseBean#isNullable()
+ * @see org.jboss.weld.bean.RIBean#isNullable()
*/
public boolean isNullable() {
return !isPrimitive();
View
32 tests-arquillian/src/test/java/org/jboss/weld/tests/named/Carp.java
@@ -0,0 +1,32 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2011, Red Hat, Inc., and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.weld.tests.named;
+
+import javax.inject.Named;
+
+/**
+ * @author <a href="mailto:ales.justin@jboss.org">Ales Justin</a>
+ */
+@Named
+public class Carp implements Fish {
+}
View
29 tests-arquillian/src/test/java/org/jboss/weld/tests/named/Fish.java
@@ -0,0 +1,29 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2011, Red Hat, Inc., and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.weld.tests.named;
+
+/**
+ * @author <a href="mailto:ales.justin@jboss.org">Ales Justin</a>
+ */
+public interface Fish {
+}
View
58 tests-arquillian/src/test/java/org/jboss/weld/tests/named/SmokeNamedTest.java
@@ -0,0 +1,58 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2011, Red Hat, Inc., and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.weld.tests.named;
+
+import org.jboss.arquillian.container.test.api.Deployment;
+import org.jboss.arquillian.junit.Arquillian;
+import org.jboss.shrinkwrap.api.Archive;
+import org.jboss.shrinkwrap.api.ShrinkWrap;
+import org.jboss.shrinkwrap.api.asset.EmptyAsset;
+import org.jboss.shrinkwrap.api.spec.JavaArchive;
+import org.junit.Assert;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import javax.inject.Inject;
+import javax.inject.Named;
+
+/**
+ * @author <a href="mailto:ales.justin@jboss.org">Ales Justin</a>
+ */
+@RunWith(Arquillian.class)
+public class SmokeNamedTest {
+
+ @Deployment
+ public static Archive getDeployment() {
+ return ShrinkWrap.create(JavaArchive.class)
+ .addPackage(Fish.class.getPackage())
+ .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
+ }
+
+ @Inject @Named
+ private Fish carp;
+
+ @Test
+ public void testNamedInject() throws Exception {
+ Assert.assertNotNull(carp);
+ }
+}

0 comments on commit fb40498

Please sign in to comment.