Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
Check for required injections
Browse files Browse the repository at this point in the history
  • Loading branch information
seanf committed Nov 13, 2013
1 parent a54dc5c commit cc3373a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
Expand Up @@ -47,5 +47,5 @@ public static final ComponentAccessor newInstance(Method m) {

public abstract String getComponentName();

public abstract Class getComponentType();
public abstract Class<?> getComponentType();
}
40 changes: 32 additions & 8 deletions zanata-war/src/test/java/org/zanata/seam/SeamAutowire.java
Expand Up @@ -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;
Expand Down Expand Up @@ -174,13 +176,10 @@ public Object getComponent(String name) {
* spec.
* @return The component.
*/
private <T> T create(Class<T> componentClass) {
// If the component type is an interface, try to find a declared
// implementation
if (componentClass.isInterface()
&& this.componentImpls.containsKey(componentClass)) {
componentClass = (Class<T>) this.componentImpls.get(componentClass);
}
private <T> T create(Class<T> fieldClass) {
// field might be an interface, but we need to find the
// implementation class
Class<T> componentClass = getImplClass(fieldClass);

try {
Constructor<T> constructor =
Expand Down Expand Up @@ -214,6 +213,18 @@ private <T> T create(Class<T> componentClass) {
}
}

private <T> Class<T> getImplClass(Class<T> 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<T>) this.componentImpls.get(fieldClass);
}
return fieldClass;
}

/**
* Autowires and returns the component instance for the provided class.
*
Expand Down Expand Up @@ -245,13 +256,26 @@ public <T> 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);
Expand Down

0 comments on commit cc3373a

Please sign in to comment.