[webbeans-commits] Webbeans SVN: r38 - in ri/trunk/webbeans-impl/src: main/java/org/jboss/webbeans/ejb and 3 other directories.
webbeans-commits at lists.jboss.org
webbeans-commits at lists.jboss.org
Thu Jul 3 09:02:30 EDT 2008
Author: pete.muir at jboss.org
Date: 2008-07-03 09:02:30 -0400 (Thu, 03 Jul 2008)
New Revision: 38
Added:
ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/ComponentInstanceImpl.java
ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/ComponentMetaModel.java
ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/injectable/
ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/injectable/ConstructorMetaModel.java
ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/injectable/ElementMetaModel.java
ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/injectable/MethodMetaModel.java
ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/injectable/ParameterMetaModel.java
ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/injectable/UnitMetaModel.java
ri/trunk/webbeans-impl/src/test/java/org/jboss/webbeans/test/ComponentMetaModelTest.java
Removed:
ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/AbstractInjectedThingMetaModel.java
ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/ComponentInstanceImpl.java
ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/ConstructorMetaModel.java
ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/InjectedParameterMetaModel.java
ri/trunk/webbeans-impl/src/test/java/org/jboss/webbeans/test/ComponentInstanceTest.java
Modified:
ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/ejb/EJB.java
ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/ejb/EjbMetaData.java
ri/trunk/webbeans-impl/src/test/java/org/jboss/webbeans/test/ConstructorMetaModelTest.java
ri/trunk/webbeans-impl/src/test/java/org/jboss/webbeans/test/components/Goose.java
Log:
Refactor component meta model
Deleted: ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/AbstractInjectedThingMetaModel.java
===================================================================
--- ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/AbstractInjectedThingMetaModel.java 2008-07-03 01:55:49 UTC (rev 37)
+++ ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/AbstractInjectedThingMetaModel.java 2008-07-03 13:02:30 UTC (rev 38)
@@ -1,33 +0,0 @@
-package org.jboss.webbeans;
-
-import java.lang.annotation.Annotation;
-
-public abstract class AbstractInjectedThingMetaModel
-{
-
- private Annotation[] bindingTypes;
-
- public AbstractInjectedThingMetaModel(Annotation[] bindingTypes)
- {
- this.bindingTypes = bindingTypes;
- }
-
- public AbstractInjectedThingMetaModel()
- {
- this.bindingTypes = new Annotation[0];
- }
-
- public Annotation[] getBindingTypes()
- {
- return bindingTypes;
- }
-
- public abstract Class<?> getType();
-
- @Override
- public String toString()
- {
- return getType() + " with binding types " + getBindingTypes();
- }
-
-}
Deleted: ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/ComponentInstanceImpl.java
===================================================================
--- ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/ComponentInstanceImpl.java 2008-07-03 01:55:49 UTC (rev 37)
+++ ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/ComponentInstanceImpl.java 2008-07-03 13:02:30 UTC (rev 38)
@@ -1,422 +0,0 @@
-package org.jboss.webbeans;
-
-import java.lang.annotation.Annotation;
-import java.lang.reflect.Constructor;
-import java.lang.reflect.Method;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-import java.util.logging.Logger;
-
-import javax.webbeans.BindingType;
-import javax.webbeans.ComponentInstance;
-import javax.webbeans.Container;
-import javax.webbeans.Dependent;
-import javax.webbeans.DeploymentType;
-import javax.webbeans.Named;
-import javax.webbeans.ScopeType;
-
-import org.jboss.webbeans.bindings.CurrentBinding;
-import org.jboss.webbeans.bindings.DependentBinding;
-import org.jboss.webbeans.bindings.ProductionBinding;
-import org.jboss.webbeans.ejb.EJB;
-import org.jboss.webbeans.util.AnnotatedItem;
-import org.jboss.webbeans.util.LoggerUtil;
-import org.jboss.webbeans.util.Reflections;
-import org.jboss.webbeans.util.Strings;
-
-/**
- * Web Beans Component meta model
- *
- * @author Pete Muir
- *
- */
-public class ComponentInstanceImpl<T> extends ComponentInstance<T>
-{
-
- public enum ComponentType
- {
- SIMPLE,
- EJB_SESSION
- ;
- }
-
- public static final String LOGGER_NAME = "componentInstance";
-
- private static Logger log = LoggerUtil.getLogger(LOGGER_NAME);
-
- private Class<? extends T> type;
- private Set<Annotation> bindingTypes;
- private Annotation deploymentType;
- private String name;
- private Annotation scopeType;
- private ComponentType componentType;
- private ConstructorMetaModel<T> constructor;
- /**
- *
- * @param annotatedItem Annotations read from java classes
- * @param xmlAnnotatedItem Annotations read from XML
- * @param container
- */
- @SuppressWarnings("unchecked")
- public ComponentInstanceImpl(AnnotatedItem annotatedItem, AnnotatedItem xmlAnnotatedItem, ContainerImpl container)
- {
- if (annotatedItem == null)
- {
- throw new NullPointerException("annotatedItem must not be null. If the component is declared just in XML, pass in an empty annotatedItem");
- }
-
- if (xmlAnnotatedItem == null)
- {
- throw new NullPointerException("xmlAnnotatedItem must not be null. If the component is declared just in Java, pass in an empty xmlAnnotatedItem");
- }
-
- this.type = (Class<? extends T>) initType(annotatedItem, xmlAnnotatedItem);
- log.fine("Building Web Bean component metadata for " + type);
- this.componentType = initComponentType(type);
- checkComponentImplementation(componentType, type);
- this.constructor = new ConstructorMetaModel<T>(type);
- MergedComponentStereotypes stereotypes = new MergedComponentStereotypes(annotatedItem, xmlAnnotatedItem, container);
- this.bindingTypes = initBindingTypes(annotatedItem, xmlAnnotatedItem);
- this.deploymentType = initDeploymentType(stereotypes, annotatedItem, xmlAnnotatedItem, container);
- this.scopeType = initScopeType(stereotypes, annotatedItem, xmlAnnotatedItem);
- this.name = initName(stereotypes, annotatedItem, xmlAnnotatedItem, componentType, type);
- checkRequiredTypesImplemented(stereotypes, type);
- checkScopeAllowed(stereotypes, scopeType, type);
- // TODO Interceptors
- }
-
- /*
- * A series of static methods which implement the algorithms defined in the Web Beans spec for component meta data
- */
-
- protected static ComponentType initComponentType(Class<?> type)
- {
- if (EJB.isStatefulEjbComponent(type) || EJB.isStatelessEjbComponent(type))
- {
- return ComponentType.EJB_SESSION;
- }
- else
- {
- return ComponentType.SIMPLE;
- }
- }
-
- protected static void checkComponentImplementation(ComponentType componentType, Class<?> type)
- {
- switch (componentType)
- {
- case SIMPLE:
- checkSimpleComponentImplementation(type);
- break;
- }
- }
-
- protected static void checkSimpleComponentImplementation(Class<?> type)
- {
- if (Reflections.isAbstract(type))
- {
- throw new RuntimeException("Web Bean implementation class " + type + " cannot be declared abstract");
- }
- }
-
- protected static boolean isDeclaredFinal(Class<?> type)
- {
- if (Reflections.isFinal(type))
- {
- return true;
- }
- for (Method method : type.getDeclaredMethods())
- {
- if (Reflections.isFinal(method))
- {
- return true;
- }
- }
- return false;
- }
-
- @SuppressWarnings("unchecked")
- protected static Class<?> initType(AnnotatedItem annotatedItem, AnnotatedItem xmlAnnotatedItem)
- {
- if (annotatedItem.getAnnotatedClass() != null && xmlAnnotatedItem.getAnnotatedClass() != null && !annotatedItem.getAnnotatedClass().equals(xmlAnnotatedItem.getAnnotatedClass()))
- {
- throw new IllegalArgumentException("Cannot build a component which specifies different classes in XML and Java");
- }
- else if (xmlAnnotatedItem.getAnnotatedClass() != null)
- {
- log.finest("Component type specified in XML");
- return xmlAnnotatedItem.getAnnotatedClass();
- }
- else if (annotatedItem.getAnnotatedClass() != null)
- {
- log.finest("Component type specified in Java");
- return annotatedItem.getAnnotatedClass();
- }
- else
- {
- throw new IllegalArgumentException("Cannot build a component which doesn't specify a type");
- }
- }
-
- /**
- * Check that the scope type is allowed by the stereotypes on the component and the component type
- * @param type
- */
- protected static void checkScopeAllowed(MergedComponentStereotypes stereotypes, Annotation scopeType, Class<?> type)
- {
- log.finest("Checking if " + scopeType + " is allowed");
- if (stereotypes.getSupportedScopes().size() > 0)
- {
- if (!stereotypes.getSupportedScopes().contains(scopeType.annotationType()))
- {
- throw new RuntimeException("Scope " + scopeType + " is not an allowed by the component's stereotype");
- }
- }
- if (isDeclaredFinal(type) && !scopeType.annotationType().equals(Dependent.class))
- {
- throw new RuntimeException("Scope " + scopeType + " is not allowed as the component is declared final or has methods declared final");
- }
- }
-
- /**
- * Check that the types required by the stereotypes on the component are implemented
- */
- protected static void checkRequiredTypesImplemented(MergedComponentStereotypes stereotypes, Class<?> type)
- {
- for (Class<?> requiredType : stereotypes.getRequiredTypes())
- {
- log.finest("Checking if required type " + requiredType + " is implemented");
- if (!requiredType.isAssignableFrom(type))
- {
- throw new RuntimeException("Required type " + requiredType + " isn't implement on " + type);
- }
- }
- }
-
- /**
- * Return the scope of the component
- */
- protected static Annotation initScopeType(MergedComponentStereotypes stereotypes, AnnotatedItem annotatedItem, AnnotatedItem xmlAnnotatedItem)
- {
- Set<Annotation> xmlScopes = xmlAnnotatedItem.getAnnotations(ScopeType.class);
- if (xmlScopes.size() > 1)
- {
- throw new RuntimeException("At most one scope may be specified in XML");
- }
-
- if (xmlScopes.size() == 1)
- {
- Annotation scope = xmlScopes.iterator().next();
- log.finest("Scope " + scope + " specified in XML");
- return scope;
- }
-
- Set<Annotation> scopes = annotatedItem.getAnnotations(ScopeType.class);
- if (scopes.size() > 1)
- {
- throw new RuntimeException("At most one scope may be specified");
- }
-
- if (scopes.size() == 1)
- {
- Annotation scope = scopes.iterator().next();
- log.finest("Scope " + scope + " specified b annotation");
- return scope;
- }
-
- if (stereotypes.getPossibleScopeTypes().size() == 1)
- {
- Annotation scope = stereotypes.getPossibleScopeTypes().iterator().next();
- log.finest("Scope " + scope + " specified by stereotype");
- return scope;
- }
- else if (stereotypes.getPossibleScopeTypes().size() > 1)
- {
- throw new RuntimeException("All stereotypes must specify the same scope OR a scope must be specified on the component");
- }
-
- log.finest("Using default @Dependent scope");
- return new DependentBinding();
- }
-
- protected static Annotation initDeploymentType(MergedComponentStereotypes stereotypes, AnnotatedItem annotatedItem, AnnotatedItem xmlAnnotatedItem, ContainerImpl container)
- {
- Set<Annotation> xmlDeploymentTypes = xmlAnnotatedItem.getAnnotations(DeploymentType.class);
-
- if (xmlDeploymentTypes.size() > 1)
- {
- throw new RuntimeException("At most one deployment type may be specified (" + xmlDeploymentTypes + " are specified)");
- }
-
- if (xmlDeploymentTypes.size() == 1)
- {
- Annotation deploymentType = xmlDeploymentTypes.iterator().next();
- log.finest("Deployment type " + deploymentType + " specified in XML");
- return deploymentType;
- }
-
- if (xmlAnnotatedItem.getAnnotatedClass() == null)
- {
-
- Set<Annotation> deploymentTypes = annotatedItem.getAnnotations(DeploymentType.class);
-
- if (deploymentTypes.size() > 1)
- {
- throw new RuntimeException("At most one deployment type may be specified (" + deploymentTypes + " are specified)");
- }
- if (deploymentTypes.size() == 1)
- {
- Annotation deploymentType = deploymentTypes.iterator().next();
- log.finest("Deployment type " + deploymentType + " specified by annotation");
- return deploymentType;
- }
- }
-
- if (stereotypes.getPossibleDeploymentTypes().size() > 0)
- {
- Annotation deploymentType = getDeploymentType(container.getEnabledDeploymentTypes(), stereotypes.getPossibleDeploymentTypes());
- log.finest("Deployment type " + deploymentType + " specified by stereotype");
- return deploymentType;
- }
-
- if (xmlAnnotatedItem.getAnnotatedClass() != null)
- {
- log.finest("Using default @Production deployment type");
- return new ProductionBinding();
- }
- throw new RuntimeException("All Java annotated classes have a deployment type");
- }
-
- protected static Set<Annotation> initBindingTypes(AnnotatedItem annotatedItem, AnnotatedItem xmlAnnotatedItem)
- {
- Set<Annotation> xmlBindingTypes = xmlAnnotatedItem.getAnnotations(BindingType.class);
- if (xmlBindingTypes.size() > 0)
- {
- // TODO support producer expression default binding type
- log.finest("Using binding types " + xmlBindingTypes + " specified in XML");
- return xmlBindingTypes;
- }
-
- Set<Annotation> bindingTypes = annotatedItem.getAnnotations(BindingType.class);
-
- if (bindingTypes.size() == 0)
- {
- log.finest("Adding default @Current binding type");
- bindingTypes.add(new CurrentBinding());
- }
- else
- {
- log.finest("Using binding types " + bindingTypes + " specified by annotations");
- }
- return bindingTypes;
- }
-
- protected static String initName(MergedComponentStereotypes stereotypes, AnnotatedItem annotatedItem, AnnotatedItem xmlAnnotatedItem, ComponentType componentType, Class<?> type)
- {
- boolean componentNameDefaulted = false;
- String name = null;
- if (xmlAnnotatedItem.isAnnotationPresent(Named.class))
- {
- name = xmlAnnotatedItem.getAnnotation(Named.class).value();
- if ("".equals(name))
- {
- log.finest("Using default name (specified in XML)");
- componentNameDefaulted = true;
- }
- else
- {
- log.finest("Using name " + name + " specified in XML");
- }
- }
- else if (annotatedItem.isAnnotationPresent(Named.class))
- {
- name = annotatedItem.getAnnotation(Named.class).value();
- if ("".equals(name))
- {
- log.finest("Using default name (specified by annotations)");
- componentNameDefaulted = true;
- }
- else
- {
- log.finest("Using name " + name + " specified in XML");
- }
- }
- if ("".equals(name) && (componentNameDefaulted || stereotypes.isComponentNameDefaulted()))
- {
- if (ComponentType.SIMPLE.equals(componentType))
- {
- name = Strings.decapitalize(type.getSimpleName());
- }
- log.finest("Default name of " + type + " is " + name );
- }
- return name;
- }
-
- public static Annotation getDeploymentType(List<Annotation> enabledDeploymentTypes, Map<Class<? extends Annotation>, Annotation> possibleDeploymentTypes)
- {
- for (int i = (enabledDeploymentTypes.size() - 1); i > 0; i--)
- {
- if (possibleDeploymentTypes.containsKey((enabledDeploymentTypes.get(i).annotationType())))
- {
- return enabledDeploymentTypes.get(i);
- }
- }
- return null;
- }
-
- @Override
- public T create(Container container)
- {
- return null;
- }
-
- @Override
- public void destroy(Container container, Object instance)
- {
- // TODO Auto-generated method stub
-
- }
-
- @Override
- public Set<Annotation> getBindingTypes()
- {
- return bindingTypes;
- }
-
- @Override
- public Annotation getDeploymentType()
- {
- return deploymentType;
- }
-
- @Override
- public String getName()
- {
- return name;
- }
-
- @Override
- public Set<Class> getTypes()
- {
- // TODO Auto-generated method stub
- return null;
- }
-
- @Override
- public Annotation getScopeType()
- {
- return scopeType;
- }
-
- public ConstructorMetaModel<T> getConstructor()
- {
- return constructor;
- }
-
- public ComponentType getComponentType()
- {
- return componentType;
- }
-
-}
Added: ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/ComponentInstanceImpl.java
===================================================================
--- ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/ComponentInstanceImpl.java (rev 0)
+++ ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/ComponentInstanceImpl.java 2008-07-03 13:02:30 UTC (rev 38)
@@ -0,0 +1,70 @@
+package org.jboss.webbeans;
+
+import java.lang.annotation.Annotation;
+import java.util.Set;
+import java.util.logging.Logger;
+
+import javax.webbeans.ComponentInstance;
+import javax.webbeans.Container;
+
+import org.jboss.webbeans.util.LoggerUtil;
+
+public class ComponentInstanceImpl<T> extends ComponentInstance<T>
+{
+
+public static final String LOGGER_NAME = "componentMetaModel";
+
+ private static Logger log = LoggerUtil.getLogger(LOGGER_NAME);
+
+ private ComponentMetaModel<T> componentMetaModel;
+
+ public ComponentInstanceImpl(ComponentMetaModel<T> componentMetaModel)
+ {
+ this.componentMetaModel = componentMetaModel;
+ }
+
+ @Override
+ public T create(Container container)
+ {
+ return componentMetaModel.getConstructor().invoke(container);
+ }
+
+ @Override
+ public void destroy(Container container, T instance)
+ {
+ // TODO Auto-generated method stub
+
+ }
+
+ @Override
+ public Set<Annotation> getBindingTypes()
+ {
+ return componentMetaModel.getBindingTypes();
+ }
+
+ @Override
+ public Annotation getDeploymentType()
+ {
+ return componentMetaModel.getDeploymentType();
+ }
+
+ @Override
+ public String getName()
+ {
+ return componentMetaModel.getName();
+ }
+
+ @Override
+ public Annotation getScopeType()
+ {
+ return componentMetaModel.getScopeType();
+ }
+
+ @Override
+ public Set<Class> getTypes()
+ {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+}
Copied: ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/ComponentMetaModel.java (from rev 34, ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/ComponentInstanceImpl.java)
===================================================================
--- ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/ComponentMetaModel.java (rev 0)
+++ ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/ComponentMetaModel.java 2008-07-03 13:02:30 UTC (rev 38)
@@ -0,0 +1,526 @@
+package org.jboss.webbeans;
+
+import static org.jboss.webbeans.ComponentMetaModel.ComponentType.*;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.logging.Logger;
+
+import javax.webbeans.ApplicationScoped;
+import javax.webbeans.BindingType;
+import javax.webbeans.Dependent;
+import javax.webbeans.DeploymentType;
+import javax.webbeans.Destroys;
+import javax.webbeans.Initializer;
+import javax.webbeans.Named;
+import javax.webbeans.ScopeType;
+
+import org.jboss.webbeans.bindings.CurrentBinding;
+import org.jboss.webbeans.bindings.DependentBinding;
+import org.jboss.webbeans.bindings.ProductionBinding;
+import org.jboss.webbeans.ejb.EJB;
+import org.jboss.webbeans.ejb.EjbMetaData;
+import org.jboss.webbeans.injectable.ConstructorMetaModel;
+import org.jboss.webbeans.injectable.MethodMetaModel;
+import org.jboss.webbeans.util.AnnotatedItem;
+import org.jboss.webbeans.util.LoggerUtil;
+import org.jboss.webbeans.util.Reflections;
+import org.jboss.webbeans.util.Strings;
+
+/**
+ * Web Beans Component meta model
+ *
+ * @author Pete Muir
+ *
+ */
+public class ComponentMetaModel<T>
+{
+
+ public enum ComponentType
+ {
+ SIMPLE,
+ ENTERPRISE;
+ }
+
+ public static final String LOGGER_NAME = "componentMetaModel";
+
+ private static Logger log = LoggerUtil.getLogger(LOGGER_NAME);
+
+ private Class<? extends T> type;
+ private Set<Annotation> bindingTypes;
+ private Annotation deploymentType;
+ private String name;
+ private Annotation scopeType;
+ private ComponentType componentType;
+ private ConstructorMetaModel<T> constructor;
+ private EjbMetaData<T> ejbMetaData;
+ private MethodMetaModel<?> removeMethod;
+ /**
+ *
+ * @param annotatedItem Annotations read from java classes
+ * @param xmlAnnotatedItem Annotations read from XML
+ * @param container
+ */
+ @SuppressWarnings("unchecked")
+ public ComponentMetaModel(AnnotatedItem annotatedItem, AnnotatedItem xmlAnnotatedItem, ContainerImpl container)
+ {
+ if (annotatedItem == null)
+ {
+ throw new NullPointerException("annotatedItem must not be null. If the component is declared just in XML, pass in an empty annotatedItem");
+ }
+
+ if (xmlAnnotatedItem == null)
+ {
+ throw new NullPointerException("xmlAnnotatedItem must not be null. If the component is declared just in Java, pass in an empty xmlAnnotatedItem");
+ }
+
+ this.type = (Class<? extends T>) initType(annotatedItem, xmlAnnotatedItem);
+ log.fine("Building Web Bean component metadata for " + type);
+ this.ejbMetaData = EJB.getEjbMetaData(type);
+ this.componentType = initComponentType(type, ejbMetaData);
+ checkComponentImplementation(componentType, type);
+ this.constructor = initConstructor(type);
+ MergedComponentStereotypes stereotypes = new MergedComponentStereotypes(annotatedItem, xmlAnnotatedItem, container);
+ this.bindingTypes = initBindingTypes(annotatedItem, xmlAnnotatedItem);
+ this.deploymentType = initDeploymentType(stereotypes, annotatedItem, xmlAnnotatedItem, container);
+ this.scopeType = initScopeType(stereotypes, annotatedItem, xmlAnnotatedItem);
+ this.name = initName(stereotypes, annotatedItem, xmlAnnotatedItem, componentType, type);
+ this.removeMethod = initRemoveMethod(componentType, ejbMetaData, type);
+ checkRequiredTypesImplemented(stereotypes, type);
+ checkScopeAllowed(stereotypes, scopeType, type, componentType, ejbMetaData);
+ // TODO Interceptors
+ }
+
+ /*
+ * A series of static methods which implement the algorithms defined in the Web Beans spec for component meta data
+ */
+
+ @SuppressWarnings("unchecked")
+ protected static <T> ConstructorMetaModel<T> initConstructor(Class<? extends T> type)
+ {
+ if (type.getConstructors().length == 1)
+ {
+ Constructor<T> constructor = type.getConstructors()[0];
+ log.finest("Exactly one constructor (" + constructor +") defined, using it as the component constructor for " + type);
+ return new ConstructorMetaModel<T>(constructor);
+ }
+
+ if (type.getConstructors().length > 1)
+ {
+ List<Constructor<T>> initializerAnnotatedConstructors = Reflections.getConstructors(type, Initializer.class);
+ List<Constructor<T>> bindingTypeAnnotatedConstructors = Reflections.getConstructorsForMetaAnnotatedParameter(type, BindingType.class);
+ log.finest("Found " + initializerAnnotatedConstructors + " constructors annotated with @Initializer for " + type);
+ log.finest("Found " + bindingTypeAnnotatedConstructors + " with parameters annotated with binding types for " + type);
+ if ((initializerAnnotatedConstructors.size() + bindingTypeAnnotatedConstructors.size()) > 1)
+ {
+ if (initializerAnnotatedConstructors.size() > 1)
+ {
+ throw new RuntimeException("Cannot have more than one constructor annotated with @Initializer for " + type);
+ }
+
+ else if (bindingTypeAnnotatedConstructors.size() > 1)
+ {
+ throw new RuntimeException("Cannot have more than one constructor with binding types specified on constructor parameters for " + type);
+ }
+ else
+ {
+ throw new RuntimeException("Specify a constructor either annotated with @Initializer or with parameters annotated with binding types for " + type);
+ }
+ }
+ else if (initializerAnnotatedConstructors.size() == 1)
+ {
+ Constructor<T> constructor = initializerAnnotatedConstructors.get(0);
+ log.finest("Exactly one constructor (" + constructor +") annotated with @Initializer defined, using it as the component constructor for " + type);
+ return new ConstructorMetaModel<T>(constructor);
+ }
+ else if (bindingTypeAnnotatedConstructors.size() == 1)
+ {
+ Constructor<T> constructor = bindingTypeAnnotatedConstructors.get(0);
+ log.finest("Exactly one constructor (" + constructor +") with parameters annotated with binding types defined, using it as the component constructor for " + type);
+ return new ConstructorMetaModel<T>(constructor);
+ }
+ }
+
+ if (type.getConstructors().length == 0)
+ {
+ Constructor<T> constructor = (Constructor<T>) Reflections.getConstructor(type);
+ log.finest("No constructor defined, using implicit no arguement constructor for " + type);
+ return new ConstructorMetaModel<T>(constructor);
+ }
+
+ throw new RuntimeException("Cannot determine constructor to use for " + type);
+ }
+
+ protected static <T> MethodMetaModel<?> initRemoveMethod(ComponentType componentType, EjbMetaData<T> ejbMetaData, Class<? extends T> type)
+ {
+ if (componentType.equals(ENTERPRISE) && ejbMetaData.isStateful())
+ {
+ if (ejbMetaData.getRemoveMethods().size() == 1)
+ {
+ return new MethodMetaModel<Object>(ejbMetaData.getRemoveMethods().get(0));
+ }
+ else if (ejbMetaData.getRemoveMethods().size() > 1)
+ {
+ List<Method> possibleRemoveMethods = new ArrayList<Method>();
+ for (Method removeMethod : ejbMetaData.getRemoveMethods())
+ {
+ if (removeMethod.isAnnotationPresent(Destroys.class))
+ {
+ possibleRemoveMethods.add(removeMethod);
+ }
+ }
+ if (possibleRemoveMethods.size() == 1)
+ {
+ return new MethodMetaModel<Object>(possibleRemoveMethods.get(0));
+ }
+ else if (possibleRemoveMethods.size() > 1)
+ {
+ throw new RuntimeException("Multiple remove methods are annotated @Destroys for " + type);
+ }
+ else if (possibleRemoveMethods.size() == 0)
+ {
+ throw new RuntimeException("Multiple remove methods are declared, and none are annotated @Destroys for " + type);
+ }
+ }
+ else if (ejbMetaData.getRemoveMethods().size() == 0)
+ {
+ throw new RuntimeException("Stateful enterprise bean component has no remove methods declared for " + type);
+ }
+ }
+ else
+ {
+ List<Method> destroysMethods = Reflections.getMethods(type, Destroys.class);
+ if (destroysMethods.size() > 0)
+ {
+ throw new RuntimeException("Only stateful enterprise bean components can have methods annotated @Destroys; " + type + " is not a stateful enterprise bean component");
+ }
+ }
+ return null;
+ }
+
+ protected static <T> ComponentType initComponentType(Class<? extends T> type, EjbMetaData<T> ejbMetaData)
+ {
+ if (ejbMetaData != null && (ejbMetaData.isMessageDriven() || ejbMetaData.isSingleton() || ejbMetaData.isStateful() || ejbMetaData.isStateless()))
+ {
+ log.finest(type + " is an enterprise bean component");
+ return ENTERPRISE;
+ }
+ else
+ {
+ log.finest(type + " is an simple component");
+ return SIMPLE;
+ }
+ }
+
+ protected static void checkComponentImplementation(ComponentType componentType, Class<?> type)
+ {
+ switch (componentType)
+ {
+ case SIMPLE:
+ checkSimpleComponentImplementation(type);
+ break;
+ }
+ }
+
+ protected static void checkSimpleComponentImplementation(Class<?> type)
+ {
+ if (Reflections.isAbstract(type))
+ {
+ throw new RuntimeException("Web Bean implementation class " + type + " cannot be declared abstract");
+ }
+ }
+
+ protected static boolean isDeclaredFinal(Class<?> type)
+ {
+ if (Reflections.isFinal(type))
+ {
+ return true;
+ }
+ for (Method method : type.getDeclaredMethods())
+ {
+ if (Reflections.isFinal(method))
+ {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ @SuppressWarnings("unchecked")
+ protected static Class<?> initType(AnnotatedItem annotatedItem, AnnotatedItem xmlAnnotatedItem)
+ {
+ if (annotatedItem.getAnnotatedClass() != null && xmlAnnotatedItem.getAnnotatedClass() != null && !annotatedItem.getAnnotatedClass().equals(xmlAnnotatedItem.getAnnotatedClass()))
+ {
+ throw new IllegalArgumentException("Cannot build a component which specifies different classes in XML and Java");
+ }
+ else if (xmlAnnotatedItem.getAnnotatedClass() != null)
+ {
+ log.finest("Component type specified in XML");
+ return xmlAnnotatedItem.getAnnotatedClass();
+ }
+ else if (annotatedItem.getAnnotatedClass() != null)
+ {
+ log.finest("Component type specified in Java");
+ return annotatedItem.getAnnotatedClass();
+ }
+ else
+ {
+ throw new IllegalArgumentException("Cannot build a component which doesn't specify a type");
+ }
+ }
+
+ /**
+ * Check that the scope type is allowed by the stereotypes on the component and the component type
+ * @param type
+ */
+ protected static void checkScopeAllowed(MergedComponentStereotypes stereotypes, Annotation scopeType, Class<?> type, ComponentType componentType, EjbMetaData ejbMetaData)
+ {
+ log.finest("Checking if " + scopeType + " is allowed for " + type);
+ if (stereotypes.getSupportedScopes().size() > 0)
+ {
+ if (!stereotypes.getSupportedScopes().contains(scopeType.annotationType()))
+ {
+ throw new RuntimeException("Scope " + scopeType + " is not an allowed by the stereotype for " + type);
+ }
+ }
+ if (isDeclaredFinal(type) && !scopeType.annotationType().equals(Dependent.class))
+ {
+ throw new RuntimeException("Scope " + scopeType + " is not allowed as the class is declared final or has methods declared final for " + type + ". Only @Dependent is allowed for final components");
+ }
+ if (componentType.equals(ComponentType.ENTERPRISE) && ejbMetaData.isStateless() && !scopeType.annotationType().equals(Dependent.class))
+ {
+ throw new RuntimeException("Scope " + scopeType + " is not allowed on stateless enterpise bean components for " + type + ". Only @Dependent is allowed on stateless enterprise bean components");
+ }
+ if (componentType.equals(ComponentType.ENTERPRISE) && ejbMetaData.isSingleton() && (!scopeType.annotationType().equals(Dependent.class) || !scopeType.annotationType().equals(ApplicationScoped.class)))
+ {
+ throw new RuntimeException("Scope " + scopeType + " is not allowed on singleton enterpise bean components for " + type + ". Only @Dependent or @ApplicationScoped is allowed on singleton enterprise bean components");
+ }
+ }
+
+ /**
+ * Check that the types required by the stereotypes on the component are implemented
+ */
+ protected static void checkRequiredTypesImplemented(MergedComponentStereotypes stereotypes, Class<?> type)
+ {
+ for (Class<?> requiredType : stereotypes.getRequiredTypes())
+ {
+ log.finest("Checking if required type " + requiredType + " is implemented");
+ if (!requiredType.isAssignableFrom(type))
+ {
+ throw new RuntimeException("Required type " + requiredType + " isn't implement on " + type);
+ }
+ }
+ }
+
+ /**
+ * Return the scope of the component
+ */
+ protected static Annotation initScopeType(MergedComponentStereotypes stereotypes, AnnotatedItem annotatedItem, AnnotatedItem xmlAnnotatedItem)
+ {
+ Set<Annotation> xmlScopes = xmlAnnotatedItem.getAnnotations(ScopeType.class);
+ if (xmlScopes.size() > 1)
+ {
+ throw new RuntimeException("At most one scope may be specified in XML");
+ }
+
+ if (xmlScopes.size() == 1)
+ {
+ Annotation scope = xmlScopes.iterator().next();
+ log.finest("Scope " + scope + " specified in XML");
+ return scope;
+ }
+
+ Set<Annotation> scopes = annotatedItem.getAnnotations(ScopeType.class);
+ if (scopes.size() > 1)
+ {
+ throw new RuntimeException("At most one scope may be specified");
+ }
+
+ if (scopes.size() == 1)
+ {
+ Annotation scope = scopes.iterator().next();
+ log.finest("Scope " + scope + " specified b annotation");
+ return scope;
+ }
+
+ if (stereotypes.getPossibleScopeTypes().size() == 1)
+ {
+ Annotation scope = stereotypes.getPossibleScopeTypes().iterator().next();
+ log.finest("Scope " + scope + " specified by stereotype");
+ return scope;
+ }
+ else if (stereotypes.getPossibleScopeTypes().size() > 1)
+ {
+ throw new RuntimeException("All stereotypes must specify the same scope OR a scope must be specified on the component");
+ }
+
+ log.finest("Using default @Dependent scope");
+ return new DependentBinding();
+ }
+
+ protected static Annotation initDeploymentType(MergedComponentStereotypes stereotypes, AnnotatedItem annotatedItem, AnnotatedItem xmlAnnotatedItem, ContainerImpl container)
+ {
+ Set<Annotation> xmlDeploymentTypes = xmlAnnotatedItem.getAnnotations(DeploymentType.class);
+
+ if (xmlDeploymentTypes.size() > 1)
+ {
+ throw new RuntimeException("At most one deployment type may be specified (" + xmlDeploymentTypes + " are specified)");
+ }
+
+ if (xmlDeploymentTypes.size() == 1)
+ {
+ Annotation deploymentType = xmlDeploymentTypes.iterator().next();
+ log.finest("Deployment type " + deploymentType + " specified in XML");
+ return deploymentType;
+ }
+
+ if (xmlAnnotatedItem.getAnnotatedClass() == null)
+ {
+
+ Set<Annotation> deploymentTypes = annotatedItem.getAnnotations(DeploymentType.class);
+
+ if (deploymentTypes.size() > 1)
+ {
+ throw new RuntimeException("At most one deployment type may be specified (" + deploymentTypes + " are specified)");
+ }
+ if (deploymentTypes.size() == 1)
+ {
+ Annotation deploymentType = deploymentTypes.iterator().next();
+ log.finest("Deployment type " + deploymentType + " specified by annotation");
+ return deploymentType;
+ }
+ }
+
+ if (stereotypes.getPossibleDeploymentTypes().size() > 0)
+ {
+ Annotation deploymentType = getDeploymentType(container.getEnabledDeploymentTypes(), stereotypes.getPossibleDeploymentTypes());
+ log.finest("Deployment type " + deploymentType + " specified by stereotype");
+ return deploymentType;
+ }
+
+ if (xmlAnnotatedItem.getAnnotatedClass() != null)
+ {
+ log.finest("Using default @Production deployment type");
+ return new ProductionBinding();
+ }
+ throw new RuntimeException("All Java annotated classes have a deployment type");
+ }
+
+ protected static Set<Annotation> initBindingTypes(AnnotatedItem annotatedItem, AnnotatedItem xmlAnnotatedItem)
+ {
+ Set<Annotation> xmlBindingTypes = xmlAnnotatedItem.getAnnotations(BindingType.class);
+ if (xmlBindingTypes.size() > 0)
+ {
+ // TODO support producer expression default binding type
+ log.finest("Using binding types " + xmlBindingTypes + " specified in XML");
+ return xmlBindingTypes;
+ }
+
+ Set<Annotation> bindingTypes = annotatedItem.getAnnotations(BindingType.class);
+
+ if (bindingTypes.size() == 0)
+ {
+ log.finest("Adding default @Current binding type");
+ bindingTypes.add(new CurrentBinding());
+ }
+ else
+ {
+ log.finest("Using binding types " + bindingTypes + " specified by annotations");
+ }
+ return bindingTypes;
+ }
+
+ protected static String initName(MergedComponentStereotypes stereotypes, AnnotatedItem annotatedItem, AnnotatedItem xmlAnnotatedItem, ComponentType componentType, Class<?> type)
+ {
+ boolean componentNameDefaulted = false;
+ String name = null;
+ if (xmlAnnotatedItem.isAnnotationPresent(Named.class))
+ {
+ name = xmlAnnotatedItem.getAnnotation(Named.class).value();
+ if ("".equals(name))
+ {
+ log.finest("Using default name (specified in XML)");
+ componentNameDefaulted = true;
+ }
+ else
+ {
+ log.finest("Using name " + name + " specified in XML");
+ }
+ }
+ else if (annotatedItem.isAnnotationPresent(Named.class))
+ {
+ name = annotatedItem.getAnnotation(Named.class).value();
+ if ("".equals(name))
+ {
+ log.finest("Using default name (specified by annotations)");
+ componentNameDefaulted = true;
+ }
+ else
+ {
+ log.finest("Using name " + name + " specified in XML");
+ }
+ }
+ if ("".equals(name) && (componentNameDefaulted || stereotypes.isComponentNameDefaulted()))
+ {
+ if (ComponentType.SIMPLE.equals(componentType))
+ {
+ name = Strings.decapitalize(type.getSimpleName());
+ }
+ log.finest("Default name of " + type + " is " + name );
+ }
+ return name;
+ }
+
+ public static Annotation getDeploymentType(List<Annotation> enabledDeploymentTypes, Map<Class<? extends Annotation>, Annotation> possibleDeploymentTypes)
+ {
+ for (int i = (enabledDeploymentTypes.size() - 1); i > 0; i--)
+ {
+ if (possibleDeploymentTypes.containsKey((enabledDeploymentTypes.get(i).annotationType())))
+ {
+ return enabledDeploymentTypes.get(i);
+ }
+ }
+ return null;
+ }
+
+ public Set<Annotation> getBindingTypes()
+ {
+ return bindingTypes;
+ }
+
+ public Annotation getDeploymentType()
+ {
+ return deploymentType;
+ }
+
+ public String getName()
+ {
+ return name;
+ }
+
+ public Annotation getScopeType()
+ {
+ return scopeType;
+ }
+
+ public ConstructorMetaModel<T> getConstructor()
+ {
+ return constructor;
+ }
+
+ public ComponentType getComponentType()
+ {
+ return componentType;
+ }
+
+ public MethodMetaModel<?> getRemoveMethod()
+ {
+ return removeMethod;
+ }
+
+}
Property changes on: ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/ComponentMetaModel.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Deleted: ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/ConstructorMetaModel.java
===================================================================
--- ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/ConstructorMetaModel.java 2008-07-03 01:55:49 UTC (rev 37)
+++ ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/ConstructorMetaModel.java 2008-07-03 13:02:30 UTC (rev 38)
@@ -1,128 +0,0 @@
-package org.jboss.webbeans;
-
-import java.lang.reflect.Constructor;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.logging.Level;
-import java.util.logging.Logger;
-
-import javax.webbeans.BindingType;
-import javax.webbeans.Container;
-import javax.webbeans.Initializer;
-
-import org.jboss.webbeans.util.LoggerUtil;
-import org.jboss.webbeans.util.Reflections;
-
-public class ConstructorMetaModel<T>
-{
-
-public static final String LOGGER_NAME = "componentConstructor";
-
-
-
- private static Logger log = LoggerUtil.getLogger(LOGGER_NAME);
-
- private Constructor<T> constructor;
-
- private List<AbstractInjectedThingMetaModel> injectedParameters;
-
- @SuppressWarnings("unchecked")
- public ConstructorMetaModel(Class<? extends T> type)
- {
- this.injectedParameters = new ArrayList<AbstractInjectedThingMetaModel>();
- if (type.getConstructors().length == 1)
- {
- this.constructor = type.getConstructors()[0];
- log.finest("Exactly one constructor (" + constructor +") defined, using it as the component constructor for " + type);
- }
- else if (type.getConstructors().length > 1)
- {
- List<Constructor<T>> initializerAnnotatedConstructors = Reflections.getConstructors(type, Initializer.class);
- List<Constructor<T>> bindingTypeAnnotatedConstructors = Reflections.getConstructorsForMetaAnnotatedParameter(type, BindingType.class);
- log.finest("Found " + initializerAnnotatedConstructors + " constructors annotated with @Initializer for " + type);
- log.finest("Found " + bindingTypeAnnotatedConstructors + " with parameters annotated with binding types for " + type);
- if ((initializerAnnotatedConstructors.size() + bindingTypeAnnotatedConstructors.size()) > 1)
- {
- if (initializerAnnotatedConstructors.size() > 1)
- {
- throw new RuntimeException("Cannot have more than one constructor annotated with @Initializer for " + type);
- }
-
- else if (bindingTypeAnnotatedConstructors.size() > 1)
- {
- throw new RuntimeException("Cannot have more than one constructor with binding types specified on constructor parameters for " + type);
- }
-
- else
- {
- throw new RuntimeException("Specify a constructor either annotated with @Initializer or with parameters annotated with binding types for " + type);
- }
- }
- else if (initializerAnnotatedConstructors.size() == 1)
- {
- this.constructor = initializerAnnotatedConstructors.get(0);
- log.finest("Exactly one constructor (" + constructor +") annotated with @Initializer defined, using it as the component constructor for " + type);
- }
- else if (bindingTypeAnnotatedConstructors.size() == 1)
- {
- this.constructor = bindingTypeAnnotatedConstructors.get(0);
- log.finest("Exactly one constructor (" + constructor +") with parameters annotated with binding types defined, using it as the component constructor for " + type);
- }
- }
- else if (type.getConstructors().length == 0)
- {
- this.constructor = (Constructor<T>) Reflections.getConstructor(type);
- log.finest("No constructor defined, using implicit no arguement constructor for " + type);
- }
-
- if (this.constructor == null)
- {
- throw new RuntimeException("Cannot determine constructor to use for " + type);
- }
-
- for (int i = 0; i < constructor.getParameterTypes().length; i++)
- {
- if (constructor.getParameterAnnotations()[i].length > 0)
- {
- InjectedParameterMetaModel parameter = new InjectedParameterMetaModel(constructor.getParameterAnnotations()[i], constructor.getParameterTypes()[i]);
- injectedParameters.add(i, parameter);
- }
- else
- {
- InjectedParameterMetaModel parameter = new InjectedParameterMetaModel(constructor.getParameterTypes()[i]);
- injectedParameters.add(i, parameter);
- }
- }
- log.finest("Initialized metadata for " + constructor + " with injectable parameters " + injectedParameters);
-
- }
-
- public Constructor<T> getConstructor()
- {
- return constructor;
- }
-
- public List<AbstractInjectedThingMetaModel> getInjectedAttributes()
- {
- return injectedParameters;
- }
-
- public T newInstance(Container container)
- {
- Object[] parameters = new Object[injectedParameters.size()];
- log.finest("Creating new instance of " + constructor.getDeclaringClass() + " with injected parameters " + injectedParameters);
- for (int i = 0; i < injectedParameters.size(); i++)
- {
- parameters[i] = container.getInstanceByType(injectedParameters.get(i).getType(), injectedParameters.get(0).getBindingTypes());
- }
- try
- {
- return constructor.newInstance(parameters);
- }
- catch (Exception e)
- {
- throw new RuntimeException("Error instantiating " + constructor.getDeclaringClass(), e);
- }
- }
-
-}
Deleted: ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/InjectedParameterMetaModel.java
===================================================================
--- ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/InjectedParameterMetaModel.java 2008-07-03 01:55:49 UTC (rev 37)
+++ ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/InjectedParameterMetaModel.java 2008-07-03 13:02:30 UTC (rev 38)
@@ -1,31 +0,0 @@
-package org.jboss.webbeans;
-
-import java.lang.annotation.Annotation;
-
-import org.jboss.webbeans.bindings.CurrentBinding;
-
-public class InjectedParameterMetaModel extends AbstractInjectedThingMetaModel
-{
-
- private static Annotation[] currentBinding = {new CurrentBinding()};
-
- private Class<?> type;
-
- public InjectedParameterMetaModel(Annotation[] bindingTypes, Class<?> type)
- {
- super(bindingTypes);
- this.type = type;
- }
-
- public InjectedParameterMetaModel(Class<?> type)
- {
- super(currentBinding);
- this.type = type;
- }
-
- public Class<?> getType()
- {
- return type;
- }
-
-}
Modified: ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/ejb/EJB.java
===================================================================
--- ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/ejb/EJB.java 2008-07-03 01:55:49 UTC (rev 37)
+++ ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/ejb/EJB.java 2008-07-03 13:02:30 UTC (rev 38)
@@ -1,6 +1,8 @@
package org.jboss.webbeans.ejb;
import java.lang.annotation.Annotation;
+import java.util.HashMap;
+import java.util.Map;
import org.jboss.webbeans.util.Reflections;
@@ -9,17 +11,23 @@
public class EJB
{
+ private static Map<Class<?>, EjbMetaData> ejbMetaDataMap = new HashMap<Class<?>, EjbMetaData>();
+
public @interface Dummy {}
- public static final Class<Annotation> STATELESS;
- public static final Class<Annotation> STATEFUL;
- public static final Class<Annotation> MESSAGE_DRIVEN;
+ public static final Class<Annotation> STATELESS_ANNOTATION;
+ public static final Class<Annotation> STATEFUL_ANNOTATION;
+ public static final Class<Annotation> MESSAGE_DRIVEN_ANNOTATION;
+ public static final Class<Annotation> SINGLETON_ANNOTATION;
+ public static final Class<Annotation> REMOVE_ANNOTATION;
static
{
- STATELESS = classForName("javax.ejb.Stateless");
- STATEFUL = classForName("javax.ejb.Stateful");
- MESSAGE_DRIVEN = classForName("javax.ejb.MessageDriven");
+ STATELESS_ANNOTATION = classForName("javax.ejb.Stateless");
+ STATEFUL_ANNOTATION = classForName("javax.ejb.Stateful");
+ MESSAGE_DRIVEN_ANNOTATION = classForName("javax.ejb.MessageDriven");
+ SINGLETON_ANNOTATION = classForName("javax.ejb.Singleton");
+ REMOVE_ANNOTATION = classForName("javax.ejb.Remove");
}
private static Class classForName(String name)
@@ -34,39 +42,20 @@
}
}
- public static boolean isStatelessEjbComponent(Class<?> clazz)
+ public static <T> EjbMetaData<T> getEjbMetaData(Class<? extends T> clazz)
{
- EjbMetaData ejbMetaData = getEjbMetaData(clazz);
- if (ejbMetaData != null)
+ // TODO replace with an application lookup
+ if (!ejbMetaDataMap.containsKey(clazz))
{
- return ejbMetaData.isStateless();
+ EjbMetaData<T> ejbMetaData = new EjbMetaData(clazz);
+ ejbMetaDataMap.put(clazz, ejbMetaData);
+ return ejbMetaData;
}
- return false;
- }
-
- public static boolean isStatefulEjbComponent(Class<?> clazz)
- {
- EjbMetaData ejbMetaData = getEjbMetaData(clazz);
- if (ejbMetaData != null)
+ else
{
- return ejbMetaData.isStateful();
+ return ejbMetaDataMap.get(clazz);
}
- return false;
+
}
- public static boolean isMessageDrivenEjbComponent(Class<?> clazz)
- {
- EjbMetaData ejbMetaData = getEjbMetaData(clazz);
- if (ejbMetaData != null)
- {
- return ejbMetaData.isMessageDriven();
- }
- return false;
- }
-
- public static EjbMetaData getEjbMetaData(Class<?> clazz)
- {
- return null;
- }
-
}
Modified: ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/ejb/EjbMetaData.java
===================================================================
--- ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/ejb/EjbMetaData.java 2008-07-03 01:55:49 UTC (rev 37)
+++ ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/ejb/EjbMetaData.java 2008-07-03 13:02:30 UTC (rev 38)
@@ -1,24 +1,84 @@
package org.jboss.webbeans.ejb;
-public class EjbMetaData
+import static org.jboss.webbeans.ejb.EJB.MESSAGE_DRIVEN_ANNOTATION;
+import static org.jboss.webbeans.ejb.EJB.SINGLETON_ANNOTATION;
+import static org.jboss.webbeans.ejb.EJB.STATEFUL_ANNOTATION;
+import static org.jboss.webbeans.ejb.EJB.REMOVE_ANNOTATION;
+import static org.jboss.webbeans.ejb.EJB.STATELESS_ANNOTATION;
+import static org.jboss.webbeans.ejb.EjbMetaData.EjbType.MESSAGE_DRIVEN;
+import static org.jboss.webbeans.ejb.EjbMetaData.EjbType.SINGLETON;
+import static org.jboss.webbeans.ejb.EjbMetaData.EjbType.STATEFUL;
+import static org.jboss.webbeans.ejb.EjbMetaData.EjbType.STATELESS;
+
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.jboss.webbeans.util.Reflections;
+
+public class EjbMetaData<T>
{
+ public enum EjbType
+ {
+ STATELESS,
+ STATEFUL,
+ SINGLETON,
+ MESSAGE_DRIVEN;
+ }
+
+ private EjbType ejbType;
+ private List<Method> removeMethods;
+
+ public EjbMetaData(Class<T> type)
+ {
+ // TODO Merge in ejb-jar.xml
+ if (type.isAnnotationPresent(STATELESS_ANNOTATION))
+ {
+ this.ejbType = STATELESS;
+ }
+ else if (type.isAnnotationPresent(STATEFUL_ANNOTATION))
+ {
+ this.ejbType = STATEFUL;
+ this.removeMethods = new ArrayList<Method>();
+ for (Method removeMethod : Reflections.getMethods(type, REMOVE_ANNOTATION))
+ {
+ removeMethods.add(removeMethod);
+ }
+ }
+ else if (type.isAnnotationPresent(MESSAGE_DRIVEN_ANNOTATION))
+ {
+ this.ejbType = MESSAGE_DRIVEN;
+ }
+ else if (type.isAnnotationPresent(SINGLETON_ANNOTATION))
+ {
+ this.ejbType = SINGLETON;
+ }
+ }
+
public boolean isStateless()
{
- // TODO Auto-generated method stub
- return false;
+ return STATELESS.equals(ejbType);
}
public boolean isStateful()
{
- // TODO Auto-generated method stub
- return false;
+ return STATEFUL.equals(ejbType);
}
public boolean isMessageDriven()
{
- // TODO Auto-generated method stub
- return false;
+ return MESSAGE_DRIVEN.equals(ejbType);
}
+ public boolean isSingleton()
+ {
+ return SINGLETON.equals(ejbType);
+ }
+
+ public List<Method> getRemoveMethods()
+ {
+ return removeMethods;
+ }
+
}
Copied: ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/injectable/ConstructorMetaModel.java (from rev 34, ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/ConstructorMetaModel.java)
===================================================================
--- ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/injectable/ConstructorMetaModel.java (rev 0)
+++ ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/injectable/ConstructorMetaModel.java 2008-07-03 13:02:30 UTC (rev 38)
@@ -0,0 +1,45 @@
+package org.jboss.webbeans.injectable;
+
+import java.lang.reflect.Constructor;
+import java.util.logging.Logger;
+
+import javax.webbeans.Container;
+
+import org.jboss.webbeans.util.LoggerUtil;
+
+public class ConstructorMetaModel<T> extends UnitMetaModel<T>
+{
+
+ public static final String LOGGER_NAME = "componentConstructor";
+
+ private static Logger log = LoggerUtil.getLogger(LOGGER_NAME);
+
+ private Constructor<T> constructor;
+
+ @SuppressWarnings("unchecked")
+ public ConstructorMetaModel(Constructor<T> constructor)
+ {
+ super(constructor.getParameterTypes(), constructor.getParameterAnnotations());
+ this.constructor = constructor;
+ log.finest("Initialized metadata for " + constructor + " with injectable parameters " + getParameters());
+ }
+
+ public Constructor<T> getConstructor()
+ {
+ return constructor;
+ }
+
+ public T invoke(Container container)
+ {
+ try
+ {
+ log.finest("Creating new instance of " + constructor.getDeclaringClass() + " with injected parameters " + getParameters());
+ return constructor.newInstance(getParameterValues(container));
+ }
+ catch (Exception e)
+ {
+ throw new RuntimeException("Error instantiating " + constructor.getDeclaringClass(), e);
+ }
+ }
+
+}
Property changes on: ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/injectable/ConstructorMetaModel.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Copied: ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/injectable/ElementMetaModel.java (from rev 28, ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/AbstractInjectedThingMetaModel.java)
===================================================================
--- ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/injectable/ElementMetaModel.java (rev 0)
+++ ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/injectable/ElementMetaModel.java 2008-07-03 13:02:30 UTC (rev 38)
@@ -0,0 +1,40 @@
+package org.jboss.webbeans.injectable;
+
+import java.lang.annotation.Annotation;
+
+import javax.webbeans.Container;
+
+public abstract class ElementMetaModel<T>
+{
+
+ private Annotation[] bindingTypes;
+
+ public ElementMetaModel(Annotation[] bindingTypes)
+ {
+ this.bindingTypes = bindingTypes;
+ }
+
+ public ElementMetaModel()
+ {
+ this.bindingTypes = new Annotation[0];
+ }
+
+ public Annotation[] getBindingTypes()
+ {
+ return bindingTypes;
+ }
+
+ @Override
+ public String toString()
+ {
+ return getType() + " with binding types " + getBindingTypes();
+ }
+
+ public T getValue(Container container)
+ {
+ return container.getInstanceByType(getType(), getBindingTypes());
+ }
+
+ public abstract Class<? extends T> getType();
+
+}
Property changes on: ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/injectable/ElementMetaModel.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Added: ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/injectable/MethodMetaModel.java
===================================================================
--- ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/injectable/MethodMetaModel.java (rev 0)
+++ ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/injectable/MethodMetaModel.java 2008-07-03 13:02:30 UTC (rev 38)
@@ -0,0 +1,31 @@
+package org.jboss.webbeans.injectable;
+
+import java.lang.reflect.Method;
+
+import javax.webbeans.Container;
+
+public class MethodMetaModel<T> extends UnitMetaModel<T>
+{
+
+ private Method method;
+
+ public MethodMetaModel(Method method)
+ {
+ super(method.getParameterTypes(), method.getParameterAnnotations());
+ this.method = method;
+ }
+
+ @SuppressWarnings("unchecked")
+ public T invoke(Container container, Object instance)
+ {
+ try
+ {
+ return (T) method.invoke(instance, getParameterValues(container));
+ }
+ catch (Exception e)
+ {
+ throw new RuntimeException("Unable to invoke " + method + " on " + instance, e);
+ }
+ }
+
+}
Property changes on: ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/injectable/MethodMetaModel.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Copied: ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/injectable/ParameterMetaModel.java (from rev 34, ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/InjectedParameterMetaModel.java)
===================================================================
--- ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/injectable/ParameterMetaModel.java (rev 0)
+++ ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/injectable/ParameterMetaModel.java 2008-07-03 13:02:30 UTC (rev 38)
@@ -0,0 +1,33 @@
+package org.jboss.webbeans.injectable;
+
+import java.lang.annotation.Annotation;
+
+import org.jboss.webbeans.bindings.CurrentBinding;
+
+public class ParameterMetaModel<T> extends ElementMetaModel<T>
+{
+
+ private static Annotation[] currentBinding = {new CurrentBinding()};
+
+ private Class<? extends T> type;
+
+ public ParameterMetaModel(Annotation[] bindingTypes, Class<? extends T> type)
+ {
+ super(bindingTypes);
+ this.type = type;
+ }
+
+ public ParameterMetaModel(Class<? extends T> type)
+ {
+ super(currentBinding);
+ this.type = type;
+ }
+
+ public Class<? extends T> getType()
+ {
+ return type;
+ }
+
+
+
+}
Property changes on: ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/injectable/ParameterMetaModel.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Added: ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/injectable/UnitMetaModel.java
===================================================================
--- ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/injectable/UnitMetaModel.java (rev 0)
+++ ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/injectable/UnitMetaModel.java 2008-07-03 13:02:30 UTC (rev 38)
@@ -0,0 +1,54 @@
+package org.jboss.webbeans.injectable;
+
+import java.lang.annotation.Annotation;
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.webbeans.Container;
+
+public abstract class UnitMetaModel<T>
+{
+
+ private List<ElementMetaModel<Object>> parameters;
+
+ public UnitMetaModel(Class<?>[] parameterTypes, Annotation[][] parameterAnnotations)
+ {
+ parameters = initParameters(parameterTypes, parameterAnnotations);
+ }
+
+ public List<ElementMetaModel<Object>> getParameters()
+ {
+ return parameters;
+ }
+
+ @SuppressWarnings("unchecked")
+ protected static List<ElementMetaModel<Object>> initParameters(Class<?>[] parameterTypes, Annotation[][] parameterAnnotations)
+ {
+ List<ElementMetaModel<Object>> injectedParameters = new ArrayList<ElementMetaModel<Object>>();
+ for (int i = 0; i < parameterTypes.length; i++)
+ {
+ if (parameterAnnotations[i].length > 0)
+ {
+ ParameterMetaModel<Object> parameter = new ParameterMetaModel(parameterAnnotations[i], parameterTypes[i]);
+ injectedParameters.add(i, parameter);
+ }
+ else
+ {
+ ParameterMetaModel<Object> parameter = new ParameterMetaModel(parameterTypes[i]);
+ injectedParameters.add(i, parameter);
+ }
+ }
+ return injectedParameters;
+ }
+
+ public Object[] getParameterValues(Container container)
+ {
+ Object[] parameterValues = new Object[parameters.size()];
+ for (int i = 0; i < parameterValues.length; i++)
+ {
+ parameterValues[i] = parameters.get(i).getValue(container);
+ }
+ return parameterValues;
+ }
+
+}
Property changes on: ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/injectable/UnitMetaModel.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Deleted: ri/trunk/webbeans-impl/src/test/java/org/jboss/webbeans/test/ComponentInstanceTest.java
===================================================================
--- ri/trunk/webbeans-impl/src/test/java/org/jboss/webbeans/test/ComponentInstanceTest.java 2008-07-03 01:55:49 UTC (rev 37)
+++ ri/trunk/webbeans-impl/src/test/java/org/jboss/webbeans/test/ComponentInstanceTest.java 2008-07-03 13:02:30 UTC (rev 38)
@@ -1,570 +0,0 @@
-package org.jboss.webbeans.test;
-
-import static org.jboss.webbeans.test.util.Util.annotationSetMatches;
-
-import java.lang.annotation.Annotation;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
-import javax.webbeans.ApplicationScoped;
-import javax.webbeans.ComponentInstance;
-import javax.webbeans.ConversationScoped;
-import javax.webbeans.Current;
-import javax.webbeans.Dependent;
-import javax.webbeans.Named;
-import javax.webbeans.Production;
-import javax.webbeans.RequestScoped;
-
-import org.jboss.webbeans.ComponentInstanceImpl;
-import org.jboss.webbeans.ContainerImpl;
-import org.jboss.webbeans.StereotypeMetaModel;
-import org.jboss.webbeans.ComponentInstanceImpl.ComponentType;
-import org.jboss.webbeans.bindings.ConversationScopedBinding;
-import org.jboss.webbeans.bindings.CurrentBinding;
-import org.jboss.webbeans.bindings.DependentBinding;
-import org.jboss.webbeans.bindings.NamedBinding;
-import org.jboss.webbeans.bindings.RequestScopedBinding;
-import org.jboss.webbeans.bindings.StandardBinding;
-import org.jboss.webbeans.test.annotations.AnimalStereotype;
-import org.jboss.webbeans.test.annotations.AnotherDeploymentType;
-import org.jboss.webbeans.test.annotations.Asynchronous;
-import org.jboss.webbeans.test.annotations.FishStereotype;
-import org.jboss.webbeans.test.annotations.HornedAnimalDeploymentType;
-import org.jboss.webbeans.test.annotations.HornedMammalStereotype;
-import org.jboss.webbeans.test.annotations.MammalStereotype;
-import org.jboss.webbeans.test.annotations.RequestScopedAnimalStereotype;
-import org.jboss.webbeans.test.annotations.RiverFishStereotype;
-import org.jboss.webbeans.test.annotations.Synchronous;
-import org.jboss.webbeans.test.bindings.AnimalStereotypeBinding;
-import org.jboss.webbeans.test.bindings.AnotherDeploymentTypeBinding;
-import org.jboss.webbeans.test.bindings.AsynchronousBinding;
-import org.jboss.webbeans.test.bindings.FishStereotypeBinding;
-import org.jboss.webbeans.test.bindings.HornedAnimalDeploymentTypeBinding;
-import org.jboss.webbeans.test.bindings.HornedMamalStereotypeBinding;
-import org.jboss.webbeans.test.bindings.RiverFishStereotypeBinding;
-import org.jboss.webbeans.test.bindings.SynchronousBinding;
-import org.jboss.webbeans.test.components.Antelope;
-import org.jboss.webbeans.test.components.Carp;
-import org.jboss.webbeans.test.components.Cat;
-import org.jboss.webbeans.test.components.Chair;
-import org.jboss.webbeans.test.components.ComponentWithTooManyDeploymentTypes;
-import org.jboss.webbeans.test.components.ComponentWithTooManyScopeTypes;
-import org.jboss.webbeans.test.components.Cow;
-import org.jboss.webbeans.test.components.Goldfish;
-import org.jboss.webbeans.test.components.Gorilla;
-import org.jboss.webbeans.test.components.Haddock;
-import org.jboss.webbeans.test.components.Horse;
-import org.jboss.webbeans.test.components.Moose;
-import org.jboss.webbeans.test.components.Order;
-import org.jboss.webbeans.test.components.Pig;
-import org.jboss.webbeans.test.components.SeaBass;
-import org.jboss.webbeans.test.components.Tuna;
-import org.jboss.webbeans.test.mock.MockContainerImpl;
-import org.jboss.webbeans.util.AnnotatedItem;
-import org.jboss.webbeans.util.ClassAnnotatedItem;
-import org.jboss.webbeans.util.MutableAnnotatedItem;
-import org.junit.Before;
-import org.junit.Test;
-
-public class ComponentInstanceTest
-{
-
- private ContainerImpl container;
-
- private AnnotatedItem emptyAnnotatedItem;
-
- @Before
- public void before()
- {
- emptyAnnotatedItem = new MutableAnnotatedItem(null, new HashMap<Class<? extends Annotation>, Annotation>());
-
- List<Annotation> enabledDeploymentTypes = new ArrayList<Annotation>();
- enabledDeploymentTypes.add(new StandardBinding());
- enabledDeploymentTypes.add(new AnotherDeploymentTypeBinding());
- enabledDeploymentTypes.add(new HornedAnimalDeploymentTypeBinding());
- container = new MockContainerImpl(enabledDeploymentTypes);
-
- initStereotypes(container);
- }
-
- private void initStereotypes(ContainerImpl container)
- {
- container.getStereotypeManager().addStereotype(new StereotypeMetaModel(new ClassAnnotatedItem(AnimalStereotype.class)));
- container.getStereotypeManager().addStereotype(new StereotypeMetaModel(new ClassAnnotatedItem(HornedMammalStereotype.class)));
- container.getStereotypeManager().addStereotype(new StereotypeMetaModel(new ClassAnnotatedItem(MammalStereotype.class)));
- container.getStereotypeManager().addStereotype(new StereotypeMetaModel(new ClassAnnotatedItem(FishStereotype.class)));
- container.getStereotypeManager().addStereotype(new StereotypeMetaModel(new ClassAnnotatedItem(RiverFishStereotype.class)));
- container.getStereotypeManager().addStereotype(new StereotypeMetaModel(new ClassAnnotatedItem(RequestScopedAnimalStereotype.class)));
- }
-
- @Test
- public void testTooManyDeploymentTypes()
- {
- boolean exception = false;
- try
- {
- new ComponentInstanceImpl<ComponentWithTooManyDeploymentTypes>(new ClassAnnotatedItem(ComponentWithTooManyDeploymentTypes.class), emptyAnnotatedItem, container);
- }
- catch (Exception e)
- {
- exception = true;
- }
- assert exception;
- }
-
- // **** TESTS FOR DEPLOYMENT TYPE **** //
-
- @Test
- public void testXmlDeploymentTypeOverridesJava()
- {
- Map<Class<? extends Annotation>, Annotation> xmlDefinedDeploymentTypeAnnotations = new HashMap<Class<? extends Annotation>, Annotation>();
- xmlDefinedDeploymentTypeAnnotations.put(AnotherDeploymentType.class, new AnotherDeploymentTypeBinding());
- AnnotatedItem xmlDefinedDeploymentTypeAnnotatedItem = new MutableAnnotatedItem(ComponentWithTooManyDeploymentTypes.class, xmlDefinedDeploymentTypeAnnotations);
-
- ComponentInstance<ComponentWithTooManyDeploymentTypes> component = new ComponentInstanceImpl<ComponentWithTooManyDeploymentTypes>(new ClassAnnotatedItem(ComponentWithTooManyDeploymentTypes.class), xmlDefinedDeploymentTypeAnnotatedItem, container);
- assert component.getDeploymentType().annotationType().equals(AnotherDeploymentType.class);
- }
-
- @Test
- public void testXmlDefaultDeploymentType()
- {
- AnnotatedItem antelopeAnnotatedItem = new MutableAnnotatedItem(Antelope.class, new HashMap<Class<? extends Annotation>, Annotation>());
- ComponentInstance<Antelope> antelope = new ComponentInstanceImpl<Antelope>(emptyAnnotatedItem, antelopeAnnotatedItem, container);
- assert antelope.getDeploymentType().annotationType().equals(Production.class);
- }
-
- @Test
- public void testXmlIgnoresJavaDeploymentType()
- {
- AnnotatedItem annotatedItem = new MutableAnnotatedItem(Tuna.class, new HashMap<Class<? extends Annotation>, Annotation>());
- ComponentInstance<Tuna> tuna = new ComponentInstanceImpl<Tuna>(new ClassAnnotatedItem(Tuna.class), annotatedItem, container);
- assert tuna.getDeploymentType().annotationType().equals(Production.class);
- }
-
- @Test
- public void testDeploymentTypePrecedenceSelection()
- {
- Map<Class<? extends Annotation>, Annotation> annotations = new HashMap<Class<? extends Annotation>, Annotation>();
- annotations.put(HornedMammalStereotype.class, new HornedMamalStereotypeBinding());
- AnnotatedItem annotatedItem = new MutableAnnotatedItem(Moose.class, annotations);
-
- ComponentInstance<Moose> moose = new ComponentInstanceImpl<Moose>(new ClassAnnotatedItem(Moose.class), annotatedItem, container);
- assert moose.getDeploymentType().annotationType().equals(HornedAnimalDeploymentType.class);
-
- }
-
- // **** TESTS FOR BINDING TYPE **** //
-
- @SuppressWarnings("unchecked")
- @Test
- public void testXmlBindingTypeOverridesJava()
- {
- Map<Class<? extends Annotation>, Annotation> annotations = new HashMap<Class<? extends Annotation>, Annotation>();
- annotations.put(Asynchronous.class, new AsynchronousBinding());
- AnnotatedItem annotatedItem = new MutableAnnotatedItem(Cat.class, annotations);
-
- ComponentInstance<Cat> cat = new ComponentInstanceImpl<Cat>(new ClassAnnotatedItem(Cat.class), annotatedItem, container);
- assert cat.getBindingTypes().size() == 1;
- assert annotationSetMatches(cat.getBindingTypes(), Asynchronous.class);
- }
-
- @SuppressWarnings("unchecked")
- @Test
- public void testBindingTypesDeclaredInJava()
- {
- ComponentInstance<Cat> cat = new ComponentInstanceImpl<Cat>(new ClassAnnotatedItem(Cat.class), emptyAnnotatedItem, container);
- assert cat.getBindingTypes().size() == 1;
- assert annotationSetMatches(cat.getBindingTypes(), Synchronous.class);
- }
-
- @SuppressWarnings("unchecked")
- @Test
- public void testBindingTypesDeclaredInXml()
- {
- Map<Class<? extends Annotation>, Annotation> annotations = new HashMap<Class<? extends Annotation>, Annotation>();
- annotations.put(Asynchronous.class, new AsynchronousBinding());
- AnnotatedItem annotatedItem = new MutableAnnotatedItem(Antelope.class, annotations);
-
- ComponentInstance<Antelope> antelope = new ComponentInstanceImpl<Antelope>(emptyAnnotatedItem, annotatedItem, container);
- assert annotationSetMatches(antelope.getBindingTypes(), Asynchronous.class);
- }
-
- @SuppressWarnings("unchecked")
- @Test
- public void testDefaultBindingType()
- {
- ComponentInstance<Order> order = new ComponentInstanceImpl<Order>(new ClassAnnotatedItem(Order.class), emptyAnnotatedItem, container);
- assert order.getBindingTypes().size() == 1;
- order.getBindingTypes().iterator().next().annotationType().equals(Current.class);
- }
-
- // **** TESTS FOR SCOPES **** //
-
- @Test
- public void testScopeDeclaredInJava()
- {
- ComponentInstance<SeaBass> trout = new ComponentInstanceImpl<SeaBass>(new ClassAnnotatedItem(SeaBass.class), emptyAnnotatedItem, container);
- assert trout.getScopeType().annotationType().equals(RequestScoped.class);
- }
-
- @Test
- public void testScopeDeclaredInXml()
- {
- Map<Class<? extends Annotation>, Annotation> annotations = new HashMap<Class<? extends Annotation>, Annotation>();
- annotations.put(RequestScoped.class, new RequestScopedBinding());
- AnnotatedItem annotatedItem = new MutableAnnotatedItem(Order.class, annotations);
-
- ComponentInstance<Order> order = new ComponentInstanceImpl<Order>(new ClassAnnotatedItem(Order.class), annotatedItem, container);
- assert order.getScopeType().annotationType().equals(RequestScoped.class);
- }
-
- @Test
- public void testScopeDeclaredInXmlOverridesJava()
- {
- Map<Class<? extends Annotation>, Annotation> annotations = new HashMap<Class<? extends Annotation>, Annotation>();
- annotations.put(ConversationScoped.class, new ConversationScopedBinding());
- AnnotatedItem annotatedItem = new MutableAnnotatedItem(SeaBass.class, annotations);
- ComponentInstance<SeaBass> trout = new ComponentInstanceImpl<SeaBass>(new ClassAnnotatedItem(SeaBass.class), annotatedItem, container);
- assert trout.getScopeType().annotationType().equals(ConversationScoped.class);
- }
-
- @Test
- public void testScopeMissingInXml()
- {
- Map<Class<? extends Annotation>, Annotation> annotations = new HashMap<Class<? extends Annotation>, Annotation>();
- AnnotatedItem annotatedItem = new MutableAnnotatedItem(SeaBass.class, annotations);
-
- ComponentInstance<SeaBass> trout = new ComponentInstanceImpl<SeaBass>(new ClassAnnotatedItem(SeaBass.class), annotatedItem, container);
- assert trout.getScopeType().annotationType().equals(RequestScoped.class);
- }
-
- @Test
- public void testNoScopeSpecified()
- {
- ComponentInstance<Order> order = new ComponentInstanceImpl<Order>(new ClassAnnotatedItem(Order.class), emptyAnnotatedItem, container);
- assert order.getScopeType().annotationType().equals(Dependent.class);
- }
-
- @Test
- public void testTooManyScopesSpecified()
- {
- boolean exception = false;
- try
- {
- new ComponentInstanceImpl<ComponentWithTooManyScopeTypes>(new ClassAnnotatedItem(ComponentWithTooManyScopeTypes.class), emptyAnnotatedItem, container);
- }
- catch (Exception e)
- {
- exception = true;
- }
- assert exception;
- }
-
- @Test
- public void testTooManyScopesSpecifiedInXml()
- {
- boolean exception = false;
- try
- {
- Map<Class<? extends Annotation>, Annotation> annotations = new HashMap<Class<? extends Annotation>, Annotation>();
- annotations.put(RequestScoped.class, new RequestScopedBinding());
- annotations.put(ConversationScoped.class, new ConversationScopedBinding());
- AnnotatedItem antelopeAnnotatedItem = new MutableAnnotatedItem(Antelope.class, annotations);
- new ComponentInstanceImpl<Antelope>(emptyAnnotatedItem, antelopeAnnotatedItem, container);
- }
- catch (Exception e)
- {
- exception = true;
- }
- assert exception;
- }
-
- @Test
- public void testScopeSpecifiedAndStereotyped()
- {
- Map<Class<? extends Annotation>, Annotation> annotations = new HashMap<Class<? extends Annotation>, Annotation>();
- annotations.put(FishStereotype.class, new FishStereotypeBinding());
- AnnotatedItem annotatedItem = new MutableAnnotatedItem(SeaBass.class, annotations);
- ComponentInstance<SeaBass> trout = new ComponentInstanceImpl<SeaBass>(new ClassAnnotatedItem(SeaBass.class), annotatedItem, container);
- assert trout.getScopeType().annotationType().equals(RequestScoped.class);
- }
-
- @Test
- public void testMutipleIncompatibleScopeStereotypes()
- {
- Map<Class<? extends Annotation>, Annotation> annotations = new HashMap<Class<? extends Annotation>, Annotation>();
- annotations.put(FishStereotype.class, new FishStereotypeBinding());
- annotations.put(AnimalStereotype.class, new AnimalStereotypeBinding());
- AnnotatedItem annotatedItem = new MutableAnnotatedItem(Haddock.class, annotations);
-
- boolean exception = false;
- try
- {
- new ComponentInstanceImpl<Haddock>(new ClassAnnotatedItem(Haddock.class), annotatedItem, container);
- }
- catch (Exception e)
- {
- exception = true;
- }
- assert exception;
- }
-
- @Test
- public void testMutipleIncompatibleScopeStereotypesWithScopeSpecified()
- {
- Map<Class<? extends Annotation>, Annotation> annotations = new HashMap<Class<? extends Annotation>, Annotation>();
- annotations.put(FishStereotype.class, new FishStereotypeBinding());
- annotations.put(AnimalStereotype.class, new AnimalStereotypeBinding());
- AnnotatedItem annotatedItem = new MutableAnnotatedItem(SeaBass.class, annotations);
-
- ComponentInstance<SeaBass> trout = new ComponentInstanceImpl<SeaBass>(new ClassAnnotatedItem(SeaBass.class), annotatedItem, container);
- assert trout.getScopeType().annotationType().equals(RequestScoped.class);
- }
-
- @Test
- public void testMutipleCompatibleScopeStereotypes()
- {
- Map<Class<? extends Annotation>, Annotation> annotations = new HashMap<Class<? extends Annotation>, Annotation>();
- annotations.put(FishStereotype.class, new FishStereotypeBinding());
- annotations.put(RiverFishStereotype.class, new RiverFishStereotypeBinding());
- AnnotatedItem annotatedItem = new MutableAnnotatedItem(Haddock.class, annotations);
-
- ComponentInstance<Haddock> haddock = new ComponentInstanceImpl<Haddock>(new ClassAnnotatedItem(Haddock.class), annotatedItem, container);
- assert haddock.getScopeType().annotationType().equals(ApplicationScoped.class);
- }
-
- // **** TESTS FOR COMPONENT NAME **** /
-
- @Test
- public void testNamed()
- {
- ComponentInstanceImpl<Haddock> haddock = new ComponentInstanceImpl<Haddock>(new ClassAnnotatedItem(Haddock.class), emptyAnnotatedItem, container);
- assert haddock.getName() != null;
- haddock.getComponentType().equals(ComponentType.SIMPLE);
- assert haddock.getName().equals("haddock");
- }
-
- @Test
- public void testXmlNamed()
- {
- Map<Class<? extends Annotation>, Annotation> annotations = new HashMap<Class<? extends Annotation>, Annotation>();
- annotations.put(Named.class, new NamedBinding()
- {
-
- public String value()
- {
- return "";
- }
- });
- AnnotatedItem annotatedItem = new MutableAnnotatedItem(SeaBass.class, annotations);
- ComponentInstanceImpl<SeaBass> trout = new ComponentInstanceImpl<SeaBass>(new ClassAnnotatedItem(SeaBass.class), annotatedItem, container);
-
- assert trout.getName() != null;
- trout.getComponentType().equals(ComponentType.SIMPLE);
- assert trout.getName().equals("seaBass");
- }
-
- @Test
- public void testNonDefaultXmlNamed()
- {
- Map<Class<? extends Annotation>, Annotation> annotations = new HashMap<Class<? extends Annotation>, Annotation>();
- annotations.put(Named.class, new NamedBinding()
- {
-
- public String value()
- {
- return "aTrout";
- }
- });
- AnnotatedItem annotatedItem = new MutableAnnotatedItem(SeaBass.class, annotations);
- ComponentInstance<SeaBass> trout = new ComponentInstanceImpl<SeaBass>(new ClassAnnotatedItem(SeaBass.class), annotatedItem, container);
-
- assert trout.getName().equals("aTrout");
- }
-
- @Test
- public void testNotNamed()
- {
- ComponentInstance<SeaBass> trout = new ComponentInstanceImpl<SeaBass>(new ClassAnnotatedItem(SeaBass.class), emptyAnnotatedItem, container);
- assert trout.getName() == null;
- }
-
- @Test
- public void testNonDefaultNamed()
- {
- ComponentInstance<Moose> moose = new ComponentInstanceImpl<Moose>(new ClassAnnotatedItem(Moose.class), emptyAnnotatedItem, container);
- assert moose.getName().equals("aMoose");
- }
-
-
- // **** TESTS FOR STEREOTYPES **** //
-
- @SuppressWarnings("unchecked")
- @Test
- public void testStereotypeDeclaredInXmlAndJava()
- {
- Map<Class<? extends Annotation>, Annotation> orderXmlAnnotations = new HashMap<Class<? extends Annotation>, Annotation>();
- orderXmlAnnotations.put(Current.class, new CurrentBinding());
- orderXmlAnnotations.put(Synchronous.class, new SynchronousBinding());
- orderXmlAnnotations.put(Named.class, new NamedBinding()
- {
- public String value()
- {
- return "currentSynchronousOrder";
- }
- });
- AnnotatedItem currentSynchronousOrderAnnotatedItem = new MutableAnnotatedItem(Order.class, orderXmlAnnotations);
-
- ComponentInstance<Order> order = new ComponentInstanceImpl<Order>(new ClassAnnotatedItem(Order.class), currentSynchronousOrderAnnotatedItem, container);
- assert Production.class.equals(order.getDeploymentType().annotationType());
- assert "currentSynchronousOrder".equals(order.getName());
- assert order.getBindingTypes().size() == 2;
- assert annotationSetMatches(order.getBindingTypes(), Current.class, Synchronous.class);
- assert order.getScopeType().annotationType().equals(Dependent.class);
- }
-
- @Test
- public void testSingleStereotype()
- {
- ComponentInstance<Gorilla> gorilla = new ComponentInstanceImpl<Gorilla>(new ClassAnnotatedItem(Gorilla.class), emptyAnnotatedItem, container);
- assert gorilla.getName() == null;
- assert gorilla.getDeploymentType().annotationType().equals(Production.class);
- assert gorilla.getBindingTypes().iterator().next().annotationType().equals(Current.class);
- assert gorilla.getScopeType().annotationType().equals(RequestScoped.class);
- }
-
- @Test
- public void testRequiredTypeIsImplemented()
- {
- try
- {
- new ComponentInstanceImpl<Gorilla>(new ClassAnnotatedItem(Gorilla.class), emptyAnnotatedItem, container);
- }
- catch (Exception e)
- {
- assert false;
- }
-
- }
-
- @Test
- public void testRequiredTypeIsNotImplemented()
- {
- boolean exception = false;
- try
- {
- new ComponentInstanceImpl<Chair>(new ClassAnnotatedItem(Chair.class), emptyAnnotatedItem, container);
- }
- catch (Exception e)
- {
- exception = true;
- }
- assert exception;
-
- }
-
- @Test
- public void testScopeIsSupported()
- {
- try
- {
- new ComponentInstanceImpl<Goldfish>(new ClassAnnotatedItem(Goldfish.class), emptyAnnotatedItem, container);
- }
- catch (Exception e)
- {
- e.printStackTrace();
- assert false;
- }
-
- }
-
- @Test
- public void testScopeIsNotSupported()
- {
- boolean exception = false;
- try
- {
- new ComponentInstanceImpl<Carp>(new ClassAnnotatedItem(Carp.class), emptyAnnotatedItem, container);
- }
- catch (Exception e)
- {
- exception = true;
- }
- assert exception;
-
- }
-
- //*** COMPONENT CLASS CHECKS ****//
-
- @Test
- public void testAbstractClassIsNotAllowed()
- {
- boolean exception = false;
- try
- {
- new ComponentInstanceImpl<Cow>(new ClassAnnotatedItem(Cow.class), emptyAnnotatedItem, container);
- }
- catch (Exception e)
- {
- exception = true;
- }
- assert exception;
- }
-
- @Test
- public void testFinalClassMustBeDependentScoped()
- {
- boolean exception = false;
- try
- {
- new ComponentInstanceImpl<Horse>(new ClassAnnotatedItem(Horse.class), emptyAnnotatedItem, container);
- }
- catch (Exception e)
- {
- exception = true;
- }
- assert exception;
-
- Map<Class<? extends Annotation>, Annotation> annotations = new HashMap<Class<? extends Annotation>, Annotation>();
- annotations.put(Dependent.class, new DependentBinding());
- AnnotatedItem annotatedItem = new MutableAnnotatedItem(Horse.class, annotations);
- try
- {
- new ComponentInstanceImpl<Horse>(new ClassAnnotatedItem(Horse.class), annotatedItem, container);
- }
- catch (Exception e)
- {
- assert false;
- }
- }
-
- @Test
- public void testClassWithFinalMethodMustBeDependentScoped()
- {
- boolean exception = false;
- try
- {
- new ComponentInstanceImpl<Pig>(new ClassAnnotatedItem(Pig.class), emptyAnnotatedItem, container);
- }
- catch (Exception e)
- {
- exception = true;
- }
- assert exception;
-
- Map<Class<? extends Annotation>, Annotation> annotations = new HashMap<Class<? extends Annotation>, Annotation>();
- annotations.put(Dependent.class, new DependentBinding());
- AnnotatedItem annotatedItem = new MutableAnnotatedItem(Pig.class, annotations);
- try
- {
- new ComponentInstanceImpl<Pig>(new ClassAnnotatedItem(Pig.class), annotatedItem, container);
- }
- catch (Exception e)
- {
- assert false;
- }
- }
-
-}
Copied: ri/trunk/webbeans-impl/src/test/java/org/jboss/webbeans/test/ComponentMetaModelTest.java (from rev 35, ri/trunk/webbeans-impl/src/test/java/org/jboss/webbeans/test/ComponentInstanceTest.java)
===================================================================
--- ri/trunk/webbeans-impl/src/test/java/org/jboss/webbeans/test/ComponentMetaModelTest.java (rev 0)
+++ ri/trunk/webbeans-impl/src/test/java/org/jboss/webbeans/test/ComponentMetaModelTest.java 2008-07-03 13:02:30 UTC (rev 38)
@@ -0,0 +1,569 @@
+package org.jboss.webbeans.test;
+
+import static org.jboss.webbeans.test.util.Util.annotationSetMatches;
+
+import java.lang.annotation.Annotation;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import javax.webbeans.ApplicationScoped;
+import javax.webbeans.ConversationScoped;
+import javax.webbeans.Current;
+import javax.webbeans.Dependent;
+import javax.webbeans.Named;
+import javax.webbeans.Production;
+import javax.webbeans.RequestScoped;
+
+import org.jboss.webbeans.ComponentMetaModel;
+import org.jboss.webbeans.ContainerImpl;
+import org.jboss.webbeans.StereotypeMetaModel;
+import org.jboss.webbeans.ComponentMetaModel.ComponentType;
+import org.jboss.webbeans.bindings.ConversationScopedBinding;
+import org.jboss.webbeans.bindings.CurrentBinding;
+import org.jboss.webbeans.bindings.DependentBinding;
+import org.jboss.webbeans.bindings.NamedBinding;
+import org.jboss.webbeans.bindings.RequestScopedBinding;
+import org.jboss.webbeans.bindings.StandardBinding;
+import org.jboss.webbeans.test.annotations.AnimalStereotype;
+import org.jboss.webbeans.test.annotations.AnotherDeploymentType;
+import org.jboss.webbeans.test.annotations.Asynchronous;
+import org.jboss.webbeans.test.annotations.FishStereotype;
+import org.jboss.webbeans.test.annotations.HornedAnimalDeploymentType;
+import org.jboss.webbeans.test.annotations.HornedMammalStereotype;
+import org.jboss.webbeans.test.annotations.MammalStereotype;
+import org.jboss.webbeans.test.annotations.RequestScopedAnimalStereotype;
+import org.jboss.webbeans.test.annotations.RiverFishStereotype;
+import org.jboss.webbeans.test.annotations.Synchronous;
+import org.jboss.webbeans.test.bindings.AnimalStereotypeBinding;
+import org.jboss.webbeans.test.bindings.AnotherDeploymentTypeBinding;
+import org.jboss.webbeans.test.bindings.AsynchronousBinding;
+import org.jboss.webbeans.test.bindings.FishStereotypeBinding;
+import org.jboss.webbeans.test.bindings.HornedAnimalDeploymentTypeBinding;
+import org.jboss.webbeans.test.bindings.HornedMamalStereotypeBinding;
+import org.jboss.webbeans.test.bindings.RiverFishStereotypeBinding;
+import org.jboss.webbeans.test.bindings.SynchronousBinding;
+import org.jboss.webbeans.test.components.Antelope;
+import org.jboss.webbeans.test.components.Carp;
+import org.jboss.webbeans.test.components.Cat;
+import org.jboss.webbeans.test.components.Chair;
+import org.jboss.webbeans.test.components.ComponentWithTooManyDeploymentTypes;
+import org.jboss.webbeans.test.components.ComponentWithTooManyScopeTypes;
+import org.jboss.webbeans.test.components.Cow;
+import org.jboss.webbeans.test.components.Goldfish;
+import org.jboss.webbeans.test.components.Gorilla;
+import org.jboss.webbeans.test.components.Haddock;
+import org.jboss.webbeans.test.components.Horse;
+import org.jboss.webbeans.test.components.Moose;
+import org.jboss.webbeans.test.components.Order;
+import org.jboss.webbeans.test.components.Pig;
+import org.jboss.webbeans.test.components.SeaBass;
+import org.jboss.webbeans.test.components.Tuna;
+import org.jboss.webbeans.test.mock.MockContainerImpl;
+import org.jboss.webbeans.util.AnnotatedItem;
+import org.jboss.webbeans.util.ClassAnnotatedItem;
+import org.jboss.webbeans.util.MutableAnnotatedItem;
+import org.junit.Before;
+import org.junit.Test;
+
+public class ComponentMetaModelTest
+{
+
+ private ContainerImpl container;
+
+ private AnnotatedItem emptyAnnotatedItem;
+
+ @Before
+ public void before()
+ {
+ emptyAnnotatedItem = new MutableAnnotatedItem(null, new HashMap<Class<? extends Annotation>, Annotation>());
+
+ List<Annotation> enabledDeploymentTypes = new ArrayList<Annotation>();
+ enabledDeploymentTypes.add(new StandardBinding());
+ enabledDeploymentTypes.add(new AnotherDeploymentTypeBinding());
+ enabledDeploymentTypes.add(new HornedAnimalDeploymentTypeBinding());
+ container = new MockContainerImpl(enabledDeploymentTypes);
+
+ initStereotypes(container);
+ }
+
+ private void initStereotypes(ContainerImpl container)
+ {
+ container.getStereotypeManager().addStereotype(new StereotypeMetaModel(new ClassAnnotatedItem(AnimalStereotype.class)));
+ container.getStereotypeManager().addStereotype(new StereotypeMetaModel(new ClassAnnotatedItem(HornedMammalStereotype.class)));
+ container.getStereotypeManager().addStereotype(new StereotypeMetaModel(new ClassAnnotatedItem(MammalStereotype.class)));
+ container.getStereotypeManager().addStereotype(new StereotypeMetaModel(new ClassAnnotatedItem(FishStereotype.class)));
+ container.getStereotypeManager().addStereotype(new StereotypeMetaModel(new ClassAnnotatedItem(RiverFishStereotype.class)));
+ container.getStereotypeManager().addStereotype(new StereotypeMetaModel(new ClassAnnotatedItem(RequestScopedAnimalStereotype.class)));
+ }
+
+ @Test
+ public void testTooManyDeploymentTypes()
+ {
+ boolean exception = false;
+ try
+ {
+ new ComponentMetaModel<ComponentWithTooManyDeploymentTypes>(new ClassAnnotatedItem(ComponentWithTooManyDeploymentTypes.class), emptyAnnotatedItem, container);
+ }
+ catch (Exception e)
+ {
+ exception = true;
+ }
+ assert exception;
+ }
+
+ // **** TESTS FOR DEPLOYMENT TYPE **** //
+
+ @Test
+ public void testXmlDeploymentTypeOverridesJava()
+ {
+ Map<Class<? extends Annotation>, Annotation> xmlDefinedDeploymentTypeAnnotations = new HashMap<Class<? extends Annotation>, Annotation>();
+ xmlDefinedDeploymentTypeAnnotations.put(AnotherDeploymentType.class, new AnotherDeploymentTypeBinding());
+ AnnotatedItem xmlDefinedDeploymentTypeAnnotatedItem = new MutableAnnotatedItem(ComponentWithTooManyDeploymentTypes.class, xmlDefinedDeploymentTypeAnnotations);
+
+ ComponentMetaModel<ComponentWithTooManyDeploymentTypes> component = new ComponentMetaModel<ComponentWithTooManyDeploymentTypes>(new ClassAnnotatedItem(ComponentWithTooManyDeploymentTypes.class), xmlDefinedDeploymentTypeAnnotatedItem, container);
+ assert component.getDeploymentType().annotationType().equals(AnotherDeploymentType.class);
+ }
+
+ @Test
+ public void testXmlDefaultDeploymentType()
+ {
+ AnnotatedItem antelopeAnnotatedItem = new MutableAnnotatedItem(Antelope.class, new HashMap<Class<? extends Annotation>, Annotation>());
+ ComponentMetaModel<Antelope> antelope = new ComponentMetaModel<Antelope>(emptyAnnotatedItem, antelopeAnnotatedItem, container);
+ assert antelope.getDeploymentType().annotationType().equals(Production.class);
+ }
+
+ @Test
+ public void testXmlIgnoresJavaDeploymentType()
+ {
+ AnnotatedItem annotatedItem = new MutableAnnotatedItem(Tuna.class, new HashMap<Class<? extends Annotation>, Annotation>());
+ ComponentMetaModel<Tuna> tuna = new ComponentMetaModel<Tuna>(new ClassAnnotatedItem(Tuna.class), annotatedItem, container);
+ assert tuna.getDeploymentType().annotationType().equals(Production.class);
+ }
+
+ @Test
+ public void testDeploymentTypePrecedenceSelection()
+ {
+ Map<Class<? extends Annotation>, Annotation> annotations = new HashMap<Class<? extends Annotation>, Annotation>();
+ annotations.put(HornedMammalStereotype.class, new HornedMamalStereotypeBinding());
+ AnnotatedItem annotatedItem = new MutableAnnotatedItem(Moose.class, annotations);
+
+ ComponentMetaModel<Moose> moose = new ComponentMetaModel<Moose>(new ClassAnnotatedItem(Moose.class), annotatedItem, container);
+ assert moose.getDeploymentType().annotationType().equals(HornedAnimalDeploymentType.class);
+
+ }
+
+ // **** TESTS FOR BINDING TYPE **** //
+
+ @SuppressWarnings("unchecked")
+ @Test
+ public void testXmlBindingTypeOverridesJava()
+ {
+ Map<Class<? extends Annotation>, Annotation> annotations = new HashMap<Class<? extends Annotation>, Annotation>();
+ annotations.put(Asynchronous.class, new AsynchronousBinding());
+ AnnotatedItem annotatedItem = new MutableAnnotatedItem(Cat.class, annotations);
+
+ ComponentMetaModel<Cat> cat = new ComponentMetaModel<Cat>(new ClassAnnotatedItem(Cat.class), annotatedItem, container);
+ assert cat.getBindingTypes().size() == 1;
+ assert annotationSetMatches(cat.getBindingTypes(), Asynchronous.class);
+ }
+
+ @SuppressWarnings("unchecked")
+ @Test
+ public void testBindingTypesDeclaredInJava()
+ {
+ ComponentMetaModel<Cat> cat = new ComponentMetaModel<Cat>(new ClassAnnotatedItem(Cat.class), emptyAnnotatedItem, container);
+ assert cat.getBindingTypes().size() == 1;
+ assert annotationSetMatches(cat.getBindingTypes(), Synchronous.class);
+ }
+
+ @SuppressWarnings("unchecked")
+ @Test
+ public void testBindingTypesDeclaredInXml()
+ {
+ Map<Class<? extends Annotation>, Annotation> annotations = new HashMap<Class<? extends Annotation>, Annotation>();
+ annotations.put(Asynchronous.class, new AsynchronousBinding());
+ AnnotatedItem annotatedItem = new MutableAnnotatedItem(Antelope.class, annotations);
+
+ ComponentMetaModel<Antelope> antelope = new ComponentMetaModel<Antelope>(emptyAnnotatedItem, annotatedItem, container);
+ assert annotationSetMatches(antelope.getBindingTypes(), Asynchronous.class);
+ }
+
+ @SuppressWarnings("unchecked")
+ @Test
+ public void testDefaultBindingType()
+ {
+ ComponentMetaModel<Order> order = new ComponentMetaModel<Order>(new ClassAnnotatedItem(Order.class), emptyAnnotatedItem, container);
+ assert order.getBindingTypes().size() == 1;
+ order.getBindingTypes().iterator().next().annotationType().equals(Current.class);
+ }
+
+ // **** TESTS FOR SCOPES **** //
+
+ @Test
+ public void testScopeDeclaredInJava()
+ {
+ ComponentMetaModel<SeaBass> trout = new ComponentMetaModel<SeaBass>(new ClassAnnotatedItem(SeaBass.class), emptyAnnotatedItem, container);
+ assert trout.getScopeType().annotationType().equals(RequestScoped.class);
+ }
+
+ @Test
+ public void testScopeDeclaredInXml()
+ {
+ Map<Class<? extends Annotation>, Annotation> annotations = new HashMap<Class<? extends Annotation>, Annotation>();
+ annotations.put(RequestScoped.class, new RequestScopedBinding());
+ AnnotatedItem annotatedItem = new MutableAnnotatedItem(Order.class, annotations);
+
+ ComponentMetaModel<Order> order = new ComponentMetaModel<Order>(new ClassAnnotatedItem(Order.class), annotatedItem, container);
+ assert order.getScopeType().annotationType().equals(RequestScoped.class);
+ }
+
+ @Test
+ public void testScopeDeclaredInXmlOverridesJava()
+ {
+ Map<Class<? extends Annotation>, Annotation> annotations = new HashMap<Class<? extends Annotation>, Annotation>();
+ annotations.put(ConversationScoped.class, new ConversationScopedBinding());
+ AnnotatedItem annotatedItem = new MutableAnnotatedItem(SeaBass.class, annotations);
+ ComponentMetaModel<SeaBass> trout = new ComponentMetaModel<SeaBass>(new ClassAnnotatedItem(SeaBass.class), annotatedItem, container);
+ assert trout.getScopeType().annotationType().equals(ConversationScoped.class);
+ }
+
+ @Test
+ public void testScopeMissingInXml()
+ {
+ Map<Class<? extends Annotation>, Annotation> annotations = new HashMap<Class<? extends Annotation>, Annotation>();
+ AnnotatedItem annotatedItem = new MutableAnnotatedItem(SeaBass.class, annotations);
+
+ ComponentMetaModel<SeaBass> trout = new ComponentMetaModel<SeaBass>(new ClassAnnotatedItem(SeaBass.class), annotatedItem, container);
+ assert trout.getScopeType().annotationType().equals(RequestScoped.class);
+ }
+
+ @Test
+ public void testNoScopeSpecified()
+ {
+ ComponentMetaModel<Order> order = new ComponentMetaModel<Order>(new ClassAnnotatedItem(Order.class), emptyAnnotatedItem, container);
+ assert order.getScopeType().annotationType().equals(Dependent.class);
+ }
+
+ @Test
+ public void testTooManyScopesSpecified()
+ {
+ boolean exception = false;
+ try
+ {
+ new ComponentMetaModel<ComponentWithTooManyScopeTypes>(new ClassAnnotatedItem(ComponentWithTooManyScopeTypes.class), emptyAnnotatedItem, container);
+ }
+ catch (Exception e)
+ {
+ exception = true;
+ }
+ assert exception;
+ }
+
+ @Test
+ public void testTooManyScopesSpecifiedInXml()
+ {
+ boolean exception = false;
+ try
+ {
+ Map<Class<? extends Annotation>, Annotation> annotations = new HashMap<Class<? extends Annotation>, Annotation>();
+ annotations.put(RequestScoped.class, new RequestScopedBinding());
+ annotations.put(ConversationScoped.class, new ConversationScopedBinding());
+ AnnotatedItem antelopeAnnotatedItem = new MutableAnnotatedItem(Antelope.class, annotations);
+ new ComponentMetaModel<Antelope>(emptyAnnotatedItem, antelopeAnnotatedItem, container);
+ }
+ catch (Exception e)
+ {
+ exception = true;
+ }
+ assert exception;
+ }
+
+ @Test
+ public void testScopeSpecifiedAndStereotyped()
+ {
+ Map<Class<? extends Annotation>, Annotation> annotations = new HashMap<Class<? extends Annotation>, Annotation>();
+ annotations.put(FishStereotype.class, new FishStereotypeBinding());
+ AnnotatedItem annotatedItem = new MutableAnnotatedItem(SeaBass.class, annotations);
+ ComponentMetaModel<SeaBass> trout = new ComponentMetaModel<SeaBass>(new ClassAnnotatedItem(SeaBass.class), annotatedItem, container);
+ assert trout.getScopeType().annotationType().equals(RequestScoped.class);
+ }
+
+ @Test
+ public void testMutipleIncompatibleScopeStereotypes()
+ {
+ Map<Class<? extends Annotation>, Annotation> annotations = new HashMap<Class<? extends Annotation>, Annotation>();
+ annotations.put(FishStereotype.class, new FishStereotypeBinding());
+ annotations.put(AnimalStereotype.class, new AnimalStereotypeBinding());
+ AnnotatedItem annotatedItem = new MutableAnnotatedItem(Haddock.class, annotations);
+
+ boolean exception = false;
+ try
+ {
+ new ComponentMetaModel<Haddock>(new ClassAnnotatedItem(Haddock.class), annotatedItem, container);
+ }
+ catch (Exception e)
+ {
+ exception = true;
+ }
+ assert exception;
+ }
+
+ @Test
+ public void testMutipleIncompatibleScopeStereotypesWithScopeSpecified()
+ {
+ Map<Class<? extends Annotation>, Annotation> annotations = new HashMap<Class<? extends Annotation>, Annotation>();
+ annotations.put(FishStereotype.class, new FishStereotypeBinding());
+ annotations.put(AnimalStereotype.class, new AnimalStereotypeBinding());
+ AnnotatedItem annotatedItem = new MutableAnnotatedItem(SeaBass.class, annotations);
+
+ ComponentMetaModel<SeaBass> trout = new ComponentMetaModel<SeaBass>(new ClassAnnotatedItem(SeaBass.class), annotatedItem, container);
+ assert trout.getScopeType().annotationType().equals(RequestScoped.class);
+ }
+
+ @Test
+ public void testMutipleCompatibleScopeStereotypes()
+ {
+ Map<Class<? extends Annotation>, Annotation> annotations = new HashMap<Class<? extends Annotation>, Annotation>();
+ annotations.put(FishStereotype.class, new FishStereotypeBinding());
+ annotations.put(RiverFishStereotype.class, new RiverFishStereotypeBinding());
+ AnnotatedItem annotatedItem = new MutableAnnotatedItem(Haddock.class, annotations);
+
+ ComponentMetaModel<Haddock> haddock = new ComponentMetaModel<Haddock>(new ClassAnnotatedItem(Haddock.class), annotatedItem, container);
+ assert haddock.getScopeType().annotationType().equals(ApplicationScoped.class);
+ }
+
+ // **** TESTS FOR COMPONENT NAME **** /
+
+ @Test
+ public void testNamed()
+ {
+ ComponentMetaModel<Haddock> haddock = new ComponentMetaModel<Haddock>(new ClassAnnotatedItem(Haddock.class), emptyAnnotatedItem, container);
+ assert haddock.getName() != null;
+ haddock.getComponentType().equals(ComponentType.SIMPLE);
+ assert haddock.getName().equals("haddock");
+ }
+
+ @Test
+ public void testXmlNamed()
+ {
+ Map<Class<? extends Annotation>, Annotation> annotations = new HashMap<Class<? extends Annotation>, Annotation>();
+ annotations.put(Named.class, new NamedBinding()
+ {
+
+ public String value()
+ {
+ return "";
+ }
+ });
+ AnnotatedItem annotatedItem = new MutableAnnotatedItem(SeaBass.class, annotations);
+ ComponentMetaModel<SeaBass> trout = new ComponentMetaModel<SeaBass>(new ClassAnnotatedItem(SeaBass.class), annotatedItem, container);
+
+ assert trout.getName() != null;
+ trout.getComponentType().equals(ComponentType.SIMPLE);
+ assert trout.getName().equals("seaBass");
+ }
+
+ @Test
+ public void testNonDefaultXmlNamed()
+ {
+ Map<Class<? extends Annotation>, Annotation> annotations = new HashMap<Class<? extends Annotation>, Annotation>();
+ annotations.put(Named.class, new NamedBinding()
+ {
+
+ public String value()
+ {
+ return "aTrout";
+ }
+ });
+ AnnotatedItem annotatedItem = new MutableAnnotatedItem(SeaBass.class, annotations);
+ ComponentMetaModel<SeaBass> trout = new ComponentMetaModel<SeaBass>(new ClassAnnotatedItem(SeaBass.class), annotatedItem, container);
+
+ assert trout.getName().equals("aTrout");
+ }
+
+ @Test
+ public void testNotNamed()
+ {
+ ComponentMetaModel<SeaBass> trout = new ComponentMetaModel<SeaBass>(new ClassAnnotatedItem(SeaBass.class), emptyAnnotatedItem, container);
+ assert trout.getName() == null;
+ }
+
+ @Test
+ public void testNonDefaultNamed()
+ {
+ ComponentMetaModel<Moose> moose = new ComponentMetaModel<Moose>(new ClassAnnotatedItem(Moose.class), emptyAnnotatedItem, container);
+ assert moose.getName().equals("aMoose");
+ }
+
+
+ // **** TESTS FOR STEREOTYPES **** //
+
+ @SuppressWarnings("unchecked")
+ @Test
+ public void testStereotypeDeclaredInXmlAndJava()
+ {
+ Map<Class<? extends Annotation>, Annotation> orderXmlAnnotations = new HashMap<Class<? extends Annotation>, Annotation>();
+ orderXmlAnnotations.put(Current.class, new CurrentBinding());
+ orderXmlAnnotations.put(Synchronous.class, new SynchronousBinding());
+ orderXmlAnnotations.put(Named.class, new NamedBinding()
+ {
+ public String value()
+ {
+ return "currentSynchronousOrder";
+ }
+ });
+ AnnotatedItem currentSynchronousOrderAnnotatedItem = new MutableAnnotatedItem(Order.class, orderXmlAnnotations);
+
+ ComponentMetaModel<Order> order = new ComponentMetaModel<Order>(new ClassAnnotatedItem(Order.class), currentSynchronousOrderAnnotatedItem, container);
+ assert Production.class.equals(order.getDeploymentType().annotationType());
+ assert "currentSynchronousOrder".equals(order.getName());
+ assert order.getBindingTypes().size() == 2;
+ assert annotationSetMatches(order.getBindingTypes(), Current.class, Synchronous.class);
+ assert order.getScopeType().annotationType().equals(Dependent.class);
+ }
+
+ @Test
+ public void testSingleStereotype()
+ {
+ ComponentMetaModel<Gorilla> gorilla = new ComponentMetaModel<Gorilla>(new ClassAnnotatedItem(Gorilla.class), emptyAnnotatedItem, container);
+ assert gorilla.getName() == null;
+ assert gorilla.getDeploymentType().annotationType().equals(Production.class);
+ assert gorilla.getBindingTypes().iterator().next().annotationType().equals(Current.class);
+ assert gorilla.getScopeType().annotationType().equals(RequestScoped.class);
+ }
+
+ @Test
+ public void testRequiredTypeIsImplemented()
+ {
+ try
+ {
+ new ComponentMetaModel<Gorilla>(new ClassAnnotatedItem(Gorilla.class), emptyAnnotatedItem, container);
+ }
+ catch (Exception e)
+ {
+ assert false;
+ }
+
+ }
+
+ @Test
+ public void testRequiredTypeIsNotImplemented()
+ {
+ boolean exception = false;
+ try
+ {
+ new ComponentMetaModel<Chair>(new ClassAnnotatedItem(Chair.class), emptyAnnotatedItem, container);
+ }
+ catch (Exception e)
+ {
+ exception = true;
+ }
+ assert exception;
+
+ }
+
+ @Test
+ public void testScopeIsSupported()
+ {
+ try
+ {
+ new ComponentMetaModel<Goldfish>(new ClassAnnotatedItem(Goldfish.class), emptyAnnotatedItem, container);
+ }
+ catch (Exception e)
+ {
+ e.printStackTrace();
+ assert false;
+ }
+
+ }
+
+ @Test
+ public void testScopeIsNotSupported()
+ {
+ boolean exception = false;
+ try
+ {
+ new ComponentMetaModel<Carp>(new ClassAnnotatedItem(Carp.class), emptyAnnotatedItem, container);
+ }
+ catch (Exception e)
+ {
+ exception = true;
+ }
+ assert exception;
+
+ }
+
+ //*** COMPONENT CLASS CHECKS ****//
+
+ @Test
+ public void testAbstractClassIsNotAllowed()
+ {
+ boolean exception = false;
+ try
+ {
+ new ComponentMetaModel<Cow>(new ClassAnnotatedItem(Cow.class), emptyAnnotatedItem, container);
+ }
+ catch (Exception e)
+ {
+ exception = true;
+ }
+ assert exception;
+ }
+
+ @Test
+ public void testFinalClassMustBeDependentScoped()
+ {
+ boolean exception = false;
+ try
+ {
+ new ComponentMetaModel<Horse>(new ClassAnnotatedItem(Horse.class), emptyAnnotatedItem, container);
+ }
+ catch (Exception e)
+ {
+ exception = true;
+ }
+ assert exception;
+
+ Map<Class<? extends Annotation>, Annotation> annotations = new HashMap<Class<? extends Annotation>, Annotation>();
+ annotations.put(Dependent.class, new DependentBinding());
+ AnnotatedItem annotatedItem = new MutableAnnotatedItem(Horse.class, annotations);
+ try
+ {
+ new ComponentMetaModel<Horse>(new ClassAnnotatedItem(Horse.class), annotatedItem, container);
+ }
+ catch (Exception e)
+ {
+ assert false;
+ }
+ }
+
+ @Test
+ public void testClassWithFinalMethodMustBeDependentScoped()
+ {
+ boolean exception = false;
+ try
+ {
+ new ComponentMetaModel<Pig>(new ClassAnnotatedItem(Pig.class), emptyAnnotatedItem, container);
+ }
+ catch (Exception e)
+ {
+ exception = true;
+ }
+ assert exception;
+
+ Map<Class<? extends Annotation>, Annotation> annotations = new HashMap<Class<? extends Annotation>, Annotation>();
+ annotations.put(Dependent.class, new DependentBinding());
+ AnnotatedItem annotatedItem = new MutableAnnotatedItem(Pig.class, annotations);
+ try
+ {
+ new ComponentMetaModel<Pig>(new ClassAnnotatedItem(Pig.class), annotatedItem, container);
+ }
+ catch (Exception e)
+ {
+ assert false;
+ }
+ }
+
+}
Property changes on: ri/trunk/webbeans-impl/src/test/java/org/jboss/webbeans/test/ComponentMetaModelTest.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Modified: ri/trunk/webbeans-impl/src/test/java/org/jboss/webbeans/test/ConstructorMetaModelTest.java
===================================================================
--- ri/trunk/webbeans-impl/src/test/java/org/jboss/webbeans/test/ConstructorMetaModelTest.java 2008-07-03 01:55:49 UTC (rev 37)
+++ ri/trunk/webbeans-impl/src/test/java/org/jboss/webbeans/test/ConstructorMetaModelTest.java 2008-07-03 13:02:30 UTC (rev 38)
@@ -1,9 +1,19 @@
package org.jboss.webbeans.test;
+import java.lang.annotation.Annotation;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+
import javax.webbeans.Current;
-import org.jboss.webbeans.ConstructorMetaModel;
+import org.jboss.webbeans.ComponentMetaModel;
+import org.jboss.webbeans.ContainerImpl;
+import org.jboss.webbeans.bindings.StandardBinding;
+import org.jboss.webbeans.injectable.ConstructorMetaModel;
import org.jboss.webbeans.test.annotations.Synchronous;
+import org.jboss.webbeans.test.bindings.AnotherDeploymentTypeBinding;
+import org.jboss.webbeans.test.bindings.HornedAnimalDeploymentTypeBinding;
import org.jboss.webbeans.test.components.Chicken;
import org.jboss.webbeans.test.components.Donkey;
import org.jboss.webbeans.test.components.Duck;
@@ -12,65 +22,82 @@
import org.jboss.webbeans.test.components.Order;
import org.jboss.webbeans.test.components.Sheep;
import org.jboss.webbeans.test.components.Turkey;
+import org.jboss.webbeans.test.mock.MockContainerImpl;
+import org.jboss.webbeans.util.AnnotatedItem;
+import org.jboss.webbeans.util.ClassAnnotatedItem;
+import org.jboss.webbeans.util.MutableAnnotatedItem;
+import org.junit.Before;
import org.junit.Test;
public class ConstructorMetaModelTest
{
+ private ContainerImpl container;
+
+ private AnnotatedItem emptyAnnotatedItem;
+
+ @Before
+ public void before()
+ {
+ emptyAnnotatedItem = new MutableAnnotatedItem(null, new HashMap<Class<? extends Annotation>, Annotation>());
+ container = new MockContainerImpl(null);
+
+ }
+
@Test
public void testImplicitConstructor()
{
- ConstructorMetaModel<Order> constructor = new ConstructorMetaModel<Order>(Order.class);
+ ConstructorMetaModel<Order> constructor = new ComponentMetaModel<Order>(new ClassAnnotatedItem(Order.class), emptyAnnotatedItem, container).getConstructor();
assert constructor.getConstructor().getDeclaringClass().equals(Order.class);
assert constructor.getConstructor().getParameterTypes().length == 0;
- assert constructor.getInjectedAttributes().size() == 0;
+ assert constructor.getParameters().size() == 0;
}
@Test
public void testSingleConstructor()
{
- ConstructorMetaModel<Donkey> constructor = new ConstructorMetaModel<Donkey>(Donkey.class);
+ ConstructorMetaModel<Donkey> constructor = new ComponentMetaModel<Donkey>(new ClassAnnotatedItem(Donkey.class), emptyAnnotatedItem, container).getConstructor();
assert constructor.getConstructor().getDeclaringClass().equals(Donkey.class);
assert constructor.getConstructor().getParameterTypes().length == 1;
assert constructor.getConstructor().getParameterTypes()[0].equals(String.class);
- assert constructor.getInjectedAttributes().size() == 1;
- assert constructor.getInjectedAttributes().get(0).getType().equals(String.class);
- assert constructor.getInjectedAttributes().get(0).getBindingTypes().length == 1;
- assert constructor.getInjectedAttributes().get(0).getBindingTypes()[0].annotationType().equals(Current.class);
+ assert constructor.getParameters().size() == 1;
+ assert constructor.getParameters().get(0).getType().equals(String.class);
+ assert constructor.getParameters().get(0).getBindingTypes().length == 1;
+ assert constructor.getParameters().get(0).getBindingTypes()[0].annotationType().equals(Current.class);
}
@Test
public void testInitializerAnnotatedConstructor()
{
- ConstructorMetaModel<Sheep> constructor = new ConstructorMetaModel<Sheep>(Sheep.class);
+ ConstructorMetaModel<Sheep> constructor = new ComponentMetaModel<Sheep>(new ClassAnnotatedItem(Sheep.class), emptyAnnotatedItem, container).getConstructor();
assert constructor.getConstructor().getDeclaringClass().equals(Sheep.class);
assert constructor.getConstructor().getParameterTypes().length == 2;
assert constructor.getConstructor().getParameterTypes()[0].equals(String.class);
assert constructor.getConstructor().getParameterTypes()[1].equals(Double.class);
- assert constructor.getInjectedAttributes().size() == 2;
- assert constructor.getInjectedAttributes().get(0).getType().equals(String.class);
- assert constructor.getInjectedAttributes().get(1).getType().equals(Double.class);
- assert constructor.getInjectedAttributes().get(0).getBindingTypes().length == 1;
- assert constructor.getInjectedAttributes().get(0).getBindingTypes()[0].annotationType().equals(Current.class);
- assert constructor.getInjectedAttributes().get(1).getBindingTypes().length == 1;
- assert constructor.getInjectedAttributes().get(1).getBindingTypes()[0].annotationType().equals(Current.class);
+ assert constructor.getParameters().size() == 2;
+ assert constructor.getParameters().get(0).getType().equals(String.class);
+ assert constructor.getParameters().get(1).getType().equals(Double.class);
+ assert constructor.getParameters().get(0).getBindingTypes().length == 1;
+ assert constructor.getParameters().get(0).getBindingTypes()[0].annotationType().equals(Current.class);
+ assert constructor.getParameters().get(1).getBindingTypes().length == 1;
+ assert constructor.getParameters().get(1).getBindingTypes()[0].annotationType().equals(Current.class);
}
@Test
public void testBindingTypeAnnotatedConstructor()
{
- ConstructorMetaModel<Duck> constructor = new ConstructorMetaModel<Duck>(Duck.class);
+ ConstructorMetaModel<Duck> constructor = new ComponentMetaModel<Duck>(new ClassAnnotatedItem(Duck.class), emptyAnnotatedItem, container).getConstructor();
assert constructor.getConstructor().getDeclaringClass().equals(Duck.class);
assert constructor.getConstructor().getParameterTypes().length == 2;
assert constructor.getConstructor().getParameterTypes()[0].equals(String.class);
assert constructor.getConstructor().getParameterTypes()[1].equals(Integer.class);
- assert constructor.getInjectedAttributes().size() == 2;
- assert constructor.getInjectedAttributes().get(0).getType().equals(String.class);
- assert constructor.getInjectedAttributes().get(1).getType().equals(Integer.class);
- assert constructor.getInjectedAttributes().get(0).getBindingTypes().length == 1;
- assert constructor.getInjectedAttributes().get(0).getBindingTypes()[0].annotationType().equals(Current.class);
- assert constructor.getInjectedAttributes().get(1).getBindingTypes().length == 1;
- assert constructor.getInjectedAttributes().get(1).getBindingTypes()[0].annotationType().equals(Synchronous.class);
+ assert constructor.getParameters().size() == 2;
+ assert constructor.getParameters().get(0).getType().equals(String.class);
+ assert constructor.getParameters().get(1).getType().equals(Integer.class);
+ assert constructor.getParameters().get(0).getBindingTypes().length == 1;
+ assert constructor.getParameters().get(0).getBindingTypes()[0].annotationType().equals(Current.class);
+ assert constructor.getParameters().get(1).getBindingTypes().length == 1;
+ assert constructor.getParameters().get(1).getBindingTypes()[0].annotationType().equals(Synchronous.class);
}
@Test
@@ -79,7 +106,7 @@
boolean exception = false;
try
{
- new ConstructorMetaModel<Chicken>(Chicken.class);
+ new ComponentMetaModel<Chicken>(new ClassAnnotatedItem(Chicken.class), emptyAnnotatedItem, container);
}
catch (Exception e)
{
@@ -95,7 +122,7 @@
boolean exception = false;
try
{
- new ConstructorMetaModel<Turkey>(Turkey.class);
+ new ComponentMetaModel<Turkey>(new ClassAnnotatedItem(Turkey.class), emptyAnnotatedItem, container);
}
catch (Exception e)
{
@@ -111,7 +138,7 @@
boolean exception = false;
try
{
- new ConstructorMetaModel<Goat>(Goat.class);
+ new ComponentMetaModel<Goat>(new ClassAnnotatedItem(Goat.class), emptyAnnotatedItem, container);
}
catch (Exception e)
{
@@ -127,7 +154,7 @@
boolean exception = false;
try
{
- new ConstructorMetaModel<Goose>(Goose.class);
+ new ComponentMetaModel<Goose>(new ClassAnnotatedItem(Goose.class), emptyAnnotatedItem, container);
}
catch (Exception e)
{
Modified: ri/trunk/webbeans-impl/src/test/java/org/jboss/webbeans/test/components/Goose.java
===================================================================
--- ri/trunk/webbeans-impl/src/test/java/org/jboss/webbeans/test/components/Goose.java 2008-07-03 01:55:49 UTC (rev 37)
+++ ri/trunk/webbeans-impl/src/test/java/org/jboss/webbeans/test/components/Goose.java 2008-07-03 13:02:30 UTC (rev 38)
@@ -3,7 +3,6 @@
import javax.webbeans.Initializer;
import javax.webbeans.Production;
-import org.jboss.webbeans.test.annotations.Asynchronous;
import org.jboss.webbeans.test.annotations.Synchronous;
@Production
More information about the weld-commits
mailing list