Set<Class> refListenerClasses = new HashSet<Class>(baf.findAnnotatedClasses(ReferenceListener.class));
Set<Class> regListenerClasses = new HashSet<Class>(baf.findAnnotatedClasses(RegistrationListener.class));
Map<String, TreferenceListener> reflMap = new HashMap<String, TreferenceListener>();
Map<String, TregistrationListener> reglMap = new HashMap<String, TregistrationListener>();
Tblueprint tblueprint = new Tblueprint();
if (!blueprintClasses.isEmpty()) {
// use the first annotated blueprint annotation
Blueprint blueprint = (Blueprint)blueprintClasses.iterator().next().getAnnotation(Blueprint.class);
tblueprint.setDefaultActivation(blueprint.defaultActivation());
tblueprint.setDefaultAvailability(blueprint.defaultAvailability());
tblueprint.setDefaultTimeout(convertToBigInteger(blueprint.defaultTimeout()));
}
List<Object> components = tblueprint.getServiceOrReferenceListOrBean();
// try to process classes that have @ReferenceListener or @RegistrationLister first
// as we want the refl and regl maps populated before processing @Bean annotation.
for (Class refListener : refListenerClasses) {
Bean bean = (Bean) refListener.getAnnotation(Bean.class);
// register the treference with its id
TreferenceListener tref = generateTrefListener(refListener);
if (bean.id().length() > 0) {
reflMap.put(bean.id(), tref);
} else {
throw new BlueprintAnnotationException("Unable to find the id for the @ReferenceListener annotated class " + refListener.getName());
}
}
for (Class regListener : regListenerClasses) {
Bean bean = (Bean) regListener.getAnnotation(Bean.class);
// register the tregistrationListener with its id
TregistrationListener tref = generateTregListener(regListener);
if (bean.id().length() > 0) {
reglMap.put(bean.id(), tref);
} else {
throw new BlueprintAnnotationException("Unable to find the id for the @RegistrationListener annotated class " + regListener.getName());
}
}
for (Class clazz : beanClasses) {
// @Bean annotation detected
Bean bean = (Bean)clazz.getAnnotation(Bean.class);
Tbean tbean = new Tbean();
// process depends-on property
String[] dependsOn = bean.dependsOn();
if (!containsValid(dependsOn)) {
tbean.setDependsOn(null);
} else {
List<String> dons = Arrays.asList(dependsOn);
tbean.setDependsOn(dons);
}
// process id property
String id = bean.id();
if (id.length() > 0) {
tbean.setId(id);
} else {
// should we auto generate an id, based on the class name?
tbean.setId(clazz.getSimpleName());
}
// process the clazz property
tbean.setClazz(clazz.getName());
// process activation
String activation = bean.activation();
if (activation.length() > 0) {
if (activation.equalsIgnoreCase("eager") || activation.equalsIgnoreCase("lazy")) {
tbean.setActivation(bean.activation());
} else {
throw new BlueprintAnnotationException("Invalid bean activation value " + activation + " for " + clazz.getName());
}
}
// process description
if (bean.description().length() > 0) {
Tdescription desp = new Tdescription();
desp.getContent().add(bean.description());
tbean.setDescription(desp);
}
// process scope
String scope = bean.scope();
if (scope.length() > 0) {
if (scope.equalsIgnoreCase("singleton") || scope.equalsIgnoreCase("prototype")) {
tbean.setScope(scope);
} else {
throw new BlueprintAnnotationException("Invalid bean scope value " + scope + " for " + clazz.getName());
}
}
// process factory ref
String factoryRef = bean.factoryRef();
if (factoryRef.length() > 0) {
tbean.setFactoryRef(factoryRef);
}
// process factory method
String factoryMethod = bean.factoryMethod();
if (factoryMethod.length() > 0) {
tbean.setFactoryMethod(factoryMethod);
}
List<Object> props = tbean.getArgumentOrPropertyOrAny();
// process args
Arg[] args = bean.args();
if (args.length > 0) {
for (int i = 0; i < args.length; i++) {
Targument targ = createTargument(args[i]);
if (targ != null) {
props.add(targ);
}
}
}
Field[] fields = clazz.getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
if (fields[i].isAnnotationPresent(Inject.class)) {
if (fields[i].isAnnotationPresent(Reference.class)) {
// the field is also annotated with @Reference
Reference ref = fields[i].getAnnotation(Reference.class);
Treference tref = generateTref(ref, reflMap);
components.add(tref);
} else if (fields[i].isAnnotationPresent(ReferenceList.class)) {
// the field is also annotated with @ReferenceList
ReferenceList ref = fields[i].getAnnotation(ReferenceList.class);
TreferenceList tref = generateTrefList(ref, reflMap);
components.add(tref);
} else {
Tproperty tp = createTproperty(fields[i].getName(), fields[i].getAnnotation(Inject.class));
props.add(tp);
}
}
}
// check if the bean also declares init, destroy or inject annotation on methods
Method[] methods = clazz.getDeclaredMethods();
for (int i = 0; i < methods.length; i++) {
if (methods[i].isAnnotationPresent(Init.class)) {
tbean.setInitMethod(methods[i].getName());
} else if (methods[i].isAnnotationPresent(Destroy.class)) {
tbean.setDestroyMethod(methods[i].getName());
} else if (methods[i].isAnnotationPresent(Inject.class)) {
String propertyName = convertFromMethodName(methods[i].getName());
Tproperty tp = createTproperty(propertyName, methods[i].getAnnotation(Inject.class));
props.add(tp);
} else if (methods[i].isAnnotationPresent(Arg.class)) {
Targument targ = createTargument(methods[i].getAnnotation(Arg.class));
props.add(targ);
}
}
// check if the bean also declares service
if (clazz.getAnnotation(Service.class) != null) {
Tservice tservice = generateTservice(clazz, id, reglMap);
components.add(tservice);
}
// check if the clazz implement Converter, if so, it is Converter
boolean isConverter = isConverter(clazz);
if (isConverter) {
TtypeConverters converters = tblueprint.getTypeConverters();
List<Object> objects = converters.getBeanOrReferenceOrRef();
objects.add(tbean);
} else {
components.add(tbean);
}