net.jbock.writing.OptionStatesMethod Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jbock-compiler Show documentation
Show all versions of jbock-compiler Show documentation
jbock annotation processor
package net.jbock.writing;
import io.jbock.javapoet.ClassName;
import io.jbock.javapoet.CodeBlock;
import io.jbock.javapoet.MethodSpec;
import io.jbock.javapoet.ParameterSpec;
import io.jbock.simple.Inject;
import net.jbock.annotated.Option;
import net.jbock.convert.Mapping;
import net.jbock.parse.OptionState;
import net.jbock.parse.OptionStateModeFlag;
import net.jbock.parse.OptionStateNonRepeatable;
import net.jbock.parse.OptionStateRepeatable;
import java.util.EnumMap;
import java.util.Map;
import java.util.function.Supplier;
import static javax.lang.model.element.Modifier.PRIVATE;
import static net.jbock.common.Constants.mapOf;
import static net.jbock.common.Suppliers.memoize;
final class OptionStatesMethod extends HasCommandRepresentation {
@Inject
OptionStatesMethod(CommandRepresentation commandRepresentation) {
super(commandRepresentation);
}
private final Supplier define = memoize(() -> {
ParameterSpec result = ParameterSpec.builder(
mapOf(optType(), ClassName.get(OptionState.class)), "result").build();
CodeBlock.Builder code = CodeBlock.builder();
if (namedOptions().isEmpty()) {
code.addStatement("$T $N = $T.of()", result.type, result, Map.class);
} else {
code.addStatement("$T $N = new $T<>($T.class)", result.type, result, EnumMap.class, sourceElement().optionEnumType());
}
for (Mapping