summaryrefslogtreecommitdiff
path: root/src/fjbg/ch/epfl/lamp/fjbg/FJBGContext.java
blob: 4c5bc272372b07e13ec7cf1bc652277b3689c9f8 (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
/* 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;

/**
 * Context in which FJBG executes. Used both as a factory for most
 * FJBG classes and as a repository for other factories.
 *
 * @author Michel Schinz
 * @version 1.0
 */

public class FJBGContext {
    /** Class file major version */
    final int MAJOR_VERSION;

    /** Class file minor version */
    final int MINOR_VERSION;

    public FJBGContext() {
        this(45, 3);
    }

    public FJBGContext(int major, int minor) {
        MAJOR_VERSION = major;
        MINOR_VERSION = minor;
    }

    // Factory methods
    //////////////////////////////////////////////////////////////////////

    public JClass JClass(int accessFlags,
                         String name,
                         String superclassName,
                         String[] interfaceNames,
                         String sourceFileName) {
        return new JClass(this,
                          accessFlags,
                          name,
                          superclassName,
                          interfaceNames,
                          sourceFileName);
    }

    public JClass JClass(DataInputStream stream)
        throws IOException {
        return new JClass(this, stream);
    }

    public JConstantPool JConstantPool() {
        return new JConstantPool(this);
    }

    public JConstantPool JConstantPool(DataInputStream stream)
        throws IOException {
        return new JConstantPool(this, stream);
    }

    public JField JField(JClass owner,
			 int accessFlags,
			 String name,
			 JType type) {
        return new JField(this,
                          owner,
			  accessFlags,
			  name,
			  type);
    }

    public JField JField(JClass owner, DataInputStream stream)
        throws IOException {
        return new JField(this, owner, stream);
    }

    public JMethod JMethod(JClass owner,
                           int accessFlags,
                           String name,
                           JType returnType,
                           JType[] argTypes,
                           String[] argNames) {
        return new JMethod(this,
                           owner,
                           accessFlags,
                           name,
                           returnType,
                           argTypes,
                           argNames);
    }

    public JMethod JMethod(JClass owner,
                           int accessFlags,
                           String name,
                           JMethodType type,
                           String[] argNames) {
        return JMethod(owner,
                       accessFlags,
                       name,
                       type.getReturnType(),
                       type.getArgumentTypes(),
                       argNames);
    }

    public JMethod JMethod(JClass owner, DataInputStream stream)
        throws IOException {
        return new JMethod(this, owner, stream);
    }

    public JLocalVariable JLocalVariable(JMethod owner,
                                         JType type,
                                         String name,
                                         int index) {
        return new JLocalVariable(this, owner, type, name, index);
    }

    public JCode JCode(JClass clazz, JMethod owner) {
        return new JExtendedCode(this, clazz, owner);
    }

    public JCode JCode(JClass clazz, JMethod owner, DataInputStream stream)
        throws IOException {
        return new JCode(this, clazz, owner, stream);
    }

    public JAttributeFactory JAttributeFactory() {
        return new JAttributeFactory(this);
    }

    // Attributes
    public JCodeAttribute JCodeAttribute(JClass clazz, JMethod owner) {
        return new JCodeAttribute(this, clazz, owner);
    }

    public JEnclosingMethodAttribute JEnclosingMethodAttribute(JClass clazz,
                                                               String className,
                                                               String methodName,
                                                               JType methodType) {
        return new JEnclosingMethodAttribute(this, clazz, className, methodName, methodType);
    }

    public JExceptionsAttribute JExceptionsAttribute(JClass clazz,
                                                     JMethod owner) {
        return new JExceptionsAttribute(this, clazz, owner);
    }

    public JLineNumberTableAttribute JLineNumberTableAttribute(JClass clazz,
                                                               JCode owner) {
        return new JLineNumberTableAttribute(this, clazz, owner);
    }

    public JLocalVariableTableAttribute JLocalVariableTableAttribute(JClass clazz,
                                                                     JCode owner) {
        return new JLocalVariableTableAttribute(this, clazz, owner);
    }

    public JOtherAttribute JOtherAttribute(JClass clazz,
                                           Object owner,
                                           String name,
                                           byte[] contents,
                                           int length) {
        return new JOtherAttribute(this, clazz, owner, name, contents, length);
    }

    public JOtherAttribute JOtherAttribute(JClass clazz,
                                           Object owner,
                                           String name,
                                           byte[] contents) {
        return JOtherAttribute(clazz, owner, name, contents, contents.length);
    }

    public JSourceFileAttribute JSourceFileAttribute(JClass clazz,
                                                     String sourceFileName) {
        return new JSourceFileAttribute(this, clazz, sourceFileName);
    }

    public JStackMapTableAttribute JStackMapTableAttribute(JClass clazz,
                                                           JCode owner) {
        return new JStackMapTableAttribute(this, clazz, owner);
    }

    /// Repository
    //////////////////////////////////////////////////////////////////////

    protected JAttributeFactory jAttributeFactory = null;
    public JAttributeFactory getJAttributeFactory() {
        if (jAttributeFactory == null)
            jAttributeFactory = JAttributeFactory();
        return jAttributeFactory;
    }
}