Automatically create Java getter / setter definitions

About this article

In Java, I thought that getter / setter can be defined in a standard way for class fields, including function names, so I wrote a script in Python 3 so that I don't have to enter it manually. to introduce.

As a getter / setter, consider only the minimum functions. For example, if you want to check for invalid values with setter, add it manually.

Environment (roughly)

Language: Python3 OS: macOS (I think other OS is okay)

What I want to do: Define getters / setters in Java

I will explain what I want to do through an example.

Suppose you have a Wizard class that represents a wizard, and you have a name that represents a name and a power that represents magical power as fields, as shown below.

Wizard.java


public class Wizard {
    private String name;
    private double power;
    ...
}

In this case, I would like to define getter / setter as follows. The method name should be in the form getName / setName for the field name name.

Wizard.java


public class Wizard {
    private String name;
    private double power;
    ...
    public String getName() {
        return this.name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public double getPower() {
        return this.power;
    }
    public void setPower(double power) {
        this.power = power;
    }
    ...
}

Script and execution example

script

The script I wrote is shown below.

The first three lines are about the location of the files. The general flow is to read the fields line by line and write getters / setters from the type and variable names.

make_getter_and_setter.py


import os

path_of_this_script = os.path.dirname(__file__)
path_r = os.path.join(path_of_this_script, 'fields.txt')

with open(path_r) as f_r:
    for line in f_r: # line ex) "private double power;\n"
        line_list = line.rstrip().split(" ") # ex) ['private', 'double', 'power;']
        var_name = line_list[-1].rstrip(";") # ex) power
        pascal_var_name = var_name[0].upper() + var_name[1:] # ex) Power
        type_name = line_list[-2] # ex) double

        #getter ex)
        # public double getPower() {
        #     return this.power;
        # }
        print("public " + type_name + " get" + pascal_var_name + "()" + " {")
        print("    return this." + var_name + ";")
        print("}")

        #setter ex)
        # public void setPower(double power) {
        #     this.power = power;
        # }
        print("public void set" + pascal_var_name + "(" + type_name + " " + var_name + ")" + " {")
        print("    this." + var_name + " = " + var_name + ";")
        print("}")

Execution example

First of all, to use it, put a file called fields.txt in the same folder as the above script, and copy and paste the fields from the Java code as shown below.

fields.txt


private String name;
private double power;

Then, when you execute the script, the getter / setter definition will be output as shown below. Please copy and paste this into the original Java code.

python


$ python make_getter_and_setter.py 
public String getName() {
    return this.name;
}
public void setName(String name) {
    this.name = name;
}
public double getPower() {
    return this.power;
}
public void setPower(double power) {
    this.power = power;
}

At the end

I made it for fun, so I don't know if it will actually be used, but it was fun to write the script. If you have any opinions or impressions, please comment.

You are free to copy or modify the code in this article for any purpose.

Recommended Posts

Automatically create Java getter / setter definitions
setter, getter
setter and getter