@Override
public String createAssignment(TreeLogger logger, ResourceContext context,
JMethod method) throws UnableToCompleteException {
SourceWriter sw = new StringSourceWriter();
// Write the expression to create the subtype.
sw.println("new " + method.getReturnType().getQualifiedSourceName()
+ "() {");
sw.indent();
JClassType cssResourceSubtype = method.getReturnType().isInterface();
assert cssResourceSubtype != null;
Map<String, Map<JMethod, String>> replacementsWithPrefix = new HashMap<String, Map<JMethod, String>>();
replacementsWithPrefix.put("",
computeReplacementsForType(cssResourceSubtype));
Import imp = method.getAnnotation(Import.class);
if (imp != null) {
boolean fail = false;
for (Class<? extends CssResource> clazz : imp.value()) {
JClassType importType = context.getGeneratorContext().getTypeOracle().findType(
clazz.getName().replace('$', '.'));
String prefix = importType.getSimpleSourceName();
ImportedWithPrefix exp = importType.getAnnotation(ImportedWithPrefix.class);
if (exp != null) {
prefix = exp.value();
}
assert importType != null;
if (replacementsWithPrefix.put(prefix + "-",
computeReplacementsForType(importType)) != null) {
logger.log(TreeLogger.ERROR,
"Multiple imports that would use the prefix " + prefix);
fail = true;
}
}
if (fail) {
throw new UnableToCompleteException();
}
}
/*
* getOverridableMethods is used to handle CssResources extending
* non-CssResource types. See the discussion in computeReplacementsForType.
*/
for (JMethod toImplement : cssResourceSubtype.getOverridableMethods()) {
String name = toImplement.getName();
if ("getName".equals(name) || "getText".equals(name)) {
continue;
}
if (toImplement.getReturnType().equals(stringType)
&& toImplement.getParameters().length == 0) {
writeClassAssignment(sw, toImplement, replacementsWithPrefix.get(""));
} else if (toImplement.getReturnType().isPrimitive() != null
&& toImplement.getParameters().length == 0) {
writeDefAssignment(logger, sw, toImplement, stylesheetMap.get(method));
} else {
logger.log(TreeLogger.ERROR, "Don't know how to implement method "
+ toImplement.getName());
throw new UnableToCompleteException();
}
}
sw.println("public String getText() {");
sw.indent();
boolean strict = method.getAnnotation(Strict.class) != null;
if (!strict) {
/*
* The developer may choose to force strict behavior onto the system. If
* the method does already have the @Strict annotation, print a warning.
*/
try {
PropertyOracle propertyOracle = context.getGeneratorContext().getPropertyOracle();
String propertyValue = propertyOracle.getPropertyValue(logger,
"CssResource.forceStrict");
if (Boolean.valueOf(propertyValue)) {
logger.log(TreeLogger.WARN, "CssResource.forceStrict is true, but "
+ method.getName() + "() is missing the @Strict annotation.");
strict = true;
}
} catch (BadPropertyValueException e) {
// Ignore
}
}
String cssExpression = makeExpression(logger, context, cssResourceSubtype,
stylesheetMap.get(method), replacementsWithPrefix, strict);
sw.println("return " + cssExpression + ";");
sw.outdent();
sw.println("}");
sw.println("public String getName() {");
sw.indent();
sw.println("return \"" + method.getName() + "\";");
sw.outdent();
sw.println("}");
sw.outdent();
sw.println("}");
return sw.toString();
}