diff --git a/zanata-war/src/test/java/org/zanata/seam/ComponentAccessor.java b/zanata-war/src/test/java/org/zanata/seam/ComponentAccessor.java index 4a9664e353..aec32c4b66 100644 --- a/zanata-war/src/test/java/org/zanata/seam/ComponentAccessor.java +++ b/zanata-war/src/test/java/org/zanata/seam/ComponentAccessor.java @@ -47,5 +47,5 @@ public static final ComponentAccessor newInstance(Method m) { public abstract String getComponentName(); - public abstract Class getComponentType(); + public abstract Class getComponentType(); } diff --git a/zanata-war/src/test/java/org/zanata/seam/SeamAutowire.java b/zanata-war/src/test/java/org/zanata/seam/SeamAutowire.java index 95b5713f2e..56b19eb394 100644 --- a/zanata-war/src/test/java/org/zanata/seam/SeamAutowire.java +++ b/zanata-war/src/test/java/org/zanata/seam/SeamAutowire.java @@ -25,7 +25,9 @@ import javassist.CtClass; import javassist.CtMethod; import javassist.NotFoundException; + import org.apache.commons.lang.ArrayUtils; +import org.jboss.seam.annotations.AutoCreate; import org.jboss.seam.annotations.In; import org.jboss.seam.annotations.Logger; import org.jboss.seam.log.Logging; @@ -174,13 +176,10 @@ public Object getComponent(String name) { * spec. * @return The component. */ - private T create(Class componentClass) { - // If the component type is an interface, try to find a declared - // implementation - if (componentClass.isInterface() - && this.componentImpls.containsKey(componentClass)) { - componentClass = (Class) this.componentImpls.get(componentClass); - } + private T create(Class fieldClass) { + // field might be an interface, but we need to find the + // implementation class + Class componentClass = getImplClass(fieldClass); try { Constructor constructor = @@ -214,6 +213,18 @@ private T create(Class componentClass) { } } + private Class getImplClass(Class fieldClass) { + // If the component type is an interface, try to find a declared + // implementation + // TODO field class might be abstract, or a concrete superclass + // of the impl class + if (fieldClass.isInterface() + && this.componentImpls.containsKey(fieldClass)) { + fieldClass = (Class) this.componentImpls.get(fieldClass); + } + return fieldClass; + } + /** * Autowires and returns the component instance for the provided class. * @@ -245,13 +256,26 @@ public T autowire(T component) { // Resolve injected Components for (ComponentAccessor accessor : getAllComponentAccessors(component)) { // Another annotated component - if (accessor.getAnnotation(In.class) != null) { + In inAnnotation = accessor.getAnnotation(In.class); + if (inAnnotation != null) { Object fieldVal = null; String compName = accessor.getComponentName(); Class compType = accessor.getComponentType(); + Class implType = getImplClass(compType); // autowire the component if not done yet if (!namedComponents.containsKey(compName)) { + boolean required = inAnnotation.required(); + boolean autoCreate = implType.isAnnotationPresent(AutoCreate.class); + boolean mayCreate = inAnnotation.create() || autoCreate; + if (required && !mayCreate) { + String msg = "Not allowed to create required component "+compName+" with impl "+implType+". Try @AutoCreate or @In(create=true)."; + if (ignoreNonResolvable) { + log.warn(msg); + } else { + throw new RuntimeException(msg); + } + } Object newComponent = null; try { newComponent = create(compType);