diff options
Diffstat (limited to 'src/main/java/io/devnulllabs/openjava/ptree/InstanceofExpression.java')
-rw-r--r-- | src/main/java/io/devnulllabs/openjava/ptree/InstanceofExpression.java | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/src/main/java/io/devnulllabs/openjava/ptree/InstanceofExpression.java b/src/main/java/io/devnulllabs/openjava/ptree/InstanceofExpression.java new file mode 100644 index 0000000..5b21002 --- /dev/null +++ b/src/main/java/io/devnulllabs/openjava/ptree/InstanceofExpression.java @@ -0,0 +1,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); + } + +} |