Skip to content
This repository has been archived by the owner on Aug 9, 2022. It is now read-only.

Commit

Permalink
exclude private and package-private classes during documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
anmeng10101 committed May 15, 2020
1 parent a2d934e commit c22c9e1
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/microsoft/build/YmlFilesBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ void addSuperclassAndInterfacesReferences(TypeElement classElement, MetadataFile

void addInnerClassesReferences(TypeElement classElement, MetadataFile classMetadataFile) {
classMetadataFile.getReferences().addAll(
ElementFilter.typesIn(classElement.getEnclosedElements()).stream()
ElementFilter.typesIn(elementUtil.extractSortedElements(classElement)).stream()
.map(this::buildClassReference)
.collect(Collectors.toList()));
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/microsoft/util/ElementUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ public ElementUtil(String[] excludePackages, String[] excludeClasses) {

public List<TypeElement> extractSortedElements(Element element) {
// Need to apply sorting, because order of result items for Element.getEnclosedElements() depend on JDK implementation
// By default, exclude private and package-private items
// todo allow pass parameter for filter items by access modifiers
return ElementFilter.typesIn(element.getEnclosedElements()).stream()
.filter(o -> !isPrivateOrPackagePrivate(o))
.filter(o -> !matchAnyPattern(excludeClasses, String.valueOf(o.getQualifiedName())))
.sorted((o1, o2) ->
StringUtils.compare(String.valueOf(o1.getSimpleName()), String.valueOf(o2.getSimpleName()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,12 @@ public CustomException(String message) {
public void makeSomething() throws CustomException {
throw new CustomException("It happened!");
}

/**
* This is a private class which will not be documented.
*/

private class PrivateException {
private String message;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.microsoft.samples.subpackage;

/**
* This is a package-private class which will not be documented.
*/

class InternalException extends Exception {

public InternalException(String message) {
super(message);
}
}
20 changes: 15 additions & 5 deletions src/test/java/com/microsoft/util/ElementUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,23 @@ public void extractPackageElements() {
public void extractSortedElements() {
Element element = elements.getPackageElement("com.microsoft.samples.subpackage");

List<String> result = elementUtil.extractSortedElements(element)
List<String> allElements = element.getEnclosedElements()
.stream().map(String::valueOf).collect(Collectors.toList());

// Ensure items to exclude exist.
assertThat("Wrong enclosed elements number", allElements.size(), is(6));
assertTrue("Unexpected package private class", allElements.contains("com.microsoft.samples.subpackage.InternalException"));
assertTrue("Unexpected to-exclude class", allElements.contains("com.microsoft.samples.subpackage.SomeExcludedClass"));


List<String> extractedElements = elementUtil.extractSortedElements(element)
.stream().map(String::valueOf).collect(Collectors.toList());

assertThat("Wrong result list size", result.size(), is(4));
assertThat("Unexpected first item", result.get(0), is("com.microsoft.samples.subpackage.CustomException"));
assertThat("Unexpected second item", result.get(1), is("com.microsoft.samples.subpackage.Display"));
assertThat("Unexpected third item", result.get(2), is("com.microsoft.samples.subpackage.Person"));
// Verify filtered and sorted result
assertThat("Wrong result list size", extractedElements.size(), is(4));
assertThat("Unexpected first item", extractedElements.get(0), is("com.microsoft.samples.subpackage.CustomException"));
assertThat("Unexpected second item", extractedElements.get(1), is("com.microsoft.samples.subpackage.Display"));
assertThat("Unexpected third item", extractedElements.get(2), is("com.microsoft.samples.subpackage.Person"));
}

@Test
Expand Down

0 comments on commit c22c9e1

Please # to comment.