KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > expressions > InstanceofExpression


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.core.internal.expressions;
12
13 import org.w3c.dom.Element JavaDoc;
14
15 import org.eclipse.core.runtime.Assert;
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.runtime.IConfigurationElement;
18
19 import org.eclipse.core.expressions.EvaluationResult;
20 import org.eclipse.core.expressions.Expression;
21 import org.eclipse.core.expressions.ExpressionInfo;
22 import org.eclipse.core.expressions.IEvaluationContext;
23
24 public class InstanceofExpression extends Expression {
25     /**
26      * The seed for the hash code for all instance of expressions.
27      */

28     private static final int HASH_INITIAL= InstanceofExpression.class.getName().hashCode();
29
30     private String JavaDoc fTypeName;
31     
32     public InstanceofExpression(IConfigurationElement element) throws CoreException {
33         fTypeName= element.getAttribute(ATT_VALUE);
34         Expressions.checkAttribute(ATT_VALUE, fTypeName);
35     }
36
37     public InstanceofExpression(Element JavaDoc element) throws CoreException {
38         fTypeName= element.getAttribute(ATT_VALUE);
39         Expressions.checkAttribute(ATT_VALUE, fTypeName.length() > 0 ? fTypeName : null);
40     }
41
42     public InstanceofExpression(String JavaDoc typeName) {
43         Assert.isNotNull(typeName);
44         fTypeName= typeName;
45     }
46     
47     /* (non-Javadoc)
48      * @see org.eclipse.jdt.internal.corext.refactoring.participants.Expression#evaluate(java.lang.Object)
49      */

50     public EvaluationResult evaluate(IEvaluationContext context) {
51         Object JavaDoc element= context.getDefaultVariable();
52         return EvaluationResult.valueOf(Expressions.isInstanceOf(element, fTypeName));
53     }
54     
55     public void collectExpressionInfo(ExpressionInfo info) {
56         info.markDefaultVariableAccessed();
57     }
58
59     public boolean equals(final Object JavaDoc object) {
60         if (!(object instanceof InstanceofExpression))
61             return false;
62         
63         final InstanceofExpression that= (InstanceofExpression) object;
64         return this.fTypeName.equals(that.fTypeName);
65     }
66
67     protected int computeHashCode() {
68         return HASH_INITIAL * HASH_FACTOR + fTypeName.hashCode();
69     }
70     
71     //---- Debugging ---------------------------------------------------
72

73     /* (non-Javadoc)
74      * @see java.lang.Object#toString()
75      */

76     public String JavaDoc toString() {
77         return "<instanceof value=\"" + fTypeName + "\"/>"; //$NON-NLS-1$ //$NON-NLS-2$
78
}
79 }
80
Popular Tags