componentIds.add(componentId);
JClassType componentType = definition.isGenerated() ? definition.getGeneratedType() : definition.getComponentType();
String componentClassName = componentType.getQualifiedSourceName();
BeanOracle beanOracle = new BeanOracleBuilder(typeOracle).build(logger, componentType);
Set<ComponentDefinition> nestedComponentDefinitions = new HashSet<ComponentDefinition>();
boolean factoryComponent = isFactory(componentType, typeOracle);
// first we check that if the component is a factory component and if it is also configured as a singleton. We
// only support singleton factory components as it makes no sense to have proptotype ones.
if (factoryComponent && !definition.isSingleton()) {
logger.error("FactoryComponent '" + componentId + "' is not a singleton. It makes no sense " +
"to create a proptotype factory component!!!");
throw new UnableToCompleteException();
}
writer.println();
writer.println();
writer.beginJavaDocComment();
writer.println("Component: '" + componentId + "'");
writer.endJavaDocComment();
writer.println();
// if the component is a singleton, we declare a static class variable to hold the single instance of the bean.
if (definition.isSingleton()) {
writer.println("private static " + componentClassName + " " + componentId + ";");
}
writer.println();
writer.println("protected static Object get_" + componentId + "() {");
writer.indent();
// if the bean definition is a singleton
if (definition.isSingleton()) {
if (factoryComponent) {
writer.println("if (" + componentId + " != null) { return " + componentId + ".getObject();}");
} else {
writer.println("if (" + componentId + " != null) { return " + componentId + "; }");
}
}
ConstructorInjectionDefinition ctorDef = definition.getConstructorInjectionDefinition();
if (ctorDef == null) {
if (definition.isGenerated()) {
writer.println(componentClassName + " component = (" + componentClassName + ") GWT.create(" + definition.getComponentType().getQualifiedSourceName() + ".class);");
} else {
writer.println(componentClassName + " component = new " + componentClassName + "();");
}
} else {
StringBuilder paramsBuilder = new StringBuilder();
for (ParameterSetting paramSetting : ctorDef.getParameterSettings()) {
if (paramsBuilder.length() != 0) {
paramsBuilder.append(", ");
}
JParameter parameter = paramSetting.getParameter();
Value value = paramSetting.getValue();
String valueString = generateValueCode(value, logger, writer, oracle, typeOracle, parameter.getType(), Collections.EMPTY_SET);
paramsBuilder.append("((" + parameter.getType().getQualifiedSourceName() + ") " + valueString + ")");
}
writer.println(componentClassName + " component = new " + componentClassName + "(" + paramsBuilder.toString() + ");");
}
// if it's a singleton then initializing the static class variable. It's important to set the static
// instance now to support cyclic dependencies with singletons.
if (definition.isSingleton()) {
writer.println(componentId + " = component;");
}
for (PropertySetting setting : definition.getPropertySettings()) {
String propertyName = setting.getPropertyName();
if (logger.debugEnabled()) {
logger.debug("Writing property wiring code for '" + propertyName + "' of '" +
definition.getComponentType().getQualifiedSourceName() + "' bean class");
}
JProperty property = beanOracle.getProperty(propertyName);
if (property == null || !property.isMutable()) {
logger.error("Cannot set property '" + propertyName + "' on component '" +
componentClassName + "' in container. The property is missing or is read only");
throw new UnableToCompleteException();
}