1 | /******************************************************************************* |
---|---|
2 | * Copyright (c) 2005, 2010 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 | * |
11 | * Contributors: |
12 | * IBM Corporation - initial API and implementation |
13 | *******************************************************************************/ |
14 | package org.eclipse.jdt.core.dom; |
15 | |
16 | import org.eclipse.jdt.core.ICompilationUnit; |
17 | |
18 | /** |
19 | * An AST requestor handles ASTs for compilation units passed to |
20 | * {@link ASTParser#createASTs(ICompilationUnit[], String[], ASTRequestor, org.eclipse.core.runtime.IProgressMonitor) ASTParser.createASTs}. |
21 | * <p> |
22 | * {@link #acceptAST(ICompilationUnit, CompilationUnit) ASTRequestor.acceptAST} is called for each of the |
23 | * compilation units passed to {@link ASTParser#createASTs(ICompilationUnit[], String[], ASTRequestor, org.eclipse.core.runtime.IProgressMonitor) ASTParser.createASTs}. |
24 | * After all the compilation units have been processed, |
25 | * {@link #acceptBinding(String, IBinding) ASTRequestor.acceptBindings} is called for each |
26 | * of the binding keys passed to {@link ASTParser#createASTs(ICompilationUnit[], String[], ASTRequestor, org.eclipse.core.runtime.IProgressMonitor) ASTParser.createASTs}. |
27 | * </p> |
28 | * <p> |
29 | * This class is intended to be subclassed by clients. |
30 | * AST requestors are serially reusable, but neither reentrant nor thread-safe. |
31 | * </p> |
32 | * |
33 | * @see ASTParser#createASTs(ICompilationUnit[], String[], ASTRequestor, org.eclipse.core.runtime.IProgressMonitor) |
34 | * @since 3.1 |
35 | */ |
36 | public abstract class ASTRequestor { |
37 | |
38 | /** |
39 | * The compilation unit resolver used to resolve bindings, or |
40 | * <code>null</code> if none. Note that this field is non-null |
41 | * only within the dynamic scope of a call to |
42 | * <code>ASTParser.createASTs</code>. |
43 | */ |
44 | CompilationUnitResolver compilationUnitResolver = null; |
45 | |
46 | /** |
47 | * Creates a new instance. |
48 | */ |
49 | protected ASTRequestor() { |
50 | // do nothing |
51 | } |
52 | |
53 | /** |
54 | * Accepts an AST corresponding to the compilation unit. |
55 | * That is, <code>ast</code> is an AST for <code>source</code>. |
56 | * <p> |
57 | * The default implementation of this method does nothing. |
58 | * Clients should override to process the resulting AST. |
59 | * </p> |
60 | * |
61 | * @param source the compilation unit the ast is coming from |
62 | * @param ast the requested abtract syntax tree |
63 | */ |
64 | public void acceptAST(ICompilationUnit source, CompilationUnit ast) { |
65 | // do nothing |
66 | } |
67 | |
68 | /** |
69 | * Accepts a binding corresponding to the binding key. |
70 | * That is, <code>binding</code> is the binding for |
71 | * <code>bindingKey</code>; <code>binding</code> is <code>null</code> |
72 | * if the key cannot be resolved. |
73 | * <p> |
74 | * The default implementation of this method does nothing. |
75 | * Clients should override to process the resulting binding. |
76 | * </p> |
77 | * |
78 | * @param bindingKey the key of the requested binding |
79 | * @param binding the requested binding, or <code>null</code> if none |
80 | */ |
81 | public void acceptBinding(String bindingKey, IBinding binding) { |
82 | // do nothing |
83 | } |
84 | |
85 | /** |
86 | * Resolves bindings for the given binding keys. |
87 | * The given binding keys must have been obtained earlier |
88 | * using {@link IBinding#getKey()}. |
89 | * <p> |
90 | * If a binding key cannot be resolved, <code>null</code> is put in the resulting array. |
91 | * Bindings can only be resolved in the dynamic scope of a <code>ASTParser.createASTs</code>, |
92 | * and only if <code>ASTParser.resolveBindings(true)</code> was specified. |
93 | * </p> |
94 | * <p> |
95 | * Caveat: During an <code>acceptAST</code> callback, there are implementation |
96 | * limitations concerning the look up of binding keys representing local elements. |
97 | * In some cases, the binding is unavailable, and <code>null</code> will be returned. |
98 | * This is only an issue during an <code>acceptAST</code> callback, and only |
99 | * when the binding key represents a local element (e.g., local variable, |
100 | * local class, method declared in anonymous class). There is no such limitation |
101 | * outside of <code>acceptAST</code> callbacks, or for top-level types and their |
102 | * members even within <code>acceptAST</code> callbacks. |
103 | * </p> |
104 | * |
105 | * @param bindingKeys the binding keys to look up |
106 | * @return a list of bindings paralleling the <code>bindingKeys</code> parameter, |
107 | * with <code>null</code> entries for keys that could not be resolved |
108 | */ |
109 | public final IBinding[] createBindings(String[] bindingKeys) { |
110 | int length = bindingKeys.length; |
111 | IBinding[] result = new IBinding[length]; |
112 | for (int i = 0; i < length; i++) { |
113 | result[i] = null; |
114 | if (this.compilationUnitResolver != null) { |
115 | result[i] = this.compilationUnitResolver.createBinding(bindingKeys[i]); |
116 | } |
117 | } |
118 | return result; |
119 | } |
120 | } |
121 |
Members