Skip to content

Commit

Permalink
WBRI-156, WBRI-173
Browse files Browse the repository at this point in the history
git-svn-id: http://anonsvn.jboss.org/repos/weld/ri/trunk@2118 1c488680-804c-0410-94cd-c6b725194a0e
  • Loading branch information
pmuir committed Mar 20, 2009
1 parent c7c6ff1 commit 94f3302
Show file tree
Hide file tree
Showing 26 changed files with 715 additions and 773 deletions.
41 changes: 39 additions & 2 deletions impl/src/main/java/org/jboss/webbeans/BeanValidator.java
Expand Up @@ -17,16 +17,23 @@
package org.jboss.webbeans;

import java.lang.annotation.Annotation;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.TypeVariable;
import java.lang.reflect.WildcardType;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Set;

import javax.event.Event;
import javax.event.Fires;
import javax.inject.AmbiguousDependencyException;
import javax.inject.DefinitionException;
import javax.inject.InconsistentSpecializationException;
import javax.inject.Instance;
import javax.inject.New;
import javax.inject.NullableDependencyException;
import javax.inject.Obtains;
import javax.inject.UnproxyableDependencyException;
import javax.inject.UnsatisfiedDependencyException;
import javax.inject.UnserializableDependencyException;
Expand All @@ -36,7 +43,7 @@
import org.jboss.webbeans.bean.NewEnterpriseBean;
import org.jboss.webbeans.bean.NewSimpleBean;
import org.jboss.webbeans.bean.RIBean;
import org.jboss.webbeans.injection.ResolvableAnnotatedClass;
import org.jboss.webbeans.injection.resolution.ResolvableAnnotatedClass;
import org.jboss.webbeans.introspector.AnnotatedItem;
import org.jboss.webbeans.metadata.MetaDataCache;
import org.jboss.webbeans.util.Beans;
Expand Down Expand Up @@ -78,9 +85,11 @@ public void validate()
{
throw new DefinitionException("The injection point " + injectionPoint + " is annotated with @New which cannot be combined with other binding types");
}
checkFacadeInjectionPoint(injectionPoint, Obtains.class, Instance.class);
checkFacadeInjectionPoint(injectionPoint, Fires.class, Event.class);
Annotation[] bindings = injectionPoint.getBindings().toArray(new Annotation[0]);
AnnotatedItem<?, ?> annotatedItem = ResolvableAnnotatedClass.of(injectionPoint.getType(), bindings);
Set<?> resolvedBeans = manager.resolveByType(annotatedItem, bindings);
Set<?> resolvedBeans = manager.resolveByType(annotatedItem, injectionPoint, bindings);
if (resolvedBeans.isEmpty())
{
throw new UnsatisfiedDependencyException("The injection point " + injectionPoint + " with binding types " + Names.annotationsToString(injectionPoint.getBindings()) + " in " + bean + " has unsatisfied dependencies with binding types ");
Expand Down Expand Up @@ -133,5 +142,33 @@ private boolean hasHigherPrecedence(Class<? extends Annotation> deploymentType,
Comparator<Class<? extends Annotation>> comparator = new ListComparator<Class<? extends Annotation>>(manager.getEnabledDeploymentTypes());
return comparator.compare(deploymentType, otherDeploymentType) > 0;
}

private void checkFacadeInjectionPoint(InjectionPoint injectionPoint, Class<? extends Annotation> annotationType, Class<?> type)
{
if (injectionPoint.isAnnotationPresent(annotationType))
{
if (injectionPoint.getType() instanceof ParameterizedType)
{
ParameterizedType parameterizedType = (ParameterizedType) injectionPoint.getType();
if (!type.isAssignableFrom((Class<?>) parameterizedType.getRawType()))
{
throw new DefinitionException("An injection point annotated " + annotationType + " must have type " + type + " " + injectionPoint);
}
if (parameterizedType.getActualTypeArguments()[0] instanceof TypeVariable)
{
throw new DefinitionException("An injection point annotated " + annotationType + " cannot have a type variable type parameter " + injectionPoint);
}
if (parameterizedType.getActualTypeArguments()[0] instanceof WildcardType)
{
throw new DefinitionException("An injection point annotated " + annotationType + " cannot have a wildcard type parameter " + injectionPoint);
}
}
else
{
throw new DefinitionException("An injection point annotated " + annotationType + " must have a type parameter " + injectionPoint);
}
}

}

}
65 changes: 18 additions & 47 deletions impl/src/main/java/org/jboss/webbeans/FacadeImpl.java
Expand Up @@ -24,7 +24,7 @@
import javax.inject.DuplicateBindingTypeException;
import javax.inject.manager.Manager;

import org.jboss.webbeans.util.Reflections;
import org.jboss.webbeans.metadata.MetaDataCache;

/**
* Common implementation for binding-type-based helpers
Expand All @@ -35,6 +35,9 @@
*/
public abstract class FacadeImpl<T>
{

private static final Annotation[] EMPTY_BINDINGS = new Annotation[0];

// The binding types the helper operates on
protected final Set<? extends Annotation> bindings;
// The Web Beans manager
Expand All @@ -49,72 +52,40 @@ public abstract class FacadeImpl<T>
* @param manager The Web Beans manager
* @param bindings The binding types
*/
protected FacadeImpl(Class<T> type, Manager manager, Annotation... bindings)
protected FacadeImpl(Class<T> type, Manager manager, Set<? extends Annotation> bindings)
{
this.manager = manager;
this.type = type;
this.bindings = mergeBindings(new HashSet<Annotation>(), bindings);
this.bindings = bindings;
}

/**
* Merges and validates the current and new bindings
*
* Checks with an abstract method for annotations to exclude
* Gets a string representation
*
* @param currentBindings Existing bindings
* @param newBindings New bindings
* @return The union of the bindings
* @return A string representation
*/
protected Set<Annotation> mergeBindings(Set<? extends Annotation> currentBindings, Annotation... newBindings)
@Override
public String toString()
{
return "Abstract facade implmentation";
}

protected Annotation[] mergeInBindings(Annotation... newBindings)
{
Set<Annotation> result = new HashSet<Annotation>();
result.addAll(currentBindings);
result.addAll(bindings);
for (Annotation newAnnotation : newBindings)
{
if (!Reflections.isBindings(newAnnotation))
if (!MetaDataCache.instance().getBindingTypeModel(newAnnotation.annotationType()).isValid())
{
throw new IllegalArgumentException(newAnnotation + " is not a binding for " + this);
}
if (result.contains(newAnnotation))
{
throw new DuplicateBindingTypeException(newAnnotation + " is already present in the bindings list for " + this);
}
if (!getFilteredAnnotations().contains(newAnnotation.annotationType()))
{
result.add(newAnnotation);
}
}
return result;
}

/**
* Gets a set of annotation classes to ignore
*
* @return A set of annotation classes to ignore
*/
protected abstract Set<Class<? extends Annotation>> getFilteredAnnotations();

/**
* Merges the binding this helper operates upon with the parameters
*
* @param bindings The bindings to merge
*
* @return The union of the binding types
*/
protected Annotation[] mergeBindings(Annotation... newBindings)
{
return mergeBindings(bindings, newBindings).toArray(new Annotation[0]);
}

/**
* Gets a string representation
*
* @return A string representation
*/
@Override
public String toString()
{
return "Abstract facade implmentation";
return result.toArray(EMPTY_BINDINGS);
}

}
27 changes: 9 additions & 18 deletions impl/src/main/java/org/jboss/webbeans/InstanceImpl.java
Expand Up @@ -18,7 +18,6 @@
package org.jboss.webbeans;

import java.lang.annotation.Annotation;
import java.util.Collections;
import java.util.Set;

import javax.inject.Instance;
Expand All @@ -34,14 +33,21 @@
*/
public class InstanceImpl<T> extends FacadeImpl<T> implements Instance<T>
{


public static <I> Instance<I> of(Class<I> clazz, ManagerImpl manager, Set<Annotation> annotations)
{
return new InstanceImpl<I>(clazz, manager, annotations);
}

/**
* Constructor
*
* @param type The obtainable type
* @param manager The Web Beans manager
* @param bindings The binding types
*/
public InstanceImpl(Class<T> type, Manager manager, Annotation... bindings)
private InstanceImpl(Class<T> type, Manager manager, Set<Annotation> bindings)
{
super(type, manager, bindings);
}
Expand All @@ -57,7 +63,7 @@ public InstanceImpl(Class<T> type, Manager manager, Annotation... bindings)
*/
public T get(Annotation... bindings)
{
return manager.getInstanceByType(type, mergeBindings(bindings));
return manager.getInstanceByType(type, mergeInBindings(bindings));
}

/**
Expand All @@ -71,19 +77,4 @@ public String toString()
return "Obtainable instance for type " + type + " and binding types " + bindings;
}

/**
* Filters annotations from the binding type or parameter lists
*
* This implementation filters no annotations
*
* @return A set of annotations to filter
*
* @see org.jboss.webbeans.FacadeImpl#getFilteredAnnotations
*/
@Override
protected Set<Class<? extends Annotation>> getFilteredAnnotations()
{
return Collections.emptySet();
}

}

0 comments on commit 94f3302

Please sign in to comment.