Skip to content

Commit

Permalink
WBRI-304, WBRI-325, WBRI-309
Browse files Browse the repository at this point in the history
git-svn-id: http://anonsvn.jboss.org/repos/weld/ri/trunk@3414 1c488680-804c-0410-94cd-c6b725194a0e
  • Loading branch information
pmuir committed Aug 9, 2009
1 parent 8c4f36c commit 7939f98
Show file tree
Hide file tree
Showing 51 changed files with 1,066 additions and 912 deletions.
16 changes: 9 additions & 7 deletions impl/src/main/java/org/jboss/webbeans/BeanManagerImpl.java
Expand Up @@ -86,6 +86,7 @@
import org.jboss.webbeans.resolution.TypeSafeDecoratorResolver;
import org.jboss.webbeans.resolution.TypeSafeObserverResolver;
import org.jboss.webbeans.resolution.TypeSafeResolver;
import org.jboss.webbeans.resources.ClassTransformer;
import org.jboss.webbeans.util.Beans;
import org.jboss.webbeans.util.Proxies;
import org.jboss.webbeans.util.Reflections;
Expand Down Expand Up @@ -743,6 +744,11 @@ public List<DecoratorBean<?>> getDecorators()
{
return Collections.unmodifiableList(decorators);
}

public Iterable<Bean<?>> getAccessibleBeans()
{
return createDynamicAccessibleIterable(Transform.BEAN);
}

/**
* Get all the spec defined beans, including interceptor beans and decorator
Expand Down Expand Up @@ -1241,14 +1247,10 @@ public Namespace getRootNamespace()
return rootNamespace;
}

public <T> InjectionTarget<T> createInjectionTarget(Class<T> type)
{
throw new UnsupportedOperationException("Not yet implemented");
}

public <T> InjectionTarget<T> createInjectionTarget(AnnotatedType<T> type)
{
throw new UnsupportedOperationException("Not yet implemented");
// TODO Cache on our side?
return new SimpleInjectionTarget<T>(getServices().get(ClassTransformer.class).loadClass(type), this);
}

public <X> Bean<? extends X> getMostSpecializedBean(Bean<X> bean)
Expand Down Expand Up @@ -1329,7 +1331,7 @@ public <T> WBCreationalContext<T> createCreationalContext(Contextual<T> contextu

public <T> AnnotatedType<T> createAnnotatedType(Class<T> type)
{
throw new UnsupportedOperationException();
return getServices().get(ClassTransformer.class).loadClass(type);
}

public <X> Bean<? extends X> resolve(Set<Bean<? extends X>> beans)
Expand Down
113 changes: 113 additions & 0 deletions impl/src/main/java/org/jboss/webbeans/SimpleInjectionTarget.java
@@ -0,0 +1,113 @@
/*
* 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;

import java.util.HashSet;
import java.util.Set;

import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.spi.InjectionPoint;
import javax.enterprise.inject.spi.InjectionTarget;

import org.jboss.webbeans.injection.ConstructorInjectionPoint;
import org.jboss.webbeans.injection.FieldInjectionPoint;
import org.jboss.webbeans.injection.MethodInjectionPoint;
import org.jboss.webbeans.introspector.WBClass;
import org.jboss.webbeans.introspector.WBMethod;
import org.jboss.webbeans.util.Beans;

/**
* @author pmuir
*
*/
public class SimpleInjectionTarget<T> implements InjectionTarget<T>
{

private final BeanManagerImpl beanManager;
private final WBClass<T> type;
private final ConstructorInjectionPoint<T> constructor;
private final Set<FieldInjectionPoint<?, ?>> injectableFields;
private final Set<MethodInjectionPoint<?, ?>> initializerMethods;
private final WBMethod<?, ?> postConstruct;
private final WBMethod<?, ?> preDestroy;
private final Set<InjectionPoint> injectionPoints;

public SimpleInjectionTarget(WBClass<T> type, BeanManagerImpl beanManager)
{
this.beanManager = beanManager;
this.type = type;
this.injectionPoints = new HashSet<InjectionPoint>();
this.constructor = Beans.getBeanConstructor(null, type);
this.injectionPoints.addAll(Beans.getParameterInjectionPoints(null, constructor));
this.injectableFields = new HashSet<FieldInjectionPoint<?,?>>();
this.injectableFields.addAll(Beans.getFieldInjectionPoints(null, type));
this.injectionPoints.addAll(injectableFields);
this.initializerMethods = new HashSet<MethodInjectionPoint<?,?>>();
this.initializerMethods.addAll(Beans.getInitializerMethods(null, type));
this.injectionPoints.addAll(Beans.getParameterInjectionPoints(null, initializerMethods));
this.postConstruct = Beans.getPostConstruct(type);
this.preDestroy = Beans.getPreDestroy(type);
}

public T produce(CreationalContext<T> ctx)
{
return constructor.newInstance(beanManager, ctx);
}

public void inject(T instance, CreationalContext<T> ctx)
{
for (FieldInjectionPoint<?, ?> injectionPoint : injectableFields)
{
injectionPoint.inject(instance, beanManager, ctx);
}
}

public void postConstruct(T instance)
{
try
{
postConstruct.invoke(instance);
}
catch (Exception e)
{
throw new RuntimeException("Error invoking postConstruct() " + postConstruct, e);
}
}

public void preDestroy(T instance)
{
try
{
preDestroy.invoke(instance);
}
catch (Exception e)
{
throw new RuntimeException("Error invoking preDestroy() " + preDestroy, e);
}
}

public void dispose(T instance)
{
// No-op
}

public Set<InjectionPoint> getInjectionPoints()
{
return injectionPoints;
}

}
36 changes: 35 additions & 1 deletion impl/src/main/java/org/jboss/webbeans/Validator.java
Expand Up @@ -22,6 +22,8 @@
import java.lang.reflect.TypeVariable;
import java.lang.reflect.WildcardType;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -54,6 +56,10 @@
import org.jboss.webbeans.util.Proxies;
import org.jboss.webbeans.util.Reflections;

import com.google.common.base.Supplier;
import com.google.common.collect.Multimap;
import com.google.common.collect.Multimaps;

/**
* Checks a list of beans for DeploymentExceptions and their subclasses
*
Expand Down Expand Up @@ -203,7 +209,35 @@ public void validateDeployment(BeanManagerImpl manager, BeanDeployerEnvironment
validateEnabledDecoratorClasses(manager);
validateEnabledPolicies(manager);
validateDisposalMethods(environment);

validateBeanNames(manager);
}

public void validateBeanNames(BeanManagerImpl beanManager)
{
Multimap<String, Bean<?>> namedAccessibleBeans = Multimaps.newSetMultimap(new HashMap<String, Collection<Bean<?>>>(), new Supplier<Set<Bean<?>>>()
{

public Set<Bean<?>> get()
{
return new HashSet<Bean<?>>();
}

});
for (Bean<?> bean : beanManager.getAccessibleBeans())
{
if (bean.getName() != null)
{
namedAccessibleBeans.put(bean.getName(), bean);
}
}
for (String name : namedAccessibleBeans.keySet())
{
Set<Bean<?>> resolvedBeans = beanManager.getBeanResolver().resolve(namedAccessibleBeans.get(name));
if (resolvedBeans.size() > 1)
{
throw new DeploymentException("An unresolvable ambiguous EL name exists for " + name + "; found " + resolvedBeans );
}
}
}

private void validateEnabledDecoratorClasses(BeanManagerImpl beanManager)
Expand Down
Expand Up @@ -256,7 +256,7 @@ private boolean checkInjectionPointsAreSerializable()
Bean<?> resolvedBean = manager.getBeans(injectionPoint.getJavaClass(), bindings).iterator().next();
if (passivating)
{
if (Dependent.class.equals(resolvedBean.getScopeType()) && !Reflections.isSerializable(resolvedBean.getBeanClass()) && (((injectionPoint instanceof WBField<?>) && !((WBField<?>) injectionPoint).isTransient()) || (injectionPoint instanceof WBParameter<?>)))
if (Dependent.class.equals(resolvedBean.getScopeType()) && !Reflections.isSerializable(resolvedBean.getBeanClass()) && (((injectionPoint instanceof WBField<?, ?>) && !((WBField<?, ?>) injectionPoint).isTransient()) || (injectionPoint instanceof WBParameter<?, ?>)))
{
return false;
}
Expand Down
63 changes: 18 additions & 45 deletions impl/src/main/java/org/jboss/webbeans/bean/AbstractClassBean.java
Expand Up @@ -31,24 +31,18 @@
import javax.enterprise.context.Dependent;
import javax.enterprise.context.ScopeType;
import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.event.Observes;
import javax.enterprise.inject.CreationException;
import javax.enterprise.inject.Disposes;
import javax.enterprise.inject.Initializer;
import javax.enterprise.inject.Produces;
import javax.enterprise.inject.spi.Decorator;
import javax.enterprise.inject.spi.InjectionPoint;
import javax.enterprise.inject.spi.InjectionTarget;

import org.jboss.webbeans.BeanManagerImpl;
import org.jboss.webbeans.DefinitionException;
import org.jboss.webbeans.bean.proxy.DecoratorProxyMethodHandler;
import org.jboss.webbeans.bootstrap.BeanDeployerEnvironment;
import org.jboss.webbeans.injection.FieldInjectionPoint;
import org.jboss.webbeans.injection.MethodInjectionPoint;
import org.jboss.webbeans.injection.ParameterInjectionPoint;
import org.jboss.webbeans.introspector.WBClass;
import org.jboss.webbeans.introspector.WBMethod;
import org.jboss.webbeans.introspector.WBParameter;
import org.jboss.webbeans.log.LogProvider;
import org.jboss.webbeans.log.Logging;
import org.jboss.webbeans.util.Beans;
Expand All @@ -63,16 +57,16 @@
* @param <T>
* @param <E>
*/
public abstract class AbstractClassBean<T> extends AbstractBean<T, Class<T>>
public abstract class AbstractClassBean<T> extends AbstractBean<T, Class<T>> implements InjectionTarget<T>
{
// Logger
private static final LogProvider log = Logging.getLogProvider(AbstractClassBean.class);
// The item representation
protected WBClass<T> annotatedItem;
// The injectable fields
private Set<FieldInjectionPoint<?>> injectableFields;
private Set<FieldInjectionPoint<?, ?>> injectableFields;
// The initializer methods
private Set<MethodInjectionPoint<?>> initializerMethods;
private Set<MethodInjectionPoint<?, ?>> initializerMethods;
private Set<String> dependencies;

private List<Decorator<?>> decorators;
Expand Down Expand Up @@ -212,7 +206,7 @@ public List<Decorator<?>> getDecorators()
*/
protected void injectBoundFields(T instance, CreationalContext<T> creationalContext)
{
for (FieldInjectionPoint<?> injectableField : injectableFields)
for (FieldInjectionPoint<?, ?> injectableField : injectableFields)
{
injectableField.inject(instance, manager, creationalContext);
}
Expand All @@ -225,11 +219,17 @@ protected void injectBoundFields(T instance, CreationalContext<T> creationalCont
*/
protected void callInitializers(T instance, CreationalContext<T> creationalContext)
{
for (MethodInjectionPoint<?> initializer : getInitializerMethods())
for (MethodInjectionPoint<?, ?> initializer : getInitializerMethods())
{
initializer.invoke(instance, manager, creationalContext, CreationException.class);
}
}

public void dispose(T instance)
{
// No-op for class beans
}


/**
* Initializes the bean type
Expand All @@ -250,7 +250,7 @@ protected void initType()
*/
protected void initInjectableFields()
{
injectableFields = new HashSet<FieldInjectionPoint<?>>(Beans.getFieldInjectionPoints(annotatedItem, this));
injectableFields = new HashSet<FieldInjectionPoint<?, ?>>(Beans.getFieldInjectionPoints(this, annotatedItem));
addInjectionPoints(injectableFields);
}

Expand All @@ -259,41 +259,14 @@ protected void initInjectableFields()
*/
protected void initInitializerMethods()
{
initializerMethods = new HashSet<MethodInjectionPoint<?>>();
for (WBMethod<?> method : annotatedItem.getAnnotatedMethods(Initializer.class))
{
if (method.isStatic())
{
throw new DefinitionException("Initializer method " + method.toString() + " cannot be static on " + getAnnotatedItem());
}
else if (method.getAnnotation(Produces.class) != null)
{
throw new DefinitionException("Initializer method " + method.toString() + " cannot be annotated @Produces on " + getAnnotatedItem());
}
else if (method.getAnnotatedParameters(Disposes.class).size() > 0)
{
throw new DefinitionException("Initializer method " + method.toString() + " cannot have parameters annotated @Disposes on " + getAnnotatedItem());
}
else if (method.getAnnotatedParameters(Observes.class).size() > 0)
{
throw new DefinitionException("Initializer method " + method.toString() + " cannot be annotated @Observes on " + getAnnotatedItem());
}
else
{
MethodInjectionPoint<?> initializerMethod = MethodInjectionPoint.of(this, method);
initializerMethods.add(initializerMethod);
for (WBParameter<?> parameter : initializerMethod.getParameters())
{
addInjectionPoint(ParameterInjectionPoint.of(this, parameter));
}
}
}
initializerMethods = Beans.getInitializerMethods(this, getAnnotatedItem());
addInjectionPoints(Beans.getParameterInjectionPoints(this, initializerMethods));
}

@Override
protected void initScopeType()
{
for (WBClass<?> clazz = getAnnotatedItem(); clazz != null; clazz = clazz.getSuperclass())
for (WBClass<?> clazz = getAnnotatedItem(); clazz != null; clazz = clazz.getWBSuperclass())
{
Set<Annotation> scopeTypes = clazz.getDeclaredMetaAnnotations(ScopeType.class);
scopeTypes = clazz.getDeclaredMetaAnnotations(ScopeType.class);
Expand Down Expand Up @@ -333,7 +306,7 @@ protected void checkBeanImplementation() {}
protected void preSpecialize(BeanDeployerEnvironment environment)
{
super.preSpecialize(environment);
if (getAnnotatedItem().getSuperclass() == null || getAnnotatedItem().getSuperclass().getJavaClass().equals(Object.class))
if (getAnnotatedItem().getWBSuperclass() == null || getAnnotatedItem().getWBSuperclass().getJavaClass().equals(Object.class))
{
throw new DefinitionException("Specializing bean must extend another bean " + toString());
}
Expand Down Expand Up @@ -368,7 +341,7 @@ protected String getDefaultName()
*
* @return The set of annotated methods
*/
public Set<? extends MethodInjectionPoint<?>> getInitializerMethods()
public Set<? extends MethodInjectionPoint<?, ?>> getInitializerMethods()
{
return initializerMethods;
}
Expand Down

0 comments on commit 7939f98

Please sign in to comment.