private JClass mapType(Filter filter) throws JClassAlreadyExistsException { if (!(filter.getOption() == null || filter.getOption().isEmpty())) { return getParamTypeEnum(filter); } String type = filter.getType(); Class<?> typeCls = typeMap.get(type); checkState(typeCls != null, "Unexpected type: %s", type); return model.ref(typeCls); }
private JClass getParamTypeEnum(Filter filter) throws JClassAlreadyExistsException { String enumName = camel(filter.getName(), true) + "Option"; if (pkg.isDefined(enumName)) { return pkg._getClass(enumName); } List<Option> options = filter.getOption(); if (options.size() == 1) { /*turn into '*Only' method?*/ } if (options.size() == 2) { if (ImmutableSet.of("true", "false") .equals( ImmutableSet.of( options.get(0).getValue().toLowerCase(), options.get(1).getValue().toLowerCase()))) { return model.ref(Boolean.class); } } JDefinedClass valueEnum = pkg._enum(enumName); JFieldVar valField = valueEnum.field(JMod.PRIVATE | JMod.FINAL, String.class, "value"); JMethod ctor = valueEnum.constructor(JMod.PRIVATE); JVar param = ctor.param(String.class, "val"); ctor.body().assign(valField, param); JMethod toString = valueEnum.method(JMod.PUBLIC, String.class, "toString"); toString.annotate(Override.class); toString.body()._return(valField); for (Option option : options) { String optionName = option.getValue().toUpperCase().replace(' ', '_'); JEnumConstant optionCst = valueEnum.enumConstant(optionName); optionCst.arg(JExpr.lit(option.getValue().replace(' ', '+'))); } return valueEnum; }