Skip to content

Instantly share code, notes, and snippets.

@pedrotoliveira
Created June 20, 2012 20:55
Show Gist options
  • Save pedrotoliveira/2962157 to your computer and use it in GitHub Desktop.
Save pedrotoliveira/2962157 to your computer and use it in GitHub Desktop.
Shell Script to Generate Builder Classes from POJOs
#!/usr/bin/sh
### Escrever Builder ####
function escreverBuilder() {
echo "package domain.builder;";
echo "";
echo "import domain.*;";
echo "";
echo "public final class ${POJO}Builder extends Builder<${POJO}> {";
echo "";
echo " public ${POJO}Builder() {";
echo " super(new ${POJO}());";
echo " }";
echo " public ${POJO}Builder(final ${POJO} instance) {";
echo " super(instance);";
echo " }";
cat ${POJO}.java | grep "public void set" | while read METHODS;
do
param_plus_type=$(echo ${METHODS} | cut -d "(" -f2);
param=$(echo ${param_plus_type} | cut -d " " -f2);
type=$(echo ${param_plus_type} | cut -d " " -f1);
set_method=$(echo ${METHODS} | cut -d "(" -f1);
set_method=$(echo ${set_method} | sed s/public\ void//);
set_method=$(echo ${set_method} | tr -d " ");
echo " public ${POJO}Builder com${set_method:3}(final ${type} ${param} {";
echo " this.instance.${set_method}(${param};";
echo " return this;";
echo " }";
done;
echo "@Override";
echo " public ${POJO} build() {";
echo " return this.instance;";
echo " }";
echo "@Override";
echo "void preencherValoresPadroes() {";
echo " //TODO: Implementar ";
echo " }";
echo "}";
exit 0;
}
## Main ##
ERROR="Usage ./gerarBuilder.sh [pojo_name]";
if [ "${#}" -lt 1 ];
then
echo $ERROR;
exit 1;
else
export POJO="${1}";
fi
if [ ! -f "${POJO}.java" ];
then
echo "Arquivo não encontrado";
echo ${ERROR};
exit 1;
fi
escreverBuilder;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment