Skip to content

Commit

Permalink
Start to check init methods
Browse files Browse the repository at this point in the history
git-svn-id: http://anonsvn.jboss.org/repos/weld/ri/trunk@243 1c488680-804c-0410-94cd-c6b725194a0e
  • Loading branch information
pmuir committed Nov 5, 2008
1 parent 5a0a7c1 commit 52c0c6b
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 3 deletions.
Expand Up @@ -33,7 +33,7 @@ public interface AnnotatedType<T> extends AnnotatedItem<T, Class<T>>
* If no annotations are present which are annotated with the given
* annotation an empty set is returned
*/
public Set<AnnotatedField<Object>> getAnnotatedField(Class<? extends Annotation> annotationType);
public Set<AnnotatedField<Object>> getAnnotatedFields(Class<? extends Annotation> annotationType);

/**
* Get all fields which are annotated with the given meta annotation
Expand All @@ -45,4 +45,14 @@ public interface AnnotatedType<T> extends AnnotatedItem<T, Class<T>>
public Set<AnnotatedField<Object>> getMetaAnnotatedFields(
Class<? extends Annotation> metaAnnotationType);

/**
* Get all fields which are annotated with the given meta annotation
* type
*
* If no annotations are present which are annotated with the given meta
* annotation an empty set is returned
*/
public Set<AnnotatedMethod<Object>> getAnnotatedMethods(
Class<? extends Annotation> annotationType);

}
Expand Up @@ -2,6 +2,7 @@

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.HashMap;
Expand All @@ -25,6 +26,9 @@ public class SimpleAnnotatedType<T> extends AbstractAnnotatedItem<T, Class<T>> i
private Map<Class<? extends Annotation>, Set<AnnotatedField<Object>>> annotatedFields;
private Map<Class<? extends Annotation>, Set<AnnotatedField<Object>>> metaAnnotatedFields;

private Set<AnnotatedMethod<Object>> methods;
private Map<Class<? extends Annotation>, Set<AnnotatedMethod<Object>>> annotatedMethods;

public SimpleAnnotatedType(Class<T> annotatedClass, Map<Class<? extends Annotation>, Annotation> annotationMap)
{
super(annotationMap);
Expand Down Expand Up @@ -108,7 +112,7 @@ protected static <T extends Annotation> Map<Class<? extends Annotation>, Set<Ann
return metaAnnotatedFields;
}

public Set<AnnotatedField<Object>> getAnnotatedField(
public Set<AnnotatedField<Object>> getAnnotatedFields(
Class<? extends Annotation> annotationType)
{
if (annotatedFields == null)
Expand Down Expand Up @@ -147,5 +151,51 @@ public Type[] getActualTypeArguments()
{
return actualTypeArguments;
}

private void initMethods()
{
this.methods = new HashSet<AnnotatedMethod<Object>>();
for (Method member : clazz.getDeclaredMethods())
{
methods.add(new SimpleAnnotatedMethod<Object>(member));
}
}

public Set<AnnotatedMethod<Object>> getAnnotatedMethods(Class<? extends Annotation> annotationType)
{
if (annotatedMethods == null)
{
initAnnotatedMethods();
}

if (!annotatedMethods.containsKey(annotationType))
{
return new HashSet<AnnotatedMethod<Object>>();
}
else
{
return annotatedMethods.get(annotationType);
}
}

private void initAnnotatedMethods()
{
if (methods == null)
{
initMethods();
}
annotatedMethods = new HashMap<Class<? extends Annotation>, Set<AnnotatedMethod<Object>>>();
for (AnnotatedMethod<Object> member : methods)
{
for (Annotation annotation : member.getAnnotations())
{
if (!annotatedMethods.containsKey(annotation))
{
annotatedMethods.put(annotation.annotationType(), new HashSet<AnnotatedMethod<Object>>());
}
annotatedMethods.get(annotation.annotationType()).add(member);
}
}
}

}
Expand Up @@ -278,7 +278,7 @@ protected void initDeploymentType()

if (deploymentTypes.size() > 1)
{
throw new DefinitionException("At most one deployment type may be specified (" + deploymentTypes + " are specified) on " + getAnnotatedItem().getDelegate());
throw new DefinitionException("At most one deployment type may be specified (" + deploymentTypes + " are specified) on " + getAnnotatedItem().toString());
}
if (deploymentTypes.size() == 1)
{
Expand Down
Expand Up @@ -6,10 +6,15 @@

import javax.webbeans.BindingType;
import javax.webbeans.DefinitionException;
import javax.webbeans.Destructor;
import javax.webbeans.Initializer;
import javax.webbeans.Produces;

import org.jboss.webbeans.ManagerImpl;
import org.jboss.webbeans.injectable.InjectableField;
import org.jboss.webbeans.injectable.InjectableMethod;
import org.jboss.webbeans.introspector.AnnotatedField;
import org.jboss.webbeans.introspector.AnnotatedMethod;
import org.jboss.webbeans.introspector.AnnotatedType;
import org.jboss.webbeans.util.LoggerUtil;
import org.jboss.webbeans.util.Reflections;
Expand All @@ -31,6 +36,7 @@ public abstract class AbstractClassBeanModel<T> extends AbstractBeanModel<T, Cla
private AnnotatedType<T> annotatedItem;
private AnnotatedType<T> xmlAnnotatedItem;
private Set<InjectableField<?>> injectableFields;
private Set<InjectableMethod<Object>> initializerMethods;

/**
*
Expand Down Expand Up @@ -62,6 +68,7 @@ protected void init(ManagerImpl container)
// TODO This is too high
checkBeanImplementation();
// TODO Interceptors
initInitializerMethods();
}

@Override
Expand Down Expand Up @@ -119,6 +126,27 @@ protected void initInjectionPoints()
}
}

protected void initInitializerMethods()
{
// TODO Support XML
initializerMethods = new HashSet<InjectableMethod<Object>>();
for (AnnotatedMethod<Object> annotatedMethod : annotatedItem.getAnnotatedMethods(Initializer.class))
{
if (Reflections.isStatic(annotatedMethod.getDelegate()))
{
throw new DefinitionException("Initializer method " + annotatedMethod.toString() + " cannot be static");
}
if (annotatedMethod.getAnnotation(Produces.class) != null)
{
throw new DefinitionException("Initializer method " + annotatedMethod.toString() + " cannot be annotated @Produces");
}
if (annotatedMethod.getAnnotation(Destructor.class) != null)
{
throw new DefinitionException("Initializer method " + annotatedMethod.toString() + " cannot be annotated @Destructor");
}
}
}

@Override
protected String getDefaultName()
{
Expand Down Expand Up @@ -170,5 +198,10 @@ public Set<InjectableField<?>> getInjectableFields()
{
return injectableFields;
}

public Set<InjectableMethod<Object>> getInitializerMethods()
{
return initializerMethods;
}

}
Expand Up @@ -96,6 +96,11 @@ public static boolean isStatic(Field field)
return Modifier.isStatic(field.getModifiers());
}

public static boolean isStatic(Method method)
{
return Modifier.isStatic(method.getModifiers());
}

public static boolean isAbstract(Class<?> clazz)
{
return Modifier.isAbstract(clazz.getModifiers());
Expand Down

0 comments on commit 52c0c6b

Please sign in to comment.