3535import io .micrometer .common .util .internal .logging .InternalLogger ;
3636import io .micrometer .common .util .internal .logging .InternalLoggerFactory ;
3737import io .micrometer .observation .Observation ;
38+ import org .jboss .forge .roaster .Internal ;
3839import org .jboss .forge .roaster .Roaster ;
3940import org .jboss .forge .roaster ._shade .org .eclipse .jdt .core .dom .AbstractTypeDeclaration ;
4041import org .jboss .forge .roaster ._shade .org .eclipse .jdt .core .dom .CompilationUnit ;
@@ -65,7 +66,7 @@ public class ParsingUtils {
6566 private static final InternalLogger logger = InternalLoggerFactory .getInstance (ParsingUtils .class );
6667
6768 public static void updateKeyValuesFromEnum (JavaEnumImpl parentEnum , JavaSource <?> source , Class requiredClass ,
68- Collection <KeyValueEntry > keyValues ) {
69+ Collection <KeyValueEntry > keyValues , @ Nullable String methodName ) {
6970 if (!(source instanceof JavaEnumImpl )) {
7071 return ;
7172 }
@@ -78,7 +79,7 @@ public static void updateKeyValuesFromEnum(JavaEnumImpl parentEnum, JavaSource<?
7879 return ;
7980 }
8081 for (EnumConstantSource enumConstant : myEnum .getEnumConstants ()) {
81- String keyValue = enumKeyValue (enumConstant );
82+ String keyValue = enumKeyValue (enumConstant , methodName );
8283 keyValues .add (new KeyValueEntry (keyValue , enumConstant .getJavaDoc ().getText ()));
8384 }
8485 }
@@ -153,14 +154,19 @@ private static String filePath(String className, File parent) {
153154
154155 public static Collection <KeyValueEntry > keyValueEntries (JavaEnumImpl myEnum , MethodDeclaration methodDeclaration ,
155156 Class requiredClass ) {
157+ return keyValueEntries (myEnum , methodDeclaration , requiredClass , null );
158+ }
159+
160+ public static Collection <KeyValueEntry > keyValueEntries (JavaEnumImpl myEnum , MethodDeclaration methodDeclaration ,
161+ Class requiredClass , @ Nullable String methodName ) {
156162 Collection <String > enumNames = readClassValue (methodDeclaration );
157163 Collection <KeyValueEntry > keyValues = new TreeSet <>();
158164 enumNames .forEach (enumName -> {
159165 List <JavaSource <?>> nestedTypes = myEnum .getNestedTypes ();
160166 JavaSource <?> nestedSource = nestedTypes .stream ()
161167 .filter (javaSource -> javaSource .getName ().equals (enumName )).findFirst ().orElseThrow (
162168 () -> new IllegalStateException ("There's no nested type with name [" + enumName + "]" ));
163- ParsingUtils .updateKeyValuesFromEnum (myEnum , nestedSource , requiredClass , keyValues );
169+ ParsingUtils .updateKeyValuesFromEnum (myEnum , nestedSource , requiredClass , keyValues , methodName );
164170 });
165171 return keyValues ;
166172 }
@@ -198,13 +204,18 @@ else if (!methodInvocation.toString().endsWith(".values()")) {
198204 return Collections .singletonList (methodInvocation .getExpression ().toString ());
199205 }
200206
201- private static String enumKeyValue (EnumConstantSource enumConstant ) {
207+ private static String enumKeyValue (EnumConstantSource enumConstant , @ Nullable String methodName ) {
202208 List <MemberSource <EnumConstantSource .Body , ?>> members = enumConstant .getBody ().getMembers ();
203209 if (members .isEmpty ()) {
204210 logger .warn ("No method declarations in the enum." );
205211 return "" ;
206212 }
207- Object internal = members .get (0 ).getInternal ();
213+ Object internal ;
214+ if (methodName == null ) {
215+ internal = members .get (0 ).getInternal ();
216+ } else {
217+ internal = members .stream ().filter (bodyMemberSource -> bodyMemberSource .getName ().equals (methodName )).findFirst ().map (Internal ::getInternal ).orElse (null );
218+ }
208219 if (!(internal instanceof MethodDeclaration )) {
209220 logger .warn ("Can't read the member [" + internal .getClass () + "] as a method declaration." );
210221 return "" ;
0 commit comments