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

package ch.epfl.lamp.fjbg;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
 * Abstract superclass for a Java class, field or method.
 *
 * @author Nikolay Mihaylov
 * @version 1.0
 */

abstract public class JMember {

    protected boolean frozen = false;

    protected final FJBGContext context;

    protected String name;

    protected int accessFlags;

    protected final List/*<JAttribute>*/ attributes = new LinkedList();

    protected JMember(FJBGContext context) { this.context = context; }

    protected JMember(FJBGContext context, int accessFlags, String name) {
        this(context);
        this.name = name;
        this.accessFlags = accessFlags;
    }

    /**
     * Gets the access flags of the class.
     * @return The int representing the access flags of the class.
     */
    public int getAccessFlags() { return accessFlags; }

    /**
     * Gets the name of the member.
     * @return The string representing the name of the member.
     */
    public String getName() { return name; }

    /**
     * Gets the type of the objects that are instances of the class.
     * @return The type of the instances of the class.
     */
    public abstract JType getType();

    /**
     * Gets the class corresponding to/owning this member
     * @return The class owning this member or the class itself.
     */
    public abstract JClass getJClass();

    /**
     * Gets the constant pool of the class.
     * @return The constant pool of the class.
     */
    public JConstantPool getConstantPool() { return getJClass().getConstantPool(); }

    public FJBGContext getContext() { return context; }

    /**
     * Adds an attribute to the class.
     * @param attr The attribute to be added.
     */
    public void addAttribute(JAttribute attr) {
        assert !frozen;
        attributes.add(attr);
    }

    /**
     * Gets the list of all attributes of the class.
     * @return The list of the attributes of the class representation.
     */
    public List/*<JAttribute>*/ getAttributes() {
        return attributes;
    }

    /**
     * Get the attribute with the given name, or null if it doesn't
     * exist.
     */
    public JAttribute getAttribute(String name) {
        Iterator attrIt = getAttributes().iterator();
        while (attrIt.hasNext()) {
            JAttribute attr = (JAttribute)attrIt.next();
            if (attr.getName().equals(name))
                return attr;
        }
        return null;
    }

    protected static String toExternalName(String name) {
        return name.replace('/', '.');
    }

    protected static String toExternalName(JType tpe) {
        return tpe.toString().replace(':', '.');
    }
}