Elffuss BraidHub

kikocis/java-driver public

51386aeba860 · 4.1 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 java.lang.annotation.Annotation;
19import java.lang.reflect.*;
20import java.lang.reflect.Field;
21import java.util.*;
22
23import com.datastax.driver.mapping.annotations.*;
24
25/**
26 * Various checks on mapping annotations.
27 */
28class AnnotationChecks {
29
30 // The package containing the mapping annotations
31 private static final Package MAPPING_PACKAGE = Table.class.getPackage();
32
33 /**
34 * Checks that a class is decorated with the given annotation, and return the annotation instance.
35 * Also validates that no other mapping annotation is present.
36 */
37 static <T extends Annotation> T getTypeAnnotation(Class<T> annotation, Class<?> annotatedClass) {
38 T instance = annotatedClass.getAnnotation(annotation);
39 if (instance == null)
40 throw new IllegalArgumentException(String.format("@%s annotation was not found on type %s",
41 annotation.getSimpleName(), annotatedClass.getName()));
42
43 // Check that no other mapping annotations are present
44 validateAnnotations(annotatedClass, annotation);
45
46 return instance;
47 }
48
49 private static void validateAnnotations(Class<?> clazz, Class<? extends Annotation> allowed) {
50 @SuppressWarnings("unchecked")
51 Class<? extends Annotation> invalid = validateAnnotations(clazz.getAnnotations(), allowed);
52 if (invalid != null)
53 throw new IllegalArgumentException(String.format("Cannot have both @%s and @%s on type %s",
54 allowed.getSimpleName(), invalid.getSimpleName(),
55 clazz.getName()));
56 }
57
58 /**
59 * Checks that a field is only annotated with the given mapping annotations, and that its "frozen" annotations are valid.
60 */
61 static void validateAnnotations(Field field, String classDescription, Class<? extends Annotation>... allowed) {
62 Class<? extends Annotation> invalid = validateAnnotations(field.getAnnotations(), allowed);
63 if (invalid != null)
64 throw new IllegalArgumentException(String.format("Annotation @%s is not allowed on field %s of %s %s",
65 invalid.getSimpleName(),
66 field.getName(), classDescription,
67 field.getDeclaringClass().getName()));
68
69 checkValidComputed(field);
70 }
71
72 // Returns the offending annotation if there is one
73 private static Class<? extends Annotation> validateAnnotations(Annotation[] annotations, Class<? extends Annotation>... allowed) {
74 for (Annotation annotation : annotations) {
75 Class<? extends Annotation> actual = annotation.annotationType();
76 if (actual.getPackage().equals(MAPPING_PACKAGE) && !contains(allowed, actual))
77 return actual;
78 }
79 return null;
80 }
81
82 private static boolean contains(Object[] array, Object target) {
83 for (Object element : array)
84 if (element.equals(target))
85 return true;
86 return false;
87 }
88
89 static void checkValidComputed(Field field) {
90 Computed computed = field.getAnnotation(Computed.class);
91 if (computed != null && computed.value().isEmpty()){
92 throw new IllegalArgumentException(String.format("Field %s: attribute 'value' of annotation @Computed is mandatory for computed fields", field.getName()));
93 }
94 }
95}