Repository navigation
Expand file tree
/
Copy pathAnnotationChecks.java
More file actions
167 lines (147 loc) · 7.74 KB
/
Copy pathAnnotationChecks.java
File metadata and controls
167 lines (147 loc) · 7.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
* Copyright (C) 2012-2015 DataStax Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.datastax.driver.mapping;
import java.lang.annotation.Annotation;
import java.lang.reflect.*;
import java.lang.reflect.Field;
import java.util.*;
import com.datastax.driver.mapping.annotations.*;
/**
* Various checks on mapping annotations.
*/
class AnnotationChecks {
// The package containing the mapping annotations
private static final Package MAPPING_PACKAGE = Table.class.getPackage();
/**
* Checks that a class is decorated with the given annotation, and return the annotation instance.
* Also validates that no other mapping annotation is present.
*/
static <T extends Annotation> T getTypeAnnotation(Class<T> annotation, Class<?> annotatedClass) {
T instance = annotatedClass.getAnnotation(annotation);
if (instance == null)
throw new IllegalArgumentException(String.format("@%s annotation was not found on type %s",
annotation.getSimpleName(), annotatedClass.getName()));
// Check that no other mapping annotations are present
validateAnnotations(annotatedClass, annotation);
return instance;
}
private static void validateAnnotations(Class<?> clazz, Class<? extends Annotation> allowed) {
@SuppressWarnings("unchecked")
Class<? extends Annotation> invalid = validateAnnotations(clazz.getAnnotations(), allowed);
if (invalid != null)
throw new IllegalArgumentException(String.format("Cannot have both @%s and @%s on type %s",
allowed.getSimpleName(), invalid.getSimpleName(),
clazz.getName()));
}
/**
* Checks that a field is only annotated with the given mapping annotations, and that its "frozen" annotations are valid.
*/
static void validateAnnotations(Field field, String classDescription, Class<? extends Annotation>... allowed) {
Class<? extends Annotation> invalid = validateAnnotations(field.getAnnotations(), allowed);
if (invalid != null)
throw new IllegalArgumentException(String.format("Annotation @%s is not allowed on field %s of %s %s",
invalid.getSimpleName(),
field.getName(), classDescription,
field.getDeclaringClass().getName()));
if (field.getAnnotation(Transient.class) == null) {
try {
checkFrozenTypes(field);
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException(String.format("Error while checking frozen types on field %s of %s %s: %s",
field.getName(), classDescription,
field.getDeclaringClass().getName(), e.getMessage()));
}
}
}
// Returns the offending annotation if there is one
private static Class<? extends Annotation> validateAnnotations(Annotation[] annotations, Class<? extends Annotation>... allowed) {
for (Annotation annotation : annotations) {
Class<? extends Annotation> actual = annotation.annotationType();
if (actual.getPackage().equals(MAPPING_PACKAGE) && !contains(allowed, actual))
return actual;
}
return null;
}
private static boolean contains(Object[] array, Object target) {
for (Object element : array)
if (element.equals(target))
return true;
return false;
}
static void checkFrozenTypes(Field field) {
Type javaType = field.getGenericType();
DeclaredFrozenType declaredFrozenType = getDeclaredFrozenType(field);
checkFrozenTypes(javaType, declaredFrozenType);
}
// Builds a DeclaredFrozenType hierarchy based on the @Frozen* annotations on a field.
private static DeclaredFrozenType getDeclaredFrozenType(Field field) {
Frozen frozen = field.getAnnotation(Frozen.class);
if (frozen != null)
return DeclaredFrozenType.parse(frozen.value());
boolean frozenKey = field.getAnnotation(FrozenKey.class) != null;
boolean frozenValue = field.getAnnotation(FrozenValue.class) != null;
if (frozenKey && frozenValue)
return DeclaredFrozenType.FROZEN_MAP_KEY_AND_VALUE;
else if (frozenKey)
return DeclaredFrozenType.FROZEN_MAP_KEY;
else if (frozenValue && field.getType().equals(Map.class))
return DeclaredFrozenType.FROZEN_MAP_VALUE;
else if (frozenValue)
return DeclaredFrozenType.FROZEN_ELEMENT;
else
return DeclaredFrozenType.UNFROZEN_SIMPLE;
}
// Traverses the Java type and CQLType hierarchies in parallel, to ensure that all
// Java types mapping to UDTs, tuples and nested collections are marked as frozen in the CQLType.
// We accept that parts of the CQLType be null, in which case the matching parts
// in the Java type will be considered as not frozen.
private static void checkFrozenTypes(Type javaType, DeclaredFrozenType declaredFrozenType) {
checkFrozenTypes(javaType, declaredFrozenType, true);
}
private static void checkFrozenTypes(Type javaType, DeclaredFrozenType declaredFrozenType, boolean isRoot) {
Class<?> javaClass;
Type[] childrenJavaTypes;
if (javaType instanceof Class<?>) {
javaClass = (Class<?>)javaType;
childrenJavaTypes = null;
} else if (javaType instanceof ParameterizedType){
ParameterizedType pt = (ParameterizedType)javaType;
javaClass = (Class<?>)pt.getRawType();
childrenJavaTypes = pt.getActualTypeArguments();
} else
throw new IllegalArgumentException("unexpected type: " + javaType);
boolean frozen = (declaredFrozenType != null && declaredFrozenType.frozen);
checkValidFrozen(javaClass, isRoot, frozen);
if (childrenJavaTypes != null) {
for (int i = 0; i < childrenJavaTypes.length; i++) {
Type childJavaType = childrenJavaTypes[i];
DeclaredFrozenType childDeclaredFrozenType = null;
if (declaredFrozenType != null && declaredFrozenType.subTypes != null && declaredFrozenType.subTypes.size() > i)
childDeclaredFrozenType = declaredFrozenType.subTypes.get(i);
checkFrozenTypes(childJavaType, childDeclaredFrozenType, false);
}
}
}
private static void checkValidFrozen(Class<?> clazz, boolean isRoot, boolean isDeclaredFrozen) {
boolean shouldBeFrozen = (TypeMappings.mapsToCollection(clazz) && !isRoot)
|| TypeMappings.mapsToUserTypeOrTuple(clazz);
if (shouldBeFrozen != isDeclaredFrozen)
throw new IllegalArgumentException(String.format("expected %s to be %sfrozen but was %sfrozen",
clazz.getSimpleName(),
shouldBeFrozen ? "" : "not ",
isDeclaredFrozen ? "" : "not "));
}
}