Skip to content

Commit e77d0a3

Browse files
committed
To support jdk 16, add converters and module-info.
Closes #1057.
1 parent 7cde6b9 commit e77d0a3

File tree

9 files changed

+175
-19
lines changed

9 files changed

+175
-19
lines changed

pom.xml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
</parent>
1919

2020
<properties>
21-
<couchbase>3.1.6</couchbase>
22-
<couchbase.osgi>3.1.6</couchbase.osgi>
21+
<couchbase>3.2.0</couchbase>
22+
<couchbase.osgi>3.2.0</couchbase.osgi>
2323
<springdata.commons>2.6.0-SNAPSHOT</springdata.commons>
2424
<java-module-name>spring.data.couchbase</java-module-name>
2525
</properties>
@@ -273,6 +273,14 @@
273273
<groupId>org.asciidoctor</groupId>
274274
<artifactId>asciidoctor-maven-plugin</artifactId>
275275
</plugin>
276+
<plugin>
277+
<artifactId>maven-compiler-plugin</artifactId>
278+
<configuration>
279+
<source>9</source>
280+
<target>9</target>
281+
</configuration>
282+
</plugin>
283+
276284
</plugins>
277285
</build>
278286
</project>

src/main/java/module-info.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
module spring.data.couchbase {
2+
requires spring.tx;
3+
requires com.couchbase.client.core;
4+
requires com.couchbase.client.java;
5+
requires spring.data.commons;
6+
requires spring.beans;
7+
requires java.xml;
8+
requires spring.context;
9+
requires spring.core;
10+
requires reactor.core;
11+
requires slf4j.api;
12+
requires spring.expression;
13+
requires org.reactivestreams;
14+
requires validation.api;
15+
requires spring.aop;
16+
requires java.desktop;
17+
requires com.fasterxml.jackson.databind;
18+
requires spring.context.support;
19+
requires org.joda.time;
20+
21+
// mvn integration-test > opens.log 2>&2
22+
// awk -F'"' '/does not/ {print $2 ";"}' opens.log | sort -u
23+
24+
opens org.springframework.data.couchbase.core.mapping;
25+
opens org.springframework.data.couchbase.core.convert.translation;
26+
opens org.springframework.data.couchbase.core.query;
27+
opens org.springframework.data.couchbase.domain;
28+
opens org.springframework.data.couchbase.repository.query;
29+
opens org.springframework.data.couchbase.util;
30+
opens org.springframework.data.couchbase.repository;
31+
opens org.springframework.data.couchbase.core;
32+
33+
opens org.springframework.data.couchbase.config to spring.core;
34+
opens org.springframework.data.couchbase.repository.auditing to spring.core;
35+
36+
37+
exports org.springframework.data.couchbase.repository.config to spring.beans, spring.core;
38+
exports org.springframework.data.couchbase.repository;
39+
exports org.springframework.data.couchbase.config to spring.beans;
40+
exports org.springframework.data.couchbase.repository.support to spring.beans, spring.data.commons, spring.aop;
41+
exports org.springframework.data.couchbase.repository.auditing to spring.core, spring.beans;
42+
exports org.springframework.data.couchbase.core.mapping.event to spring.beans, spring.core;
43+
exports org.springframework.data.couchbase.core.mapping.id to spring.core; // comment out to reproduce NPE in validateAnnotation
44+
exports org.springframework.data.couchbase.core.index to spring.core;
45+
46+
}

src/main/java/org/springframework/data/couchbase/core/CouchbaseTemplateSupport.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public long getCas(final Object entity) {
133133

134134
long cas = 0;
135135
if (versionProperty != null) {
136-
Object casObject = (Number) accessor.getProperty(versionProperty);
136+
Object casObject = accessor.getProperty(versionProperty);
137137
if (casObject instanceof Number) {
138138
cas = ((Number) casObject).longValue();
139139
}

src/main/java/org/springframework/data/couchbase/core/ReactiveCouchbaseTemplateSupport.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ public Long getCas(final Object entity) {
140140

141141
long cas = 0;
142142
if (versionProperty != null) {
143-
Object casObject = (Number) accessor.getProperty(versionProperty);
143+
Object casObject = accessor.getProperty(versionProperty);
144144
if (casObject instanceof Number) {
145145
cas = ((Number) casObject).longValue();
146146
}

src/main/java/org/springframework/data/couchbase/core/convert/CouchbaseCustomConversions.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2020 the original author or authors.
2+
* Copyright 2017-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -50,6 +50,7 @@ public class CouchbaseCustomConversions extends org.springframework.data.convert
5050

5151
converters.addAll(DateConverters.getConvertersToRegister());
5252
converters.addAll(CouchbaseJsr310Converters.getConvertersToRegister());
53+
converters.addAll(OtherConverters.getConvertersToRegister());
5354

5455
STORE_CONVERTERS = Collections.unmodifiableList(converters);
5556
STORE_CONVERSIONS = StoreConversions.of(SimpleTypeHolder.DEFAULT, STORE_CONVERTERS);

src/main/java/org/springframework/data/couchbase/core/convert/MappingCouchbaseConverter.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors
2+
* Copyright 2012-2021 the original author or authors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -35,6 +35,7 @@
3535
import org.springframework.core.convert.ConversionService;
3636
import org.springframework.core.convert.support.DefaultConversionService;
3737
import org.springframework.data.annotation.Transient;
38+
import org.springframework.data.convert.CustomConversions;
3839
import org.springframework.data.convert.EntityInstantiator;
3940
import org.springframework.data.couchbase.core.mapping.CouchbaseDocument;
4041
import org.springframework.data.couchbase.core.mapping.CouchbaseList;
@@ -117,11 +118,7 @@ public class MappingCouchbaseConverter extends AbstractCouchbaseConverter implem
117118
private @Nullable EntityCallbacks entityCallbacks;
118119

119120
public MappingCouchbaseConverter() {
120-
super(new DefaultConversionService());
121-
122-
this.typeMapper = new DefaultCouchbaseTypeMapper(TYPEKEY_DEFAULT);
123-
this.mappingContext = new CouchbaseMappingContext();
124-
this.spELContext = new SpELContext(CouchbaseDocumentPropertyAccessor.INSTANCE);
121+
this(new CouchbaseMappingContext(), null);
125122
}
126123

127124
/**
@@ -131,7 +128,7 @@ public MappingCouchbaseConverter() {
131128
*/
132129
public MappingCouchbaseConverter(
133130
final MappingContext<? extends CouchbasePersistentEntity<?>, CouchbasePersistentProperty> mappingContext) {
134-
this(mappingContext, TYPEKEY_DEFAULT);
131+
this(mappingContext, null);
135132
}
136133

137134
/**
@@ -145,8 +142,14 @@ public MappingCouchbaseConverter(
145142
final MappingContext<? extends CouchbasePersistentEntity<?>, CouchbasePersistentProperty> mappingContext,
146143
final String typeKey) {
147144
super(new DefaultConversionService());
148-
149145
this.mappingContext = mappingContext;
146+
// this is how the MappingCouchbaseConverter gets the custom conversions.
147+
// the conversions Service gets them in afterPropertiesSet()
148+
CustomConversions customConversions = new CouchbaseCustomConversions(Collections.emptyList());
149+
this.setCustomConversions(customConversions);
150+
// if the mappingContext does not have the SimpleTypes, it will not know that they have converters, then it will
151+
// try to access the fields of the type and (maybe) fail with InaccessibleObjectException
152+
((CouchbaseMappingContext) mappingContext).setSimpleTypeHolder(customConversions.getSimpleTypeHolder());
150153
typeMapper = new DefaultCouchbaseTypeMapper(typeKey != null ? typeKey : TYPEKEY_DEFAULT);
151154
spELContext = new SpELContext(CouchbaseDocumentPropertyAccessor.INSTANCE);
152155
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright 2021 the original author or authors
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+
* https://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+
*/
16+
17+
package org.springframework.data.couchbase.core.convert;
18+
19+
import java.math.BigInteger;
20+
import java.util.ArrayList;
21+
import java.util.Collection;
22+
import java.util.List;
23+
import java.util.UUID;
24+
25+
import org.springframework.core.convert.converter.Converter;
26+
import org.springframework.data.convert.ReadingConverter;
27+
import org.springframework.data.convert.WritingConverter;
28+
29+
/**
30+
* Out of the box conversions for java dates and calendars.
31+
*
32+
* @author Michael Reiche
33+
*/
34+
public final class OtherConverters {
35+
36+
private OtherConverters() {}
37+
38+
/**
39+
* Returns all converters by this class that can be registered.
40+
*
41+
* @return the list of converters to register.
42+
*/
43+
public static Collection<Converter<?, ?>> getConvertersToRegister() {
44+
List<Converter<?, ?>> converters = new ArrayList<Converter<?, ?>>();
45+
46+
converters.add(UuidToString.INSTANCE);
47+
converters.add(StringToUuid.INSTANCE);
48+
converters.add(BigIntegerToString.INSTANCE);
49+
converters.add(StringToBigInteger.INSTANCE);
50+
51+
return converters;
52+
}
53+
54+
@WritingConverter public enum UuidToString implements Converter<UUID,String>
55+
{
56+
INSTANCE;
57+
58+
@Override
59+
public String convert(UUID source) {
60+
return source == null ? null : source.toString();
61+
}}
62+
63+
@ReadingConverter public enum StringToUuid implements Converter<String,UUID>{INSTANCE;
64+
65+
@Override
66+
public UUID convert(String source) {
67+
return source == null ? null : UUID.fromString(source);
68+
}}
69+
70+
@WritingConverter public enum BigIntegerToString implements Converter<BigInteger,String>{INSTANCE;
71+
72+
@Override
73+
public String convert(BigInteger source) {
74+
return source == null ? null : source.toString();
75+
}}
76+
77+
@ReadingConverter public enum StringToBigInteger implements Converter<String,BigInteger>{INSTANCE;
78+
79+
@Override
80+
public BigInteger convert(String source) {
81+
return source == null ? null : new BigInteger(source);
82+
}
83+
}}

src/main/java/org/springframework/data/couchbase/core/mapping/CouchbaseMappingContext.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors
2+
* Copyright 2012-2021 the original author or authors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,13 +16,15 @@
1616

1717
package org.springframework.data.couchbase.core.mapping;
1818

19+
import java.lang.reflect.InaccessibleObjectException;
1920
import java.util.Optional;
2021

2122
import org.springframework.beans.BeansException;
2223
import org.springframework.context.ApplicationContext;
2324
import org.springframework.context.ApplicationContextAware;
2425
import org.springframework.context.ApplicationEventPublisher;
2526
import org.springframework.data.couchbase.core.index.CouchbasePersistentEntityIndexCreator;
27+
import org.springframework.data.mapping.MappingException;
2628
import org.springframework.data.mapping.context.AbstractMappingContext;
2729
import org.springframework.data.mapping.context.MappingContextEvent;
2830
import org.springframework.data.mapping.model.FieldNamingStrategy;
@@ -131,12 +133,17 @@ public void setAutoIndexCreation(boolean autoCreateIndexes) {
131133
/**
132134
* override method from AbstractMappingContext as that method will not publishEvent() if it finds the entity has
133135
* already been cached
134-
*
136+
*
135137
* @param typeInformation - entity type
136138
*/
137139
@Override
138140
protected Optional<BasicCouchbasePersistentEntity<?>> addPersistentEntity(TypeInformation<?> typeInformation) {
139-
Optional<BasicCouchbasePersistentEntity<?>> entity = super.addPersistentEntity(typeInformation);
141+
Optional<BasicCouchbasePersistentEntity<?>> entity = null;
142+
try {
143+
entity = super.addPersistentEntity(typeInformation);
144+
} catch (InaccessibleObjectException ioe) {
145+
throw new MappingException("due to InaccessibleObjectException", ioe);
146+
}
140147

141148
if (this.eventPublisher != null && entity.isPresent()) {
142149
if (this.indexCreator != null) {

src/test/java/org/springframework/data/couchbase/core/mapping/MappingCouchbaseConverterTests.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.springframework.core.convert.converter.Converter;
3030
import org.springframework.data.annotation.Id;
3131
import org.springframework.data.annotation.TypeAlias;
32+
import org.springframework.data.convert.CustomConversions;
3233
import org.springframework.data.convert.ReadingConverter;
3334
import org.springframework.data.convert.WritingConverter;
3435
import org.springframework.data.couchbase.core.convert.CouchbaseCustomConversions;
@@ -421,8 +422,10 @@ void writesAndReadsCustomConvertedClass() {
421422
List<Object> converters = new ArrayList<>();
422423
converters.add(BigDecimalToStringConverter.INSTANCE);
423424
converters.add(StringToBigDecimalConverter.INSTANCE);
424-
converter.setCustomConversions(new CouchbaseCustomConversions(converters));
425+
CustomConversions customConversions = new CouchbaseCustomConversions(converters);
426+
converter.setCustomConversions(customConversions);
425427
converter.afterPropertiesSet();
428+
((CouchbaseMappingContext) converter.getMappingContext()).setSimpleTypeHolder(customConversions.getSimpleTypeHolder());
426429

427430
CouchbaseDocument converted = new CouchbaseDocument();
428431

@@ -469,8 +472,11 @@ void writesAndReadsCustomFieldsConvertedClass() {
469472
List<Object> converters = new ArrayList<>();
470473
converters.add(BigDecimalToStringConverter.INSTANCE);
471474
converters.add(StringToBigDecimalConverter.INSTANCE);
472-
converter.setCustomConversions(new CouchbaseCustomConversions(converters));
475+
CustomConversions customConversions = new CouchbaseCustomConversions(converters);
476+
converter.setCustomConversions(customConversions);
473477
converter.afterPropertiesSet();
478+
((CouchbaseMappingContext) converter.getMappingContext()).setSimpleTypeHolder(customConversions.getSimpleTypeHolder());
479+
474480

475481
CouchbaseDocument converted = new CouchbaseDocument();
476482

@@ -517,8 +523,10 @@ void writesAndReadsClassContainingCustomConvertedObjects() {
517523
List<Object> converters = new ArrayList<>();
518524
converters.add(BigDecimalToStringConverter.INSTANCE);
519525
converters.add(StringToBigDecimalConverter.INSTANCE);
520-
converter.setCustomConversions(new CouchbaseCustomConversions(converters));
526+
CustomConversions customConversions = new CouchbaseCustomConversions(converters);
527+
converter.setCustomConversions(customConversions);
521528
converter.afterPropertiesSet();
529+
((CouchbaseMappingContext) converter.getMappingContext()).setSimpleTypeHolder(customConversions.getSimpleTypeHolder());
522530

523531
CouchbaseDocument converted = new CouchbaseDocument();
524532

0 commit comments

Comments
 (0)