GetterSetter

For an actual type parameter Person this factory generates a class PersonWithGetterSetter that extends class Person. PersonWithGetterSetter overrides each public member variable of Person with a private member variable of the same type and name and declares a public getter- and setter-method for each of it.

This is more an academic example that demonstrates the reflexion capabilities of Factory/Java. In real world applications getter- and setter-methods are usually used in order to do something more than just reading or writing a single member variable.

Factory/Java Source

The source can be found in the Factory/Java .zip-archive in factory/classes/examples/GetterSetter.factory:

<param>
<bound> java.lang.Object </bound>    // optional
<var> T </var>
</param>

package test;

public class
<apply>
    <apply>
        <class> factory.Toolbox </class>
        <method> getRelativeName </method>
        <args>
            <var> T </var>
        </args>
    </apply>
    <method> concat </method>
    <args>
        <const> "WithGetterSetter" </const>
    </args>
</apply>
extends <var> T </var>
{
  // iterate over public attributes of T
    <for> <var> I </var>
    <apply>
        <var> T </var>
        <method> getFields </method>
    </apply>
    <body>
        <let> <var> FT </var>
        <apply>
            <var> I </var>
            <method> getType </method>
        </apply>
        <body>
        <let> <var> FN </var>
        <apply>
            <var> I </var>
            <method> getName </method>
        </apply>
        <body>

        // override attribute with private one
        private <var> FT </var> <var> FN </var>;

        // generate getter
        public <var> FT </var>
        <apply>
            <const> "get" </const>
            <method> concat </method>
            <args>
                <apply>
                    <class> factory.Toolbox </class>
                    <method> capitalize </method>
                    <args> <var> FN </var> </args>
                </apply>
            </args>
        </apply>
        () {
            return this.<var> FN </var>;
        }

        // generate setter
        public void
        <apply>
            <const> "set" </const>
            <method> concat </method>
            <args>
                <apply>
                    <class> factory.Toolbox </class>
                    <method> capitalize </method>
                    <args> <var> FN </var> </args>
                </apply>
            </args>
        </apply>
        (<var> FT </var> value) {
            this.<var> FN </var> = value;
        }
        </body> </let>
        </body> </let>
    </body> </for>
}

Test Program

The following unparameterized factory can be found in factory/src/test/GetterSetterTest.factory in the Factory/Java .zip-archive. It applies factory GetterSetter to class Person, instantiates an object of the resulting class PersonWithGetterSetter and uses its setter-method for its private attribute plz. Then the getter-method for plz is used to get and print out the value of plz.

If we uncomment the line of Java code that tries to access plz directly, we will not be able to compile it, because a private attribute cannot be accessed from without its class.

package test;

class GetterSetterTest {
    public static void main(String argv[]) {
        <let> <var> T </var>
        <apply>
            <factory> examples/GetterSetter </factory>
            <args> <const> test.Person </const> </args>
        </apply>
        <body>
            <var> T </var> p = new <var> T </var>();
       
            p.setPlz(1);
            System.out.println(p.getPlz());
       
            // would cause error: plz has private access
            // p.plz++;   
        </body> </let>
    }
}

This factory can be compiled in the factory directory with
java -classpath classes factory.Factory -javad src -classd classes src/test/GetterSetterTest
and run with
java -classpath classes test.GetterSetterTest