如何从字段生成 toString() 方法?

如何从字段生成 toString() 方法?

我有一个 Java 类,其中包含如下字段:

private int port;
private String portName;
private ArrayList portList;
private int id;

我需要一个命令或脚本,以便我可以直接将上述块粘贴到终端中并将其作为该命令或脚本的输入,并给我以下输出:

public String toString() {
    return "{ " + "port=" + port + ",portName=" + portName + ",portList=" + portList + ",id=" + id + "}";
}

答案1

使用以下命令awk(即 GNU Awk):

awk 'BEGIN{printf"public String toString() {\n    return \"{ \""}{gsub(";$","");printf" + \"";if(NR>1){printf","};printf$3"=\" + "$3}END{print" + \"}\";\n}"}'

使用方式如下:

$ awk 'BEGIN{printf"public String toString() {\n    return \"{ \""}{gsub(";$","");printf" + \"";if(NR>1){printf","};printf$3"=\" + "$3}END{print" + \"}\";\n}"}' <<EOF
> private int port;
> private String portName;
> private ArrayList portList;
> private int id;
> EOF
public String toString() {
    return "{ " + "port=" + port + ",portName=" + portName + ",portList=" + portList + ",id=" + id + "}";
}

附有解说词的脚本版本

#!/usr/bin/awk -f
# do the following before processing the text
BEGIN {
  # print this text, '\n' is a line break and '\"' a literal '"'
  printf "public String toString() {\n    return \"{ \""
}
{
  # remove semicolon at the end of the line
  gsub(";$","");
  # print ' + "'
  printf " + \"";
  # if the line number is > 1 print ','
  if (NR > 1) { printf "," };
  # print the third row followed by '=" + ' and the third row again
  printf $3"=\" + "$3
}
# do the following after processing the text
END {
  # print ' + "}";\n}', '\n' is a line break
  print " + \"}\";\n}"
}

将其另存为例如generate_toString,使其可执行chmod +x generate_toString并像这样使用它:

$ ./generate_toString <<EOF
> private int port;
> private String portName;
> private ArrayList portList;
> private int id;
> EOF
public String toString() {
    return "{ " + "port=" + port + ",portName=" + portName + ",portList=" + portList + ",id=" + id + "}";
}

虽然这可以满足您的要求,但为了解决您的实际问题,远的更好的方法,参见@DavidFoerster以及@nickb上面的评论。没有必要重新发明轮子。

答案2

我想这就是你想要的。

#!/bin/bash
# Create Java toString() method from stdin.

while read line; do
    word_last="${line##* }"
    name="${word_last%;}"
    if [[ $pairs ]]; then
        printf -v pair '",%s=" + %s + ' "$name" "$name"
    else
        printf -v pair '"%s=" + %s + ' "$name" "$name"
    fi
    pairs+="$pair"
done

printf '%s\n' 'public String toString() {'
printf '    return "{ " + %s"}";\n' "$pairs"
printf '%s\n' '}'

像这样调用:

$ bash create_java_toString.sh << EOF
> private int port;
> private String portName;
> private ArrayList portList;
> private int id;
> EOF
public String toString() {
    return "{ " + "port=" + port + ",portName=" + portName + ",portList=" + portList + ",id=" + id + "}";
}

更多示例:

$ bash create_java_toString.sh << EOF
> private int i;
> private int j;
> EOF
public String toString() {
    return "{ " + "i=" + i + ",j=" + j + "}";
}
$ bash create_java_toString.sh << EOF
> EOF
public String toString() {
    return "{ " + "}";
}

答案3

我不知道您打算如何使用这些脚本,但这应该可以为您指明正确的方向:

#!/usr/bin/env bash

# get the argumetenst from the command line

# Sample terminal usage
# tostring -p port -n portName -l portLiat -i id


echo ""
echo ""

while getopts :p:n:l:i: option
do
        case "${option}" in
                p) port=${OPTARG};;
                n) portName=${OPTARG};;
                l) portList=${OPTARG};;
                i) id=${OPTARG};;
                \?) 
                        echo ""
                        echo -e "\e[0;31m Please supply the required values: $OPTARG \e[0m" 1>&2 
                        echo ""
                        echo " Usage: tostring -p port -n portName -l portList -i id"
                        echo ""
                        exit 1
                        ;;
                : )
                        echo "Invalid option $OPTARG requires an argument" 1>&2
                        ;;
        esac
done
shift $((OPTIND -1))

echo ""
echo ""

echo -e "\e[0;33m public String toString() {"
echo -e "       return "{ " + "port=" + "$port" + ",portName=" + "$portName" + ",portList=" + "$portList" + ",id=" + "$id" "}";"
echo ""
echo ""
echo -e "}"
echo -e "\e[0m"

更多信息:

https://sookocheff.com/post/bash/parsing-bash-script-arguments-with-shopts/

相关内容