Skip to content

Commit e66b3d2

Browse files
committed
Refactor and code format
Signed-off-by: Manoel Campos <manoelcampos@gmail.com>
1 parent cd7d1cd commit e66b3d2

File tree

4 files changed

+69
-74
lines changed

4 files changed

+69
-74
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.github.markusbernhardt.xmldoclet;
22

33
import jdk.javadoc.doclet.Doclet;
4+
import org.apache.commons.cli.Option;
45

56
import java.util.ArrayList;
67
import java.util.Collections;
@@ -21,15 +22,16 @@ public class CustomOption implements Doclet.Option {
2122
* @param cliOption Apache Commons CLI Option instance
2223
* @return the created {@link CustomOption}
2324
*/
24-
public static CustomOption of(org.apache.commons.cli.Option cliOption) {
25+
public static CustomOption of(final Option cliOption) {
2526
return new CustomOption(
2627
cliOption.getArgs(),
2728
cliOption.getDescription(),
2829
List.of(cliOption.getOpt()),
2930
cliOption.getArgName());
3031
}
3132

32-
public CustomOption(final int argumentCount, final String description,
33+
public CustomOption(
34+
final int argumentCount, final String description,
3335
final List<String> names, final String parameters) {
3436
this.argumentCount = argumentCount;
3537
this.description = description;
@@ -65,7 +67,7 @@ public String getParameters() {
6567

6668
/**
6769
* Gets the list of parameters as an array of strings.
68-
*
70+
*
6971
* @see #getParameters()
7072
*/
7173
public String[] getParameterArray() {

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

Lines changed: 41 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ private String getJavaDoc(final Element element) {
6767

6868
/**
6969
* {@return the tags inside a JavaDoc comment}
70-
*
70+
*
7171
* @param element the Java element to get its JavaDoc tags
7272
*/
7373
public List<? extends DocTree> getTags(final Element element) {
@@ -87,8 +87,7 @@ public Root parseRootDoc() {
8787
final Package packageNode = getPackage(rootNode, classDoc);
8888

8989
switch (classDoc.getKind()) {
90-
case ANNOTATION_TYPE ->
91-
packageNode.getAnnotation().add(parseAnnotationTypeDoc(classDoc));
90+
case ANNOTATION_TYPE -> packageNode.getAnnotation().add(parseAnnotationTypeDoc(classDoc));
9291
case ENUM -> packageNode.getEnum().add(parseEnum(classDoc));
9392
case INTERFACE -> packageNode.getInterface().add(parseInterface(classDoc));
9493
default -> packageNode.getClazz().add(parseClass(classDoc));
@@ -100,7 +99,7 @@ public Root parseRootDoc() {
10099

101100
/**
102101
* {@return the package node for the given class element}
103-
*
102+
*
104103
* @param rootNode
105104
* @param classElement class to get its package
106105
*/
@@ -140,7 +139,7 @@ protected Package parsePackage(final PackageElement packageDoc) {
140139
protected Annotation parseAnnotationTypeDoc(final TypeElement annotationTypeDoc) {
141140
final Annotation annotationNode = objectFactory.createAnnotation();
142141
annotationNode.setName(annotationTypeDoc.getSimpleName().toString());
143-
annotationNode.setQualified(annotationTypeDoc.getQualifiedName().toString());
142+
annotationNode.setQualified(getQualifiedName(annotationTypeDoc));
144143
final String comment = getJavaDoc(annotationTypeDoc);
145144
if (!comment.isEmpty()) {
146145
annotationNode.setComment(comment);
@@ -199,22 +198,20 @@ protected AnnotationElement parseAnnotationTypeElementDoc(
199198
*/
200199
protected AnnotationInstance parseAnnotationDesc(final AnnotationMirror annotationDesc,
201200
final Name programElement) {
202-
final AnnotationInstance annotationInstanceNode = objectFactory.createAnnotationInstance();
201+
final var annotationInstance = objectFactory.createAnnotationInstance();
203202

204203
try {
205204
final var annotTypeInfo = annotationDesc.getAnnotationType();
206-
annotationInstanceNode.setName(annotTypeInfo.asElement().getSimpleName().toString());
207-
annotationInstanceNode
208-
.setQualified(annotTypeInfo.asElement().getSimpleName().toString());
205+
annotationInstance.setName(annotTypeInfo.asElement().getSimpleName().toString());
206+
annotationInstance.setQualified(annotTypeInfo.asElement().getSimpleName().toString());
209207
} catch (ClassCastException castException) {
210208
LOGGER.severe(
211209
"Unable to obtain type data about an annotation found on: " + programElement);
212210
LOGGER.severe("Add to the classpath the class/jar that defines this annotation.");
213211
}
214212

215213
for (final var elementValuesPair : annotationDesc.getElementValues().entrySet()) {
216-
final AnnotationArgument annotationArgumentNode =
217-
objectFactory.createAnnotationArgument();
214+
final AnnotationArgument annotationArgumentNode = objectFactory.createAnnotationArgument();
218215
annotationArgumentNode.setName(elementValuesPair.getKey().getSimpleName().toString());
219216

220217
final TypeMirror annotationArgumentType = elementValuesPair.getKey().asType();
@@ -227,33 +224,37 @@ protected AnnotationInstance parseAnnotationDesc(final AnnotationMirror annotati
227224
case AnnotationValue[] annotationValues -> {
228225
for (final AnnotationValue annotationValue : annotationValues) {
229226
if (annotationValue.getValue() instanceof AnnotationMirror annoDesc) {
230-
annotationArgumentNode.getAnnotation()
231-
.add(parseAnnotationDesc(annoDesc, programElement));
227+
annotationArgumentNode.getAnnotation().add(parseAnnotationDesc(annoDesc, programElement));
232228
} else {
233-
annotationArgumentNode.getValue()
234-
.add(annotationValue.getValue().toString());
229+
annotationArgumentNode.getValue().add(annotationValue.getValue().toString());
235230
}
236231
}
237232
}
238-
case VariableElement fieldDoc ->
239-
annotationArgumentNode.getValue().add(fieldDoc.getSimpleName().toString());
240-
case TypeElement classDoc ->
241-
annotationArgumentNode.getValue().add(classDoc.getQualifiedName().toString());
233+
case VariableElement fieldDoc -> annotationArgumentNode.getValue().add(getSimpleName(fieldDoc));
234+
case TypeElement classDoc -> annotationArgumentNode.getValue().add(getQualifiedName(classDoc));
242235
case null -> {
243236
}
244237
default -> annotationArgumentNode.getValue().add(objValue.toString());
245238
}
246239

247-
annotationInstanceNode.getArgument().add(annotationArgumentNode);
240+
annotationInstance.getArgument().add(annotationArgumentNode);
248241
}
249242

250-
return annotationInstanceNode;
243+
return annotationInstance;
244+
}
245+
246+
private static String getQualifiedName(final TypeElement typeElement) {
247+
return typeElement.getQualifiedName().toString();
248+
}
249+
250+
private static String getSimpleName(final VariableElement element) {
251+
return element.getSimpleName().toString();
251252
}
252253

253254
protected Enum parseEnum(final TypeElement classDoc) {
254255
final Enum enumNode = objectFactory.createEnum();
255256
enumNode.setName(classDoc.getSimpleName().toString());
256-
enumNode.setQualified(classDoc.getQualifiedName().toString());
257+
enumNode.setQualified(getQualifiedName(classDoc));
257258
final String comment = getJavaDoc(classDoc);
258259
if (!comment.isEmpty()) {
259260
enumNode.setComment(comment);
@@ -278,8 +279,7 @@ protected Enum parseEnum(final TypeElement classDoc) {
278279
}
279280

280281
for (final AnnotationMirror annotationDesc : classDoc.getAnnotationMirrors()) {
281-
enumNode.getAnnotation()
282-
.add(parseAnnotationDesc(annotationDesc, classDoc.getQualifiedName()));
282+
enumNode.getAnnotation().add(parseAnnotationDesc(annotationDesc, classDoc.getQualifiedName()));
283283
}
284284

285285
for (final DocTree tag : getTags(classDoc)) {
@@ -297,15 +297,14 @@ protected Enum parseEnum(final TypeElement classDoc) {
297297
*/
298298
protected EnumConstant parseEnumConstant(final VariableElement fieldDoc) {
299299
final EnumConstant enumConstant = objectFactory.createEnumConstant();
300-
enumConstant.setName(fieldDoc.getSimpleName().toString());
300+
enumConstant.setName(getSimpleName(fieldDoc));
301301
final String comment = getJavaDoc(fieldDoc);
302302
if (!comment.isEmpty()) {
303303
enumConstant.setComment(comment);
304304
}
305305

306306
for (final AnnotationMirror annotationDesc : fieldDoc.getAnnotationMirrors()) {
307-
enumConstant.getAnnotation()
308-
.add(parseAnnotationDesc(annotationDesc, fieldDoc.getSimpleName()));
307+
enumConstant.getAnnotation().add(parseAnnotationDesc(annotationDesc, fieldDoc.getSimpleName()));
309308
}
310309

311310
for (final DocTree tag : getTags(fieldDoc)) {
@@ -318,7 +317,7 @@ protected EnumConstant parseEnumConstant(final VariableElement fieldDoc) {
318317
protected Interface parseInterface(final TypeElement classDoc) {
319318
final Interface interfaceNode = objectFactory.createInterface();
320319
interfaceNode.setName(classDoc.getSimpleName().toString());
321-
interfaceNode.setQualified(classDoc.getQualifiedName().toString());
320+
interfaceNode.setQualified(getQualifiedName(classDoc));
322321
final String comment = getJavaDoc(classDoc);
323322
if (!comment.isEmpty()) {
324323
interfaceNode.setComment(comment);
@@ -342,8 +341,7 @@ protected Interface parseInterface(final TypeElement classDoc) {
342341
}
343342

344343
for (final AnnotationMirror annotationDesc : classDoc.getAnnotationMirrors()) {
345-
interfaceNode.getAnnotation()
346-
.add(parseAnnotationDesc(annotationDesc, classDoc.getQualifiedName()));
344+
interfaceNode.getAnnotation().add(parseAnnotationDesc(annotationDesc, classDoc.getQualifiedName()));
347345
}
348346

349347
for (final DocTree tag : getTags(classDoc)) {
@@ -360,7 +358,7 @@ protected Interface parseInterface(final TypeElement classDoc) {
360358
protected Class parseClass(final TypeElement classDoc) {
361359
final Class classNode = objectFactory.createClass();
362360
classNode.setName(classDoc.getSimpleName().toString());
363-
classNode.setQualified(classDoc.getQualifiedName().toString());
361+
classNode.setQualified(getQualifiedName(classDoc));
364362
final String comment = getJavaDoc(classDoc);
365363
if (!comment.isEmpty()) {
366364
classNode.setComment(comment);
@@ -394,8 +392,7 @@ protected Class parseClass(final TypeElement classDoc) {
394392
}
395393

396394
for (final AnnotationMirror annotationDesc : classDoc.getAnnotationMirrors()) {
397-
final var annotationInstance =
398-
parseAnnotationDesc(annotationDesc, classDoc.getQualifiedName());
395+
final var annotationInstance = parseAnnotationDesc(annotationDesc, classDoc.getQualifiedName());
399396
classNode.getAnnotation().add(annotationInstance);
400397
}
401398

@@ -444,8 +441,7 @@ protected Constructor parseConstructor(final ExecutableElement constructorDoc) {
444441
}
445442

446443
for (final AnnotationMirror annotationDesc : constructorDoc.getAnnotationMirrors()) {
447-
final var annotationInstance =
448-
parseAnnotationDesc(annotationDesc, constructorDoc.getSimpleName());
444+
final var annotationInstance = parseAnnotationDesc(annotationDesc, constructorDoc.getSimpleName());
449445
constructorNode.getAnnotation().add(annotationInstance);
450446
}
451447

@@ -488,8 +484,7 @@ protected Method parseMethod(final ExecutableElement methodDoc) {
488484
}
489485

490486
for (final AnnotationMirror annotationDesc : methodDoc.getAnnotationMirrors()) {
491-
final var annotationInstance =
492-
parseAnnotationDesc(annotationDesc, methodDoc.getSimpleName());
487+
final var annotationInstance = parseAnnotationDesc(annotationDesc, methodDoc.getSimpleName());
493488
methodNode.getAnnotation().add(annotationInstance);
494489
}
495490

@@ -502,12 +497,11 @@ protected Method parseMethod(final ExecutableElement methodDoc) {
502497

503498
protected MethodParameter parseMethodParameter(final VariableElement parameter) {
504499
final MethodParameter parameterMethodNode = objectFactory.createMethodParameter();
505-
parameterMethodNode.setName(parameter.getSimpleName().toString());
500+
parameterMethodNode.setName(getSimpleName(parameter));
506501
parameterMethodNode.setType(parseTypeInfo(parameter.asType()));
507502

508503
for (final AnnotationMirror annotationDesc : parameter.getAnnotationMirrors()) {
509-
final var annotationInstance =
510-
parseAnnotationDesc(annotationDesc, parameter.getSimpleName());
504+
final var annotationInstance = parseAnnotationDesc(annotationDesc, parameter.getSimpleName());
511505
parameterMethodNode.getAnnotation().add(annotationInstance);
512506
}
513507

@@ -517,8 +511,8 @@ protected MethodParameter parseMethodParameter(final VariableElement parameter)
517511
protected Field parseField(final VariableElement fieldDoc) {
518512
final Field fieldNode = objectFactory.createField();
519513
fieldNode.setType(parseTypeInfo(fieldDoc.asType()));
520-
fieldNode.setName(fieldDoc.getSimpleName().toString());
521-
fieldNode.setQualified(fieldDoc.getSimpleName().toString());
514+
fieldNode.setName(getSimpleName(fieldDoc));
515+
fieldNode.setQualified(getSimpleName(fieldDoc));
522516
String comment = getJavaDoc(fieldDoc);
523517
if (!comment.isEmpty()) {
524518
fieldNode.setComment(comment);
@@ -531,8 +525,7 @@ protected Field parseField(final VariableElement fieldDoc) {
531525
fieldNode.setConstant(requireNonNullElse(fieldDoc.getConstantValue(), "").toString());
532526

533527
for (final AnnotationMirror annotationDesc : fieldDoc.getAnnotationMirrors()) {
534-
fieldNode.getAnnotation()
535-
.add(parseAnnotationDesc(annotationDesc, fieldDoc.getSimpleName()));
528+
fieldNode.getAnnotation().add(parseAnnotationDesc(annotationDesc, fieldDoc.getSimpleName()));
536529
}
537530

538531
for (final DocTree tag : getTags(fieldDoc)) {
@@ -608,15 +601,15 @@ protected TagInfo parseTag(final DocTree tagDoc) {
608601

609602
/**
610603
* {@return string representation of the element scope}
611-
*
604+
*
612605
* @param doc the element to get its scope
613606
*/
614607
protected String parseScope(final Element doc) {
615-
if (doc.getModifiers().contains(Modifier.PRIVATE)) {
608+
if (hasModifier(doc, Modifier.PRIVATE)) {
616609
return "private";
617-
} else if (doc.getModifiers().contains(Modifier.PROTECTED)) {
610+
} else if (hasModifier(doc, Modifier.PROTECTED)) {
618611
return "protected";
619-
} else if (doc.getModifiers().contains(Modifier.PUBLIC)) {
612+
} else if (hasModifier(doc, Modifier.PUBLIC)) {
620613
return "public";
621614
}
622615

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,23 +51,25 @@ Set<CustomOption> toDocletOptions() {
5151
}
5252

5353
private void newOneArgOption(final String optionName, final String description) {
54-
final var option = Option.builder(optionName)
55-
.argName(optionName)
54+
final var parsedOptionName = parseOptionName(optionName);
55+
final var option = Option.builder(parsedOptionName)
56+
.argName(parsedOptionName)
5657
.required(false)
5758
.numberOfArgs(1)
5859
.desc(description)
5960
.build();
61+
6062
cliOptions.addOption(option);
6163
}
6264

63-
private void newArgOption(final String optionName, final String argName,
64-
final String description) {
65+
private void newArgOption(final String optionName, final String argName, final String description) {
6566
final var option = Option.builder(parseOptionName(optionName))
6667
.argName(parseOptionName(argName))
6768
.required(false)
6869
.hasArg()
6970
.desc(description)
7071
.build();
72+
7173
cliOptions.addOption(option);
7274
}
7375

@@ -83,6 +85,7 @@ private void newNoArgOption(final String optionName, final String description) {
8385
.hasArg(false)
8486
.desc(description)
8587
.build();
88+
8689
cliOptions.addOption(option);
8790
}
8891
}

0 commit comments

Comments
 (0)