diff --git a/src/main/java/com/microsoft/build/YmlFilesBuilder.java b/src/main/java/com/microsoft/build/YmlFilesBuilder.java index d247d6a..ede7a95 100644 --- a/src/main/java/com/microsoft/build/YmlFilesBuilder.java +++ b/src/main/java/com/microsoft/build/YmlFilesBuilder.java @@ -7,6 +7,7 @@ import com.microsoft.model.*; import com.microsoft.util.ElementUtil; import com.microsoft.util.FileUtil; +import com.microsoft.util.Utils; import com.microsoft.util.YamlUtil; import jdk.javadoc.doclet.DocletEnvironment; import org.apache.commons.lang3.RegExUtils; @@ -166,7 +167,7 @@ void addChildren(TypeElement classElement, List children) { List filterPrivateElements(List elements) { return elements.stream() - .filter(element -> !ElementUtil.isPrivateOrPackagePrivate(element)).collect(Collectors.toList()); + .filter(element -> !Utils.isPrivateOrPackagePrivate(element)).collect(Collectors.toList()); } void collect(TypeElement classElement, List children, @@ -193,7 +194,7 @@ void addConstructorsInfo(TypeElement classElement, MetadataFile classMetadataFil void addMethodsInfo(TypeElement classElement, MetadataFile classMetadataFile) { ElementFilter.methodsIn(classElement.getEnclosedElements()).stream() - .filter(methodElement -> !ElementUtil.isPrivateOrPackagePrivate(methodElement)) + .filter(methodElement -> !Utils.isPrivateOrPackagePrivate(methodElement)) .forEach(methodElement -> { MetadataFileItem methodItem = buildMetadataFileItem(methodElement); methodItem.setOverload(classItemsLookup.extractOverload(methodElement)); @@ -212,7 +213,7 @@ void addMethodsInfo(TypeElement classElement, MetadataFile classMetadataFile) { void addFieldsInfo(TypeElement classElement, MetadataFile classMetadataFile) { ElementFilter.fieldsIn(classElement.getEnclosedElements()).stream() - .filter(fieldElement -> !ElementUtil.isPrivateOrPackagePrivate(fieldElement)) + .filter(fieldElement -> !Utils.isPrivateOrPackagePrivate(fieldElement)) .forEach(fieldElement -> { MetadataFileItem fieldItem = buildMetadataFileItem(fieldElement); fieldItem.setContent(classItemsLookup.extractFieldContent(fieldElement)); diff --git a/src/main/java/com/microsoft/lookup/ClassLookup.java b/src/main/java/com/microsoft/lookup/ClassLookup.java index e875836..f5c67cb 100644 --- a/src/main/java/com/microsoft/lookup/ClassLookup.java +++ b/src/main/java/com/microsoft/lookup/ClassLookup.java @@ -3,7 +3,7 @@ import com.microsoft.lookup.model.ExtendedMetadataFileItem; import com.microsoft.model.MetadataFileItem; import com.microsoft.model.TypeParameter; -import com.microsoft.util.ElementUtil; +import com.microsoft.util.Utils; import jdk.javadoc.doclet.DocletEnvironment; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; @@ -146,7 +146,7 @@ void appendInheritedMethods(TypeElement element, List .orElse(0); for (Element m : members) { - if (m.getKind() == ElementKind.METHOD && !ElementUtil.isPrivateOrPackagePrivate(m)) { + if (m.getKind() == ElementKind.METHOD && !Utils.isPrivateOrPackagePrivate(m)) { String uid = element.getQualifiedName().toString().concat(".") + String.valueOf(m); ExtendedMetadataFileItem item = new ExtendedMetadataFileItem(uid); diff --git a/src/main/java/com/microsoft/util/ElementUtil.java b/src/main/java/com/microsoft/util/ElementUtil.java index 85f9b24..fa83605 100644 --- a/src/main/java/com/microsoft/util/ElementUtil.java +++ b/src/main/java/com/microsoft/util/ElementUtil.java @@ -3,7 +3,6 @@ import org.apache.commons.lang3.StringUtils; import javax.lang.model.element.Element; -import javax.lang.model.element.Modifier; import javax.lang.model.element.PackageElement; import javax.lang.model.element.TypeElement; import javax.lang.model.util.ElementFilter; @@ -31,7 +30,7 @@ public List extractSortedElements(Element element) { // 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 -> !Utils.isPrivateOrPackagePrivate(o)) .filter(o -> !matchAnyPattern(excludeClasses, String.valueOf(o.getQualifiedName()))) .sorted((o1, o2) -> StringUtils.compare(String.valueOf(o1.getSimpleName()), String.valueOf(o2.getSimpleName())) @@ -54,24 +53,4 @@ boolean matchAnyPattern(Set patterns, String stringToCheck) { } return false; } - - public static boolean isPackagePrivate(Element e) { - return !(isPublic(e) || isPrivate(e) || isProtected(e)); - } - - public static boolean isPrivate(Element e) { - return e.getModifiers().contains(Modifier.PRIVATE); - } - - public static boolean isProtected(Element e) { - return e.getModifiers().contains(Modifier.PROTECTED); - } - - public static boolean isPublic(Element e) { - return e.getModifiers().contains(Modifier.PUBLIC); - } - - public static boolean isPrivateOrPackagePrivate(Element e) { - return isPrivate(e) || isPackagePrivate(e); - } } diff --git a/src/main/java/com/microsoft/util/Utils.java b/src/main/java/com/microsoft/util/Utils.java new file mode 100644 index 0000000..a18edff --- /dev/null +++ b/src/main/java/com/microsoft/util/Utils.java @@ -0,0 +1,27 @@ +package com.microsoft.util; + +import javax.lang.model.element.Element; +import javax.lang.model.element.Modifier; + +public class Utils { + + public static boolean isPackagePrivate(Element e) { + return !(isPublic(e) || isPrivate(e) || isProtected(e)); + } + + public static boolean isPrivate(Element e) { + return e.getModifiers().contains(Modifier.PRIVATE); + } + + public static boolean isProtected(Element e) { + return e.getModifiers().contains(Modifier.PROTECTED); + } + + public static boolean isPublic(Element e) { + return e.getModifiers().contains(Modifier.PUBLIC); + } + + public static boolean isPrivateOrPackagePrivate(Element e) { + return isPrivate(e) || isPackagePrivate(e); + } +} diff --git a/src/test/java/com/microsoft/samples/SuperHero.java b/src/test/java/com/microsoft/samples/SuperHero.java index 25d2da2..54276e6 100644 --- a/src/test/java/com/microsoft/samples/SuperHero.java +++ b/src/test/java/com/microsoft/samples/SuperHero.java @@ -39,7 +39,7 @@ public SuperHero(String heroName, String uniquePower, int health, int defense) { *

* * @param incomingDamage the amount of incoming damage for {@link SuperHero} - * @param damageType type of damage with similar word damageTypeLong, sure + * @param damageType type of damage with similar word damageTypeLong, sure * @return the amount of health hero has after attack * @throws IllegalArgumentException when incomingDamage is negative and thanks for {@link Exception} * @version 1.2 @@ -84,11 +84,11 @@ public void setUniquePower(String uniquePower) { this.uniquePower = uniquePower; } - public int getHealth() { + protected int getHealth() { return health; } - public void setHealth(int health) { + protected void setHealth(int health) { this.health = health; } @@ -100,7 +100,11 @@ public void setDefense(int defense) { this.defense = defense; } - private void somePrivateMethod() {} + private void setHobby(String hobby) { + this.hobby = hobby; + } - void somePackagePrivateMethod() {} + String getHobby() { + return hobby; + } } diff --git a/src/test/java/com/microsoft/util/ElementUtilTest.java b/src/test/java/com/microsoft/util/ElementUtilTest.java index 73138f5..cf2b871 100644 --- a/src/test/java/com/microsoft/util/ElementUtilTest.java +++ b/src/test/java/com/microsoft/util/ElementUtilTest.java @@ -60,8 +60,8 @@ public void extractSortedElements() { // 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")); + assertThat("Unexpected package private class", allElements.contains("com.microsoft.samples.subpackage.InternalException"), is(true)); + assertThat("Unexpected to-exclude class", allElements.contains("com.microsoft.samples.subpackage.SomeExcludedClass"), is(true)); List extractedElements = elementUtil.extractSortedElements(element) @@ -83,28 +83,4 @@ public void matchAnyPattern() { assertFalse(elementUtil.matchAnyPattern(patterns, "EngineFive")); assertFalse(elementUtil.matchAnyPattern(patterns, "com.ms.Awesome")); } - - @Test - public void isPackagePrivate() { - Element element = elements.getTypeElement("com.microsoft.samples.SuperHero"); - - List result = element.getEnclosedElements() - .stream().filter(e -> ElementUtil.isPackagePrivate(e)).collect(Collectors.toList()); - - assertThat("Wrong result list size", result.size(), is(2)); - assertThat("Unexpected package private field", String.valueOf(result.get(0)), is("hobby")); - assertThat("Unexpected package private method", String.valueOf(result.get(1)), is("somePackagePrivateMethod()")); - } - - @Test - public void isPrivateOrPackagePrivate() { - Element element = elements.getTypeElement("com.microsoft.samples.SuperHero"); - - List result = element.getEnclosedElements() - .stream().filter(e -> ElementUtil.isPrivateOrPackagePrivate(e)).collect(Collectors.toList()); - - assertThat("Wrong result list size", result.size(), is(7)); - assertThat("Unexpected private method", String.valueOf(result.get(5)), is("somePrivateMethod()")); - assertThat("Unexpected package private method", String.valueOf(result.get(6)), is("somePackagePrivateMethod()")); - } } diff --git a/src/test/java/com/microsoft/util/UtilsTest.java b/src/test/java/com/microsoft/util/UtilsTest.java new file mode 100644 index 0000000..679dbd8 --- /dev/null +++ b/src/test/java/com/microsoft/util/UtilsTest.java @@ -0,0 +1,100 @@ +package com.microsoft.util; + +import java.util.List; +import java.util.stream.Collectors; + +import javax.lang.model.element.Element; +import javax.lang.model.element.ElementKind; +import javax.lang.model.util.Elements; + +import org.junit.*; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.mockito.junit.MockitoJUnitRunner; + +import com.google.testing.compile.CompilationRule; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.*; + +@RunWith(MockitoJUnitRunner.class) +public class UtilsTest { + + @Rule + public CompilationRule rule = new CompilationRule(); + private Elements elements; + private List allElements; + + @Before + public void setup() { + elements = rule.getElements(); + Element element = elements.getTypeElement("com.microsoft.samples.SuperHero"); + allElements = element.getEnclosedElements().stream().collect(Collectors.toList()); + } + + // Test isPackagePrivate() method + @Test + public void isPackagePrivate_True_PackagePrivateMethod() { + Element method = getElementByKindAndName(allElements, ElementKind.METHOD, "getHobby()"); + assertThat(Utils.isPackagePrivate(method), is(true)); + } + + @Test + public void isPackagePrivate_True_PackagePrivateField() { + Element field = getElementByKindAndName(allElements, ElementKind.FIELD, "hobby"); + assertThat(Utils.isPackagePrivate(field), is(true)); + } + + // Test isPrivate() method + @Test + public void isPrivate_True_PrivateMethod() { + Element method = getElementByKindAndName(allElements, ElementKind.METHOD, "setHobby(java.lang.String)"); + assertThat(Utils.isPrivate(method), is(true)); + } + + @Test + public void isPrivate_True_PrivateField() { + Element field = getElementByKindAndName(allElements, ElementKind.FIELD, "uniquePower"); + assertThat(Utils.isPrivate(field), is(true)); + } + + // Test isPrivateOrPackagePrivate() method + @Test + public void isPrivateOrPackagePrivate_True_PackagePrivateMethod() { + Element method = getElementByKindAndName(allElements, ElementKind.METHOD, "getHobby()"); + assertThat(Utils.isPrivateOrPackagePrivate(method), is(true)); + } + + @Test + public void isPrivateOrPackagePrivate_True_PrivateFiled() { + Element field = getElementByKindAndName(allElements, ElementKind.FIELD, "uniquePower"); + assertThat(Utils.isPrivateOrPackagePrivate(field), is(true)); + } + + @Test + public void isPrivateOrPackagePrivate_False_PublicMethod() { + Element method = getElementByKindAndName(allElements, ElementKind.METHOD, "getUniquePower()"); + assertThat(Utils.isPrivateOrPackagePrivate(method), is(false)); + } + + @Test + public void isPrivateOrPackagePrivate_False_PublicField() { + Element field = getElementByKindAndName(allElements, ElementKind.FIELD, "SOME_PUBLIC_STRING"); + assertThat(Utils.isPrivateOrPackagePrivate(field), is(false)); + } + + @Test + public void isPrivateOrPackagePrivate_False_ProtectedMethod() { + Element method = getElementByKindAndName(allElements, ElementKind.METHOD, "getHealth()"); + assertThat(Utils.isPrivateOrPackagePrivate(method), is(false)); + } + + private Element getElementByKindAndName(List elements, ElementKind elementKind, String name) { + return elements.stream() + .filter(e -> e.toString().equals(name)) + .filter(e -> e.getKind() == elementKind) + .findFirst().orElse(null); + } +} diff --git a/src/test/resources/expected-generated-files/com.microsoft.samples.SuperHero.yml b/src/test/resources/expected-generated-files/com.microsoft.samples.SuperHero.yml index b63bd8b..9cab56c 100644 --- a/src/test/resources/expected-generated-files/com.microsoft.samples.SuperHero.yml +++ b/src/test/resources/expected-generated-files/com.microsoft.samples.SuperHero.yml @@ -128,7 +128,7 @@ items: type: "Method" package: "com.microsoft.samples" syntax: - content: "public int getHealth()" + content: "protected int getHealth()" return: type: "int" - uid: "com.microsoft.samples.SuperHero.getHeroName()" @@ -206,7 +206,7 @@ items: type: "Method" package: "com.microsoft.samples" syntax: - content: "public void setHealth(int health)" + content: "protected void setHealth(int health)" parameters: - id: "health" type: "int"