Skip to content

Commit 5b98f49

Browse files
reugntfaulkestimf
authored
Skip static final fields (#123)
* Static final field fix (#119) - Added a check to see if a field is static final and skip the field if it is. - Added test case. Co-authored-by: timf <[email protected]> * improve unit tests for static final fields Co-authored-by: Tim Faulkes <[email protected]> Co-authored-by: timf <[email protected]>
1 parent 8da6b64 commit 5b98f49

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-0
lines changed

src/main/java/com/aerospike/mapper/tools/ClassCacheEntry.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,10 @@ private void loadFieldsFromClass() {
630630
for (Field thisField : this.clazz.getDeclaredFields()) {
631631
boolean isKey = false;
632632
BinConfig thisBin = getBinFromField(thisField);
633+
if (Modifier.isFinal(thisField.getModifiers()) && Modifier.isStatic(thisField.getModifiers())) {
634+
// We cannot map static final fields
635+
continue;
636+
}
633637
if (thisField.isAnnotationPresent(AerospikeKey.class) || (!StringUtils.isBlank(keyField) && keyField.equals(thisField.getName()))) {
634638
if (thisField.isAnnotationPresent(AerospikeExclude.class) || (thisBin != null && thisBin.isExclude() != null && thisBin.isExclude())) {
635639
throw new AerospikeException("Class " + clazz.getName() + " cannot have a field which is both a key and excluded.");
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package com.aerospike.mapper;
2+
3+
import com.aerospike.mapper.annotations.AerospikeExclude;
4+
import com.aerospike.mapper.annotations.AerospikeGetter;
5+
import com.aerospike.mapper.annotations.AerospikeKey;
6+
import com.aerospike.mapper.annotations.AerospikeRecord;
7+
import com.aerospike.mapper.annotations.AerospikeSetter;
8+
import com.aerospike.mapper.tools.AeroMapper;
9+
import org.junit.jupiter.api.Test;
10+
11+
import java.util.Date;
12+
13+
import static org.junit.jupiter.api.Assertions.assertEquals;
14+
15+
public class FinalFieldMappingTest extends AeroMapperBaseTest {
16+
17+
@Test
18+
void test() {
19+
AeroMapper mapper = new AeroMapper.Builder(client).build();
20+
Customer customer = new Customer();
21+
customer.id = 1;
22+
customer.name = "tim";
23+
customer.dob = new Date();
24+
25+
Doc doc = new Doc();
26+
doc.id = 1;
27+
doc.data = "This is some data";
28+
29+
mapper.save(customer, doc);
30+
Customer readCustomer = mapper.read(Customer.class, customer.getKey());
31+
Doc readDoc = mapper.read(Doc.class, doc.getKey());
32+
assertEquals(doc.toString(), readDoc.toString());
33+
34+
ReferencingClass ref = new ReferencingClass();
35+
ref.id = 2;
36+
ref.cust = readCustomer;
37+
ref.doc = doc;
38+
mapper.save(null, ref);
39+
ReferencingClass readRef = mapper.read(ReferencingClass.class, ref.id);
40+
assertEquals(ref.toString(), readRef.toString());
41+
}
42+
43+
@AerospikeRecord(namespace = "test", set = "common")
44+
public static class Customer {
45+
@AerospikeExclude
46+
public static final String PREFIX = "CUST:";
47+
@AerospikeExclude
48+
private int id;
49+
private String name;
50+
private Date dob;
51+
52+
@AerospikeKey
53+
@AerospikeGetter(name = "key")
54+
public String getKey() {
55+
return PREFIX + this.id;
56+
}
57+
58+
@AerospikeKey(setter = true)
59+
@AerospikeSetter(name = "key")
60+
public void setKey(String key) {
61+
this.id = Integer.parseInt(key.substring(PREFIX.length()));
62+
}
63+
64+
@Override
65+
public String toString() {
66+
return String.format("{id=%d,name='%s',dob=%s}", id, name, dob);
67+
}
68+
}
69+
70+
@AerospikeRecord(namespace = "test", set = "common")
71+
public static class Doc {
72+
private static final String PREFIX = "DOC:";
73+
@AerospikeExclude
74+
public int id;
75+
private String data;
76+
77+
@AerospikeKey
78+
@AerospikeGetter(name = "key")
79+
public String getKey() {
80+
return PREFIX + this.id;
81+
}
82+
83+
@AerospikeKey(setter = true)
84+
@AerospikeSetter(name = "key")
85+
public void setKey(String key) {
86+
this.id = Integer.parseInt(key.substring(PREFIX.length()));
87+
}
88+
89+
@Override
90+
public String toString() {
91+
return String.format("{id=%d,data='%s'}", id, data);
92+
}
93+
}
94+
95+
@AerospikeRecord(namespace = "test", set = "ref")
96+
public static class ReferencingClass {
97+
@AerospikeKey
98+
public int id;
99+
public Customer cust;
100+
public Doc doc;
101+
102+
@Override
103+
public String toString() {
104+
return String.format("{id=%d,cust=%s,doc=%s}", id, cust, doc);
105+
}
106+
}
107+
}

0 commit comments

Comments
 (0)