summaryrefslogtreecommitdiff
path: root/tutorial/examples/adapter/AdapterClass.oj
blob: c0f9763a9d434ef653e110fe86c74f2e687f80e8 (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
 * AdapterClass.oj
 *
 * An OpenJava example to support programming with the Adapter pattern.
 *
 * @author   Michiaki Tatsubori
 * @version  %VERSION% %DATE%
 * @see      java.lang.Object
 *
 * COPYRIGHT 1999 by Michiaki Tatsubori, ALL RIGHTS RESERVED.
 */
package examples.adapter;


import java.lang.Object;
import openjava.mop.*;
import openjava.ptree.*;
import openjava.syntax.*;


/**
 * The metaclass <code>AdapterClass</code> supports classes
 * implementing an adapter role of the Adapter pattern.
 * The target's methods with same signatures as the adaptee's are
 * automatically implemented into the adapter class.
 * <p>
 * For example, the class <code>VectorStack</code>:
 * <pre>
 * public class VectorStack instantiates AdapterClass
 *    adapts Vector in v to Stack
 * {
 *    Vector v;
 *    public VectorStack( Vector v ) {
 *        this.v = v;
 *    }
 *    public void push( Object o ) {
 *        v.addElement( o );
 *    }
 *    public Object pop() {
 *        return v.removeElementAt( v.size() - 1 );
 *    }
 * }
 * </pre>
 * would be automatically implemented with the forwarding methods
 * size(), isEmpty(), hashCode(), etc, which are found in both
 * the class Vector(adaptee) and the class Stack(target).
 * <p>
 *
 * @author   Michiaki Tatsubori
 * @version  1.0
 * @since    %SOFTWARE% 1.0
 * @see java.lang.Object
 */
public class AdapterClass instantiates Metaclass extends OJClass
{

    public static final String KEY_ADAPTS = "adapts";

    /* overrides for translation */
    public void translateDefinition() throws MOPException {
    OJClass target = getTarget(), adaptee = getAdaptee();
        if (target == null || adaptee == null)  return;

        /* implicit forwarding to the same signature's */
        OJMethod[] adapteds = adaptee.getMethods( this );
        for (int i = 0; i < adapteds.length; ++i) {
            /* picks up the method with same signature */
        OJMethod m;
        try {
        m = getTarget().getMethod( adapteds[i].getName(),
                       adapteds[i].getParameterTypes(),
                       this );
        } catch ( NoSuchMemberException e ) { /* not match */ continue; }

            /* generate a forwarding method with forwarded's name */
            addMethod( makeForwardingMethod( m.getName(), m ) );
        }

    addInterface( getTarget() );
    }

    /**
     * Generates a forwarding method with specified name.
     *
     * @param  name  generating method's name.
     * @param forwarded  method to which the generated method forwards.
     * @return  a generated forwarding method.
     */
    private OJMethod makeForwardingMethod( String name, OJMethod forwarded )
        throws MOPException
    {
        /* generates a new method without body */
    OJMethod result = new OJMethod(
        this,
        forwarded.getModifiers().remove( OJModifier.ABSTRACT ),
        forwarded.getReturnType(),
        name,
        forwarded.getParameterTypes(),
        forwarded.getExceptionTypes(),
        null
        );

    /* generates a method call and return statement */
    ExpressionList params = result.getParameterVariables();
        Expression expr = new MethodCall( getContainer(), name, params );
        StatementList body = new StatementList();
        if (forwarded.getReturnType() == OJSystem.VOID) {
            body.add( new ExpressionStatement( expr ) );
            body.add( new ReturnStatement() );
        } else {
            body.add( new ReturnStatement( expr ) );
        }

    result.setBody( body );
        return result;
    }

    /* extended information */

    private OJClass getAdaptee() throws MOPException {
        ObjectList suffix = (ObjectList) getSuffix( KEY_ADAPTS );
    return OJClass.forName( suffix.get( 0 ).toString() );
    }

    private Variable getContainer() throws MOPException {
        ObjectList suffix = (ObjectList) getSuffix( KEY_ADAPTS );
    return new Variable( suffix.get( 1 ).toString() );
    }

    private OJClass getTarget() throws MOPException {
        ObjectList suffix = (ObjectList) getSuffix( KEY_ADAPTS );
    return OJClass.forName( suffix.get( 2 ).toString() );
    }

    /* override to extend syntax */
    public static boolean isRegisteredKeyword( String keyword ) {
        return keyword.equals( KEY_ADAPTS );
    }

    /* override to extend syntax */
    public static SyntaxRule getDeclSuffixRule( String keyword ) {
    if (keyword.equals( KEY_ADAPTS )) {
        return new CompositeRule(
                new TypeNameRule(),
        new PrepPhraseRule( "in", new NameRule() ),
        new PrepPhraseRule( "to", new TypeNameRule() ) );
    }
    return null;
    }

}