Skip to content

Commit 78a90bf

Browse files
committed
Disable frozen checks in mapper (JAVA-843)
1 parent f4f8c01 commit 78a90bf

8 files changed

Lines changed: 19 additions & 485 deletions

File tree

changelog/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
- [bug] TypeCodec.getDataTypeFor() does not handle LocalDate instances (JAVA-818)
99
- [improvement] Make ResultSet#fetchMoreResult return a
1010
ListenableFuture<ResultSet> (JAVA-836)
11+
- [improvement] Disable frozen checks in mapper (JAVA-843)
1112

1213
Merged from 2.1 branch:
1314

driver-mapping/src/main/java/com/datastax/driver/mapping/AnnotationChecks.java

Lines changed: 0 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,6 @@ static void validateAnnotations(Field field, String classDescription, Class<? ex
6666
field.getName(), classDescription,
6767
field.getDeclaringClass().getName()));
6868

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-
}
7869
checkValidComputed(field);
7970
}
8071

@@ -95,81 +86,10 @@ private static boolean contains(Object[] array, Object target) {
9586
return false;
9687
}
9788

98-
static void checkFrozenTypes(Field field) {
99-
Type javaType = field.getGenericType();
100-
DeclaredFrozenType declaredFrozenType = getDeclaredFrozenType(field);
101-
checkFrozenTypes(javaType, declaredFrozenType);
102-
}
103-
10489
static void checkValidComputed(Field field) {
10590
Computed computed = field.getAnnotation(Computed.class);
10691
if (computed != null && computed.value().isEmpty()){
10792
throw new IllegalArgumentException(String.format("Field %s: attribute 'value' of annotation @Computed is mandatory for computed fields", field.getName()));
10893
}
10994
}
110-
111-
// Builds a DeclaredFrozenType hierarchy based on the @Frozen* annotations on a field.
112-
private static DeclaredFrozenType getDeclaredFrozenType(Field field) {
113-
Frozen frozen = field.getAnnotation(Frozen.class);
114-
if (frozen != null)
115-
return DeclaredFrozenType.parse(frozen.value());
116-
117-
boolean frozenKey = field.getAnnotation(FrozenKey.class) != null;
118-
boolean frozenValue = field.getAnnotation(FrozenValue.class) != null;
119-
if (frozenKey && frozenValue)
120-
return DeclaredFrozenType.FROZEN_MAP_KEY_AND_VALUE;
121-
else if (frozenKey)
122-
return DeclaredFrozenType.FROZEN_MAP_KEY;
123-
else if (frozenValue && field.getType().equals(Map.class))
124-
return DeclaredFrozenType.FROZEN_MAP_VALUE;
125-
else if (frozenValue)
126-
return DeclaredFrozenType.FROZEN_ELEMENT;
127-
else
128-
return DeclaredFrozenType.UNFROZEN_SIMPLE;
129-
}
130-
131-
// Traverses the Java type and CQLType hierarchies in parallel, to ensure that all
132-
// Java types mapping to UDTs, tuples and nested collections are marked as frozen in the CQLType.
133-
// We accept that parts of the CQLType be null, in which case the matching parts
134-
// in the Java type will be considered as not frozen.
135-
private static void checkFrozenTypes(Type javaType, DeclaredFrozenType declaredFrozenType) {
136-
checkFrozenTypes(javaType, declaredFrozenType, true);
137-
}
138-
139-
private static void checkFrozenTypes(Type javaType, DeclaredFrozenType declaredFrozenType, boolean isRoot) {
140-
Class<?> javaClass;
141-
Type[] childrenJavaTypes;
142-
if (javaType instanceof Class<?>) {
143-
javaClass = (Class<?>)javaType;
144-
childrenJavaTypes = null;
145-
} else if (javaType instanceof ParameterizedType){
146-
ParameterizedType pt = (ParameterizedType)javaType;
147-
javaClass = (Class<?>)pt.getRawType();
148-
childrenJavaTypes = pt.getActualTypeArguments();
149-
} else
150-
throw new IllegalArgumentException("unexpected type: " + javaType);
151-
152-
boolean frozen = (declaredFrozenType != null && declaredFrozenType.frozen);
153-
checkValidFrozen(javaClass, isRoot, frozen);
154-
155-
if (childrenJavaTypes != null) {
156-
for (int i = 0; i < childrenJavaTypes.length; i++) {
157-
Type childJavaType = childrenJavaTypes[i];
158-
DeclaredFrozenType childDeclaredFrozenType = null;
159-
if (declaredFrozenType != null && declaredFrozenType.subTypes != null && declaredFrozenType.subTypes.size() > i)
160-
childDeclaredFrozenType = declaredFrozenType.subTypes.get(i);
161-
checkFrozenTypes(childJavaType, childDeclaredFrozenType, false);
162-
}
163-
}
164-
}
165-
166-
private static void checkValidFrozen(Class<?> clazz, boolean isRoot, boolean isDeclaredFrozen) {
167-
boolean shouldBeFrozen = (TypeMappings.mapsToCollection(clazz) && !isRoot)
168-
|| TypeMappings.mapsToUserTypeOrTuple(clazz);
169-
if (shouldBeFrozen != isDeclaredFrozen)
170-
throw new IllegalArgumentException(String.format("expected %s to be %sfrozen but was %sfrozen",
171-
clazz.getSimpleName(),
172-
shouldBeFrozen ? "" : "not ",
173-
isDeclaredFrozen ? "" : "not "));
174-
}
17595
}

driver-mapping/src/main/java/com/datastax/driver/mapping/DeclaredFrozenType.java

Lines changed: 0 additions & 185 deletions
This file was deleted.

driver-mapping/src/main/java/com/datastax/driver/mapping/annotations/Frozen.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@
2626
* Specifies that the field decorated with this annotation maps to a CQL type that is {@link DataType#isFrozen() frozen},
2727
* or contains frozen subtypes.
2828
* <p>
29-
* This annotation is purely informational at this stage, but will become useful when a schema generation feature is
30-
* added to the mapper.
29+
* This annotation is purely informational at this stage, the validity of the declaration is not checked.
30+
* But will become useful when a schema generation feature is added to the mapper. Therefore it is a good idea to keep
31+
* frozen declarations up-to-date.
3132
*
3233
* @see FrozenKey
3334
* @see FrozenValue
@@ -36,6 +37,8 @@
3637
@Retention(RetentionPolicy.RUNTIME)
3738
public @interface Frozen {
3839

40+
// Implementation note: frozen annotations were previously checked at runtime, this code can be found in version 2.1.7
41+
3942
/**
4043
* Contains the full CQL type of the target column. As a convenience, this can be left out when only the top-level
4144
* type is frozen.

0 commit comments

Comments
 (0)