summaryrefslogtreecommitdiff
path: root/tutorial/Syntax.html
blob: ca1fb2f14758dbf3d5ae91396a840aa40d17552a (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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<!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>6. <font color="blue">Callee-side Translation</font></h2>


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


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

<p>Most of example programs given in the text are ready to be executed by
the OpenJava system and are similar in form to:

<br><blockquote><pre><font color=darkblue>
import java.util.*;
public class StringCollection instantiates FreeArgsClass
{
    Vector contents = new Vector();
    public generous void addAll( String[] args ) {
        for (int i = 0; i < args.length; ++i)  contents.addElement( args[i] );
    }
}
</font></pre></blockquote>

<br><blockquote><pre><font color=darkblue>
public class Test
{
    void test() {
        StringCollection strs = new StringCollection();
        strs.addAll( "one", "two", "three" );
    }
}
</font></pre></blockquote>


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

<p>Like following:
<br><blockquote><pre><font color=darkblue>
public class Test
{
    void test() {
        StringCollection strs = new StringCollection();
        strs.addAll( new String[]{ "one", "two", "three" } );
    }
}
</font></pre></blockquote>


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

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

public class FreeArgsClass instantiates Metaclass extends OJClass
{
    private static final String GENEROUS = "generous";
    /** Translates definition */

    public void translateDefinition() throws MOPException {
        OJMethod[] methods = getDeclaredMethods();
        for (int i = 0; i < methods.length; ++i) {
            if (! methods[i].getModifiers().has( GENEROUS ))  continue;
            System.err.println( methods[i] );
            OJClass[] paramtypes = methods[i].getParameterTypes();
            if (paramtypes.length != 1 || ! paramtypes[0].isArray()) {
                System.err.println( "illegal parameter, ignored." );
                continue;
            }
            putMetaInfo( methods[i].getName(), paramtypes[0].getName() );
        }
    }

    public OJMethod resolveException( NoSuchMemberException e,
                                      String name, OJClass[] params )
        throws NoSuchMemberException
    {
        try {
            String argtypename = getMetaInfo( name );
            if (argtypename != null) {
                OJClass paramtype = OJClass.forName( argtypename );
                return getMethod( name, new OJClass[]{ paramtype }, this );
            }
        } catch ( OJClassNotFoundException e2 ) {
            System.err.println( e2 );
        }
        return super.resolveException( e, name, params );
    }

    /** Translates allocation parts */
    public Expression expandMethodCall( Environment env, MethodCall expr ) {
        String argtypename = getMetaInfo( expr.getName() );
        if (argtypename == null)  return expr;
        OJClass comptype;
        try {
            comptype = OJClass.forName( argtypename ).getComponentType();
        } catch ( OJClassNotFoundException e ) {
            System.err.println( e );
            return expr;
        }
        ExpressionList dim = new ExpressionList( null );
        ArrayInitializer ainit = new ArrayInitializer( expr.getArguments() );
        ArrayAllocationExpression carrier
          = new ArrayAllocationExpression( comptype, dim, ainit );
        expr.setArguments( new ExpressionList( carrier ) );
        return expr;
    }

    public static boolean isRegisteredModifier( String keyword ) {
        if (keyword.equals( GENEROUS ))  return true;
        return false;
    }
</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>