Line 0
Link Here
|
|
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 2011 Oracle and/or its affiliates. All rights reserved. |
5 |
* |
6 |
* Oracle and Java are registered trademarks of Oracle and/or its affiliates. |
7 |
* Other names may be trademarks of their respective owners. |
8 |
* |
9 |
* The contents of this file are subject to the terms of either the GNU |
10 |
* General Public License Version 2 only ("GPL") or the Common |
11 |
* Development and Distribution License("CDDL") (collectively, the |
12 |
* "License"). You may not use this file except in compliance with the |
13 |
* License. You can obtain a copy of the License at |
14 |
* http://www.netbeans.org/cddl-gplv2.html |
15 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
16 |
* specific language governing permissions and limitations under the |
17 |
* License. When distributing the software, include this License Header |
18 |
* Notice in each file and include the License file at |
19 |
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this |
20 |
* particular file as subject to the "Classpath" exception as provided |
21 |
* by Oracle in the GPL Version 2 section of the License file that |
22 |
* accompanied this code. If applicable, add the following below the |
23 |
* License Header, with the fields enclosed by brackets [] replaced by |
24 |
* your own identifying information: |
25 |
* "Portions Copyrighted [year] [name of copyright owner]" |
26 |
* |
27 |
* If you wish your version of this file to be governed by only the CDDL |
28 |
* or only the GPL Version 2, indicate your decision by adding |
29 |
* "[Contributor] elects to include this software in this distribution |
30 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
31 |
* single choice of license, a recipient has the option to distribute |
32 |
* your version of this file under either the CDDL, the GPL Version 2 or |
33 |
* to extend the choice of license to its licensees as provided above. |
34 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
35 |
* Version 2 license, then the option applies only if the new code is |
36 |
* made subject to such option by the copyright holder. |
37 |
* |
38 |
* Contributor(s): |
39 |
* |
40 |
* Portions Copyrighted 2011 Sun Microsystems, Inc. |
41 |
*/ |
42 |
package org.netbeans.modules.java.source.queries.api; |
43 |
|
44 |
import java.util.Collection; |
45 |
import org.netbeans.api.annotations.common.CheckForNull; |
46 |
import org.netbeans.api.annotations.common.NonNull; |
47 |
import org.netbeans.api.annotations.common.NullAllowed; |
48 |
import org.netbeans.modules.java.source.queries.APIAccessor; |
49 |
import org.netbeans.modules.java.source.queries.QueryException; |
50 |
import org.netbeans.modules.java.source.queries.SPIAccessor; |
51 |
import org.netbeans.modules.java.source.queries.spi.ModelOperations; |
52 |
import org.netbeans.modules.java.source.queries.spi.QueriesController; |
53 |
import org.openide.filesystems.FileObject; |
54 |
import org.openide.util.Lookup; |
55 |
import org.openide.util.Parameters; |
56 |
|
57 |
/** |
58 |
* Support for queries |
59 |
* @author Tomas Zezula |
60 |
*/ |
61 |
public class Queries { |
62 |
|
63 |
static { |
64 |
APIAccessor.setInstance(new Accessor()); |
65 |
} |
66 |
|
67 |
static final QueriesController ctl = Lookup.getDefault().lookup(QueriesController.class); |
68 |
private final FileObject forFile; |
69 |
ModelOperations impl; |
70 |
|
71 |
Queries(@NonNull final FileObject forFile) { |
72 |
assert forFile != null; |
73 |
this.forFile = forFile; |
74 |
} |
75 |
|
76 |
/** |
77 |
* Returns a file for which the {@link Queries} was created. |
78 |
* @return a file |
79 |
*/ |
80 |
@NonNull |
81 |
public final FileObject getFile() { |
82 |
return forFile; |
83 |
} |
84 |
|
85 |
/** |
86 |
* Returns the fully qualified names of top level classes in the |
87 |
* compilation unit represented by {@link Queries#getFile()} |
88 |
* @return a collection of fully qualified names |
89 |
* @throws QueryException in case of failure |
90 |
*/ |
91 |
@NonNull |
92 |
public final Collection<? extends String> getTopLevelClasses() throws QueryException { |
93 |
return impl.getTopLevelClasses(getFile()); |
94 |
} |
95 |
|
96 |
/** |
97 |
* Checks if a class represented by fully qualified name is a |
98 |
* Java Bean. (has no param public constructor). |
99 |
* @param cls the fully qualified name of the class to be checked |
100 |
* @return true if the class is a Java Bean |
101 |
* @throws QueryException in case of failure |
102 |
*/ |
103 |
public final boolean isJavaBean(final @NonNull String cls) throws QueryException { |
104 |
Parameters.notNull("cls", cls); //NOI18N |
105 |
return impl.isJavaBean(cls); |
106 |
} |
107 |
|
108 |
/** |
109 |
* Checks if a class represented given fully qualified name is |
110 |
* available on class path. |
111 |
* @param cls the fully qualified name of the class |
112 |
* @return true if the class is available |
113 |
* @throws QueryException in case of failure |
114 |
*/ |
115 |
public final boolean isAvailable(final @NonNull String cls) throws QueryException { |
116 |
Parameters.notNull("cls", cls); //NOI18N |
117 |
return impl.isAvailable(cls); |
118 |
} |
119 |
|
120 |
/** |
121 |
* Returns a binary name as specified by JLS ยง13.1 for given |
122 |
* fully qualified name |
123 |
* @param cls the fully qualified name of the class |
124 |
* @return the binary name |
125 |
* @throws QueryException in case of failure |
126 |
*/ |
127 |
@NonNull |
128 |
public final String getClassBinaryName( |
129 |
@NonNull final String cls) throws QueryException { |
130 |
Parameters.notNull("cls", cls); //NOI18N |
131 |
return impl.getClassBinaryName(cls); |
132 |
} |
133 |
|
134 |
/** |
135 |
* Returns the fully qualified name of the super class for |
136 |
* for given class. |
137 |
* @param cls the class fully qualified name |
138 |
* @return the fully qualified name of the super class |
139 |
* @throws QueryException in case of failure |
140 |
*/ |
141 |
@NonNull |
142 |
public final String getSuperClass(final @NonNull String cls) throws QueryException { |
143 |
Parameters.notNull("cls", cls); //NOI18N |
144 |
return impl.getSuperClass(cls); |
145 |
} |
146 |
|
147 |
/** |
148 |
* Returns names of the methods complying to given types. |
149 |
* @param cls the fully qualified name of the class to look up methods in |
150 |
* @param returnType the return type of the method specified by the name of the |
151 |
* primitive type or fully qualified name of the declared type (null represents |
152 |
* any type) |
153 |
* @param paramteterTypes the types of the method parameters specified by |
154 |
* name of the primitive type or fully qualified name of the declared type |
155 |
* (null represent any type) |
156 |
* @return a collection of method names |
157 |
* @throws QueryException in case of failure |
158 |
*/ |
159 |
@NonNull |
160 |
public final Collection<? extends String> getMethodNames( |
161 |
@NonNull final String cls, |
162 |
@NullAllowed final String returnType, |
163 |
@NullAllowed final String... paramteterTypes) throws QueryException { |
164 |
Parameters.notNull("cls", cls); //NOI18N |
165 |
return impl.getMethodNames(cls, returnType, paramteterTypes); |
166 |
} |
167 |
|
168 |
/** |
169 |
* Returns names of the fields complying to type. |
170 |
* @param cls the fully qualified name of the class to look up methods in |
171 |
* @param type the type of the field specified by the name of the |
172 |
* primitive type or fully qualified name of the declared type |
173 |
* (null represents any type) |
174 |
* @return a collection of field names |
175 |
* @throws QueryException in case of failure |
176 |
*/ |
177 |
@NonNull |
178 |
public final Collection<? extends String> getFieldNames( |
179 |
@NonNull final String cls, |
180 |
@NullAllowed final String type) throws QueryException { |
181 |
Parameters.notNull("cls", cls); //NOI18N |
182 |
return impl.getFieldNames(cls, type); |
183 |
} |
184 |
|
185 |
/** |
186 |
* Returns annotations annotating given method. |
187 |
* @param cls the fully qualified name of the class to look up methods in |
188 |
* @param methodName name of the method |
189 |
* @param returnType the return type of the method specified by the name of the |
190 |
* primitive type or fully qualified name of the declared type |
191 |
* @param paramteterTypes the types of the method parameters specified by |
192 |
* name of the primitive type or fully qualified name of the declared type |
193 |
* @return collection of fully qualified names |
194 |
* @throws QueryException in case of failure |
195 |
*/ |
196 |
@NonNull |
197 |
public final Collection<? extends String> getMethodAnnotations( |
198 |
@NonNull final String cls, |
199 |
@NonNull final String methodName, |
200 |
@NonNull final String returnType, |
201 |
@NonNull final String... paramteterTypes) throws QueryException { |
202 |
Parameters.notNull("cls", cls); //NOI18N |
203 |
Parameters.notNull("methodName", methodName); //NOI18N |
204 |
Parameters.notNull("returnType", returnType); //NOI18N |
205 |
Parameters.notNull("paramteterTypes", paramteterTypes); //NOI18N |
206 |
return impl.getMethodAnnotations(cls, methodName, returnType, paramteterTypes); |
207 |
} |
208 |
|
209 |
/** |
210 |
* Performs the query |
211 |
* @param forFile file implying the scope to perform query on |
212 |
* @param queryFnc the query function |
213 |
* @return the result of query function |
214 |
* @throws QueryException in case of exception |
215 |
*/ |
216 |
@CheckForNull |
217 |
public static <T> T query ( |
218 |
@NonNull final FileObject forFile, |
219 |
@NonNull final Function<Queries,T> queryFnc) throws QueryException { |
220 |
Parameters.notNull("forFile", forFile); //NOI18N |
221 |
Parameters.notNull("queryFnc", queryFnc); //NOI18N |
222 |
if (ctl == null) { |
223 |
throw new IllegalStateException("No QueriesController found in the Lookup"); //NOI18N |
224 |
} |
225 |
final Queries q = new Queries(forFile); |
226 |
final QueriesController.Context<Queries,T> ctx = SPIAccessor.getInstance().createContext(queryFnc, q); |
227 |
return ctl.runQuery(ctx); |
228 |
} |
229 |
|
230 |
|
231 |
private void attach(@NonNull final ModelOperations impl) { |
232 |
assert impl != null; |
233 |
this.impl = impl; |
234 |
} |
235 |
|
236 |
private void detach() { |
237 |
this.impl = null; |
238 |
} |
239 |
|
240 |
private static class Accessor extends APIAccessor { |
241 |
|
242 |
@Override |
243 |
public void attach(Queries q, ModelOperations ops) { |
244 |
q.attach(ops); |
245 |
} |
246 |
|
247 |
@Override |
248 |
public void detach(Queries q) { |
249 |
q.detach(); |
250 |
} |
251 |
} |
252 |
} |