Skip to content

Commit

Permalink
Fixes and tests for type safe resolution
Browse files Browse the repository at this point in the history
git-svn-id: http://anonsvn.jboss.org/repos/weld/ri/trunk@137 1c488680-804c-0410-94cd-c6b725194a0e
  • Loading branch information
pmuir committed Oct 23, 2008
1 parent eafbd62 commit 5cd1e35
Show file tree
Hide file tree
Showing 15 changed files with 240 additions and 11 deletions.
6 changes: 6 additions & 0 deletions webbeans-ri/src/main/java/org/jboss/webbeans/BeanImpl.java
Expand Up @@ -78,5 +78,11 @@ public boolean isSerializable()
// TODO Auto-generated method stub
return false;
}

@Override
public String toString()
{
return model.toString();
}

}
10 changes: 9 additions & 1 deletion webbeans-ri/src/main/java/org/jboss/webbeans/ManagerImpl.java
Expand Up @@ -104,8 +104,16 @@ public EjbManager getEjbManager()
public <T> Set<Bean<T>> resolveByType(Class<T> apiType,
Annotation... bindingTypes)
{
return getResolutionManager().get(
Set<Bean<T>> beans = getResolutionManager().get(
new SimpleInjectable<T>(apiType, bindingTypes));
if (beans == null)
{
return new HashSet<Bean<T>>();
}
else
{
return beans;
}
}

public <T> Set<Bean<T>> resolveByType(TypeLiteral<T> apiType,
Expand Down
@@ -1,6 +1,7 @@
package org.jboss.webbeans;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

Expand All @@ -17,7 +18,8 @@ public class ResolutionManager

public ResolutionManager(ManagerImpl manager)
{
resolvedInjectionPoints = new HashMap<Injectable<?, ?>, Set<?>>();
this.resolvedInjectionPoints = new HashMap<Injectable<?, ?>, Set<?>>();
this.injectionPoints = new HashSet<Injectable<?,?>>();
this.manager = manager;
}

Expand Down
Expand Up @@ -6,6 +6,7 @@
import java.util.List;
import java.util.Set;

import javax.webbeans.BindingType;
import javax.webbeans.manager.Bean;

import org.jboss.webbeans.ManagerImpl;
Expand Down Expand Up @@ -64,14 +65,11 @@ public Set<Bean<?>> getPossibleBeans(Set<Bean<?>> beans)
if (bean.getTypes().contains(getType()))
{
List<Annotation> beanBindingTypes = new ArrayList<Annotation>(bean.getBindingTypes());
for (Annotation annotation : annotatedItem.getAnnotations())
if (beanBindingTypes.containsAll(annotatedItem.getAnnotations()))
{
if (beanBindingTypes.contains(annotation))
{
// TODO inspect annotation parameters
// TODO inspect deployment types
resolvedBeans.add(bean);
}
// TODO inspect annotation parameters
// TODO inspect deployment types
resolvedBeans.add(bean);
}
}
}
Expand All @@ -81,15 +79,23 @@ public Set<Bean<?>> getPossibleBeans(Set<Bean<?>> beans)
@Override
public boolean equals(Object other)
{
// TODO Currently you must have any annotation literals on other for this to work, probably need to iterate over the set and check both directions
if (other instanceof Injectable)
{
Injectable<?, ?> that = (Injectable<?, ?>) other;
return this.getAnnotatedItem().equals(that.getAnnotatedItem());
return this.getAnnotatedItem().getType().isAssignableFrom(that.getAnnotatedItem().getType()) &&
that.getAnnotatedItem().getAnnotations(BindingType.class).equals(this.getAnnotatedItem().getAnnotations(BindingType.class));
}
else
{
return false;
}
}

@Override
public int hashCode()
{
return 0;
}

}
Expand Up @@ -108,9 +108,15 @@ public boolean equals(Object other)
if (other instanceof AnnotatedItem)
{
AnnotatedItem<?, ?> that = (AnnotatedItem<?, ?>) other;
return this.getAnnotations().equals(that.getAnnotations()) && this.getDelegate().equals(that.getDelegate());
return this.getAnnotations().equals(that.getAnnotations()) && this.getType().equals(that.getType());
}
return false;
}

@Override
public int hashCode()
{
return getType().hashCode();
}

}
Expand Up @@ -3,17 +3,26 @@
import java.util.HashSet;
import java.util.Set;

import javax.webbeans.AnnotationLiteral;
import javax.webbeans.Current;
import javax.webbeans.manager.Bean;

import org.jboss.webbeans.BeanImpl;
import org.jboss.webbeans.ResolutionManager;
import org.jboss.webbeans.bindings.CurrentAnnotationLiteral;
import org.jboss.webbeans.injectable.InjectableField;
import org.jboss.webbeans.introspector.SimpleAnnotatedType;
import org.jboss.webbeans.model.SimpleComponentModel;
import org.jboss.webbeans.test.annotations.Chunky;
import org.jboss.webbeans.test.annotations.Whitefish;
import org.jboss.webbeans.test.components.Animal;
import org.jboss.webbeans.test.components.Cod;
import org.jboss.webbeans.test.components.FishFarm;
import org.jboss.webbeans.test.components.Haddock;
import org.jboss.webbeans.test.components.Salmon;
import org.jboss.webbeans.test.components.ScottishFish;
import org.jboss.webbeans.test.components.SeaBass;
import org.jboss.webbeans.test.components.Sole;
import org.jboss.webbeans.test.components.Tuna;
import org.jboss.webbeans.util.Reflections;
import org.testng.annotations.Test;
Expand Down Expand Up @@ -44,6 +53,39 @@ public void testSingleApiTypeWithCurrent() throws Exception
assert possibleTargets.contains(tunaBean);
}

@Test
public void testOneBindingType() throws Exception
{
InjectableField<ScottishFish> scottishFishField = new InjectableField<ScottishFish>(FishFarm.class.getDeclaredField("scottishFish"));
Bean<Cod> codBean = new BeanImpl<Cod>(new SimpleComponentModel<Cod>(new SimpleAnnotatedType<Cod>(Cod.class), getEmptyAnnotatedItem(Cod.class), super.manager), manager);
Bean<Salmon> salmonBean = new BeanImpl<Salmon>(new SimpleComponentModel<Salmon>(new SimpleAnnotatedType<Salmon>(Salmon.class), getEmptyAnnotatedItem(Salmon.class), super.manager), manager);
Bean<Sole> soleBean = new BeanImpl<Sole>(new SimpleComponentModel<Sole>(new SimpleAnnotatedType<Sole>(Sole.class), getEmptyAnnotatedItem(Sole.class), super.manager), manager);
Set<Bean<?>> beans = new HashSet<Bean<?>>();
beans.add(codBean);
beans.add(salmonBean);
beans.add(soleBean);
Set<Bean<?>> possibleTargets = scottishFishField.getPossibleBeans(beans);
assert possibleTargets.size() == 2;
assert possibleTargets.contains(codBean);
assert possibleTargets.contains(soleBean);
}

@Test
public void testABindingType() throws Exception
{
InjectableField<Animal> whiteChunkyFishField = new InjectableField<Animal>(FishFarm.class.getDeclaredField("whiteChunkyFish"));
Bean<Cod> codBean = new BeanImpl<Cod>(new SimpleComponentModel<Cod>(new SimpleAnnotatedType<Cod>(Cod.class), getEmptyAnnotatedItem(Cod.class), super.manager), manager);
Bean<Salmon> salmonBean = new BeanImpl<Salmon>(new SimpleComponentModel<Salmon>(new SimpleAnnotatedType<Salmon>(Salmon.class), getEmptyAnnotatedItem(Salmon.class), super.manager), manager);
Bean<Sole> soleBean = new BeanImpl<Sole>(new SimpleComponentModel<Sole>(new SimpleAnnotatedType<Sole>(Sole.class), getEmptyAnnotatedItem(Sole.class), super.manager), manager);
Set<Bean<?>> beans = new HashSet<Bean<?>>();
beans.add(codBean);
beans.add(salmonBean);
beans.add(soleBean);
Set<Bean<?>> possibleTargets = whiteChunkyFishField.getPossibleBeans(beans);
assert possibleTargets.size() == 1;
assert possibleTargets.contains(codBean);
}

@Test
public void testMultipleApiTypeWithCurrent() throws Exception
{
Expand All @@ -59,4 +101,53 @@ public void testMultipleApiTypeWithCurrent() throws Exception
assert possibleTargets.contains(haddockBean);
}

@Test
public void testResolveByType() throws Exception
{
InjectableField<Animal> whiteChunkyFishField = new InjectableField<Animal>(FishFarm.class.getDeclaredField("whiteChunkyFish"));
InjectableField<Animal> animalField = new InjectableField<Animal>(FishFarm.class.getDeclaredField("animal"));
InjectableField<ScottishFish> scottishFishField = new InjectableField<ScottishFish>(FishFarm.class.getDeclaredField("scottishFish"));
InjectableField<Tuna> tunaField = new InjectableField<Tuna>(FishFarm.class.getDeclaredField("tuna"));

Bean<Tuna> tunaBean = new BeanImpl<Tuna>(new SimpleComponentModel<Tuna>(new SimpleAnnotatedType<Tuna>(Tuna.class), getEmptyAnnotatedItem(Tuna.class), super.manager), manager);
Bean<Cod> codBean = new BeanImpl<Cod>(new SimpleComponentModel<Cod>(new SimpleAnnotatedType<Cod>(Cod.class), getEmptyAnnotatedItem(Cod.class), super.manager), manager);
Bean<Salmon> salmonBean = new BeanImpl<Salmon>(new SimpleComponentModel<Salmon>(new SimpleAnnotatedType<Salmon>(Salmon.class), getEmptyAnnotatedItem(Salmon.class), super.manager), manager);
Bean<Sole> soleBean = new BeanImpl<Sole>(new SimpleComponentModel<Sole>(new SimpleAnnotatedType<Sole>(Sole.class), getEmptyAnnotatedItem(Sole.class), super.manager), manager);
Bean<SeaBass> seaBassBean = new BeanImpl<SeaBass>(new SimpleComponentModel<SeaBass>(new SimpleAnnotatedType<SeaBass>(SeaBass.class), getEmptyAnnotatedItem(SeaBass.class), super.manager), manager);
Bean<Haddock> haddockBean = new BeanImpl<Haddock>(new SimpleComponentModel<Haddock>(new SimpleAnnotatedType<Haddock>(Haddock.class), getEmptyAnnotatedItem(Haddock.class), super.manager), manager);

manager.addBean(tunaBean);
manager.addBean(codBean);
manager.addBean(salmonBean);
manager.addBean(soleBean);
manager.addBean(haddockBean);
manager.addBean(seaBassBean);

ResolutionManager resolutionManager = manager.getResolutionManager();
resolutionManager.addInjectionPoint(whiteChunkyFishField);
resolutionManager.addInjectionPoint(animalField);
resolutionManager.addInjectionPoint(scottishFishField);
resolutionManager.addInjectionPoint(tunaField);

resolutionManager.registerInjectionPoints();

System.out.println("injection points registered");

assert manager.resolveByType(Tuna.class, new CurrentAnnotationLiteral()).size() == 1;
assert manager.resolveByType(Tuna.class, new CurrentAnnotationLiteral()).contains(tunaBean);

assert manager.resolveByType(Animal.class, new CurrentAnnotationLiteral()).size() == 3;
assert manager.resolveByType(Animal.class, new CurrentAnnotationLiteral()).contains(salmonBean);
assert manager.resolveByType(Animal.class, new CurrentAnnotationLiteral()).contains(seaBassBean);
assert manager.resolveByType(Animal.class, new CurrentAnnotationLiteral()).contains(haddockBean);

assert manager.resolveByType(Animal.class, new AnnotationLiteral<Chunky>() {}, new AnnotationLiteral<Whitefish>() {}).size() == 1;
assert manager.resolveByType(Animal.class, new AnnotationLiteral<Chunky>() {}, new AnnotationLiteral<Whitefish>() {}).contains(codBean);

assert manager.resolveByType(ScottishFish.class, new AnnotationLiteral<Whitefish>() {}).size() == 2;
assert manager.resolveByType(ScottishFish.class, new AnnotationLiteral<Whitefish>() {}).contains(codBean);
assert manager.resolveByType(ScottishFish.class, new AnnotationLiteral<Whitefish>() {}).contains(soleBean);

}

}
@@ -0,0 +1,22 @@
package org.jboss.webbeans.test.annotations;

import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import javax.webbeans.BindingType;

@Target( { TYPE, METHOD, PARAMETER, FIELD })
@Retention(RUNTIME)
@Documented
@BindingType
public @interface Chunky
{

}
@@ -0,0 +1,22 @@
package org.jboss.webbeans.test.annotations;

import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import javax.webbeans.BindingType;

@Target( { TYPE, METHOD, PARAMETER, FIELD })
@Retention(RUNTIME)
@Documented
@BindingType
public @interface Whitefish
{

}
@@ -0,0 +1,14 @@
package org.jboss.webbeans.test.components;

import javax.webbeans.Production;

import org.jboss.webbeans.test.annotations.Chunky;
import org.jboss.webbeans.test.annotations.Whitefish;

@Production
@Whitefish
@Chunky
public class Cod implements ScottishFish
{

}
@@ -0,0 +1,9 @@
package org.jboss.webbeans.test.components;

import javax.webbeans.Production;

@Production
public class Farmer<T>
{

}
Expand Up @@ -3,6 +3,9 @@
import javax.webbeans.Current;
import javax.webbeans.Production;

import org.jboss.webbeans.test.annotations.Chunky;
import org.jboss.webbeans.test.annotations.Whitefish;

@Production
public class FishFarm
{
Expand All @@ -15,4 +18,13 @@ public class FishFarm
@Current
private Animal animal;

@SuppressWarnings("unused")
@Whitefish
private ScottishFish scottishFish;

@Whitefish @Chunky
private Animal whiteChunkyFish;

private Farmer<ScottishFish> scottishFishFarmer;

}
Expand Up @@ -3,6 +3,8 @@
import javax.webbeans.Named;
import javax.webbeans.Production;

import org.jboss.webbeans.test.annotations.Whitefish;

@Production
@Named
public class Haddock implements Animal
Expand Down
@@ -0,0 +1,11 @@
package org.jboss.webbeans.test.components;

import javax.webbeans.Production;

import org.jboss.webbeans.test.annotations.Whitefish;

@Production
public class Salmon implements ScottishFish
{

}
@@ -0,0 +1,6 @@
package org.jboss.webbeans.test.components;

public interface ScottishFish extends Animal
{

}
@@ -0,0 +1,12 @@
package org.jboss.webbeans.test.components;

import javax.webbeans.Production;

import org.jboss.webbeans.test.annotations.Whitefish;

@Production
@Whitefish
public class Sole implements ScottishFish
{

}

0 comments on commit 5cd1e35

Please sign in to comment.