Elffuss BraidHub

kikocis/java-driver public

2ce43425d440 · 7.7 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 if (field.getAnnotation(Transient.class) == null) {
70 try {
71 checkFrozenTypes(field);
72 } catch (IllegalArgumentException e) {
73 throw new IllegalArgumentException(String.format("Error while checking frozen types on field %s of %s %s: %s",
74 field.getName(), classDescription,
75 field.getDeclaringClass().getName(), e.getMessage()));
76 }
77 }
78 }
79
80 // Returns the offending annotation if there is one
81 private static Class<? extends Annotation> validateAnnotations(Annotation[] annotations, Class<? extends Annotation>... allowed) {
82 for (Annotation annotation : annotations) {
83 Class<? extends Annotation> actual = annotation.annotationType();
84 if (actual.getPackage().equals(MAPPING_PACKAGE) && !contains(allowed, actual))
85 return actual;
86 }
87 return null;
88 }
89
90 private static boolean contains(Object[] array, Object target) {
91 for (Object element : array)
92 if (element.equals(target))
93 return true;
94 return false;
95 }
96
97 static void checkFrozenTypes(Field field) {
98 Type javaType = field.getGenericType();
99 DeclaredFrozenType declaredFrozenType = getDeclaredFrozenType(field);
100 checkFrozenTypes(javaType, declaredFrozenType);
101 }
102
103 // Builds a DeclaredFrozenType hierarchy based on the @Frozen* annotations on a field.
104 private static DeclaredFrozenType getDeclaredFrozenType(Field field) {
105 Frozen frozen = field.getAnnotation(Frozen.class);
106 if (frozen != null)
107 return DeclaredFrozenType.parse(frozen.value());
108
109 boolean frozenKey = field.getAnnotation(FrozenKey.class) != null;
110 boolean frozenValue = field.getAnnotation(FrozenValue.class) != null;
111 if (frozenKey && frozenValue)
112 return DeclaredFrozenType.FROZEN_MAP_KEY_AND_VALUE;
113 else if (frozenKey)
114 return DeclaredFrozenType.FROZEN_MAP_KEY;
115 else if (frozenValue && field.getType().equals(Map.class))
116 return DeclaredFrozenType.FROZEN_MAP_VALUE;
117 else if (frozenValue)
118 return DeclaredFrozenType.FROZEN_ELEMENT;
119 else
120 return DeclaredFrozenType.UNFROZEN_SIMPLE;
121 }
122
123 // Traverses the Java type and CQLType hierarchies in parallel, to ensure that all
124 // Java types mapping to UDTs, tuples and nested collections are marked as frozen in the CQLType.
125 // We accept that parts of the CQLType be null, in which case the matching parts
126 // in the Java type will be considered as not frozen.
127 private static void checkFrozenTypes(Type javaType, DeclaredFrozenType declaredFrozenType) {
128 checkFrozenTypes(javaType, declaredFrozenType, true);
129 }
130
131 private static void checkFrozenTypes(Type javaType, DeclaredFrozenType declaredFrozenType, boolean isRoot) {
132 Class<?> javaClass;
133 Type[] childrenJavaTypes;
134 if (javaType instanceof Class<?>) {
135 javaClass = (Class<?>)javaType;
136 childrenJavaTypes = null;
137 } else if (javaType instanceof ParameterizedType){
138 ParameterizedType pt = (ParameterizedType)javaType;
139 javaClass = (Class<?>)pt.getRawType();
140 childrenJavaTypes = pt.getActualTypeArguments();
141 } else
142 throw new IllegalArgumentException("unexpected type: " + javaType);
143
144 boolean frozen = (declaredFrozenType != null && declaredFrozenType.frozen);
145 checkValidFrozen(javaClass, isRoot, frozen);
146
147 if (childrenJavaTypes != null) {
148 for (int i = 0; i < childrenJavaTypes.length; i++) {
149 Type childJavaType = childrenJavaTypes[i];
150 DeclaredFrozenType childDeclaredFrozenType = null;
151 if (declaredFrozenType != null && declaredFrozenType.subTypes != null && declaredFrozenType.subTypes.size() > i)
152 childDeclaredFrozenType = declaredFrozenType.subTypes.get(i);
153 checkFrozenTypes(childJavaType, childDeclaredFrozenType, false);
154 }
155 }
156 }
157
158 private static void checkValidFrozen(Class<?> clazz, boolean isRoot, boolean isDeclaredFrozen) {
159 boolean shouldBeFrozen = (TypeMappings.mapsToCollection(clazz) && !isRoot)
160 || TypeMappings.mapsToUserTypeOrTuple(clazz);
161 if (shouldBeFrozen != isDeclaredFrozen)
162 throw new IllegalArgumentException(String.format("expected %s to be %sfrozen but was %sfrozen",
163 clazz.getSimpleName(),
164 shouldBeFrozen ? "" : "not ",
165 isDeclaredFrozen ? "" : "not "));
166 }
167}