Elffuss BraidHub

kikocis/java-driver public

fe2228750ce8 · 3.9 KiBRawPermalink
1/*
2 * Copyright (C) 2012-2015 DataStax Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package com.datastax.driver.mapping;
17
18import com.datastax.driver.mapping.annotations.Computed;
19import com.datastax.driver.mapping.annotations.Table;
20
21import java.lang.annotation.Annotation;
22import java.lang.reflect.Field;
23
24/**
25 * Various checks on mapping annotations.
26 */
27class AnnotationChecks {
28
29 // The package containing the mapping annotations
30 private static final Package MAPPING_PACKAGE = Table.class.getPackage();
31
32 /**
33 * Checks that a class is decorated with the given annotation, and return the annotation instance.
34 * Also validates that no other mapping annotation is present.
35 */
36 static <T extends Annotation> T getTypeAnnotation(Class<T> annotation, Class<?> annotatedClass) {
37 T instance = annotatedClass.getAnnotation(annotation);
38 if (instance == null)
39 throw new IllegalArgumentException(String.format("@%s annotation was not found on type %s",
40 annotation.getSimpleName(), annotatedClass.getName()));
41
42 // Check that no other mapping annotations are present
43 validateAnnotations(annotatedClass, annotation);
44
45 return instance;
46 }
47
48 private static void validateAnnotations(Class<?> clazz, Class<? extends Annotation> allowed) {
49 @SuppressWarnings("unchecked")
50 Class<? extends Annotation> invalid = validateAnnotations(clazz.getAnnotations(), allowed);
51 if (invalid != null)
52 throw new IllegalArgumentException(String.format("Cannot have both @%s and @%s on type %s",
53 allowed.getSimpleName(), invalid.getSimpleName(),
54 clazz.getName()));
55 }
56
57 /**
58 * Checks that a field is only annotated with the given mapping annotations, and that its "frozen" annotations are valid.
59 */
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",
64 invalid.getSimpleName(),
65 field.getName(), classDescription,
66 field.getDeclaringClass().getName()));
67
68 checkValidComputed(field);
69 }
70
71 // Returns the offending annotation if there is one
72 private static Class<? extends Annotation> validateAnnotations(Annotation[] annotations, Class<? extends Annotation>... allowed) {
73 for (Annotation annotation : annotations) {
74 Class<? extends Annotation> actual = annotation.annotationType();
75 if (actual.getPackage().equals(MAPPING_PACKAGE) && !contains(allowed, actual))
76 return actual;
77 }
78 return null;
79 }
80
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;
86 }
87
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()));
92 }
93 }
94}