2525
2626import javax .lang .model .type .TypeMirror ;
2727import java .util .*;
28- import java .util .concurrent .atomic .AtomicReference ;
2928import java .util .function .Consumer ;
3029import java .util .function .Predicate ;
3130import java .util .regex .Pattern ;
@@ -119,17 +118,12 @@ public TypeSpec generateLoader(boolean nested) {
119118 result .addModifiers (STATIC ).build ();
120119 }
121120
122- if (definition .getLifecycle ().isModifiable ()) {
123- FieldSpec cleanerField = newCleanerField ();
124- result .addField (cleanerField );
125- result .addMethod (newSetMethod (resourceField , quantifierType ));
126- result .addMethod (newReloadMethod (sourceField , batchField , cleanerField , doLoadMethod ));
127- result .addMethod (newResetMethod (sourceField , doLoadMethod ));
128- }
121+ FieldSpec cleanerField = newCleanerField ();
122+ result .addField (cleanerField );
123+ result .addMethod (newSetMethod (resourceField , quantifierType ));
124+ result .addMethod (newReloadMethod (sourceField , batchField , cleanerField , doLoadMethod ));
129125
130- if (definition .getLifecycle () == Lifecycle .IMMUTABLE ) {
131- result .addMethod (newLoadMethod (className , quantifierType , getMethod ));
132- }
126+ result .addMethod (newLoadMethod (className , quantifierType , getMethod ));
133127
134128 return result .build ();
135129 }
@@ -138,13 +132,11 @@ private CodeBlock getMainJavadoc() {
138132 return CodeBlock
139133 .builder ()
140134 .add ("Custom service loader for $L.\n " , toJavadocLink (definition .getServiceType ()))
141- .add ("<br>This class $L thread-safe.\n " , definition .getLifecycle ().isThreadSafe () ? "is" : "is not" )
142135 .add ("<p>Properties:\n " )
143136 .add ("<ul>\n " )
144137 .add ("<li>Quantifier: $L</li>\n " , definition .getQuantifier ())
145138 .add ("<li>Fallback: $L</li>\n " , toJavadocLink (definition .getFallback ()))
146139 .add ("<li>Preprocessing: $L</li>\n " , getPreprocessingJavadoc ())
147- .add ("<li>Mutability: $L</li>\n " , definition .getLifecycle ().toMutability ())
148140 .add ("<li>Name: $L</li>\n " , definition .getLoaderName ().isEmpty () ? "null" : definition .getLoaderName ())
149141 .add ("<li>Backend: $L</li>\n " , definition .getBackend ().map (HasTypeMirror ::getTypeName ).orElse ("null" ))
150142 .add ("<li>Cleaner: $L</li>\n " , definition .getCleaner ().map (HasTypeMirror ::getTypeName ).orElse ("null" ))
@@ -401,23 +393,11 @@ private FieldSpec getResourceField(MethodSpec doLoadMethod, TypeName quantifierT
401393 }
402394
403395 private FieldSpec .Builder getResourceFieldBuilder (TypeName quantifierType ) {
404- String name = "resource" ;
405- switch (definition .getLifecycle ()) {
406- case IMMUTABLE :
407- return FieldSpec .builder (quantifierType , name , PRIVATE , FINAL );
408- case MUTABLE :
409- return FieldSpec .builder (quantifierType , name , PRIVATE );
410- case CONCURRENT :
411- return FieldSpec .builder (typeOf (AtomicReference .class , quantifierType ), name , PRIVATE , FINAL );
412- default :
413- throw new Unreachable ();
414- }
396+ return FieldSpec .builder (quantifierType , "resource" , PRIVATE );
415397 }
416398
417399 private CodeBlock getResourceInitializer (MethodSpec doLoadMethod ) {
418- return definition .getLifecycle ().isAtomicReference ()
419- ? CodeBlock .of ("new $T<>($N())" , ClassName .get (AtomicReference .class ), doLoadMethod )
420- : CodeBlock .of ("$N()" , doLoadMethod );
400+ return CodeBlock .of ("$N()" , doLoadMethod );
421401 }
422402
423403 private MethodSpec getGetMethod (FieldSpec resourceField , TypeName quantifierType ) {
@@ -426,7 +406,6 @@ private MethodSpec getGetMethod(FieldSpec resourceField, TypeName quantifierType
426406 .addJavadoc (CodeBlock
427407 .builder ()
428408 .add (getGetDescription ())
429- .add (getThreadSafetyComment ())
430409 .add ("@return the current non-null value\n " )
431410 .build ())
432411 .addModifiers (PUBLIC )
@@ -449,9 +428,7 @@ private CodeBlock getGetDescription() {
449428 }
450429
451430 private CodeBlock getGetterStatement (FieldSpec resourceField ) {
452- return definition .getLifecycle ().isAtomicReference ()
453- ? CodeBlock .of ("return $N.get()" , resourceField )
454- : CodeBlock .of ("return $N" , resourceField );
431+ return CodeBlock .of ("return $N" , resourceField );
455432 }
456433
457434 private MethodSpec newSetMethod (FieldSpec resourceField , TypeName quantifierType ) {
@@ -460,10 +437,9 @@ private MethodSpec newSetMethod(FieldSpec resourceField, TypeName quantifierType
460437 .addJavadoc (CodeBlock
461438 .builder ()
462439 .add (getSetDescription ())
463- .add (getThreadSafetyComment ())
464440 .add ("@param newValue new non-null value\n " )
465441 .build ())
466- .addModifiers (PUBLIC )
442+ .addModifiers (PRIVATE )
467443 .addParameter (quantifierType , "newValue" )
468444 .addStatement (getSetterStatement (resourceField ))
469445 .build ();
@@ -483,9 +459,7 @@ private CodeBlock getSetDescription() {
483459 }
484460
485461 private CodeBlock getSetterStatement (FieldSpec resourceField ) {
486- return definition .getLifecycle ().isAtomicReference ()
487- ? CodeBlock .of ("$N.set($T.requireNonNull(newValue))" , resourceField , Objects .class )
488- : CodeBlock .of ("$N = $T.requireNonNull(newValue)" , resourceField , Objects .class );
462+ return CodeBlock .of ("$N = $T.requireNonNull(newValue)" , resourceField , Objects .class );
489463 }
490464
491465 private MethodSpec newReloadMethod (
@@ -499,47 +473,14 @@ private MethodSpec newReloadMethod(
499473 .addJavadoc (CodeBlock
500474 .builder ()
501475 .add ("Reloads the content by clearing the cache and fetching available providers.\n " )
502- .add (getThreadSafetyComment ())
503476 .build ())
504477 .addModifiers (PUBLIC )
505478 .addExceptions (getQuantifierException ());
506479
507- if (definition .getLifecycle ().isAtomicReference ()) {
508- result .beginControlFlow ("synchronized($N)" , sourceField );
509- }
510-
511480 result .addStatement ("$N.accept($N)" , cleanerField , sourceField );
512481 batchField .ifPresent (fieldSpec -> result .addStatement ("$N.accept($N)" , cleanerField , fieldSpec ));
513482 result .addStatement ("set($N())" , loaderMethod );
514483
515- if (definition .getLifecycle ().isAtomicReference ()) {
516- result .endControlFlow ();
517- }
518-
519- return result .build ();
520- }
521-
522- private MethodSpec newResetMethod (FieldSpec sourceField , MethodSpec loaderMethod ) {
523- MethodSpec .Builder result = MethodSpec
524- .methodBuilder ("reset" )
525- .addJavadoc (CodeBlock
526- .builder ()
527- .add ("Resets the content without clearing the cache.\n " )
528- .add (getThreadSafetyComment ())
529- .build ())
530- .addModifiers (PUBLIC )
531- .addExceptions (getQuantifierException ());
532-
533- if (definition .getLifecycle ().isAtomicReference ()) {
534- result .beginControlFlow ("synchronized($N)" , sourceField );
535- }
536-
537- result .addStatement ("set($N())" , loaderMethod );
538-
539- if (definition .getLifecycle ().isAtomicReference ()) {
540- result .endControlFlow ();
541- }
542-
543484 return result .build ();
544485 }
545486
@@ -553,7 +494,6 @@ private MethodSpec newLoadMethod(String className, TypeName quantifierType, Meth
553494 .add (getGetDescription ())
554495 .add ("<br>This is equivalent to the following code: <code>$L</code>\n " , mainStatement )
555496 .add ("<br>Therefore, the returned value might be different at each call.\n " )
556- .add (getThreadSafetyComment ())
557497 .add ("@return a non-null value\n " )
558498 .build ())
559499 .addModifiers (PUBLIC , STATIC )
@@ -565,19 +505,14 @@ private MethodSpec newLoadMethod(String className, TypeName quantifierType, Meth
565505 return result .build ();
566506 }
567507
568-
569508 private Optional <FieldSpec > getIdPatternField () {
570- return ids .size () == 1 && !ids .get (0 ).getPattern ().isEmpty () ? Optional .of (FieldSpec
571- .builder (Pattern .class , "ID_PATTERN" )
572- .addModifiers (PUBLIC , STATIC , FINAL )
573- .initializer ("$T.compile(\" $N\" )" , Pattern .class , ids .get (0 ).getPattern ())
574- .build ()) : Optional .empty ();
575- }
576-
577- private CodeBlock getThreadSafetyComment () {
578- return definition .getLifecycle ().isThreadSafe ()
579- ? CodeBlock .of ("<br>This method is thread-safe.\n " )
580- : CodeBlock .of ("<br>This method is not thread-safe.\n " );
509+ return ids .size () == 1 && !ids .get (0 ).getPattern ().isEmpty ()
510+ ? Optional .of (FieldSpec
511+ .builder (Pattern .class , "ID_PATTERN" )
512+ .addModifiers (PUBLIC , STATIC , FINAL )
513+ .initializer ("$T.compile(\" $N\" )" , Pattern .class , ids .get (0 ).getPattern ())
514+ .build ())
515+ : Optional .empty ();
581516 }
582517
583518 private static ParameterizedTypeName typeOf (Class <?> rawType , TypeName typeArgument ) {
0 commit comments