Skip to content

Commit 3238f9d

Browse files
committed
Fix parsing of generic parameters
providing null checks. Signed-off-by: Manoel Campos <manoelcampos@gmail.com>
1 parent 95b8e3a commit 3238f9d

File tree

2 files changed

+23
-23
lines changed

2 files changed

+23
-23
lines changed

src/main/java/com/github/markusbernhardt/xmldoclet/Parser.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@
99
import jdk.javadoc.doclet.DocletEnvironment;
1010

1111
import javax.lang.model.element.*;
12+
import javax.lang.model.type.DeclaredType;
1213
import javax.lang.model.type.TypeMirror;
1314
import javax.lang.model.type.TypeVariable;
1415
import javax.lang.model.type.WildcardType;
1516
import javax.lang.model.util.ElementFilter;
16-
import java.lang.reflect.ParameterizedType;
17-
import java.lang.reflect.Type;
1817
import java.util.List;
1918
import java.util.Map;
2019
import java.util.Set;
@@ -558,11 +557,11 @@ protected TypeInfo parseTypeInfo(final TypeMirror type) {
558557
typeInfoNode.setWildcard(parseWildcard(wildcard));
559558
}
560559

561-
final ParameterizedType parameterized = getParameterizedType(type);
560+
final DeclaredType parameterized = getParameterizedType(type);
562561

563562
if (parameterized != null) {
564-
for (final Type typeArgument : parameterized.getActualTypeArguments()) {
565-
typeInfoNode.getGeneric().add(parseTypeInfo(typeUtils.getTypeMirror(typeArgument)));
563+
for (final TypeMirror typeArgument : parameterized.getTypeArguments()) {
564+
typeInfoNode.getGeneric().add(parseTypeInfo(typeArgument));
566565
}
567566
}
568567

@@ -572,15 +571,17 @@ protected TypeInfo parseTypeInfo(final TypeMirror type) {
572571
protected Wildcard parseWildcard(final WildcardType wildcard) {
573572
final Wildcard wildcardNode = objectFactory.createWildcard();
574573

575-
final TypeMirror extendType = wildcard.getExtendsBound();
576-
wildcardNode.getExtendsBound().add(parseTypeInfo(extendType));
577-
578-
final TypeMirror superType = wildcard.getSuperBound();
579-
wildcardNode.getSuperBound().add(parseTypeInfo(superType));
574+
addIfNotNull(wildcardNode.getExtendsBound(), wildcard.getExtendsBound());
575+
addIfNotNull(wildcardNode.getSuperBound(), wildcard.getSuperBound());
580576

581577
return wildcardNode;
582578
}
583579

580+
private void addIfNotNull(final List<TypeInfo> wildcardNode, final TypeMirror extendType) {
581+
if (extendType != null)
582+
wildcardNode.add(parseTypeInfo(extendType));
583+
}
584+
584585
protected TypeParameter parseTypeParameter(final TypeParameterElement typeParameter) {
585586
return parseTypeParameter((TypeVariable) typeParameter.asType());
586587
}

src/main/java/com/github/markusbernhardt/xmldoclet/TypeUtils.java

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import javax.lang.model.util.ElementFilter;
66
import javax.lang.model.util.Elements;
77
import javax.lang.model.util.Types;
8-
import java.lang.reflect.ParameterizedType;
98
import java.lang.reflect.Type;
109
import java.util.List;
1110

@@ -27,7 +26,7 @@ public static String getMethodSignature(final ExecutableElement methodDoc) {
2726

2827
/**
2928
* Checks if an element has a given modifier
30-
*
29+
*
3130
* @param element the element to check
3231
* @param modifier the modifier we are looking for in the element
3332
* @return true if the modifier is present in the element declaration, false otherwise
@@ -38,7 +37,7 @@ public static boolean hasModifier(final Element element, final Modifier modifier
3837

3938
/**
4039
* {@return the list of fields from a given class element}
41-
*
40+
*
4241
* @param classElement the class to get its fields
4342
*/
4443
public static List<VariableElement> getFields(final TypeElement classElement) {
@@ -47,7 +46,7 @@ public static List<VariableElement> getFields(final TypeElement classElement) {
4746

4847
/**
4948
* {@return the list of constructors from a given class element}
50-
*
49+
*
5150
* @param classElement the class to get its constructors
5251
*/
5352
public static List<ExecutableElement> getConstructors(final TypeElement classElement) {
@@ -56,7 +55,7 @@ public static List<ExecutableElement> getConstructors(final TypeElement classEle
5655

5756
/**
5857
* {@return the list of methods from a given class element}
59-
*
58+
*
6059
* @param classElement the class to get its methods
6160
*/
6261
public static List<ExecutableElement> getMethods(final TypeElement classElement) {
@@ -65,7 +64,7 @@ public static List<ExecutableElement> getMethods(final TypeElement classElement)
6564

6665
/**
6766
* {@return a type as WildcardType if it is such a type, or null otherwise}
68-
*
67+
*
6968
* @param typeMirror the type to get it as a wildcard type
7069
*/
7170
public static WildcardType getWildcardType(final TypeMirror typeMirror) {
@@ -77,14 +76,14 @@ public static WildcardType getWildcardType(final TypeMirror typeMirror) {
7776
}
7877

7978
/**
80-
* {@return a type as ParameterizedType if it is such a type, or null otherwise}
81-
*
79+
* {@return a type as DeclaredType if the typeMirror has type arguments (such a List<String>), or null otherwise}
80+
*
8281
* @param typeMirror the type to get it as a wildcard type
8382
*/
84-
public static ParameterizedType getParameterizedType(TypeMirror typeMirror) {
83+
public static DeclaredType getParameterizedType(TypeMirror typeMirror) {
8584
if (typeMirror instanceof DeclaredType declaredType) {
86-
if (!declaredType.getTypeArguments().isEmpty() && declaredType instanceof ParameterizedType) {
87-
return (ParameterizedType) declaredType;
85+
if (!declaredType.getTypeArguments().isEmpty()) {
86+
return declaredType;
8887
}
8988
}
9089

@@ -97,7 +96,7 @@ public static boolean isArray(final TypeMirror typeMirror) {
9796

9897
/**
9998
* {@return the dimension of type that represents an array, or an empty string if the type is not an array}
100-
*
99+
*
101100
* @param typeMirror the array type to get its dimension
102101
*/
103102
public static String getArrayDimension(final TypeMirror typeMirror) {
@@ -112,7 +111,7 @@ public static String getArrayDimension(final TypeMirror typeMirror) {
112111

113112
/**
114113
* {@return a TypeMirror for a given Type instance}
115-
*
114+
*
116115
* @param type the {@link Type} instance to get a {@link TypeMirror}
117116
*/
118117
public TypeMirror getTypeMirror(final Type type) {

0 commit comments

Comments
 (0)