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

Commit

Permalink
Merge pull request #41 from docascode/develop
Browse files Browse the repository at this point in the history
June Release for 2020
  • Loading branch information
anmeng10101 authored Jun 5, 2020
2 parents 0b3bd2a + 7732efa commit efc79d0
Show file tree
Hide file tree
Showing 34 changed files with 1,994 additions and 102 deletions.
12 changes: 7 additions & 5 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,14 +194,15 @@ 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));
methodItem.setContent(classItemsLookup.extractMethodContent(methodElement));
methodItem.setExceptions(classItemsLookup.extractExceptions(methodElement));
methodItem.setParameters(classItemsLookup.extractParameters(methodElement));
methodItem.setReturn(classItemsLookup.extractReturn(methodElement));
methodItem.setOverridden(classItemsLookup.extractOverridden(methodElement));

classMetadataFile.getItems().add(methodItem);
addExceptionReferences(methodItem, classMetadataFile);
Expand All @@ -212,7 +214,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 Expand Up @@ -286,7 +288,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 Expand Up @@ -488,7 +490,7 @@ String resolveUidFromReference(String linkContent, LookupContext lookupContext)
return uid;
}

String resolveUidByLookup(String signature, LookupContext lookupContext){
String resolveUidByLookup(String signature, LookupContext lookupContext) {
if (StringUtils.isBlank(signature) || lookupContext == null) {
return "";
}
Expand Down
42 changes: 25 additions & 17 deletions src/main/java/com/microsoft/lookup/BaseLookup.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.microsoft.model.MethodParameter;
import com.microsoft.model.Return;
import com.microsoft.model.TypeParameter;

import com.sun.source.doctree.DocCommentTree;
import com.sun.source.doctree.DocTree;
import com.sun.source.doctree.LinkTree;
Expand All @@ -14,9 +15,12 @@
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;

import jdk.javadoc.doclet.DocletEnvironment;

import org.apache.commons.lang3.RegExUtils;
import org.apache.commons.lang3.StringUtils;

Expand Down Expand Up @@ -152,6 +156,10 @@ public Set<MetadataFileItem> extractReferences(T key) {
return resolve(key).getReferences();
}

public String extractOverridden(T key) {
return resolve(key).getOverridden();
}

protected String determineType(T element) {
return elementKindLookup.get(element.getKind());
}
Expand All @@ -162,9 +170,9 @@ protected String determinePackageName(T element) {

protected String determineComment(T element) {
return getDocCommentTree(element)
.map(DocCommentTree::getFullBody)
.map(this::replaceLinksAndCodes)
.orElse(null);
.map(DocCommentTree::getFullBody)
.map(this::replaceLinksAndCodes)
.orElse(null);
}

/**
Expand All @@ -175,19 +183,19 @@ protected String determineComment(T element) {
*/
String replaceLinksAndCodes(List<? extends DocTree> items) {
return items.stream().map(
bodyItem -> {
switch (bodyItem.getKind()) {
case LINK:
case LINK_PLAIN:
return buildXrefTag((LinkTree) bodyItem);
case CODE:
return buildCodeTag((LiteralTree) bodyItem);
case LITERAL:
return expandLiteralBody((LiteralTree) bodyItem);
default:
return String.valueOf(bodyItem);
bodyItem -> {
switch (bodyItem.getKind()) {
case LINK:
case LINK_PLAIN:
return buildXrefTag((LinkTree) bodyItem);
case CODE:
return buildCodeTag((LiteralTree) bodyItem);
case LITERAL:
return expandLiteralBody((LiteralTree) bodyItem);
default:
return String.valueOf(bodyItem);
}
}
}
).collect(Collectors.joining());
}

Expand Down Expand Up @@ -223,7 +231,7 @@ public String makeTypeShort(String value) {
return value;
}
return Stream.of(StringUtils.split(value, "<"))
.map(s -> RegExUtils.removeAll(s, "\\b[a-z0-9_.]+\\."))
.collect(Collectors.joining("<"));
.map(s -> RegExUtils.removeAll(s, "\\b[a-z0-9_.]+\\."))
.collect(Collectors.joining("<"));
}
}
105 changes: 85 additions & 20 deletions src/main/java/com/microsoft/lookup/ClassItemsLookup.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,35 @@
package com.microsoft.lookup;

import com.microsoft.lookup.model.ExtendedMetadataFileItem;

import com.microsoft.model.ExceptionItem;
import com.microsoft.model.MethodParameter;
import com.microsoft.model.Return;

import com.microsoft.util.CommentHelper;
import com.microsoft.util.Utils;

import com.sun.source.doctree.DocTree;
import com.sun.source.doctree.DocTree.Kind;
import com.sun.source.doctree.ParamTree;
import com.sun.source.doctree.ReturnTree;
import com.sun.source.doctree.ThrowsTree;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import javax.lang.model.element.Element;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;

import javax.lang.model.element.*;
import javax.lang.model.type.TypeKind;

import jdk.javadoc.doclet.DocletEnvironment;

public class ClassItemsLookup extends BaseLookup<Element> {
private Utils utils;

public ClassItemsLookup(DocletEnvironment environment) {
super(environment);
utils = new Utils(environment);
}

@Override
Expand Down Expand Up @@ -48,18 +57,22 @@ protected ExtendedMetadataFileItem buildMetadataFileItem(Element element) {
ExecutableElement exeElement = (ExecutableElement) element;
List<MethodParameter> parameters = extractParameters(exeElement);
String paramsString = parameters.stream()
.map(parameter -> String.format("%s %s", makeTypeShort(parameter.getType()), parameter.getId()))
.collect(Collectors.joining(", "));
.map(parameter -> String.format("%s %s", makeTypeShort(parameter.getType()), parameter.getId()))
.collect(Collectors.joining(", "));
String nameWithoutBrackets = elementQName.replaceAll("\\(.*\\)", "");
String methodName = String.format("%s(%s)", nameWithoutBrackets, paramsString);

result.setName(methodName);
result.setMethodContent(String.format("%s %s %s", modifiers,
makeTypeShort(String.valueOf(exeElement.getReturnType())), result.getName()));
makeTypeShort(String.valueOf(exeElement.getReturnType())), result.getName()));
result.setConstructorContent(String.format("%s %s", modifiers, result.getName()));
result.setParameters(parameters);
result.setExceptions(extractExceptions(exeElement));
result.setReturn(extractReturn(exeElement));
if (exeElement.getKind() == ElementKind.METHOD) {
result.setOverridden(extractOverriddenUid(utils.overriddenMethod(exeElement)));
result.setSummary(getInheritedInlineCommentString(exeElement));
}
}
result.setNameWithType(String.format("%s.%s", classSNameWithGenericsSupport, result.getName()));
result.setFullName(String.format("%s.%s", classQNameWithGenericsSupport, result.getName()));
Expand All @@ -83,11 +96,11 @@ List<MethodParameter> extractParameters(ExecutableElement element) {

String extractParameterDescription(ExecutableElement method, String paramName) {
return getDocCommentTree(method).map(docTree -> docTree.getBlockTags().stream()
.filter(o -> o.getKind() == Kind.PARAM)
.map(o -> (ParamTree) o)
.filter(o -> paramName.equals(String.valueOf(o.getName())))
.map(o -> replaceLinksAndCodes(o.getDescription()))
.findFirst().orElse(null)
.filter(o -> o.getKind() == Kind.PARAM)
.map(o -> (ParamTree) o)
.filter(o -> paramName.equals(String.valueOf(o.getName())))
.map(o -> replaceLinksAndCodes(o.getDescription()))
.findFirst().orElse(null)
).orElse(null);
}

Expand All @@ -100,10 +113,10 @@ List<ExceptionItem> extractExceptions(ExecutableElement methodElement) {

String extractExceptionDescription(ExecutableElement methodElement) {
return getDocCommentTree(methodElement).map(docTree -> docTree.getBlockTags().stream()
.filter(o -> o.getKind() == Kind.THROWS)
.map(o -> (ThrowsTree) o)
.map(o -> replaceLinksAndCodes(o.getDescription()))
.findFirst().orElse(null)
.filter(o -> o.getKind() == Kind.THROWS)
.map(o -> (ThrowsTree) o)
.map(o -> replaceLinksAndCodes(o.getDescription()))
.findFirst().orElse(null)
).orElse(null);
}

Expand All @@ -116,10 +129,10 @@ Return extractReturn(ExecutableElement methodElement) {

String extractReturnDescription(ExecutableElement methodElement) {
return getDocCommentTree(methodElement).map(docTree -> docTree.getBlockTags().stream()
.filter(o -> o.getKind() == Kind.RETURN)
.map(o -> (ReturnTree)o)
.map(o -> replaceLinksAndCodes(o.getDescription()))
.findFirst().orElse(null)
.filter(o -> o.getKind() == Kind.RETURN)
.map(o -> (ReturnTree) o)
.map(o -> replaceLinksAndCodes(o.getDescription()))
.findFirst().orElse(null)
).orElse(null);
}

Expand All @@ -130,4 +143,56 @@ Return extractReturn(VariableElement fieldElement) {
String convertFullNameToOverload(String fullName) {
return fullName.replaceAll("\\(.*\\)", "*");
}

String extractOverriddenUid(ExecutableElement ovr) {
if (ovr != null) {
TypeElement te = utils.getEnclosingTypeElement(ovr);
String uid = te.getQualifiedName().toString().concat(".") + String.valueOf(ovr);
return uid;
}

return "";
}

/**
* If the item being inherited from is declared from external compiled package,
* or is declared in the packages like java.lang.Object,
* comments may be not available as doclet resolves from byte code.
*/
String getInheritedInlineCommentString(ExecutableElement exeElement) {
CommentHelper ch = getInheritedInlineTags(new CommentHelper(exeElement, utils));
// Remove unresolved "@inheritDoc" tag.
List<? extends DocTree> dctree = utils.removeBlockTag(ch.inlineTags, DocTree.Kind.INHERIT_DOC);
return replaceLinksAndCodes(dctree);
}

CommentHelper getInheritedInlineTags(CommentHelper input) {
CommentHelper output = input.copy();
if (!output.hasInheritDocTag()&& !output.isSimpleOverride()) {
return output;
}

CommentHelper inheritedSearchInput = input.copy();
ExecutableElement overriddenMethod = utils.overriddenMethod((ExecutableElement) input.element);

if (overriddenMethod != null) {
inheritedSearchInput.element = overriddenMethod;
CommentHelper ch = getInheritedInlineTags(inheritedSearchInput);
if (!ch.isSimpleOverride()) {
output = output.inherit(ch);
}
}

TypeElement encl = utils.getEnclosingTypeElement(input.element);
List<Element> implementedMethods = utils.getImplementedMethods(input.element.toString(), encl, new ArrayList<Element>());
for (Element implementedMethod : implementedMethods) {
inheritedSearchInput.element = implementedMethod;
CommentHelper ch = getInheritedInlineTags(inheritedSearchInput);
if (!ch.isSimpleOverride()) {
output = output.inherit(ch);
}
}

return output;
}
}
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
11 changes: 10 additions & 1 deletion src/main/java/com/microsoft/model/MetadataFileItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import org.apache.commons.lang3.RegExUtils;

@JsonPropertyOrder({"uid", "id", "parent", "children", "href", "langs", "isExternal", "name", "nameWithType",
"fullName", "overload", "type", "package", "summary", "syntax", "inheritance", "implements", "exceptions",
"fullName", "overload", "overridden", "type", "package", "summary", "syntax", "inheritance", "implements", "exceptions",
"spec.java", "inheritedMembers"})
public class MetadataFileItem implements Comparable<MetadataFileItem> {

Expand All @@ -25,6 +25,7 @@ public class MetadataFileItem implements Comparable<MetadataFileItem> {
private String nameWithType;
private String fullName;
private String overload;
private String overridden;
private String type;
@JsonProperty("package")
private String packageName;
Expand Down Expand Up @@ -232,6 +233,14 @@ public void setReturn(Return returnValue) {
syntax.setReturnValue(returnValue);
}

public void setOverridden(String overridden) {
this.overridden = overridden;
}

public String getOverridden() {
return overridden;
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand Down
Loading

0 comments on commit efc79d0

Please sign in to comment.