1515 */
1616package com .datastax .driver .mapping ;
1717
18- import com .datastax .driver .mapping .annotations .Column ;
1918import com .datastax .driver .mapping .annotations .Computed ;
2019import com .datastax .driver .mapping .annotations .Table ;
2120
2221import java .lang .annotation .Annotation ;
23- import java .util .Collection ;
24- import java .util .Collections ;
25- import java .util .HashSet ;
26- import java .util .List ;
22+ import java .lang .reflect .Field ;
2723
2824/**
2925 * Various checks on mapping annotations.
@@ -40,75 +36,59 @@ class AnnotationChecks {
4036 static <T extends Annotation > T getTypeAnnotation (Class <T > annotation , Class <?> annotatedClass ) {
4137 T instance = annotatedClass .getAnnotation (annotation );
4238 if (instance == null )
43- throw new IllegalArgumentException (String .format ("@%s annotation was not found on %s" ,
44- annotation .getSimpleName (), annotatedClass ));
39+ throw new IllegalArgumentException (String .format ("@%s annotation was not found on type %s" ,
40+ annotation .getSimpleName (), annotatedClass . getName () ));
4541
4642 // Check that no other mapping annotations are present
4743 validateAnnotations (annotatedClass , annotation );
4844
4945 return instance ;
5046 }
5147
52- @ SuppressWarnings ("unchecked" )
5348 private static void validateAnnotations (Class <?> clazz , Class <? extends Annotation > allowed ) {
5449 @ SuppressWarnings ("unchecked" )
55- Collection <Annotation > classAnnotations = new HashSet <Annotation >();
56- Collections .addAll (classAnnotations , clazz .getAnnotations ());
57- Class <? extends Annotation > invalid = validateAnnotations (classAnnotations , Collections .singleton (allowed ));
50+ Class <? extends Annotation > invalid = validateAnnotations (clazz .getAnnotations (), allowed );
5851 if (invalid != null )
59- throw new IllegalArgumentException (String .format ("Cannot have both @%s and @%s on %s" ,
52+ throw new IllegalArgumentException (String .format ("Cannot have both @%s and @%s on type %s" ,
6053 allowed .getSimpleName (), invalid .getSimpleName (),
61- clazz ));
54+ clazz . getName () ));
6255 }
6356
6457 /**
6558 * Checks that a field is only annotated with the given mapping annotations, and that its "frozen" annotations are valid.
6659 */
67- static void validateAnnotations (PropertyMapper property , Collection <? extends Class <? extends Annotation >> allowed ) {
68- Class <? extends Annotation > invalid = validateAnnotations (property .getAnnotations (), allowed );
69- if (invalid != null ) {
70- throw new IllegalArgumentException (String .format ("Annotation @%s is not allowed on property '%s' " ,
60+ static void validateAnnotations (Field field , String classDescription , Class <? extends Annotation >... allowed ) {
61+ Class <? extends Annotation > invalid = validateAnnotations (field .getAnnotations (), allowed );
62+ if (invalid != null )
63+ throw new IllegalArgumentException (String .format ("Annotation @%s is not allowed on field %s of %s %s " ,
7164 invalid .getSimpleName (),
72- property ));
73- }
74- checkValidPrimaryKey ( property );
75- checkValidComputed (property );
65+ field . getName (), classDescription ,
66+ field . getDeclaringClass (). getName ()));
67+
68+ checkValidComputed (field );
7669 }
7770
7871 // Returns the offending annotation if there is one
79- private static Class <? extends Annotation > validateAnnotations (Collection < Annotation > annotations , Collection <? extends Class <? extends Annotation >> allowed ) {
72+ private static Class <? extends Annotation > validateAnnotations (Annotation [] annotations , Class <? extends Annotation >... allowed ) {
8073 for (Annotation annotation : annotations ) {
8174 Class <? extends Annotation > actual = annotation .annotationType ();
82- if (actual .getPackage ().equals (MAPPING_PACKAGE ) && !allowed . contains (actual ))
75+ if (actual .getPackage ().equals (MAPPING_PACKAGE ) && !contains (allowed , actual ))
8376 return actual ;
8477 }
8578 return null ;
8679 }
8780
88- private static void checkValidPrimaryKey (PropertyMapper property ) {
89- if (property .isPartitionKey () && property .isClusteringColumn ())
90- throw new IllegalArgumentException (String .format ("Property '%s' cannot be annotated with both @PartitionKey and @ClusteringColumn" , property ));
91- }
92-
93- private static void checkValidComputed (PropertyMapper property ) {
94- if (property .isComputed ()) {
95- Computed computed = property .annotation (Computed .class );
96- if (computed .value ().isEmpty ()) {
97- throw new IllegalArgumentException (String .format ("Property '%s': attribute 'value' of annotation @Computed is mandatory for computed properties" , property ));
98- }
99- if (property .hasAnnotation (Column .class )) {
100- throw new IllegalArgumentException (String .format ("Property '%s' cannot be annotated with both @Column and @Computed" , property ));
101- }
102- }
81+ private static boolean contains (Object [] array , Object target ) {
82+ for (Object element : array )
83+ if (element .equals (target ))
84+ return true ;
85+ return false ;
10386 }
10487
105- static void validateOrder (List <PropertyMapper > properties , String annotation ) {
106- for (int i = 0 ; i < properties .size (); i ++) {
107- PropertyMapper property = properties .get (i );
108- int pos = property .position ;
109- if (pos != i )
110- throw new IllegalArgumentException (String .format ("Invalid ordering value %d for annotation %s of property '%s', was expecting %d" ,
111- pos , annotation , property , i ));
88+ static void checkValidComputed (Field field ) {
89+ Computed computed = field .getAnnotation (Computed .class );
90+ if (computed != null && computed .value ().isEmpty ()) {
91+ throw new IllegalArgumentException (String .format ("Field %s: attribute 'value' of annotation @Computed is mandatory for computed fields" , field .getName ()));
11292 }
11393 }
11494}
0 commit comments