summaryrefslogtreecommitdiff
path: root/src/test/java/smyth/MDistributed.oj
blob: c9d50be81ca1742677a89c3e3dfb1f5dd83dcea6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package smyth;

import io.devnulllabs.openjava.mop.*;
import io.devnulllabs.openjava.ptree.*;
import io.devnulllabs.openjava.syntax.*;

import java.util.*;

public class MDistributed instantiates Metaclass extends OJClass {
    /**
     *    Class Declarations
     */
    public static final String DISTRIBUTED = "distributed";
    public static final String SUFFIX = "Inner";

    private OJClass inner = null;

    /**
     *    Overide the isRegistered method for DISTRIUBTED
     */
    public static boolean isRegisteredModifier(String keyword) {
        if (keyword.equals(DISTRIBUTED))
            return true;
        return false;
    }

    /**
     *    Main method - translateDefinition
     */
    public void translateDefinition() throws MOPException {
        if (getModifiers().has(DISTRIBUTED)) {
            System.out.println("makeInnerClass()");
            makeInnerClass();
            System.out.println("changeOutterFields()");
            changeOutterFields();
            System.out.println("changeOutterConstructors()");
            changeOutterConstructors();
        } else {
            System.out.println(
                "You must attach the class modifier 'distributed' = for translation to take place.");
        }
    }

    /**
     *    This method makes an innerclass from the outter class.
     *
     *    The outter class then acts as a wrapper for this inner class.
     *
     */
    public void makeInnerClass() throws openjava.mop.CannotAlterException {
        /* Declarations */
        ClassDeclaration clsdecl;
        MemberDeclarationList memlist;

        /* Make the ClassDeclaration from the OJClass */
        clsdecl = (ClassDeclaration) getSourceCode().makeRecursiveCopy();

        /* Change the name of ClassDeclaration */
        clsdecl.setName(clsdecl.getName() + SUFFIX);

        /* Change the name of the Constructors to match new name */
        memlist = clsdecl.getBody();

        for (int i = 0; i < memlist.size(); i++) {
            if (memlist.get(i) instanceof ConstructorDeclaration) {
                ((ConstructorDeclaration) memlist.get(i)).setName(
                    clsdecl.getName());
            }
        }

        /* Add the inner class */
        /*original (getSourceCode().getBody()).add(clsdecl);*/
        inner = new OJClass(getEnvironment(), this, clsdecl);
        OJSystem.env.record(inner.getName(), inner);
        inner = addClass(inner);
    }

    /**
     *    This method is to remove the fields from the outter (wrapper) class.
     *
     *    It is also to add a field of the type of the (new) innner class.
     */
    public void changeOutterFields() throws MOPException {
        /* Remove the fields from the outter (wrapper) class */
        OJField[] ojfld = getDeclaredFields();
        for (int i = 0; i < ojfld.length; ++i) {
            removeField(ojfld[i]);
        }

        /* Add the new field of type of the new inner class */
        /* At the moment this is just given the variable name 'inner' */
        OJModifier modif = OJModifier.forModifier(OJModifier.PRIVATE);
        addField(new OJField(this, modif, inner, "inner"));
    }

    /* Change the constructors */
    public void changeOutterConstructors() throws MOPException {
        OJConstructor[] ojconstr = getConstructors();
        for (int i = 0; i < ojconstr.length; ++i) {
            StatementList body =
                makeStatementList(
                    "inner=new "
                        + inner.getName()
                        + "("
                        + ojconstr[i].getParameterVariables()
                        + ");");
            ojconstr[i].setBody(body);
        }
    }

}