@@ -583,32 +583,30 @@ private void findConstructor() {
583
583
}
584
584
585
585
private PropertyDefinition getOrCreateProperty (String name , Map <String , PropertyDefinition > properties ) {
586
- PropertyDefinition thisProperty = properties .get (name );
587
- if (thisProperty == null ) {
588
- thisProperty = new PropertyDefinition (name , mapper );
589
- properties .put (name , thisProperty );
590
- }
591
- return thisProperty ;
586
+ return properties .computeIfAbsent (name , n -> new PropertyDefinition (n , mapper ));
592
587
}
593
588
594
589
private void loadPropertiesFromClass () {
595
590
Map <String , PropertyDefinition > properties = new HashMap <>();
596
- PropertyDefinition keyProperty = null ;
597
591
KeyConfig keyConfig = config != null ? config .getKey () : null ;
598
- for (Method thisMethod : clazz .getDeclaredMethods ()) {
599
592
593
+ PropertyDefinition keyProperty = null ;
594
+ PropertyDefinition generationProperty = null ;
595
+
596
+ for (Method thisMethod : clazz .getDeclaredMethods ()) {
600
597
String methodName = thisMethod .getName ();
601
598
BinConfig getterConfig = getBinFromGetter (methodName );
602
599
BinConfig setterConfig = getBinFromSetter (methodName );
603
600
604
601
boolean isKey = false ;
605
602
boolean isKeyViaConfig = keyConfig != null
606
603
&& (keyConfig .isGetter (methodName ) || keyConfig .isSetter (methodName ));
607
- if (thisMethod .isAnnotationPresent (AerospikeKey .class ) || isKeyViaConfig ) {
608
604
605
+ if (thisMethod .isAnnotationPresent (AerospikeKey .class ) || isKeyViaConfig ) {
609
606
if (keyProperty == null ) {
610
607
keyProperty = new PropertyDefinition ("_key_" , mapper );
611
608
}
609
+
612
610
if (isKeyViaConfig ) {
613
611
if (keyConfig .isGetter (methodName )) {
614
612
keyProperty .setGetter (thisMethod );
@@ -623,11 +621,16 @@ private void loadPropertiesFromClass() {
623
621
keyProperty .setGetter (thisMethod );
624
622
}
625
623
}
624
+
626
625
isKey = true ;
627
626
}
628
627
629
628
// Handle @AerospikeGeneration annotation on methods (getters/setters)
630
629
if (thisMethod .isAnnotationPresent (AerospikeGeneration .class )) {
630
+ if (generationProperty == null ) {
631
+ generationProperty = new PropertyDefinition ("_generation_" , mapper );
632
+ }
633
+
631
634
// For getter methods, validate return type
632
635
if (methodName .startsWith ("get" )) {
633
636
Class <?> returnType = thisMethod .getReturnType ();
@@ -636,24 +639,29 @@ private void loadPropertiesFromClass() {
636
639
"@AerospikeGeneration getter %s in class %s must return Integer or int type, but returned %s" ,
637
640
methodName , clazz .getName (), returnType .getSimpleName ()));
638
641
}
642
+ generationProperty .setGetter (thisMethod );
639
643
}
640
644
641
645
// For setter methods, validate parameter type
642
- if (methodName .startsWith ("set" ) && thisMethod .getParameterCount () == 1 ) {
646
+ else if (methodName .startsWith ("set" )) {
647
+ if (thisMethod .getParameterCount () != 1 ) {
648
+ throw new AerospikeException (String .format (
649
+ "@AerospikeGeneration setter %s in class %s must accept a single parameter, but %d were found" ,
650
+ methodName , clazz .getName (), thisMethod .getParameterCount ()));
651
+ }
643
652
Class <?> paramType = thisMethod .getParameterTypes ()[0 ];
644
653
if (!paramType .equals (Integer .class ) && !paramType .equals (int .class )) {
645
654
throw new AerospikeException (String .format (
646
655
"@AerospikeGeneration setter %s in class %s must accept Integer or int type, but accepted %s" ,
647
656
methodName , clazz .getName (), paramType .getSimpleName ()));
648
657
}
658
+ generationProperty .setSetter (thisMethod );
649
659
}
650
-
651
- // We'll handle the generation property creation after processing all methods
652
- // to ensure we can pair getters and setters properly
653
660
}
654
661
655
662
if (thisMethod .isAnnotationPresent (AerospikeGetter .class ) || getterConfig != null ) {
656
- String getterName = (getterConfig != null ) ? getterConfig .getName ()
663
+ String getterName = (getterConfig != null )
664
+ ? getterConfig .getName ()
657
665
: thisMethod .getAnnotation (AerospikeGetter .class ).name ();
658
666
659
667
String name = ParserUtils .getInstance ().get (ParserUtils .getInstance ().get (getterName ));
@@ -665,54 +673,41 @@ private void loadPropertiesFromClass() {
665
673
}
666
674
667
675
if (thisMethod .isAnnotationPresent (AerospikeSetter .class ) || setterConfig != null ) {
668
- String setterName = (setterConfig != null ) ? setterConfig .getName ()
676
+ String setterName = (setterConfig != null )
677
+ ? setterConfig .getName ()
669
678
: thisMethod .getAnnotation (AerospikeSetter .class ).name ();
679
+
670
680
String name = ParserUtils .getInstance ().get (ParserUtils .getInstance ().get (setterName ));
671
681
PropertyDefinition thisProperty = getOrCreateProperty (name , properties );
672
682
thisProperty .setSetter (thisMethod );
673
683
}
674
684
}
675
685
676
686
if (keyProperty != null ) {
677
- keyProperty .validate (clazz .getName (), config , true );
678
-
679
687
if (key != null ) {
680
688
throw new AerospikeException (String .format ("Class %s cannot have more than one key" , clazz .getName ()));
681
689
}
682
690
691
+ keyProperty .validate (clazz .getName (), config , true );
692
+
683
693
AnnotatedType annotatedType = new AnnotatedType (config , keyProperty .getGetter ());
684
694
TypeMapper typeMapper = TypeUtils .getMapper (keyProperty .getType (), annotatedType , this .mapper );
685
695
this .key = new ValueType .MethodValue (keyProperty , typeMapper , annotatedType );
686
696
}
687
697
688
- // Handle @AerospikeGeneration annotation on methods - find getter/setter pairs
689
- PropertyDefinition generationProperty = null ;
690
- for (Method thisMethod : clazz .getDeclaredMethods ()) {
691
- if (thisMethod .isAnnotationPresent (AerospikeGeneration .class )) {
692
- if (generationProperty == null ) {
693
- generationProperty = new PropertyDefinition ("_generation_" , mapper );
694
- }
695
-
696
- String methodName = thisMethod .getName ();
697
- if (methodName .startsWith ("get" )) {
698
- generationProperty .setGetter (thisMethod );
699
- } else if (methodName .startsWith ("set" )) {
700
- generationProperty .setSetter (thisMethod );
701
- }
702
- }
703
- }
704
-
705
698
if (generationProperty != null ) {
706
699
if (generationField != null ) {
707
700
throw new AerospikeException (
708
701
String .format ("Class %s cannot have more than one @AerospikeGeneration field" , clazz .getName ()));
709
702
}
710
703
711
704
generationProperty .validate (clazz .getName (), config , false );
705
+
712
706
AnnotatedType annotatedType = new AnnotatedType (config , generationProperty .getGetter ());
713
707
TypeMapper typeMapper = TypeUtils .getMapper (generationProperty .getType (), annotatedType , this .mapper );
714
708
this .generationField = new ValueType .MethodValue (generationProperty , typeMapper , annotatedType );
715
709
}
710
+
716
711
for (String thisPropertyName : properties .keySet ()) {
717
712
PropertyDefinition thisProperty = properties .get (thisPropertyName );
718
713
thisProperty .validate (clazz .getName (), config , false );
@@ -731,15 +726,13 @@ private void loadPropertiesFromClass() {
731
726
732
727
private void loadFieldsFromClass () {
733
728
for (Field thisField : this .clazz .getDeclaredFields ()) {
734
- boolean isKey = false ;
735
729
BinConfig thisBin = getBinFromField (thisField );
736
730
737
731
if (Modifier .isFinal (thisField .getModifiers ()) && Modifier .isStatic (thisField .getModifiers ())) {
738
732
// We cannot map static final fields
739
733
continue ;
740
734
}
741
735
742
- isKey = handleKeyField (thisField , thisBin );
743
736
boolean isGenerationField = this .handleGenerationField (thisField , thisBin );
744
737
if (thisField .isAnnotationPresent (AerospikeExclude .class )
745
738
|| (thisBin != null && thisBin .isExclude () != null && thisBin .isExclude ())) {
@@ -755,6 +748,7 @@ private void loadFieldsFromClass() {
755
748
756
749
if (this .mapAll || thisField .isAnnotationPresent (AerospikeBin .class ) || thisBin != null ) {
757
750
// This field needs to be mapped
751
+ boolean isKey = handleKeyField (thisField , thisBin );
758
752
mapField (thisField , thisBin , isKey );
759
753
}
760
754
}
@@ -774,6 +768,7 @@ private boolean handleKeyField(Field thisField, BinConfig thisBin) {
774
768
if (key != null ) {
775
769
throw new AerospikeException (String .format ("Class %s cannot have more than one key" , clazz .getName ()));
776
770
}
771
+
777
772
AerospikeKey keyAnnotation = thisField .getAnnotation (AerospikeKey .class );
778
773
boolean storeAsBin = keyAnnotation == null || keyAnnotation .storeAsBin ();
779
774
@@ -864,8 +859,7 @@ private void mapField(Field thisField, BinConfig thisBin, boolean isKey) {
864
859
private Method findMethodWithNameAndParams (String name , Class <?>... params ) {
865
860
try {
866
861
Method method = this .clazz .getDeclaredMethod (name , params );
867
- // TODO: Should this ascend the inheritance hierarchy using getMethod on
868
- // superclasses?
862
+ // TODO: Should this ascend the inheritance hierarchy using getMethod on superclasses?
869
863
return method ;
870
864
} catch (NoSuchMethodException nsme ) {
871
865
return null ;
@@ -881,7 +875,8 @@ private void validateAccessorsForField(String binName, Field thisField) {
881
875
Method getter = findMethodWithNameAndParams (getterName );
882
876
if (getter == null ) {
883
877
throw new AerospikeException (String .format (
884
- "Expected to find getter for field %s on class %s due to it being configured to useAccessors, but no method with the signature \" %s %s()\" was found" ,
878
+ "Expected to find getter for field %s on class %s due to it being configured to useAccessors," +
879
+ " but no method with the signature \" %s %s()\" was found" ,
885
880
fieldName , this .clazz .getSimpleName (), thisField .getType ().getSimpleName (), getterName ));
886
881
}
887
882
@@ -894,7 +889,8 @@ private void validateAccessorsForField(String binName, Field thisField) {
894
889
}
895
890
if (setter == null ) {
896
891
throw new AerospikeException (String .format (
897
- "Expected to find setter for field %s on class %s due to it being configured to useAccessors, but no method with the name \" %s\" was found" ,
892
+ "Expected to find setter for field %s on class %s due to it being configured to useAccessors," +
893
+ " but no method with the name \" %s\" was found" ,
898
894
fieldName , this .clazz .getSimpleName (), setterName ));
899
895
}
900
896
@@ -927,7 +923,7 @@ public Object getKey(Object object) {
927
923
Object key = this ._getKey (object );
928
924
if (key == null ) {
929
925
throw new AerospikeException (String .format (
930
- "Null key from annotated object of class %s." + " Did you forget an @AerospikeKey annotation?" ,
926
+ "Null key from annotated object of class %s. Did you forget an @AerospikeKey annotation?" ,
931
927
this .clazz .getSimpleName ()));
932
928
}
933
929
return key ;
0 commit comments