summaryrefslogtreecommitdiff
path: root/src/main/java/io/devnulllabs/openjava/ptree/InstanceofExpression.java
blob: 5b21002b26a17204c8cf9d6ccfca657681701217 (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
106
107
108
109
110
111
112
113
114
115
/*
 * InstanceofExpression.java 1.0
 *
 *
 * Jun 20, 1997 by mich
 * 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 <code>InstanceofExpression</code> represents
 * the expression like :
 * <br><blockquote><pre>
 *     obj instanceof Object
 * </pre></blockquote><br>
 * If the operator in the expression of the left operand has week unity,
 * this automatically produces the code in which the left operand
 * is enclosed by parenthesises.
 * <br>
 * In the case the left is <code>obj = obj2</code> and
 * the right is <code>String</code>,
 * this produces the code :
 * <br><blockquote><pre>
 *     (obj = obj2) instanceof String
 * </pre></blockquote><br>
 *
 * @see io.devnulllabs.openjava.ptree.Expression
 * @see io.devnulllabs.openjava.ptree.TypeName
 */
public class InstanceofExpression extends NonLeaf implements Expression {

    /**
     * Allocates a new object.
     *
     * @param  lexp  the expression to test.
     * @param  tspec  the typespecifier.
     */
    public InstanceofExpression(Expression lexp, TypeName tspec) {
        super();
        set(lexp, tspec);
    }

    InstanceofExpression() {
        super();
    }

    private final boolean needsLeftPar(Expression lexpr) {
        if (lexpr instanceof AssignmentExpression
            || lexpr instanceof ConditionalExpression) {
            return true;
        }
        /* this is too strict for + */
        if (lexpr instanceof BinaryExpression) {
            return true;
        }
        return false;
    }

    /**
     * Gets the expression of the left operand to be tested
     * in this expression.
     *
     * @return the left expression.
     */
    public Expression getExpression() {
        return (Expression) elementAt(0);
    }

    /**
     * Sets the expression of the left operand to be tested
     * in this expression.
     *
     * @param  lexpr  the left expression to set.
     */
    public void setLeft(Expression lexpr) {
        setElementAt(lexpr, 0);
    }

    /**
     * Gets the type specifier of the right operand to be tested
     * in this expression.
     *
     * @return  the type specifier.
     */
    public TypeName getTypeSpecifier() {
        return (TypeName) elementAt(1);
    }

    /**
     * Sets the type specifier of the right operand to be tested
     * in this expression.
     *
     * @param  tspec  the type specifier to set.
     */
    public void setTypeSpecifier(TypeName tspec) {
        setElementAt(tspec, 1);
    }

    public OJClass getType(Environment env) throws Exception {
        return OJClass.forClass(boolean.class);
    }

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

}