@@ -308,7 +308,7 @@ private void addConfigFieldToConstructor(
308308 definition .configKey ()
309309 );
310310 definition .defaultProvider ().ifPresent (d -> code .add (", $L" , d ));
311- definition .expectedType ().ifPresent (t -> code .add (", $L" , t ));
311+ definition .expectedTypeCodeBlock ().ifPresent (t -> code .add (", $L" , t ));
312312 CodeBlock codeBlock = code .add (")" ).build ();
313313 for (UnaryOperator <CodeBlock > converter : definition .converters ()) {
314314 codeBlock = converter .apply (codeBlock );
@@ -558,11 +558,11 @@ private Optional<MemberDefinition> memberDefinition(
558558 TypeMirror converterInputType = candidate .getParameters ().get (0 ).asType ();
559559 if (isTypeOf (Optional .class , targetType )) {
560560 return memberDefinition (names , member , targetType , Optional .of (converterInputType ))
561- .map (d -> ImmutableMemberDefinition .builder ()
562- .from (d )
563- .addConverter (c -> CodeBlock .of (
561+ .map (definition -> ImmutableMemberDefinition .builder ()
562+ .from (definition )
563+ .addConverter (codeBlock -> CodeBlock .of (
564564 "$L.map($T::$N)" ,
565- c ,
565+ codeBlock ,
566566 currentType ,
567567 candidate .getSimpleName ().toString ()
568568 ))
@@ -625,6 +625,7 @@ private void validateCandidateModifiers(
625625 invalidCandidates .add (InvalidCandidate .of (candidate , "May only accept one parameter" ));
626626 }
627627 if (isTypeOf (Optional .class , targetType )) {
628+ // for a parameter declared as Optional<T>, we want to find a converter that returns T
628629 targetType = ((DeclaredType ) targetType ).getTypeArguments ().get (0 );
629630 }
630631 if (!typeUtils .isAssignable (candidate .getReturnType (), targetType )) {
@@ -698,25 +699,28 @@ private Optional<MemberDefinition> memberDefinition(
698699
699700 if (converterInputType .isPresent ()) {
700701 TypeMirror expectedType = typeUtils .erasure (converterInputType .get ());
701- var x = typeUtils .getDeclaredType (elementUtils .getTypeElement ("java.util.Optional" ), expectedType );
702+ var expectedWrappedInOptional = typeUtils .getDeclaredType (
703+ elementUtils .getTypeElement ("java.util.Optional" ),
704+ expectedType
705+ );
702706 builder
703707 .methodPrefix ("get" )
704708 .methodName ("Optional" )
705- .expectedType (CodeBlock .of ("$T.class" , expectedType ))
706- .expectedTypeRaw (expectedType )
707- .expectedTypeRawWrappedInOptional ( x );
709+ .expectedTypeCodeBlock (CodeBlock .of ("$T.class" , expectedType ))
710+ .expectedType (expectedType )
711+ .expectedTypeWrappedInOptional ( expectedWrappedInOptional );
708712 } else if (maybeInnerType .isPresent ()) {
709713 builder
710714 .methodPrefix ("get" )
711715 .methodName ("Optional" )
712- .expectedType (CodeBlock .of ("$T.class" , maybeInnerType .get ()));
716+ .expectedTypeCodeBlock (CodeBlock .of ("$T.class" , maybeInnerType .get ()));
713717 } else {
714718 return Optional .empty ();
715719 }
716720 } else {
717721 builder
718722 .methodName ("Checked" )
719- .expectedType (CodeBlock .of ("$T.class" , ClassName .get (asTypeElement (targetType ))));
723+ .expectedTypeCodeBlock (CodeBlock .of ("$T.class" , ClassName .get (asTypeElement (targetType ))));
720724 }
721725 break ;
722726 default :
@@ -783,11 +787,11 @@ interface MemberDefinition {
783787
784788 Optional <CodeBlock > defaultProvider ();
785789
786- Optional <CodeBlock > expectedType ();
790+ Optional <CodeBlock > expectedTypeCodeBlock ();
787791
788- Optional <TypeMirror > expectedTypeRaw ();
792+ Optional <TypeMirror > expectedType ();
789793
790- Optional <TypeMirror > expectedTypeRawWrappedInOptional ();
794+ Optional <TypeMirror > expectedTypeWrappedInOptional ();
791795
792796 List <UnaryOperator <CodeBlock >> converters ();
793797 }
0 commit comments