summaryrefslogtreecommitdiff
path: root/src/fjbg/ch/epfl/lamp/fjbg/JMethod.java
blob: 9b9da3c0aa72ec61955b704d5afd4c48f2ab8810 (plain) (blame)
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/* FJBG -- Fast Java Bytecode Generator
 * Copyright 2002-2011 LAMP/EPFL
 * @author  Michel Schinz
 */

package ch.epfl.lamp.fjbg;

import java.io.DataInputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
 * Representation of a Java method.
 *
 * @author Michel Schinz
 * @version 1.0
 */

public class JMethod extends JFieldOrMethod {
    public final static String CLASS_CONSTRUCTOR_NAME = "<clinit>";
    public final static String INSTANCE_CONSTRUCTOR_NAME = "<init>";

    protected /*final*/ JCode code;
    protected final String[] argNames;

    protected final LinkedList/*<JLocalVariable>*/ localVariables =
        new LinkedList();
    protected int localVariableIndex = 0;


    protected JMethod(FJBGContext context,
                      JClass owner,
                      int accessFlags,
                      String name,
                      JType returnType,
                      JType[] argTypes,
                      String[] argNames) {
        super(context,
              owner,
              accessFlags,
              name,
              new JMethodType(returnType, argTypes));
        this.argNames = argNames;

        assert argTypes.length == argNames.length;

        if (isAbstract() || isNative()) {
            code = null;
        } else {
            code = context.JCode(owner, this);
            addAttribute(context.JCodeAttribute(owner, this));

            if (!isStatic())
                addNewLocalVariable(owner.getType(), "this");

            for (int i = 0; i < argTypes.length; ++i)
                addNewLocalVariable(argTypes[i], argNames[i]);
        }
    }

    protected JMethod(FJBGContext context,
                      JClass owner,
                      DataInputStream stream)
        throws IOException {
        super(context, owner, stream);

        assert isAbstract() || isNative() || code != null;

        int n = 0;
        if (code != null) {
            for (Iterator it = code.getAttributes().iterator(); it.hasNext(); ) {
                JAttribute attr = (JAttribute)it.next();
                if (attr instanceof JLocalVariableTableAttribute)
                   n = ((JLocalVariableTableAttribute)attr).getMaxLocals();
            }
        }
        this.localVariableIndex = n;


        JType[] argTypes = ((JMethodType)getType()).getArgumentTypes();
        argNames = new String[argTypes.length]; // TODO get from attribute
        for (int i = 0; i < argNames.length; ++i)
            argNames[i] = "v"+i;
    }

    public void freeze() throws JCode.OffsetTooBigException {
        if (code != null) code.freeze();
        super.freeze();
    }

    public JType getReturnType() {
        return ((JMethodType)type).getReturnType();
    }

    public JType[] getArgumentTypes() {
        return ((JMethodType)type).getArgumentTypes();
    }

    public int getArgsSize() {
        int size = ((JMethodType)type).getArgsSize();
        if (!isStatic()) size += 1;  // for this
        return size;
    }

    public String[] getArgumentNames() {
        return argNames;
    }

    public JCode getCode() {
        assert !isAbstract();
        return code;
    }

    // Invoked by the JCode constructor
    protected void setCode(JCode code) {
        assert null == this.code;
        this.code = code;
    }

    public JCodeIterator codeIterator() {
        return new JCodeIterator(code);
    }

    // Local variables
    // FIXME : find a better management method for local variables
    public JLocalVariable addNewLocalVariable(JType type, String name) {
        assert !frozen;
        JLocalVariable var =
            context.JLocalVariable(this, type, name, localVariableIndex);
        localVariableIndex += type.getSize();
        localVariables.add(var);
        return var;
    }

    public JLocalVariable getLocalVariable(int index) {
        for (int i = 0; i < localVariables.size(); i++) {
            if (((JLocalVariable)localVariables.get(i)).index == index)
                return (JLocalVariable)localVariables.get(i);
        }
        return null;
    }

    public JLocalVariable[] getLocalVariables() {
        return (JLocalVariable[])localVariables
            .toArray(new JLocalVariable[localVariables.size()]);
    }


    public int getMaxLocals() {
        return localVariableIndex;
    }

    // Follows javap output format for methods.
    /*@Override*/ public String toString() {
        StringBuffer buf = new StringBuffer(flagsToString());
        String name = getName();
        if (CLASS_CONSTRUCTOR_NAME.equals(name))
            buf.append("{}");
        else {
            if (INSTANCE_CONSTRUCTOR_NAME.equals(name))
                name = getOwner().getName();
            else {
                buf.append(toExternalName(getReturnType()));
                buf.append(" ");
            }
            buf.append(toExternalName(name));
            buf.append("(");
            JType[] ts = getArgumentTypes();
            for (int i = 0; i < ts.length; ++i) {
                if (i > 0) buf.append(", ");
                buf.append(toExternalName(ts[i]));
            }
            buf.append(")");
        }
        buf.append(";\n");
        Iterator it = attributes.iterator();
        while(it.hasNext()) {
            JAttribute attr = (JAttribute)it.next();
            buf.append(attr);
        }
        return buf.toString();
    }

    private String flagsToString() {
        StringBuffer buf = new StringBuffer();
        if (isPublic()) buf.append("public ");
        else if (isProtected()) buf.append("protected ");
        else if (isPrivate()) buf.append("private ");
        if (isBridge()) buf.append("<bridge> ");
        if (hasVarargs()) buf.append("<varargs> ");
        if (isStatic()) buf.append("static ");
        else if (isNative()) buf.append("native ");
        if (isAbstract()) buf.append("abstract ");
        else if (isFinal()) buf.append("final ");
        if (isSynchronized()) buf.append("synchronized ");
        return buf.toString();
    }
}