01: /*******************************************************************************
02: * Copyright (c) 2000, 2006 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.jdt.internal.corext.refactoring.typeconstraints;
11:
12: import org.eclipse.core.runtime.Assert;
13:
14: import org.eclipse.jdt.core.ICompilationUnit;
15: import org.eclipse.jdt.core.dom.Assignment;
16: import org.eclipse.jdt.core.dom.ConditionalExpression;
17: import org.eclipse.jdt.core.dom.Expression;
18: import org.eclipse.jdt.core.dom.FieldAccess;
19: import org.eclipse.jdt.core.dom.IBinding;
20: import org.eclipse.jdt.core.dom.MethodInvocation;
21: import org.eclipse.jdt.core.dom.Name;
22: import org.eclipse.jdt.core.dom.ParenthesizedExpression;
23: import org.eclipse.jdt.core.dom.SuperFieldAccess;
24: import org.eclipse.jdt.core.dom.SuperMethodInvocation;
25:
26: public final class ExpressionVariable extends ConstraintVariable {
27:
28: private final CompilationUnitRange fRange;
29: private final String fSource;
30: private final IBinding fExpressionBinding;
31: private final int fExpressionType;
32:
33: public ExpressionVariable(Expression expression) {
34: super (expression.resolveTypeBinding());
35: fSource = expression.toString();
36: ICompilationUnit cu = ASTCreator.getCu(expression);
37: Assert.isNotNull(cu);
38: fRange = new CompilationUnitRange(cu, expression);
39: fExpressionBinding = resolveBinding(expression);
40: fExpressionType = expression.getNodeType();
41: }
42:
43: /* (non-Javadoc)
44: * @see java.lang.Object#toString()
45: */
46: public String toString() {
47: return "[" + fSource + "]"; //$NON-NLS-1$ //$NON-NLS-2$
48: }
49:
50: public CompilationUnitRange getCompilationUnitRange() {
51: return fRange;
52: }
53:
54: public int getExpressionType() {
55: return fExpressionType;
56: }
57:
58: public IBinding getExpressionBinding() {
59: return fExpressionBinding;
60: }
61:
62: public static IBinding resolveBinding(Expression expression) {
63: if (expression instanceof Name)
64: return ((Name) expression).resolveBinding();
65: if (expression instanceof ParenthesizedExpression)
66: return resolveBinding(((ParenthesizedExpression) expression)
67: .getExpression());
68: else if (expression instanceof Assignment)
69: return resolveBinding(((Assignment) expression)
70: .getLeftHandSide());//TODO ???
71: else if (expression instanceof MethodInvocation)
72: return ((MethodInvocation) expression)
73: .resolveMethodBinding();
74: else if (expression instanceof SuperMethodInvocation)
75: return ((SuperMethodInvocation) expression)
76: .resolveMethodBinding();
77: else if (expression instanceof FieldAccess)
78: return ((FieldAccess) expression).resolveFieldBinding();
79: else if (expression instanceof SuperFieldAccess)
80: return ((SuperFieldAccess) expression)
81: .resolveFieldBinding();
82: else if (expression instanceof ConditionalExpression)
83: return resolveBinding(((ConditionalExpression) expression)
84: .getThenExpression());
85: return null;
86: }
87:
88: }
|