summaryrefslogtreecommitdiff
path: root/tutorial/AutoImpl.html
blob: 9fd017f591ef0d57a449f18cbabd7f961ac0cbf2 (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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN"> 
<html>


<head>
<title>OpenJava : Tutorial</title>
<meta http-equiv="Keywords" content="java, openjava, reflection">
</head>


<body bgcolor="white"
        text="#000000" link="#007fff" vlink="#006fdf" alink="#ff0000">


<!---------------------------------------------------------------------->


<center>

<h1><font color="Blue">OpenJava Tutorial</font></h1>

</center>


<!---------------------------------------------------------------------->
<hr width="100%">
<!---------------------------------------------------------------------->


<h2>5. <font color="blue">Automatic Methods Implementation</font></h2>


<p>In this section, an example of simple translation at <i>callee-side</i>
is described.

<p>The callee-side of a class is whole the declration source code of the
class.  Metaprogram in the metaclass for that class (base class)
introspects that class definition in <code>this</code> class object
through the inheriting <code>OJClass</code> interface.

<p>Imagine an utility metaclass which automatically implements
empty methods in its base classes according to their interfaces.

<h3>5.1. What the base-level program should look like</h3>

<p>A base class <code>QuickTest</code> has to implement an interface
<code>java.awt.event.WindowListener</code>, which has several methods
to have to be implemented, but we want to make most of these methods
to have empty body, i.e. do nothing. 

<br><blockquote><pre><font color=darkblue>
import java.awt.event.*;
public class <font color=black>QuickTest</font> <b>instantiates <font color=black>AutoImplementerClass</font></b>
    implements <font color=black>WindowListener</font>
{
    public <font color=black>void</font> windowClosed( <font color=black>WindowEvent</font> e ) { <font color=black>System</font>.exit( 0 ); }
}
</font></pre></blockquote>


<h3>5.2. What the base-level program should be translated</h3>

<p>Like following:

<br><blockquote><pre><font color=darkblue>
public class <font color=black>QuickTest</font> instantiates <font color=black>AutoImplementerClass</font>
    implements <font color=black>WindowListener</font>
{
    public <font color=black>void</font> windowClosed( <font color=black>WindowEvent</font> e ) { <font color=black>System</font>.exit( 0 ); }
    <b>public <font color=black>void</font> windowIconified( <font color=black>WindowEvent</font> e ) { return; }</b>
    <b>public <font color=black>void</font> windowDeactivated( <font color=black>WindowEvent</font> e ) { return; }</b>
    ....
}
</font></pre></blockquote>


<h3>5.3. Write a meta-level program</h3>

<p>So.

<br><blockquote><pre><font color=darkblue>
import io.devnulllabs.openjava.mop.*;
import io.devnulllabs.openjava.ptree.*;
import io.devnulllabs.openjava.syntax.*;

public class AutoImplementerClass instantiates Metaclass extends OJClass
{
    public void translateDefinition() throws MOPException {
        OJMethod[] methods = getInheritedMethods();
        for (int i = 0; i < methods.length; ++i) {
            if (! methods[i].getModifiers().isAbstract()
                || methods[i].getReturnType() != OJSystem.VOID
                || hasDeclaredMethod( methods[i] ))  continue;
            addMethod( makeEmptyMethod( methods[i] ) );
        }
    }
    ....
}
</font></pre></blockquote>

<br><blockquote><pre><font color=darkblue>
    private boolean hasDeclaredMethod( OJMethod m ) {
    try {
        getDeclaredMethod( m.getName(), m.getParameterTypes() );
        return true;
    } catch ( NoSuchMemberException e ) {
        return false;
    }
    }
</font></pre></blockquote>

<br><blockquote><pre><font color=darkblue>
    private OJMethod makeEmptyMethod( OJMethod m ) throws MOPException {
        /* generates a new method without body */
        return new OJMethod( this,
            m.getModifiers().remove( OJModifier.ABSTRACT ),
            m.getReturnType(), m.getName(), m.getParameterTypes(),
            m.getExceptionTypes(),
            new StatementList( new ReturnStatement() )
            );
    }
</font></pre></blockquote>


<!---------------------------------------------------------------------->
<hr width="100%">
<!---------------------------------------------------------------------->


<center>

Please send any message to :
<address>
mich@acm.org
</address><BR>

</center>


<font size=1>Copyright (C) 1999 by Michaki Tatsubori.</font><br>
<font size=1>Java(TM) is a trademark of Sun Microsystems, Inc.</font>


<!---------------------------------------------------------------------->


</body>


</html>