Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ private void addClassInfo(TypeElement classElement, MetadataFile classMetadataFi
}

void addConstructorsInfo(TypeElement classElement, MetadataFile classMetadataFile) {
for (ExecutableElement constructorElement : ElementFilter.constructorsIn(classElement.getEnclosedElements())) {
for (ExecutableElement constructorElement : ElementFilter.constructorsIn(elementUtil.getEnclosedElements(classElement))) {
MetadataFileItem constructorItem = buildMetadataFileItem(constructorElement);
constructorItem.setOverload(classItemsLookup.extractOverload(constructorElement));
constructorItem.setContent(classItemsLookup.extractConstructorContent(constructorElement));
Expand All @@ -115,7 +115,7 @@ void addConstructorsInfo(TypeElement classElement, MetadataFile classMetadataFil
}

private void addMethodsInfo(TypeElement classElement, MetadataFile classMetadataFile) {
ElementFilter.methodsIn(classElement.getEnclosedElements()).stream()
ElementFilter.methodsIn(elementUtil.getEnclosedElements(classElement)).stream()
.filter(methodElement -> !Utils.isPrivateOrPackagePrivate(methodElement))
.forEach(methodElement -> {
MetadataFileItem methodItem = buildMetadataFileItem(methodElement);
Expand All @@ -135,7 +135,7 @@ private void addMethodsInfo(TypeElement classElement, MetadataFile classMetadata
}

private void addFieldsInfo(TypeElement classElement, MetadataFile classMetadataFile) {
ElementFilter.fieldsIn(classElement.getEnclosedElements()).stream()
ElementFilter.fieldsIn(elementUtil.getEnclosedElements(classElement)).stream()
.filter(fieldElement -> !Utils.isPrivateOrPackagePrivate(fieldElement))
.forEach(fieldElement -> {
MetadataFileItem fieldItem = buildMetadataFileItem(fieldElement);
Expand Down Expand Up @@ -177,7 +177,7 @@ private void collect(TypeElement classElement, List<String> children,
Function<Iterable<? extends Element>, List<? extends Element>> selectFunc,
Function<? super Element, String> mapFunc) {

List<? extends Element> elements = selectFunc.apply(classElement.getEnclosedElements());
List<? extends Element> elements = selectFunc.apply(elementUtil.getEnclosedElements(classElement));
children.addAll(filterPrivateElements(elements).stream()
.map(mapFunc).collect(Collectors.toList()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,17 @@

public class Lookup {

private Map<String, String> globalLookup = new HashMap<>();
private Map<String, Map<String, String>> localLookupByFileName = new HashMap<>();
private static final int INITIAL_CAPACITY = 10000;
Copy link
Copy Markdown
Member Author

@lqiu96 lqiu96 Jul 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Set large initial map capacity to try to reduce the number of copy/ increases needed.

private final Map<String, String> globalLookup;
private final Map<String, Map<String, String>> localLookupByFileName;

private final String UID_PACKAGE_NAME_REGEXP = "^.*?\\.(?=[A-Z].*)";
private final String PARAM_PACKAGE_NAME_REGEXP = "(?<=[\\( ]).*?(?=[A-Z].*)";
private final String METHOD_PARAMS_REGEXP = "\\s[^\\s]+?(?=[,)])";

public Lookup(List<MetadataFile> packageMetadataFiles, List<MetadataFile> classMetadataFiles) {
this.globalLookup = new HashMap<>(INITIAL_CAPACITY);
this.localLookupByFileName = new HashMap<>(INITIAL_CAPACITY);
consume(packageMetadataFiles);
consume(classMetadataFiles);
}
Expand Down Expand Up @@ -96,4 +99,4 @@ private void consume(List<MetadataFile> metadataFiles) {
localLookupByFileName.put(file.getFileNameWithPath(), map);
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ public YmlFilesBuilder(DocletEnvironment environment, String outputPath,
this.projectName = projectName;
this.disableChangelog = disableChangelog;
this.projectBuilder = new ProjectBuilder(projectName);
ClassLookup classLookup = new ClassLookup(environment);
ClassLookup classLookup = new ClassLookup(environment, elementUtil);
this.referenceBuilder = new ReferenceBuilder(environment, classLookup, elementUtil);
this.packageBuilder = new PackageBuilder(packageLookup, outputPath, referenceBuilder);
this.classBuilder = new ClassBuilder(elementUtil, classLookup, new ClassItemsLookup(environment), outputPath, referenceBuilder);
this.classBuilder = new ClassBuilder(elementUtil, classLookup, new ClassItemsLookup(environment, elementUtil), outputPath, referenceBuilder);
}

public boolean build() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.sun.source.doctree.LinkTree;
import com.sun.source.doctree.LiteralTree;
import com.sun.source.doctree.SeeTree;
import java.util.concurrent.ConcurrentHashMap;
import jdk.javadoc.doclet.DocletEnvironment;
import org.apache.commons.lang3.RegExUtils;
import org.apache.commons.lang3.StringUtils;
Expand All @@ -32,6 +33,7 @@

public abstract class BaseLookup<T extends Element> {

private static final int INITIAL_CAPACITY = 500000;
Copy link
Copy Markdown
Member Author

@lqiu96 lqiu96 Jul 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Set large initial map capacity to try to reduce the number of copy/ increases needed. This is for ClassLookup, ClassItemsLookup, and PackageLookup. Maybe I can allow configuration of this capacity for the different lookups. ClassLookup (mid size), ClassItemsLookup (large), PackageLookup (small)

protected final Map<ElementKind, String> elementKindLookup = new HashMap<>() {{
put(ElementKind.PACKAGE, "Namespace");
put(ElementKind.CLASS, "Class");
Expand All @@ -44,20 +46,17 @@ public abstract class BaseLookup<T extends Element> {
put(ElementKind.FIELD, "Field");
}};

protected Map<T, ExtendedMetadataFileItem> map = new HashMap<>();
protected Map<T, ExtendedMetadataFileItem> map;
protected final DocletEnvironment environment;

protected BaseLookup(DocletEnvironment environment) {
this.environment = environment;
this.map = new HashMap<>(INITIAL_CAPACITY);
}

protected ExtendedMetadataFileItem resolve(T key) {
ExtendedMetadataFileItem value = map.get(key);
if (value == null) {
value = buildMetadataFileItem(key);
map.put(key, value);
}
return value;
map.computeIfAbsent(key, this::buildMetadataFileItem);
return map.get(key);
}

protected abstract ExtendedMetadataFileItem buildMetadataFileItem(T key);
Expand Down Expand Up @@ -172,9 +171,9 @@ public String extractStatus(T element) {
Optional<DocCommentTree> docCommentTree = getDocCommentTree(element);
if (docCommentTree.isPresent()) {
boolean isDeprecated = docCommentTree.get().getBlockTags().stream()
.filter(docTree -> docTree.getKind().equals(DocTree.Kind.DEPRECATED))
.findFirst()
.isPresent();
.filter(docTree -> docTree.getKind().equals(DocTree.Kind.DEPRECATED))
.findFirst()
.isPresent();
if (isDeprecated) {
return DocTree.Kind.DEPRECATED.name().toLowerCase();
}
Expand All @@ -194,9 +193,9 @@ protected String determineComment(T element) {
Optional<DocCommentTree> docCommentTree = getDocCommentTree(element);
if (docCommentTree.isPresent()) {
String comment = docCommentTree
.map(DocCommentTree::getFullBody)
.map(this::replaceLinksAndCodes)
.orElse(null);
.map(DocCommentTree::getFullBody)
.map(this::replaceLinksAndCodes)
.orElse(null);
return replaceBlockTags(docCommentTree.get(), comment);
}
return null;
Expand Down Expand Up @@ -233,19 +232,19 @@ String replaceBlockTags(DocCommentTree docCommentTree, String comment) {
*/
String replaceLinksAndCodes(List<? extends DocTree> items) {
return YamlUtil.cleanupHtml(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 @@ -281,8 +280,8 @@ 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("<"));
}

private String getSeeAlsoSummary(Set<String> seeItems) {
Expand All @@ -291,16 +290,16 @@ private String getSeeAlsoSummary(Set<String> seeItems) {

private String getDeprecatedSummary(DeprecatedTree deprecatedTree) {
return String.format("\n<strong>Deprecated.</strong> <em>%s</em>\n\n",
replaceLinksAndCodes(deprecatedTree.getBody()));
replaceLinksAndCodes(deprecatedTree.getBody()));
}

private String getSeeTagRef(SeeTree seeTree) {
String ref = seeTree.getReference().stream()
.map(r -> String.valueOf(r)).collect(Collectors.joining(""));
.map(r -> String.valueOf(r)).collect(Collectors.joining(""));
// if it's already a tag, use that otherwise build xref tag
if (ref.matches("^<.+>(.|\n)*")) {
return ref.replaceAll("\n", "").replaceAll("( )+", " ");
}
return String.format("<xref uid=\"%1$s\" data-throw-if-not-resolved=\"false\">%1$s</xref>", ref);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.microsoft.model.MethodParameter;
import com.microsoft.model.Return;
import com.microsoft.util.CommentHelper;
import com.microsoft.util.ElementUtil;
import com.microsoft.util.Utils;
import com.sun.source.doctree.DocCommentTree;
import com.sun.source.doctree.DocTree;
Expand All @@ -29,9 +30,9 @@
public class ClassItemsLookup extends BaseLookup<Element> {
private Utils utils;

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

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
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 java.util.Collections;
import jdk.javadoc.doclet.DocletEnvironment;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
Expand All @@ -25,8 +27,11 @@ public class ClassLookup extends BaseLookup<TypeElement> {

private static final String JAVA_LANG_OBJECT = "java.lang.Object";

public ClassLookup(DocletEnvironment environment) {
private final ElementUtil elementUtil;

public ClassLookup(DocletEnvironment environment, ElementUtil elementUtil) {
super(environment);
this.elementUtil = elementUtil;
}

@Override
Expand Down Expand Up @@ -59,14 +64,14 @@ protected ExtendedMetadataFileItem buildMetadataFileItem(TypeElement classElemen
}

void populateContent(TypeElement classElement, String shortNameWithGenericsSupport,
ExtendedMetadataFileItem container) {
ExtendedMetadataFileItem container) {
String type = elementKindLookup.get(classElement.getKind());
String result = String.format("%s %s %s",
classElement.getModifiers().stream().map(String::valueOf)
.filter(modifier -> !("Interface".equals(type) && "abstract".equals(modifier)))
.filter(modifier -> !("Enum".equals(type) && ("static".equals(modifier) || "final".equals(modifier))))
.collect(Collectors.joining(" ")),
StringUtils.lowerCase(type), shortNameWithGenericsSupport);
classElement.getModifiers().stream().map(String::valueOf)
.filter(modifier -> !("Interface".equals(type) && "abstract".equals(modifier)))
.filter(modifier -> !("Enum".equals(type) && ("static".equals(modifier) || "final".equals(modifier))))
.collect(Collectors.joining(" ")),
StringUtils.lowerCase(type), shortNameWithGenericsSupport);

String superclass = determineSuperclass(classElement);
if (superclass != null && !JAVA_LANG_OBJECT.equals(superclass)) {
Expand All @@ -79,11 +84,11 @@ void populateContent(TypeElement classElement, String shortNameWithGenericsSuppo
if (CollectionUtils.isNotEmpty(interfaces)) {
String prefix = (classElement.getKind() == ElementKind.INTERFACE) ? " extends " : " implements ";
result += prefix + interfaces.stream().map(String::valueOf).map(this::makeTypeShort)
.collect(Collectors.joining(", "));
.collect(Collectors.joining(", "));

container.setInterfaces(interfaces.stream()
.map(String::valueOf)
.collect(Collectors.toList()));
.map(String::valueOf)
.collect(Collectors.toList()));

addInterfacesToReferencesMap(interfaces, container);

Expand All @@ -98,16 +103,16 @@ void addSuperclassToReferencesMap(String superclass, ExtendedMetadataFileItem co

void addInheritedMethodsToReferencesMap(ExtendedMetadataFileItem container) {
container.addReferences(container.getInheritedMethods().stream()
.map(o -> new MetadataFileItem(o, makeTypeShort(o), false))
.collect(Collectors.toSet())
.map(o -> new MetadataFileItem(o, makeTypeShort(o), false))
.collect(Collectors.toSet())
);
}

void addInterfacesToReferencesMap(List<? extends TypeMirror> interfaces, ExtendedMetadataFileItem container) {
container.addReferences(interfaces.stream()
.map(String::valueOf)
.map(o -> new MetadataFileItem(o, makeTypeShort(o), false))
.collect(Collectors.toSet())
.map(String::valueOf)
.map(o -> new MetadataFileItem(o, makeTypeShort(o), false))
.collect(Collectors.toSet())
);
}

Expand Down Expand Up @@ -141,14 +146,14 @@ List<String> determineNestedSuperclass(TypeElement classElement, ExtendedMetadat

List<TypeParameter> determineTypeParameters(TypeElement element) {
return element.getTypeParameters().stream()
.map(typeParameter -> new TypeParameter(String.valueOf(typeParameter)))
.collect(Collectors.toList());
.map(typeParameter -> new TypeParameter(String.valueOf(typeParameter)))
.collect(Collectors.toList());
}

void appendInheritedMethods(TypeElement element, List<ExtendedMetadataFileItem> inheritedMethods) {
List<? extends Element> members = element.getEnclosedElements();
List<? extends Element> members = elementUtil.getEnclosedElements(element);
Integer level = Optional.ofNullable(getMaxNestedLevel(inheritedMethods))
.orElse(0);
.orElse(0);

for (Element m : members) {
if (m.getKind() == ElementKind.METHOD && !Utils.isPrivateOrPackagePrivate(m)) {
Expand All @@ -168,9 +173,9 @@ Integer getMaxNestedLevel(List<ExtendedMetadataFileItem> inheritedMethods) {

if (inheritedMethods.size() > 0) {
level = inheritedMethods
.stream()
.mapToInt(v -> v.getNestedLevel())
.max().orElseThrow(NoSuchElementException::new);
.stream()
.mapToInt(v -> v.getNestedLevel())
.max().orElseThrow(NoSuchElementException::new);
}
return level;
}
Expand All @@ -191,9 +196,9 @@ List<String> determineInheritedMembers(List<ExtendedMetadataFileItem> inheritedM
}
}
List<String> methods = map.values()
.stream()
.map(x -> x.getUid())
.collect(Collectors.toList());
.stream()
.map(x -> x.getUid())
.collect(Collectors.toList());

return methods;
}
Expand All @@ -212,4 +217,4 @@ public String extractJavaType(TypeElement element) {
}
return null;
}
}
}
Loading