/*******************************************************************************
* Copyright (c) 2009 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
* Zend Technologies
*******************************************************************************/
package org2.eclipse.php.internal.core.ast.nodes;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org2.eclipse.php.internal.core.PHPVersion;
import org2.eclipse.php.internal.core.ast.match.ASTMatcher;
import org2.eclipse.php.internal.core.ast.visitor.Visitor;
/**
* Represent instanceof expression
* <pre>e.g.<pre> $a instanceof MyClass,
* foo() instanceof $myClass,
* $a instanceof $b->$myClass
*/
public class InstanceOfExpression extends Expression {
private Expression expression;
private ClassName className;
/**
* The structural property of this node type.
*/
public static final ChildPropertyDescriptor EXPRESSION_PROPERTY =
new ChildPropertyDescriptor(InstanceOfExpression.class, "expression", Expression.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$
public static final ChildPropertyDescriptor CLASSNAME_PROPERTY =
new ChildPropertyDescriptor(InstanceOfExpression.class, "className", ClassName.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$
/**
* A list of property descriptors (element type:
* {@link StructuralPropertyDescriptor}),
* or null if uninitialized.
*/
private static final List<StructuralPropertyDescriptor> PROPERTY_DESCRIPTORS;
static {
List<StructuralPropertyDescriptor> propertyList = new ArrayList<StructuralPropertyDescriptor>(2);
propertyList.add(CLASSNAME_PROPERTY);
propertyList.add(EXPRESSION_PROPERTY);
PROPERTY_DESCRIPTORS = Collections.unmodifiableList(propertyList);
}
public InstanceOfExpression(int start, int end, AST ast, Expression expr, ClassName className) {
super(start, end, ast);
if (expr == null || className == null){
throw new IllegalArgumentException();
}
setExpression(expr);
setClassName(className);
}
public InstanceOfExpression(AST ast) {
super(ast);
}
public void accept0(Visitor visitor) {
final boolean visit = visitor.visit(this);
if (visit) {
childrenAccept(visitor);
}
visitor.endVisit(this);
}
public void childrenAccept(Visitor visitor) {
expression.accept(visitor);
className.accept(visitor);
}
public void traverseTopDown(Visitor visitor) {
accept(visitor);
expression.traverseTopDown(visitor);
className.traverseTopDown(visitor);
}
public void traverseBottomUp(Visitor visitor) {
expression.traverseBottomUp(visitor);
className.traverseBottomUp(visitor);
accept(visitor);
}
public void toString(StringBuffer buffer, String tab) {
buffer.append(tab).append("<InstanceofExpression"); //$NON-NLS-1$
appendInterval(buffer);
buffer.append(">\n"); //$NON-NLS-1$
expression.toString(buffer, TAB + tab);
buffer.append("\n"); //$NON-NLS-1$
className.toString(buffer, TAB + tab);
buffer.append("\n").append(tab).append("</InstanceofExpression>"); //$NON-NLS-1$ //$NON-NLS-2$
}
public int getType() {
return ASTNode.INSTANCE_OF_EXPRESSION;
}
public ClassName getClassName() {
return className;
}
/**
* Sets the class name of this instansiation.
*
* @param classname the new class name
* @exception IllegalArgumentException if:
* <ul>
* <li>the node belongs to a different AST</li>
* <li>the node already has a parent</li>
* <li>a cycle in would be created</li>
* </ul>
*/
public void setClassName(ClassName classname) {
if (classname == null) {
throw new IllegalArgumentException();
}
ASTNode oldChild = this.className;
preReplaceChild(oldChild, classname, CLASSNAME_PROPERTY);
this.className = classname;
postReplaceChild(oldChild, classname, CLASSNAME_PROPERTY);
}
final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) {
if (property == CLASSNAME_PROPERTY) {
if (get) {
return getClassName();
} else {
setClassName((ClassName) child);
return null;
}
}
if (property == EXPRESSION_PROPERTY) {
if (get) {
return getExpression();
} else {
setExpression((Expression) child);
return null;
}
}
// allow default implementation to flag the error
return super.internalGetSetChildProperty(property, get, child);
}
/**
* @deprecated use #getExpression()
*/
public Expression getExpr() {
return expression;
}
/**
* The expression of this instance of expression
*
* @return expression of this instance of expression
*/
public Expression getExpression() {
return expression;
}
/**
* Sets the expression of this instance of expression
*
* @param expression the new expression node
* @exception IllegalArgumentException if:
* <ul>
* <li>the node belongs to a different AST</li>
* <li>the node already has a parent</li>
* <li>a cycle in would be created</li>
* </ul>
*/
public void setExpression(Expression expression) {
if (expression == null) {
throw new IllegalArgumentException();
}
ASTNode oldChild = this.expression;
preReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
this.expression = expression;
postReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
}
/*
* Method declared on ASTNode.
*/
public boolean subtreeMatch(ASTMatcher matcher, Object other) {
// dispatch to correct overloaded match method
return matcher.match(this, other);
}
@Override
ASTNode clone0(AST target) {
final Expression expr = ASTNode.copySubtree(target, getExpression());
final ClassName klass = ASTNode.copySubtree(target, getClassName());
final InstanceOfExpression result = new InstanceOfExpression(this.getStart(), this.getEnd(), target, expr, klass);
return result;
}
@Override
List<StructuralPropertyDescriptor> internalStructuralPropertiesForType(PHPVersion apiLevel) {
return PROPERTY_DESCRIPTORS;
}
}