EclipseJDT Source Viewer

Home|eclipse_jdt/src/org/eclipse/jdt/core/dom/IVariableBinding.java
1/*******************************************************************************
2 * Copyright (c) 2000, 2021 IBM Corporation and others.
3 *
4 * This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License 2.0
6 * which accompanies this distribution, and is available at
7 * https://www.eclipse.org/legal/epl-2.0/
8 *
9 * SPDX-License-Identifier: EPL-2.0
10 * Contributors:
11 *     IBM Corporation - initial API and implementation
12 *******************************************************************************/
13
14package org.eclipse.jdt.core.dom;
15
16/**
17 * A variable binding represents either a field of a class or interface, or
18 * a local variable declaration (including formal parameters, local variables,
19 * and exception variables).
20 *
21 * @see ITypeBinding#getDeclaredFields()
22 * @since 2.0
23 * @noimplement This interface is not intended to be implemented by clients.
24 */
25public interface IVariableBinding extends IBinding {
26
27    /**
28     * Returns whether this binding is for a field.
29     * Note that this method returns <code>true</code> for constants,
30     * including enum constants. This method returns <code>false</code>
31     * for local variables.
32     *
33     * @return <code>true</code> if this is the binding for a field,
34     *    and <code>false</code> otherwise
35     */
36    public boolean isField();
37
38    /**
39     * Returns whether this binding is for an enum constant.
40     * Note that this method returns <code>false</code> for local variables
41     * and for fields other than enum constants.
42     *
43     * @return <code>true</code> if this is the binding for an enum constant,
44     *    and <code>false</code> otherwise
45     * @since 3.1
46     */
47    public boolean isEnumConstant();
48
49    /**
50     * Returns whether this binding corresponds to a parameter.
51     *
52     * @return <code>true</code> if this is the binding for a parameter,
53     *    and <code>false</code> otherwise
54     * @since 3.2
55     */
56    public boolean isParameter();
57
58    /**
59     * Returns whether this binding is for a record component constant.
60     * Note that this method returns <code>false</code> for local variables
61     * and for fields other than record component.
62     *
63     * @return <code>true</code> if this is the binding for a record component,
64     *    and <code>false</code> otherwise
65     * @since 3.26
66     */
67    public default boolean isRecordComponent() {
68        return false;
69    }
70
71    /**
72     * Returns the name of the field or local variable declared in this binding.
73     * The name is always a simple identifier.
74     *
75     * @return the name of this field or local variable
76     */
77    @Override
78    public String getName();
79
80    /**
81     * Returns the type binding representing the class or interface
82     * that declares this field.
83     * <p>
84     * The declaring class of a field is the class or interface of which it is
85     * a member. Local variables have no declaring class. The field length of an
86     * array type has no declaring class.
87     * </p>
88     *
89     * @return the binding of the class or interface that declares this field,
90     *   or <code>null</code> if none
91     */
92    public ITypeBinding getDeclaringClass();
93
94    /**
95     * Returns the binding for the type of this field or local variable.
96     *
97     * @return the binding for the type of this field or local variable
98     */
99    public ITypeBinding getType();
100
101    /**
102     * Returns a small integer variable id for this variable binding.
103     * <p>
104     * <b>Local variables inside methods:</b> Local variables (and parameters)
105     * declared within a single method are assigned ascending ids in normal
106     * code reading order; var1.getVariableId()&lt;var2.getVariableId() means that var1 is
107     * declared before var2.
108     * </p>
109     * <p>
110     * <b>Local variables outside methods:</b> Local variables declared in a
111     * type's static initializers (or initializer expressions of static fields)
112     * are assigned ascending ids in normal code reading order. Local variables
113     * declared in a type's instance initializers (or initializer expressions
114     * of non-static fields) are assigned ascending ids in normal code reading
115     * order. These ids are useful when checking definite assignment for
116     * static initializers (JLS 16.7) and instance initializers (JLS 16.8),
117     * respectively.
118     * </p>
119     * <p>
120     * <b>Fields:</b> Fields declared as members of a type are assigned
121     * ascending ids in normal code reading order;
122     * field1.getVariableId()&lt;field2.getVariableId() means that field1 is declared before
123     * field2.
124     * </p>
125     *
126     * @return a small non-negative variable id
127     */
128    public int getVariableId();
129
130    /**
131     * Returns this binding's constant value if it has one.
132     * Some variables may have a value computed at compile-time. If the type of
133     * the value is a primitive type, the result is the boxed equivalent (i.e.,
134     * int returned as an <code>Integer</code>). If the type of the value is
135     * <code>String</code>, the result is the string itself. If the variable has
136     * no compile-time computed value, the result is <code>null</code>.
137     * (Note: compile-time constant expressions cannot denote <code>null</code>;
138     * JLS2 15.28.). The result is always <code>null</code> for enum constants.
139     *
140     * @return the constant value, or <code>null</code> if none
141     * @since 3.0
142     */
143    public Object getConstantValue();
144
145    /**
146     * Returns the method binding representing the method containing the scope
147     * in which this local variable is declared.
148     * <p>
149     * The declaring method of a method formal parameter is the method itself.
150     * For a local variable declared somewhere within the body of a method,
151     * the declaring method is the enclosing method. When local or anonymous
152     * classes are involved, the declaring method is the innermost such method.
153     * There is no declaring method for a field, or for a local variable
154     * declared in a static or instance initializer; this method returns
155     * <code>null</code> in those cases.
156     * </p>
157     *
158     * @return the binding of the method or constructor that declares this
159     * local variable, or <code>null</code> if none
160     * @since 3.1
161     */
162    public IMethodBinding getDeclaringMethod();
163
164    /**
165     * Returns the binding for the variable declaration corresponding to this
166     * variable binding. For a binding for a field declaration in an instance
167     * of a generic type, this method returns the binding for the corresponding
168     * field declaration in the generic type. For other variable bindings,
169     * including all ones for local variables and parameters, this method
170     * returns the same binding.
171     *
172     * @return the variable binding for the originating declaration
173     * @since 3.1
174     */
175    public IVariableBinding getVariableDeclaration();
176
177    /**
178     * Returns whether this binding corresponds to an effectively final local
179     * variable (JLS8 4.12.4). A variable is said to be effectively final if
180     * it is not final and never assigned to after its initialization.
181     *
182     * @return <code>true</code> if this is an effectively final local variable
183     *                 and <code>false</code> otherwise
184     * @since 3.10
185     */
186    public boolean isEffectivelyFinal();
187}
188
MembersX
IVariableBinding:isRecordComponent
IVariableBinding:getConstantValue
IVariableBinding:isEffectivelyFinal
IVariableBinding:isField
IVariableBinding:getName
IVariableBinding:isParameter
IVariableBinding:isEnumConstant
IVariableBinding:getDeclaringMethod
IVariableBinding:getVariableId
IVariableBinding:getType
IVariableBinding:getVariableDeclaration
IVariableBinding:getDeclaringClass
Members
X