Skip to content

Commit

Permalink
Respond to Feedback
Browse files Browse the repository at this point in the history
- Ensure JDK Proxies are registered in the right place
- Demonstrate use of Spring Data projections
  • Loading branch information
jzheaux committed Sep 4, 2024
1 parent d7a3a22 commit c058eb7
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.util.ArrayList;
import java.util.Collection;
import java.util.stream.Collectors;

import javax.sql.DataSource;

Expand All @@ -26,6 +27,7 @@

import org.springframework.aot.generate.GenerationContext;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.TypeReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.aot.BeanFactoryInitializationCode;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
Expand Down Expand Up @@ -78,6 +80,10 @@ void findsAllNeededClassesToProxy() {
assertThat(canonicalNames).contains(
"org.springframework.security.config.annotation.method.configuration.aot.Message$$SpringCGLIB$$0",
"org.springframework.security.config.annotation.method.configuration.aot.User$$SpringCGLIB$$0");
assertThat(this.hints.proxies()
.jdkProxyHints()
.filter((hint) -> hint.getProxiedInterfaces().contains(TypeReference.of(UserProjection.class)))
.collect(Collectors.toList())).hasSize(1);
}

@Configuration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,7 @@ public interface MessageRepository extends CrudRepository<Message, Long> {
@Query("select m from Message m where m.to.id = ?#{ authentication.name }")
List<Message> findAll();

@Query("from org.springframework.security.config.annotation.method.configuration.aot.User u where u.id = ?#{ authentication.name }")
UserProjection findCurrentUser();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright 2002-2024 the original author or authors.
*
* 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 org.springframework.security.config.annotation.method.configuration.aot;

public interface UserProjection {

String getFirstName();

String getLastName();

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
package org.springframework.security.aot.hint;

import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.HashSet;
import java.util.Set;

import org.springframework.aop.SpringProxy;
import org.springframework.aot.hint.MemberCategory;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
Expand Down Expand Up @@ -79,6 +81,13 @@ private void traverseType(Class<?> clazz, RuntimeHints hints) {

private void registerProxy(RuntimeHints hints, Class<?> clazz) {
Class<?> proxied = (Class<?>) this.proxyFactory.proxy(clazz);
if (Proxy.isProxyClass(proxied)) {
hints.proxies().registerJdkProxy(proxied.getInterfaces());
return;
}
if (!SpringProxy.class.isAssignableFrom(proxied)) {
return;
}
hints.reflection()
.registerType(proxied, MemberCategory.INVOKE_PUBLIC_METHODS, MemberCategory.PUBLIC_FIELDS,
MemberCategory.DECLARED_FIELDS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
package org.springframework.security.data.aot.hint;

import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.HashSet;
import java.util.Set;

import org.springframework.aop.SpringProxy;
import org.springframework.aot.hint.MemberCategory;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
Expand Down Expand Up @@ -92,6 +94,13 @@ private void traverseType(RuntimeHints hints, Class<?> clazz) {

private void registerProxy(RuntimeHints hints, Class<?> clazz) {
Class<?> proxied = (Class<?>) this.proxyFactory.proxy(clazz);
if (Proxy.isProxyClass(proxied)) {
hints.proxies().registerJdkProxy(proxied.getInterfaces());
return;
}
if (!SpringProxy.class.isAssignableFrom(proxied)) {
return;
}
hints.reflection()
.registerType(proxied, MemberCategory.INVOKE_PUBLIC_METHODS, MemberCategory.PUBLIC_FIELDS,
MemberCategory.DECLARED_FIELDS);
Expand Down

0 comments on commit c058eb7

Please sign in to comment.