Skip to content

Commit

Permalink
fix: add runtime hints for spanner (#2123)
Browse files Browse the repository at this point in the history
* fix: add runtime hints for spanner module
  • Loading branch information
mpeddada1 authored Sep 1, 2023
1 parent c92260f commit 170948e
Show file tree
Hide file tree
Showing 13 changed files with 247 additions and 22 deletions.
4 changes: 3 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@
<modules>
<module>spring-cloud-gcp-storage</module>
<module>spring-cloud-gcp-vision</module>
<module>spring-cloud-gcp-data-spanner</module>
</modules>

<dependencies>
Expand All @@ -380,8 +381,9 @@
<classesDirectory>${project.build.outputDirectory}</classesDirectory>
<systemPropertyVariables>
<!--integration tests are not invoked unless the relevant system property is set to true here. -->
<it.storage>true</it.storage>
<it.storage>true</it.storage>
<it.vision>true</it.vision>
<it.spanner>true</it.spanner>
</systemPropertyVariables>
</configuration>
</plugin>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2023 Google LLC
*
* 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
*
* https://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.google.cloud.spring.data.spanner.aot;

import java.util.Arrays;
import org.springframework.aot.hint.MemberCategory;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.RuntimeHintsRegistrar;
import org.springframework.aot.hint.TypeReference;

public class SpannerRepositoryRuntimeHints implements RuntimeHintsRegistrar {

@Override
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
hints
.reflection()
.registerTypes(
Arrays.asList(
TypeReference.of(
com.google.cloud.spring.data.spanner.repository.support.SimpleSpannerRepository
.class)),
hint ->
hint.withMembers(
MemberCategory.INVOKE_DECLARED_CONSTRUCTORS,
MemberCategory.INVOKE_DECLARED_METHODS));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2023 Google LLC
*
* 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
*
* https://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.google.cloud.spring.data.spanner.aot;

import java.util.Arrays;
import org.springframework.aot.hint.MemberCategory;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.RuntimeHintsRegistrar;
import org.springframework.aot.hint.TypeReference;

public class SpannerSchemaUtilsRuntimeHints implements RuntimeHintsRegistrar {

@Override
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
// Called reflectively by org.springframework.expression.spel.support.ReflectiveMethodExecutor
// which is invoked transitively by SpannerPersistentEntityImpl#tableName() from
// SpannerSchemaUtils.
hints
.reflection()
.registerTypes(
Arrays.asList(TypeReference.of(java.lang.String.class)),
hint ->
hint.withMembers(
MemberCategory.INVOKE_DECLARED_CONSTRUCTORS,
MemberCategory.INVOKE_DECLARED_METHODS));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.google.cloud.spanner.Key;
import com.google.cloud.spanner.Type;
import com.google.cloud.spring.data.spanner.aot.SpannerSchemaUtilsRuntimeHints;
import com.google.cloud.spring.data.spanner.core.convert.ConversionUtils;
import com.google.cloud.spring.data.spanner.core.convert.SpannerEntityProcessor;
import com.google.cloud.spring.data.spanner.core.convert.SpannerTypeMapper;
Expand All @@ -32,6 +33,7 @@
import java.util.Set;
import java.util.StringJoiner;
import java.util.function.BiFunction;
import org.springframework.context.annotation.ImportRuntimeHints;
import org.springframework.data.mapping.PropertyHandler;
import org.springframework.util.Assert;

Expand All @@ -40,6 +42,7 @@
*
* @since 1.1
*/
@ImportRuntimeHints(SpannerSchemaUtilsRuntimeHints.class)
public class SpannerSchemaUtils {

private final SpannerMappingContext mappingContext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@

package com.google.cloud.spring.data.spanner.repository.support;

import com.google.cloud.spring.data.spanner.aot.SpannerRepositoryRuntimeHints;
import com.google.cloud.spring.data.spanner.core.SpannerTemplate;
import com.google.cloud.spring.data.spanner.core.mapping.SpannerMappingContext;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.ImportRuntimeHints;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport;
import org.springframework.data.repository.core.support.RepositoryFactorySupport;
Expand All @@ -34,6 +36,7 @@
* @param <T> the repository type
* @since 1.1
*/
@ImportRuntimeHints(SpannerRepositoryRuntimeHints.class)
public class SpannerRepositoryFactoryBean<T extends Repository<S, I>, S, I>
extends RepositoryFactoryBeanSupport<T, S, I> implements ApplicationContextAware {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2023 Google LLC
*
* 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
*
* https://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.google.cloud.spring.data.spanner.aot;

import java.util.Arrays;
import org.springframework.aot.hint.MemberCategory;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.RuntimeHintsRegistrar;
import org.springframework.aot.hint.TypeReference;

public class CommitTimestampsRuntimeHints implements RuntimeHintsRegistrar {
@Override
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
hints
.reflection()
.registerTypes(
Arrays.asList(
TypeReference.of(
com.google.cloud.spring.data.spanner.test.domain.CommitTimestamps.class)),
hint ->
hint.withMembers(
MemberCategory.INVOKE_DECLARED_CONSTRUCTORS,
MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS,
MemberCategory.DECLARED_CLASSES,
MemberCategory.DECLARED_FIELDS));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 2023 Google LLC
*
* 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
*
* https://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.google.cloud.spring.data.spanner.aot;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.springframework.aot.hint.predicate.RuntimeHintsPredicates.reflection;

import com.google.cloud.spring.data.spanner.test.domain.CommitTimestamps;
import org.junit.jupiter.api.Test;
import org.springframework.aot.hint.RuntimeHints;

class CommitTimestampsRuntimeHintsTests {
@Test
void registerCommitTimestamps() {
RuntimeHints runtimeHints = new RuntimeHints();
CommitTimestampsRuntimeHints registrar = new CommitTimestampsRuntimeHints();
registrar.registerHints(runtimeHints, null);
assertThat(runtimeHints).matches(reflection().onType(CommitTimestamps.class));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2023 Google LLC
*
* 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
*
* https://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.google.cloud.spring.data.spanner.aot;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.springframework.aot.hint.predicate.RuntimeHintsPredicates.reflection;

import com.google.cloud.spring.data.spanner.repository.support.SimpleSpannerRepository;
import org.junit.jupiter.api.Test;
import org.springframework.aot.hint.RuntimeHints;

class SpannerRepositoryRuntimeHintsTests {

@Test
void registerSimpleSpannerRepository() {
RuntimeHints runtimeHints = new RuntimeHints();
SpannerRepositoryRuntimeHints registrar = new SpannerRepositoryRuntimeHints();
registrar.registerHints(runtimeHints, null);
assertThat(runtimeHints).matches(reflection().onType(SimpleSpannerRepository.class));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2023 Google LLC
*
* 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
*
* https://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.google.cloud.spring.data.spanner.aot;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.springframework.aot.hint.predicate.RuntimeHintsPredicates.reflection;

import org.junit.jupiter.api.Test;
import org.springframework.aot.hint.RuntimeHints;

class SpannerSchemaUtilsRuntimeHintsTests {
@Test
void registerString() {
RuntimeHints runtimeHints = new RuntimeHints();
SpannerSchemaUtilsRuntimeHints registrar = new SpannerSchemaUtilsRuntimeHints();
registrar.registerHints(runtimeHints, null);
assertThat(runtimeHints)
.matches(
reflection().onType(String.class));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.google.cloud.Timestamp;
import com.google.cloud.spanner.DatabaseClient;
import com.google.cloud.spanner.Key;
import com.google.cloud.spring.data.spanner.aot.CommitTimestampsRuntimeHints;
import com.google.cloud.spring.data.spanner.core.SpannerMutationFactory;
import com.google.cloud.spring.data.spanner.core.SpannerOperations;
import com.google.cloud.spring.data.spanner.core.convert.CommitTimestamp;
Expand All @@ -40,12 +41,14 @@
import org.junit.jupiter.api.condition.EnabledIfSystemProperty;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ImportRuntimeHints;
import org.springframework.core.convert.converter.Converter;
import org.springframework.test.context.junit.jupiter.SpringExtension;

/** Integration tests for the {@link CommitTimestamp} feature. */
@EnabledIfSystemProperty(named = "it.spanner", matches = "true")
@ExtendWith(SpringExtension.class)
@ImportRuntimeHints(CommitTimestampsRuntimeHints.class)
class CommitTimestampIntegrationTests extends AbstractSpannerIntegrationTest {

@Autowired private SpannerOperations spannerOperations;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package com.google.cloud.spring.data.spanner.repository.it;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assumptions.assumeThat;

import com.google.cloud.spring.data.spanner.core.SpannerTemplate;
import com.google.cloud.spring.data.spanner.core.admin.SpannerDatabaseAdminTemplate;
Expand Down
Loading

0 comments on commit 170948e

Please sign in to comment.