blob: 0eb70fa463a8867d81a98794c43c28e933cfb22d [file] [log] [blame]
//
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//
= DevFaqScanForClasses
:jbake-type: wiki
:jbake-tags: wiki, devfaq, needsreview
:jbake-status: published
:keywords: Apache NetBeans wiki DevFaqScanForClasses
:description: Apache NetBeans wiki DevFaqScanForClasses
:toc: left
:toc-title:
:syntax: true
=== How can I scan a classpath to find all classes of a particular type?
==== Prerequisites
* find the ClassPath on which the subtypes should be found. Use e.g. ClassPath.getClassPath(<file>, ClassPath.SOURCE)
* if the supertype is given as a FQN, convert to ElementHandle<TypeElement> via ElementHandle.create inside a Task or CancellableTask.
==== Finding all subtypes of given type
All subtypes of a given type on a given ClassPath can be found using ClassIndex. As ClassIndex contains only direct subtypes, the indirect subtypes need to be found on the client side:
[source,java]
----
private Set<ElementHandle<TypeElement>> findAllSubTypes(ClassPath on, ElementHandle<TypeElement> of) {
ClasspathInfo cpInfo = ClasspathInfo.create(ClassPath.EMPTY, ClassPath.EMPTY, on);
List<ElementHandle<TypeElement>> todo = new LinkedList<ElementHandle<TypeElement>>(of);
Set<ElementHandle<TypeElement>> result = new HashSet<ElementHandle<TypeElement>>();
while (!todo.isEmpty()) {
//TODO: if cancellable, check for cancel here
ElementHandle<TypeElement> curr = todo.remove(0);
result.add(curr);
Set<ElementHandle<TypeElement>> typeElements = cpInfo.getClassIndex().getElements(eh, EnumSet.of(ClassIndex.SearchKind.IMPLEMENTORS), EnumSet.of(ClassIndex.SearchScope.SOURCE));
if (typeElements != null) { //can be null for cancellable tasks
todo.addAll(typeElements);
}
}
return result;
}
----
==== Getting FileObjects corresponding to ElementHandles
The FileObject corresponding to a given ElementHandle<TypeElement> can be found using SourceUtils.getFile.
=== Apache Migration Information
The content in this page was kindly donated by Oracle Corp. to the
Apache Software Foundation.
This page was exported from link:http://wiki.netbeans.org/DevFaqScanForClasses[http://wiki.netbeans.org/DevFaqScanForClasses] ,
that was last modified by NetBeans user Jlahoda
on 2011-12-15T08:29:15Z.
*NOTE:* This document was automatically converted to the AsciiDoc format on 2018-02-07, and needs to be reviewed.