Skip to content

Commit

Permalink
[WELD-966]; fix default @nAmed to use full name binding.
Browse files Browse the repository at this point in the history
  • Loading branch information
alesj committed Oct 19, 2011
1 parent 60a848d commit fb40498
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 6 deletions.
33 changes: 27 additions & 6 deletions impl/src/main/java/org/jboss/weld/bean/AbstractBean.java
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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> {

Expand Down Expand Up @@ -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() {
Expand All @@ -238,7 +260,6 @@ protected void initName() {
if (beanNameDefaulted || getMergedStereotypes().isBeanNameDefaulted()) {
this.name = getDefaultName();
log.trace(USING_DEFAULT_NAME, name, this);
return;
}
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -325,7 +346,7 @@ protected MergedStereotypes<T, S> getMergedStereotypes() {
* 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;
Expand All @@ -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;
Expand All @@ -355,7 +376,7 @@ public Class<T> getType() {
* 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;
Expand All @@ -365,7 +386,7 @@ public Set<Type> getTypes() {
* 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();
Expand Down
@@ -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 {
}
@@ -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 {
}
@@ -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.