Skip to content

Commit

Permalink
WBRI-320, add support for @New(value=XXX.class)
Browse files Browse the repository at this point in the history
git-svn-id: http://anonsvn.jboss.org/repos/weld/ri/trunk@3468 1c488680-804c-0410-94cd-c6b725194a0e
  • Loading branch information
pmuir committed Aug 12, 2009
1 parent 63aff3f commit b600a8b
Show file tree
Hide file tree
Showing 12 changed files with 239 additions and 22 deletions.
3 changes: 3 additions & 0 deletions api/src/main/java/javax/enterprise/inject/New.java
Expand Up @@ -40,4 +40,7 @@
@BindingType
public @interface New
{

Class<?> value() default New.class;

}
1 change: 1 addition & 0 deletions impl/src/main/java/org/jboss/webbeans/BeanManagerImpl.java
Expand Up @@ -489,6 +489,7 @@ public Iterable<String> transform(BeanManagerImpl beanManager)
public void addAccessibleBeanManager(BeanManagerImpl accessibleBeanManager)
{
accessibleManagers.add(accessibleBeanManager);
beanResolver.clear();
}

protected Set<BeanManagerImpl> getAccessibleManagers()
Expand Down
18 changes: 14 additions & 4 deletions impl/src/main/java/org/jboss/webbeans/bean/NewEnterpriseBean.java
Expand Up @@ -17,7 +17,6 @@
package org.jboss.webbeans.bean;

import java.lang.annotation.Annotation;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

Expand All @@ -35,7 +34,6 @@
*/
public class NewEnterpriseBean<T> extends EnterpriseBean<T> implements NewBean
{
private static Set<Annotation> NEW_BINDING_SET = new HashSet<Annotation>(Arrays.asList(new NewLiteral()));

/**
* Creates an instance of a NewEnterpriseBean from an annotated class
Expand All @@ -48,16 +46,28 @@ public static <T> NewEnterpriseBean<T> of(WBClass<T> clazz, BeanManagerImpl mana
{
return new NewEnterpriseBean<T>(clazz, manager, environment);
}

private Set<Annotation> bindings;

/**
* Protected constructor
*
* @param type An annotated class
* @param manager The Web Beans manager
*/
protected NewEnterpriseBean(WBClass<T> type, BeanManagerImpl manager, BeanDeployerEnvironment environment)
protected NewEnterpriseBean(final WBClass<T> type, BeanManagerImpl manager, BeanDeployerEnvironment environment)
{
super(type, manager, environment);
this.bindings = new HashSet<Annotation>();
this.bindings.add(new NewLiteral()
{

public Class<?> value()
{
return type.getJavaClass();
}

});
}

/**
Expand Down Expand Up @@ -96,7 +106,7 @@ public String getName()
@Override
public Set<Annotation> getBindings()
{
return NEW_BINDING_SET;
return bindings;
}

@Override
Expand Down
18 changes: 14 additions & 4 deletions impl/src/main/java/org/jboss/webbeans/bean/NewSimpleBean.java
Expand Up @@ -17,7 +17,6 @@
package org.jboss.webbeans.bean;

import java.lang.annotation.Annotation;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

Expand All @@ -34,7 +33,6 @@
*/
public class NewSimpleBean<T> extends SimpleBean<T> implements NewBean
{
private static Set<Annotation> NEW_BINDING_SET = new HashSet<Annotation>(Arrays.asList(new NewLiteral()));

/**
* Creates an instance of a NewSimpleBean from an annotated class
Expand All @@ -47,16 +45,28 @@ public static <T> NewSimpleBean<T> of(WBClass<T> clazz, BeanManagerImpl manager)
{
return new NewSimpleBean<T>(clazz, manager);
}

private Set<Annotation> bindings;

/**
* Protected constructor
*
* @param type An annotated class
* @param manager The Web Beans manager
*/
protected NewSimpleBean(WBClass<T> type, BeanManagerImpl manager)
protected NewSimpleBean(final WBClass<T> type, BeanManagerImpl manager)
{
super(type, manager);
this.bindings = new HashSet<Annotation>();
this.bindings.add(new NewLiteral()
{

public Class<?> value()
{
return type.getJavaClass();
}

});
}

/**
Expand Down Expand Up @@ -95,7 +105,7 @@ public String getName()
@Override
public Set<Annotation> getBindings()
{
return NEW_BINDING_SET;
return bindings;
}

@Override
Expand Down
10 changes: 9 additions & 1 deletion impl/src/main/java/org/jboss/webbeans/literal/NewLiteral.java
Expand Up @@ -24,4 +24,12 @@
*
* @author Pete Muir
*/
public class NewLiteral extends AnnotationLiteral<New> implements New {}
public class NewLiteral extends AnnotationLiteral<New> implements New
{

public Class<?> value()
{
return New.class;
}

}
Expand Up @@ -44,5 +44,33 @@ public boolean isAssignableTo(Class<?> clazz)
{
return delegate().isAssignableTo(clazz);
}

public <A extends Annotation> A getAnnotation(Class<A> annotationType)
{
return delegate().getAnnotation(annotationType);
}

public Class<?> getJavaClass()
{
return delegate().getJavaClass();
}

@Override
public boolean equals(Object obj)
{
return delegate().equals(obj);
}

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

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

}
@@ -0,0 +1,88 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2008, Red Hat Middleware LLC, and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.webbeans.resolution;

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

import javax.enterprise.inject.New;

import org.jboss.webbeans.literal.NewLiteral;

/**
* @author pmuir
*
*/
public class NewResolvableTransformer implements ResolvableTransformer
{

public Resolvable transform(final Resolvable element)
{
if (element.isAnnotationPresent(New.class) && element.getJavaClass() != null)
{
New originalNewAnnotation = element.getAnnotation(New.class);
if (originalNewAnnotation.value().equals(New.class))
{
final Set<Annotation> bindings = new HashSet<Annotation>(element.getBindings());
final New newNewAnnotation = new NewLiteral()
{

@Override
public Class<?> value()
{
return element.getJavaClass();
}

};
bindings.remove(originalNewAnnotation);
bindings.add(newNewAnnotation);
return new ForwardingResolvable()
{

@Override
protected Resolvable delegate()
{
return element;
}

@Override
public Set<Annotation> getBindings()
{
return bindings;
}

@Override
public <A extends Annotation> A getAnnotation(Class<A> annotationType)
{
if (annotationType.equals(New.class))
{
return annotationType.cast(newNewAnnotation);
}
else
{
return delegate().getAnnotation(annotationType);
}
}

};
}
}
return element;
}

}
52 changes: 47 additions & 5 deletions impl/src/main/java/org/jboss/webbeans/resolution/Resolvable.java
Expand Up @@ -21,20 +21,62 @@
import java.util.Set;

/**
* Something that is resovable by the resolver
* Something that is resovable by the resolver. A resolvable is defined by it's
* bindings and type closure
*
* @author pmuir
*
*
*/
public interface Resolvable
{


/**
* Get the bindings to use for resolution. @Current will be returned if no
* bindings were specified
*
* @return the bindings
*/
public Set<Annotation> getBindings();


/**
* Check if an annotation is present
*
* @param annotationType the annotation type to look for
* @return true if it is present
*/
public boolean isAnnotationPresent(Class<? extends Annotation> annotationType);

/**
* Get the instance of the Annotation
*
* @param <A> the type of the annotation
* @param annotationType the type of the annotation
* @return the annotation instance
*/
public <A extends Annotation> A getAnnotation(Class<A> annotationType);

/**
* Check if this resolvable's type closure includes the clazz passed as an
* argument
*
* @param clazz the class to check for
* @return true if clazz is present
*/
public boolean isAssignableTo(Class<?> clazz);


/**
* The type closure of this resolvable
*
* @return
*/
public Set<Type> getTypeClosure();

/**
* Get the underlying java class used to generate this resolvable, or null
* if no java class was used
*
* @return the java class
*/
public Class<?> getJavaClass();

}
Expand Up @@ -19,7 +19,9 @@
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.jboss.webbeans.introspector.WBAnnotated;
Expand Down Expand Up @@ -59,7 +61,7 @@ private static class ResolvableImpl implements Resolvable
{

private final Set<Annotation> bindings;
private final Set<Class<? extends Annotation>> annotationTypes;
private final Map<Class<? extends Annotation>, Annotation> annotations;
private final Set<Type> typeClosure;

public ResolvableImpl(Set<Annotation> bindings, Set<Type> typeClosure)
Expand All @@ -69,11 +71,11 @@ public ResolvableImpl(Set<Annotation> bindings, Set<Type> typeClosure)
{
this.bindings.add(new CurrentLiteral());
}
this.annotationTypes = new HashSet<Class<? extends Annotation>>();
this.annotations = new HashMap<Class<? extends Annotation>, Annotation>();
this.typeClosure = typeClosure;
for (Annotation annotation : bindings)
{
annotationTypes.add(annotation.annotationType());
annotations.put(annotation.annotationType(), annotation);
}
}

Expand All @@ -84,7 +86,7 @@ public Set<Annotation> getBindings()

public boolean isAnnotationPresent(Class<? extends Annotation> annotationType)
{
return annotationTypes.contains(annotationType);
return annotations.containsKey(annotationType);
}

public Set<Type> getTypeClosure()
Expand All @@ -96,6 +98,17 @@ public boolean isAssignableTo(Class<?> clazz)
{
return Reflections.isAssignableFrom(clazz, typeClosure);
}

public <A extends Annotation> A getAnnotation(Class<A> annotationType)
{
return (A) annotations.get(annotationType);
}

public Class<?> getJavaClass()
{
// No underlying java class
return null;
}

@Override
public String toString()
Expand Down
Expand Up @@ -47,6 +47,7 @@ public class TypeSafeBeanResolver<T extends Bean<?>> extends TypeSafeResolver<T>
TRANSFORMERS = new HashSet<ResolvableTransformer>();
TRANSFORMERS.add(EventBean.TRANSFORMER);
TRANSFORMERS.add(InstanceBean.TRANSFORMER);
TRANSFORMERS.add(new NewResolvableTransformer());
}

public TypeSafeBeanResolver(BeanManagerImpl manager, Iterable<T> beans)
Expand Down

0 comments on commit b600a8b

Please sign in to comment.