logger.log(Type.ERROR, "Cannot generate with a @PropertyName anntation on the type");
throw new UnableToCompleteException();
}
String propertyName = annotation.value();
SelectionProperty property;
String value;
try {
property = context.getPropertyOracle().getSelectionProperty(logger, propertyName);
value = property.getCurrentValue();
} catch (BadPropertyValueException e) {
logger.log(Type.ERROR, "Error occured loading property: ", e);
throw new UnableToCompleteException();
}
String packageName = toGenerate.getPackage().getName();
String simpleSourceName = toGenerate.getName().replace('.', '_') + "_" + value;
PrintWriter pw = context.tryCreate(logger, packageName, simpleSourceName);
if (pw == null) {
return packageName + "." + simpleSourceName;
}
ClassSourceFileComposerFactory factory = new ClassSourceFileComposerFactory(packageName, simpleSourceName);
factory.addImplementedInterface(typeName);
SourceWriter sw = factory.createSourceWriter(context, pw);
for (JMethod method : toGenerate.getMethods()) {
if (method.getReturnType().isPrimitive() != JPrimitiveType.BOOLEAN
&& !method.getReturnType().isClass().getQualifiedSourceName().equals(
Name.getSourceNameForClass(Boolean.class))) {
logger.log(Type.ERROR, "Methods must return boolean or Boolean");
throw new UnableToCompleteException();
}
sw.println("%1$s {", method.getReadableDeclaration(false, true, true, true, true));
PropertyValue val = method.getAnnotation(PropertyValue.class);
if (val == null) {
logger.log(Type.ERROR, "Method must have a @PropertyValue annotation");
throw new UnableToCompleteException();
}
if (!property.getPossibleValues().contains(val.value()) && val.warn()) {
logger.log(Type.WARN, "Value '" + val
+ "' is not present in the current set of possible values for selection property " + propertyName);
}
sw.indentln("return %1$b;", val.value().equals(value));