summaryrefslogtreecommitdiff
path: root/src/main/java/io/devnulllabs/openjava/ptree/SelfAccess.java
blob: 95dbe706111d35829c5b13152bf214f1d250a002 (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
/*
 * SelfAccess.java 1.0
 *
 * Jun 20, 1997 by micn
 * Sep 29, 1997 by bv
 * Oct 10, 1997 by mich
 *
 * @see io.devnulllabs.openjava.ptree.ParseTree
 * @version 1.0 last updated:  Oct 10, 1997
 * @author  Michiaki Tatsubori
 */
package io.devnulllabs.openjava.ptree;

import io.devnulllabs.openjava.mop.Environment;
import io.devnulllabs.openjava.mop.OJClass;
import io.devnulllabs.openjava.ptree.util.ParseTreeVisitor;

/**
 * The class <code>SelfAccess</code> represents an access to
 * <pre>this</pre> object.
 * this or super
 *
 */
public class SelfAccess extends Leaf implements Expression {
    public static final int THIS = 0;
    public static final int SUPER = 1;

    private static SelfAccess _constantSuper = null;
    private static SelfAccess _constantThis = null;

    protected String qualifier = null;

    int id = -1;

    SelfAccess() {
        this(null);
    }

    SelfAccess(String q) {
        super(((q == null) ? "" : q + ".") + "this");
        this.qualifier = q;
        this.id = THIS;
    }

    SelfAccess(int id) {
        super(id == SUPER ? "super" : "this");
        this.id = id;
    }

    public String getQualifier() {
        return this.qualifier;
    }

    public int getAccessType() {
        return id;
    }

    public boolean isSuperAccess() {
        return (id == SUPER);
    }

    public static SelfAccess makeSuper() {
        return constantSuper();
    }

    public static SelfAccess makeThis() {
        return constantThis();
    }

    public static SelfAccess makeThis(String qualifier) {
        if (qualifier == null || qualifier.equals("")) {
            return constantThis();
        }
        return new SelfAccess(qualifier);
    }

    public static SelfAccess constantSuper() {
        if (_constantSuper == null) {
            _constantSuper = new SelfAccess(SUPER);
        }
        return _constantSuper;
    }

    public static SelfAccess constantThis() {
        if (_constantThis == null) {
            _constantThis = new SelfAccess();
        }
        return _constantThis;
    }

    public void accept(ParseTreeVisitor v) throws ParseTreeException {
        v.visit(this);
    }

    public OJClass getType(Environment env) throws Exception {
        OJClass current = env.lookupClass(env.currentClassName());
        if (isSuperAccess())
            return current.getSuperclass();
        String qualifier = getQualifier();
        if (qualifier == null)
            return current;
        return env.lookupClass(env.toQualifiedName(qualifier));
    }

}