summaryrefslogtreecommitdiff
path: root/src/fjbg/ch/epfl/lamp/fjbg/JFieldOrMethod.java
blob: 794c0f13b51f143577845f9fbfecc65167fac94e (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
/* FJBG -- Fast Java Bytecode Generator
 * Copyright 2002-2013 LAMP/EPFL
 * @author  Michel Schinz
 */

package ch.epfl.lamp.fjbg;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

/**
 * Abstract superclass for a Java field or method.
 *
 * No two methods of fields in one class file may have the same name and
 * descriptor. See sections 4.6 and 4.7 of the JVM specification.
 *
 * @author Michel Schinz
 * @version 1.0
 */

abstract public class JFieldOrMethod extends JMember {

    protected final JClass owner;
    protected final JType type;

    protected final int nameIndex, signatureIndex;

    protected JFieldOrMethod(FJBGContext context,
                             JClass owner,
                             int accessFlags,
                             String name,
                             JType type) {
        super(context, accessFlags, name);
        this.owner = owner;
        this.type = type;

        nameIndex = owner.pool.addUtf8(name);
        signatureIndex = owner.pool.addUtf8(type.getSignature());
    }

    protected JFieldOrMethod(FJBGContext context,
                             JClass owner,
                             DataInputStream stream)
        throws IOException {
        super(context);
        this.owner = owner;
        this.accessFlags = stream.readShort();
        this.nameIndex = stream.readShort();
        this.name = owner.pool.lookupUtf8(nameIndex);
        this.signatureIndex = stream.readShort();
        this.type = JType.parseSignature(owner.pool.lookupUtf8(signatureIndex));
        this.attributes.addAll(JAttribute.readFrom(context, owner, this, stream));
    }

    public void freeze() throws JCode.OffsetTooBigException {
        assert !frozen;
        frozen = true;
    }

    public JClass getOwner() { return owner; }

    public JType getType() { return type; }

    public JClass getJClass() { return owner; }

    public boolean isPublic() {
        return (accessFlags & JAccessFlags.ACC_PUBLIC) != 0;
    }

    public boolean isPrivate() {
        return (accessFlags & JAccessFlags.ACC_PRIVATE) != 0;
    }

    public boolean isProtected() {
        return (accessFlags & JAccessFlags.ACC_PROTECTED) != 0;
    }

    public boolean isStatic() {
        return (accessFlags & JAccessFlags.ACC_STATIC) != 0;
    }

    public boolean isFinal() {
        return (accessFlags & JAccessFlags.ACC_FINAL) != 0;
    }

    public boolean isSuper() {
        return (accessFlags & JAccessFlags.ACC_SUPER) != 0;
    }

    public boolean isVolatile() {
        return (accessFlags & JAccessFlags.ACC_VOLATILE) != 0;
    }

    public boolean isTransient() {
        return (accessFlags & JAccessFlags.ACC_TRANSIENT) != 0;
    }

    public boolean isNative() {
        return (accessFlags & JAccessFlags.ACC_NATIVE) != 0;
    }

    public boolean isInterface() {
        return (accessFlags & JAccessFlags.ACC_INTERFACE) != 0;
    }

    public boolean isAbstract() {
        return (accessFlags & JAccessFlags.ACC_ABSTRACT) != 0;
    }

    public boolean isStrict() {
        return (accessFlags & JAccessFlags.ACC_STRICT) != 0;
    }

    // 1.5 specifics
    public boolean isBridge() {
        return (accessFlags & JAccessFlags.ACC_BRIDGE) != 0;
    }

    public boolean hasVarargs() {
        return (accessFlags & JAccessFlags.ACC_VARARGS) != 0;
    }

    public void writeTo(DataOutputStream stream) throws IOException {
        if (! frozen) {
            try {
                freeze();
            }
            catch (JCode.OffsetTooBigException e) {
                throw new Error(e);
            }
        }
        stream.writeShort(accessFlags);
        stream.writeShort(nameIndex);
        stream.writeShort(signatureIndex);
        JAttribute.writeTo(getAttributes(), stream);
    }
}