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

Commit

Permalink
move functions for single element to a separate utility class (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
anmeng10101 committed Jun 5, 2020
1 parent 6584b8d commit 1175eb2
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 60 deletions.
7 changes: 4 additions & 3 deletions src/main/java/com/microsoft/build/YmlFilesBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -166,7 +167,7 @@ void addChildren(TypeElement classElement, List<String> children) {

List<? extends Element> filterPrivateElements(List<? extends Element> 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<String> children,
Expand All @@ -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));
Expand All @@ -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));
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/microsoft/lookup/ClassLookup.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -146,7 +146,7 @@ void appendInheritedMethods(TypeElement element, List<ExtendedMetadataFileItem>
.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);
Expand Down
23 changes: 1 addition & 22 deletions src/main/java/com/microsoft/util/ElementUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -31,7 +30,7 @@ public List<TypeElement> 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()))
Expand All @@ -54,24 +53,4 @@ boolean matchAnyPattern(Set<Pattern> 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);
}
}
27 changes: 27 additions & 0 deletions src/main/java/com/microsoft/util/Utils.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
14 changes: 9 additions & 5 deletions src/test/java/com/microsoft/samples/SuperHero.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public SuperHero(String heroName, String uniquePower, int health, int defense) {
* </p>
*
* @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
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}
}
28 changes: 2 additions & 26 deletions src/test/java/com/microsoft/util/ElementUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> extractedElements = elementUtil.extractSortedElements(element)
Expand All @@ -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<Element> 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<Element> 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()"));
}
}
100 changes: 100 additions & 0 deletions src/test/java/com/microsoft/util/UtilsTest.java
Original file line number Diff line number Diff line change
@@ -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<Element> 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<? extends Element> elements, ElementKind elementKind, String name) {
return elements.stream()
.filter(e -> e.toString().equals(name))
.filter(e -> e.getKind() == elementKind)
.findFirst().orElse(null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 1175eb2

Please # to comment.