diff --git a/java.source/apichanges.xml b/java.source/apichanges.xml --- a/java.source/apichanges.xml +++ b/java.source/apichanges.xml @@ -108,6 +108,20 @@ + + + Added ClassIndex.getResourcesForPackage and ClassIndex.getElementsForPackageto allow to find package usages. + + + + + + Added ClassIndex.getResourcesForPackage and ClassIndex.getElementsForPackageto allow to find package usages. + Currently the client which needs to find usages of all types declared in the package has to list the package and query usages of each type + individually which is inefficient. + + + Added GeneratorUtilities.addImports and several methods to CodeStyle to support organizing imports. diff --git a/java.source/nbproject/project.properties b/java.source/nbproject/project.properties --- a/java.source/nbproject/project.properties +++ b/java.source/nbproject/project.properties @@ -46,7 +46,7 @@ javadoc.title=Java Source javadoc.arch=${basedir}/arch.xml javadoc.apichanges=${basedir}/apichanges.xml -spec.version.base=0.88.0 +spec.version.base=0.89.0 test.qa-functional.cp.extra=${refactoring.java.dir}/modules/ext/javac-api-nb-7.0-b07.jar test.unit.run.cp.extra=${o.n.core.dir}/core/core.jar:\ ${o.n.core.dir}/lib/boot.jar:\ diff --git a/java.source/src/org/netbeans/api/java/source/ClassIndex.java b/java.source/src/org/netbeans/api/java/source/ClassIndex.java --- a/java.source/src/org/netbeans/api/java/source/ClassIndex.java +++ b/java.source/src/org/netbeans/api/java/source/ClassIndex.java @@ -61,7 +61,9 @@ import java.util.concurrent.atomic.AtomicBoolean; import java.util.logging.Level; import java.util.logging.Logger; +import javax.lang.model.element.Element; import javax.lang.model.element.ElementKind; +import javax.lang.model.element.PackageElement; import javax.lang.model.element.TypeElement; import org.apache.lucene.document.Document; import org.netbeans.api.annotations.common.CheckForNull; @@ -325,8 +327,8 @@ /** - * Returns a set of {@link ElementHandle}s containing reference(s) to given element. - * @param element for which usages should be found + * Returns a set of {@link ElementHandle}s containing reference(s) to given type element. + * @param element the {@link ElementHandle} of a {@link TypeElement} for which usages should be found * @param searchKind type of reference, {@see SearchKind} * @param scope to search in {@see SearchScope} * @return set of {@link ElementHandle}s containing the reference(s) @@ -337,39 +339,49 @@ final @NonNull ElementHandle element, final @NonNull Set searchKind, final @NonNull Set scope) { - assert element != null; - assert element.getSignature()[0] != null; - assert searchKind != null; - final Set> result = new HashSet> (); - final Iterable queries = this.getQueries (scope); - final Set ut = encodeSearchKind(element.getKind(),searchKind); - final Convertor> thConvertor = DocumentUtil.elementHandleConvertor(); - try { - if (!ut.isEmpty()) { - for (ClassIndexImpl query : queries) { - try { - query.search( - element, - ut, - scope, - thConvertor, - result); - } catch (Index.IndexClosedException e) { - logClosedIndex (query); - } catch (IOException e) { - Exceptions.printStackTrace(e); - } + return searchImpl( + element, + searchKind, + scope, + new Convertor>>(){ + @NonNull + @Override + public Convertor> convert(@NonNull final ClassIndexImpl p) { + return DocumentUtil.elementHandleConvertor(); } - } - return Collections.unmodifiableSet(result); - } catch (InterruptedException e) { - return null; - } + }); } - + /** - * Returns a set of source files containing reference(s) to given element. - * @param element for which usages should be found + * Returns a set of {@link ElementHandle}s containing reference(s) to given package element. + * @param element the {@link ElementHandle} of a {@link PackageElement} for which usages should be found + * @param searchKind type of reference, {@see SearchKind} + * @param scope to search in {@see SearchScope} + * @return set of {@link ElementHandle}s containing the reference(s) + * It may return null when the caller is a CancellableTask<CompilationInfo> and is cancelled + * inside call of this method. + * @since 0.89 + */ + public @NullUnknown Set> getElementsForPackage ( + final @NonNull ElementHandle element, + final @NonNull Set searchKind, + final @NonNull Set scope) { + return searchImpl( + element, + searchKind, + scope, + new Convertor>>(){ + @NonNull + @Override + public Convertor> convert(@NonNull final ClassIndexImpl p) { + return DocumentUtil.elementHandleConvertor(); + } + }); + } + + /** + * Returns a set of source files containing reference(s) to given type element. + * @param element the {@link ElementHandle} of a {@link TypeElement} for which usages should be found * @param searchKind type of reference, {@see SearchKind} * @param scope to search in {@see SearchScope} * @return set of {@link FileObject}s containing the reference(s) @@ -380,22 +392,69 @@ final @NonNull ElementHandle element, final @NonNull Set searchKind, final @NonNull Set scope) { - assert element != null; - assert element.getSignature()[0] != null; - assert searchKind != null; - final Set result = new HashSet (); - final Iterable queries = this.getQueries (scope); + return searchImpl( + element, + searchKind, + scope, + new Convertor>() { + @NonNull + @Override + public Convertor convert(@NonNull final ClassIndexImpl p) { + return DocumentUtil.fileObjectConvertor (p.getSourceRoots()); + } + }); + } + + /** + * Returns a set of source files containing reference(s) to given package element. + * @param element the {@link ElementHandle} of a {@link PackageElement} for which usages should be found + * @param searchKind type of reference, {@see SearchKind} + * @param scope to search in {@see SearchScope} + * @return set of {@link FileObject}s containing the reference(s) + * It may return null when the caller is a CancellableTask<CompilationInfo> and is cancelled + * inside call of this method. + * @since 0.89 + */ + public @NullUnknown Set getResourcesForPackage ( + final @NonNull ElementHandle element, + final @NonNull Set searchKind, + final @NonNull Set scope) { + return searchImpl( + element, + searchKind, + scope, + new Convertor>() { + @NonNull + @Override + public Convertor convert(@NonNull final ClassIndexImpl p) { + return DocumentUtil.fileObjectConvertor (p.getSourceRoots()); + } + }); + } + + @NullUnknown + private Set searchImpl( + @NonNull final ElementHandle element, + @NonNull final Set searchKind, + @NonNull final Set scope, + @NonNull final Convertor> convertor) { + Parameters.notNull("element", element); //NOI18N + Parameters.notNull("element.signatue", element.getSignature()[0]); //NOI18N + Parameters.notNull("searchKind", searchKind); //NOI18N + Parameters.notNull("scope", scope); //NOI18N + Parameters.notNull("convertor", convertor); //NOI18N + final Set result = new HashSet (); final Set ut = encodeSearchKind(element.getKind(),searchKind); - try { - if (!ut.isEmpty()) { + if (!ut.isEmpty()) { + try { + final Iterable queries = this.getQueries (scope); for (ClassIndexImpl query : queries) { - final Convertor foConvertor = DocumentUtil.fileObjectConvertor (query.getSourceRoots()); try { query.search( element, ut, scope, - foConvertor, + convertor.convert(query), result); } catch (Index.IndexClosedException e) { logClosedIndex (query); @@ -403,14 +462,12 @@ Exceptions.printStackTrace(e); } } + } catch (InterruptedException e) { + return null; } - return Collections.unmodifiableSet(result); - } catch (InterruptedException e) { - return null; } + return Collections.unmodifiableSet(result); } - - /** * Returns {@link ElementHandle}s for all declared types in given classpath corresponding to the name. * @param name case sensitive prefix, case insensitive prefix, exact simple name,