make some fixes after run on bigger project

This commit is contained in:
Laurent SCHOELENS 2023-03-27 12:18:46 +02:00
parent d273b6a3ef
commit 70929da1d0
4 changed files with 54 additions and 19 deletions

View File

@ -57,8 +57,6 @@ import javax.lang.model.type.DeclaredType;
import javax.lang.model.type.NoType; import javax.lang.model.type.NoType;
import javax.lang.model.type.TypeKind; import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror; import javax.lang.model.type.TypeMirror;
import javax.lang.model.type.TypeVariable;
import javax.lang.model.type.WildcardType;
import javax.lang.model.util.Elements; import javax.lang.model.util.Elements;
import javax.lang.model.util.Types; import javax.lang.model.util.Types;
import javax.tools.JavaFileManager; import javax.tools.JavaFileManager;
@ -810,7 +808,9 @@ class ClassGraph {
for (TypeMirror type : types) { for (TypeMirror type : types) {
// skip primitives and type variables, as well as dependencies // skip primitives and type variables, as well as dependencies
// on the source class // on the source class
if (type.getKind().isPrimitive() || type instanceof NoType || type instanceof WildcardType || type instanceof TypeVariable if (type.getKind().isPrimitive()
|| ElementUtil.isType(type, TypeKind.TYPEVAR) || ElementUtil.isType(type, TypeKind.WILDCARD)
|| type instanceof NoType
|| c.toString().equals(ElementUtil.getTypeElement(type).toString())) { || c.toString().equals(ElementUtil.getTypeElement(type).toString())) {
continue; continue;
} }
@ -856,10 +856,10 @@ class ClassGraph {
private FieldRelationInfo getFieldRelationInfo(VariableElement field) { private FieldRelationInfo getFieldRelationInfo(VariableElement field) {
TypeMirror type = field.asType(); TypeMirror type = field.asType();
if (type.getKind().isPrimitive() || type.getKind() == TypeKind.WILDCARD || type.getKind() == TypeKind.TYPEVAR) { if (type.getKind().isPrimitive() || ElementUtil.isType(type, TypeKind.WILDCARD) || ElementUtil.isType(type, TypeKind.TYPEVAR)) {
return null; return null;
} }
if (ElementUtil.dimensions(type).endsWith("[]")) { if (ElementUtil.dimensions(type).endsWith("[]")) {
return new FieldRelationInfo(ElementUtil.getTypeElement(type), true); return new FieldRelationInfo(ElementUtil.getTypeElement(type), true);
} }
@ -867,35 +867,49 @@ class ClassGraph {
Options opt = optionProvider.getOptionsFor(docTrees, ElementUtil.getTypeElement(type)); Options opt = optionProvider.getOptionsFor(docTrees, ElementUtil.getTypeElement(type));
if (opt.matchesCollPackageExpression(ElementUtil.getQualifiedName(types, type))) { if (opt.matchesCollPackageExpression(ElementUtil.getQualifiedName(types, type))) {
List<? extends TypeMirror> argTypes = getInterfaceTypeArguments(collectionClassDoc, type); List<? extends TypeMirror> argTypes = getInterfaceTypeArguments(collectionClassDoc, type);
if (argTypes != null && argTypes.size() == 1 && !argTypes.get(0).getKind().isPrimitive()) { if (argTypes != null && argTypes.size() == 1 && !argTypes.get(0).getKind().isPrimitive() && !ElementUtil.isType(argTypes.get(0), TypeKind.TYPEVAR)) {
return new FieldRelationInfo(ElementUtil.getTypeElement(argTypes.get(0)), true); TypeMirror arg = argTypes.get(0);
return new FieldRelationInfo(ElementUtil.getTypeElement(arg), true);
} }
argTypes = getInterfaceTypeArguments(mapClassDoc, type); argTypes = getInterfaceTypeArguments(mapClassDoc, type);
if (argTypes != null && argTypes.size() == 2 && !argTypes.get(1).getKind().isPrimitive()) { if (argTypes != null && argTypes.size() == 2 && !argTypes.get(1).getKind().isPrimitive() && !ElementUtil.isType(argTypes.get(1), TypeKind.TYPEVAR)) {
return new FieldRelationInfo(ElementUtil.getTypeElement(argTypes.get(1)), true); TypeMirror arg = argTypes.get(1);
return new FieldRelationInfo(ElementUtil.getTypeElement(arg), true);
} }
} }
return new FieldRelationInfo(ElementUtil.getTypeElement(type), false); return new FieldRelationInfo(ElementUtil.getTypeElement(type), false);
} }
private List<? extends TypeMirror> getInterfaceTypeArguments(TypeElement iface, TypeMirror t) { private List<? extends TypeMirror> getInterfaceTypeArguments(Element iface, TypeMirror t) {
if (t instanceof DeclaredType) { if (t instanceof DeclaredType) {
DeclaredType pt = (DeclaredType) t; DeclaredType pt = (DeclaredType) t;
if (iface != null && iface.equals(pt.asElement())) { if (iface != null && iface.equals(pt.asElement())) {
return pt.getTypeArguments(); return pt.getTypeArguments();
} else { } else {
for (TypeMirror pti : ElementUtil.getInterfacesTypes(iface)) { for (TypeMirror pti : ElementUtil.getInterfacesTypes(pt.asElement())) {
List<? extends TypeMirror> result = getInterfaceTypeArguments(iface, pti); List<? extends TypeMirror> result = getInterfaceTypeArguments(iface, pti);
if (result != null) { if (result != null) {
return result; return result;
} }
} }
if (ElementUtil.getSuperclassType(pt) != null) { TypeMirror superType = ElementUtil.getSuperclassType(pt);
return getInterfaceTypeArguments(iface, ElementUtil.getSuperclassType(pt)); if (superType != null && superType.getKind() != TypeKind.NONE) {
return getInterfaceTypeArguments(iface, superType);
} }
} }
} else if (iface instanceof TypeElement) {
for (TypeMirror pti : ElementUtil.getInterfacesTypes((TypeElement) iface)) {
List<? extends TypeMirror> result = getInterfaceTypeArguments(iface, pti);
if (result != null) {
return result;
}
}
TypeElement superType = ElementUtil.getSuperclass((TypeElement) iface);
if (superType != null && superType.asType().getKind() != TypeKind.NONE) {
return getInterfaceTypeArguments(iface, superType.asType());
}
} }
return null; return null;
} }

View File

@ -1161,8 +1161,9 @@ public class Options implements Cloneable, OptionProvider {
public boolean matchesCollPackageExpression(CharSequence s) { public boolean matchesCollPackageExpression(CharSequence s) {
for (Pattern collPattern : collPackages) { for (Pattern collPattern : collPackages) {
Matcher m = collPattern.matcher(s); Matcher m = collPattern.matcher(s);
if (strictMatching ? m.matches() : m.find()) if (strictMatching ? m.matches() : m.find()) {
return true; return true;
}
} }
return false; return false;
} }

View File

@ -129,7 +129,7 @@ public class UmlGraphDoc implements Doclet {
UmlGraph.buildGraph(reporter, root, opt, view, packageDoc); UmlGraph.buildGraph(reporter, root, opt, view, packageDoc);
runGraphviz(opt.dotExecutable, outputFolder, ElementUtil.getModuleOf(root, packageDoc), packageDoc.getQualifiedName(), packageDoc.getSimpleName(), reporter); runGraphviz(opt.dotExecutable, outputFolder, ElementUtil.getModuleOf(root, packageDoc), packageDoc.getQualifiedName(), packageDoc.getSimpleName(), reporter);
alterHtmlDocs(opt, outputFolder, ElementUtil.getModuleOf(root, packageDoc), packageDoc.getQualifiedName(), packageDoc.getSimpleName(), "package-summary.html", alterHtmlDocs(opt, outputFolder, ElementUtil.getModuleOf(root, packageDoc), packageDoc.getQualifiedName(), packageDoc.getSimpleName(), "package-summary.html",
Pattern.compile("(</[Hh]2>)|(<h1 title=\"Package\").*"), reporter); Pattern.compile("(</[Hh]2>)|(<h1 title=\"Package( |\")).*"), reporter);
} }
} }
} }

View File

@ -1,5 +1,6 @@
package org.umlgraph.doclet.util; package org.umlgraph.doclet.util;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -21,6 +22,16 @@ import javax.lang.model.util.Types;
import jdk.javadoc.doclet.DocletEnvironment; import jdk.javadoc.doclet.DocletEnvironment;
public class ElementUtil { public class ElementUtil {
public static boolean isType(TypeMirror type, TypeKind kind) {
if (type.getKind() == kind) {
return true;
}
if (type.getKind() == TypeKind.ARRAY) {
return isType(((ArrayType) type).getComponentType(), kind);
}
return false;
}
public static CharSequence getSimpleName(Types types, TypeMirror t) { public static CharSequence getSimpleName(Types types, TypeMirror t) {
if (t instanceof ArrayType) { if (t instanceof ArrayType) {
@ -71,13 +82,19 @@ public class ElementUtil {
return null; return null;
} }
if (clazz instanceof ArrayType) { if (clazz instanceof ArrayType) {
return getTypeElement(((ArrayType) clazz).getComponentType()); TypeMirror componentType = ((ArrayType) clazz).getComponentType();
return getTypeElement(componentType);
} }
if (!(clazz instanceof DeclaredType)) { if (!(clazz instanceof DeclaredType)) {
System.err.println("getTypeElement will return null for type class : " + clazz + " instanceof " + clazz.getClass() + " and kind " + clazz.getKind());
return null; return null;
} }
Element scd = ((DeclaredType) clazz).asElement(); Element scd = ((DeclaredType) clazz).asElement();
return scd instanceof TypeElement ? (TypeElement) scd : null; if (!(scd instanceof TypeElement)) {
System.err.println("getTypeElement will return null for element class : " + scd == null ? "null" : scd.getClass());
return null;
}
return (TypeElement) scd;
} }
public static TypeMirror getSuperclassType(DeclaredType pt) { public static TypeMirror getSuperclassType(DeclaredType pt) {
@ -88,8 +105,11 @@ public class ElementUtil {
return getTypeElement(element.getSuperclass()); return getTypeElement(element.getSuperclass());
} }
public static List<? extends TypeMirror> getInterfacesTypes(TypeElement element) { public static List<? extends TypeMirror> getInterfacesTypes(Element element) {
return element.getInterfaces(); if (element instanceof TypeElement) {
return ((TypeElement) element).getInterfaces();
}
return Collections.emptyList();
} }
public static List<TypeElement> getInterfaces(TypeElement element) { public static List<TypeElement> getInterfaces(TypeElement element) {