KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > refactoring > typeconstraints > ExpressionVariable


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.jdt.internal.corext.refactoring.typeconstraints;
12
13 import org.eclipse.core.runtime.Assert;
14
15 import org.eclipse.jdt.core.ICompilationUnit;
16 import org.eclipse.jdt.core.dom.Assignment;
17 import org.eclipse.jdt.core.dom.ConditionalExpression;
18 import org.eclipse.jdt.core.dom.Expression;
19 import org.eclipse.jdt.core.dom.FieldAccess;
20 import org.eclipse.jdt.core.dom.IBinding;
21 import org.eclipse.jdt.core.dom.MethodInvocation;
22 import org.eclipse.jdt.core.dom.Name;
23 import org.eclipse.jdt.core.dom.ParenthesizedExpression;
24 import org.eclipse.jdt.core.dom.SuperFieldAccess;
25 import org.eclipse.jdt.core.dom.SuperMethodInvocation;
26
27 public final class ExpressionVariable extends ConstraintVariable {
28     
29     private final CompilationUnitRange fRange;
30     private final String JavaDoc fSource;
31     private final IBinding fExpressionBinding;
32     private final int fExpressionType;
33         
34     public ExpressionVariable(Expression expression){
35         super(expression.resolveTypeBinding());
36         fSource= expression.toString();
37         ICompilationUnit cu= ASTCreator.getCu(expression);
38         Assert.isNotNull(cu);
39         fRange= new CompilationUnitRange(cu, expression);
40         fExpressionBinding= resolveBinding(expression);
41         fExpressionType= expression.getNodeType();
42     }
43     
44     /* (non-Javadoc)
45      * @see java.lang.Object#toString()
46      */

47     public String JavaDoc toString() {
48         return "[" + fSource + "]"; //$NON-NLS-1$ //$NON-NLS-2$
49
}
50     
51     public CompilationUnitRange getCompilationUnitRange() {
52         return fRange;
53     }
54     
55     public int getExpressionType() {
56         return fExpressionType;
57     }
58
59     public IBinding getExpressionBinding() {
60         return fExpressionBinding;
61     }
62     
63     public static IBinding resolveBinding(Expression expression){
64         if (expression instanceof Name)
65             return ((Name)expression).resolveBinding();
66         if (expression instanceof ParenthesizedExpression)
67             return resolveBinding(((ParenthesizedExpression)expression).getExpression());
68         else if (expression instanceof Assignment)
69             return resolveBinding(((Assignment)expression).getLeftHandSide());//TODO ???
70
else if (expression instanceof MethodInvocation)
71             return ((MethodInvocation)expression).resolveMethodBinding();
72         else if (expression instanceof SuperMethodInvocation)
73             return ((SuperMethodInvocation)expression).resolveMethodBinding();
74         else if (expression instanceof FieldAccess)
75             return ((FieldAccess)expression).resolveFieldBinding();
76         else if (expression instanceof SuperFieldAccess)
77             return ((SuperFieldAccess)expression).resolveFieldBinding();
78         else if (expression instanceof ConditionalExpression)
79             return resolveBinding(((ConditionalExpression)expression).getThenExpression());
80         return null;
81     }
82     
83 }
84
Popular Tags