Remove support for declaring class proxies hints

Closes gh-28972
This commit is contained in:
Stephane Nicoll 2022-08-17 09:25:03 +02:00
parent f4389c3114
commit 9846d28ae6
4 changed files with 1 additions and 341 deletions

View File

@ -1,172 +0,0 @@
/*
* Copyright 2002-2022 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.aot.hint;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import org.springframework.lang.Nullable;
/**
* A hint that describes the need for a proxy against a concrete class.
*
* @author Stephane Nicoll
* @author Brian Clozel
* @since 6.0
*/
public final class ClassProxyHint implements ConditionalHint {
private final TypeReference targetClass;
private final List<TypeReference> proxiedInterfaces;
@Nullable
private final TypeReference reachableType;
private ClassProxyHint(Builder builder) {
this.targetClass = builder.targetClass;
this.proxiedInterfaces = builder.proxiedInterfaces.stream().distinct().toList();
this.reachableType = builder.reachableType;
}
/**
* Initialize a builder with the target class to use.
* @param typeReference the type reference for the target class of the proxy
* @return a builder for the hint
*/
public static Builder of(TypeReference typeReference) {
return new Builder(typeReference);
}
/**
* Initialize a builder with the target class to use.
* @param targetClass the target class of the proxy
* @return a builder for the hint
*/
public static Builder of(Class<?> targetClass) {
if (targetClass.isInterface()) {
throw new IllegalArgumentException("Should not be an interface: " + targetClass);
}
return of(TypeReference.of(targetClass));
}
/**
* Return the target class of the proxy.
* @return the target class
*/
public TypeReference getTargetClass() {
return this.targetClass;
}
/**
* Return the interfaces to be proxied.
* @return the interfaces that the proxy should implement
*/
public List<TypeReference> getProxiedInterfaces() {
return this.proxiedInterfaces;
}
@Nullable
@Override
public TypeReference getReachableType() {
return this.reachableType;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
ClassProxyHint that = (ClassProxyHint) o;
return this.targetClass.equals(that.targetClass)
&& this.proxiedInterfaces.equals(that.proxiedInterfaces)
&& Objects.equals(this.reachableType, that.reachableType);
}
@Override
public int hashCode() {
return Objects.hash(this.targetClass, this.proxiedInterfaces);
}
/**
* Builder for {@link ClassProxyHint}.
*/
public static class Builder {
private final TypeReference targetClass;
private final LinkedList<TypeReference> proxiedInterfaces = new LinkedList<>();
@Nullable
private TypeReference reachableType;
Builder(TypeReference targetClass) {
this.targetClass = targetClass;
}
/**
* Add the specified interfaces that the proxy should implement.
* @param proxiedInterfaces the interfaces the proxy should implement
* @return {@code this}, to facilitate method chaining
*/
public Builder proxiedInterfaces(TypeReference... proxiedInterfaces) {
this.proxiedInterfaces.addAll(Arrays.asList(proxiedInterfaces));
return this;
}
/**
* Add the specified interfaces that the proxy should implement.
* @param proxiedInterfaces the interfaces the proxy should implement
* @return {@code this}, to facilitate method chaining
*/
public Builder proxiedInterfaces(Class<?>... proxiedInterfaces) {
this.proxiedInterfaces.addAll(Arrays.stream(proxiedInterfaces)
.map(TypeReference::of).toList());
return this;
}
/**
* Make this hint conditional on the fact that the specified type
* can be resolved.
* @param reachableType the type that should be reachable for this
* hint to apply
* @return {@code this}, to facilitate method chaining
*/
public Builder onReachableType(TypeReference reachableType) {
this.reachableType = reachableType;
return this;
}
/**
* Create a {@link ClassProxyHint} based on the state of this builder.
* @return a class proxy hint
*/
ClassProxyHint build() {
return new ClassProxyHint(this);
}
}
}

View File

@ -21,8 +21,6 @@ import java.util.Set;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.stream.Stream; import java.util.stream.Stream;
import org.springframework.aot.hint.ClassProxyHint.Builder;
/** /**
* Gather the need for using proxies at runtime. * Gather the need for using proxies at runtime.
* *
@ -33,8 +31,6 @@ public class ProxyHints {
private final Set<JdkProxyHint> jdkProxies = new LinkedHashSet<>(); private final Set<JdkProxyHint> jdkProxies = new LinkedHashSet<>();
private final Set<ClassProxyHint> classProxies = new LinkedHashSet<>();
/** /**
* Return the interface-based proxies that are required. * Return the interface-based proxies that are required.
@ -44,14 +40,6 @@ public class ProxyHints {
return this.jdkProxies.stream(); return this.jdkProxies.stream();
} }
/**
* Return the class-based proxies that are required.
* @return a stream of {@link ClassProxyHint}
*/
public Stream<ClassProxyHint> classProxies() {
return this.classProxies.stream();
}
/** /**
* Register a {@link JdkProxyHint}. * Register a {@link JdkProxyHint}.
* @param jdkProxyHint the consumer of the hint builder * @param jdkProxyHint the consumer of the hint builder
@ -90,31 +78,4 @@ public class ProxyHints {
jdkProxyHint.proxiedInterfaces(proxiedInterfaces)); jdkProxyHint.proxiedInterfaces(proxiedInterfaces));
} }
/**
* Register that a class proxy is required for the class defined by the
* specified {@link TypeReference}.
* @param typeReference the type reference for the target class of the proxy
* @param classProxyHint a builder to further customize the hint for that proxy
* @return {@code this}, to facilitate method chaining
*/
public ProxyHints registerClassProxy(TypeReference typeReference, Consumer<Builder> classProxyHint) {
return addClassProxyHint(ClassProxyHint.of(typeReference), classProxyHint);
}
/**
* Register that a class proxy is required for the specified class.
* @param targetClass the target class of the proxy
* @param classProxyHint a builder to further customize the hint for that proxy
* @return {@code this}, to facilitate method chaining
*/
public ProxyHints registerClassProxy(Class<?> targetClass, Consumer<Builder> classProxyHint) {
return addClassProxyHint(ClassProxyHint.of(targetClass), classProxyHint);
}
private ProxyHints addClassProxyHint(ClassProxyHint.Builder builder, Consumer<ClassProxyHint.Builder> classProxyHint) {
classProxyHint.accept(builder);
this.classProxies.add(builder.build());
return this;
}
} }

View File

@ -1,101 +0,0 @@
/*
* Copyright 2002-2022 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.aot.hint;
import java.io.Closeable;
import java.io.Serializable;
import java.util.Hashtable;
import java.util.Properties;
import java.util.function.Function;
import org.junit.jupiter.api.Test;
import org.springframework.aot.hint.JdkProxyHint.Builder;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link ClassProxyHint}.
*
* @author Stephane Nicoll
* @author Brian Clozel
*/
class ClassProxyHintTests {
@Test
void equalsWithSameInstanceIsTrue() {
ClassProxyHint hint = ClassProxyHint.of(Properties.class).build();
assertThat(hint).isEqualTo(hint);
}
@Test
void equalsWithSameTargetClassIsTrue() {
ClassProxyHint first = ClassProxyHint.of(Properties.class).build();
ClassProxyHint second = ClassProxyHint.of(TypeReference.of(Properties.class)).build();
assertThat(first).isEqualTo(second);
}
@Test
void equalsWithSameProxiedInterfacesIsTrue() {
ClassProxyHint first = ClassProxyHint.of(Properties.class)
.proxiedInterfaces(Serializable.class).build();
ClassProxyHint second = ClassProxyHint.of(Properties.class)
.proxiedInterfaces(TypeReference.of(Serializable.class)).build();
assertThat(first).isEqualTo(second);
}
@Test
void equalsWithDifferentTargetClassIsFalse() {
ClassProxyHint first = ClassProxyHint.of(Properties.class).build();
ClassProxyHint second = ClassProxyHint.of(Hashtable.class).build();
assertThat(first).isNotEqualTo(second);
}
@Test
void equalsWithSameProxiedInterfacesDifferentOrderIsFalse() {
ClassProxyHint first = ClassProxyHint.of(Properties.class)
.proxiedInterfaces(Serializable.class, Closeable.class).build();
ClassProxyHint second = ClassProxyHint.of(Properties.class)
.proxiedInterfaces(TypeReference.of(Closeable.class), TypeReference.of(Serializable.class))
.build();
assertThat(first).isNotEqualTo(second);
}
@Test
void equalsWithDifferentProxiedInterfacesIsFalse() {
ClassProxyHint first = ClassProxyHint.of(Properties.class)
.proxiedInterfaces(Serializable.class).build();
ClassProxyHint second = ClassProxyHint.of(Properties.class)
.proxiedInterfaces(TypeReference.of(Closeable.class)).build();
assertThat(first).isNotEqualTo(second);
}
@Test
void equalsWithNonClassProxyHintIsFalse() {
ClassProxyHint first = ClassProxyHint.of(Properties.class).build();
JdkProxyHint second = new Builder().proxiedInterfaces(Function.class).build();
assertThat(first).isNotEqualTo(second);
}
@Test
void equalsWithDifferentConditionIsFalse() {
ClassProxyHint first = ClassProxyHint.of(Properties.class).build();
ClassProxyHint second = ClassProxyHint.of(Properties.class).onReachableType(TypeReference.of("org.example.test")).build();
assertThat(first).isNotEqualTo(second);
}
}

View File

@ -16,9 +16,7 @@
package org.springframework.aot.hint; package org.springframework.aot.hint;
import java.io.Serializable;
import java.util.Arrays; import java.util.Arrays;
import java.util.Properties;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.Function; import java.util.function.Function;
@ -82,33 +80,6 @@ class ProxyHintsTests {
assertThat(this.proxyHints.jdkProxies()).singleElement().satisfies(proxiedInterfaces(Function.class)); assertThat(this.proxyHints.jdkProxies()).singleElement().satisfies(proxiedInterfaces(Function.class));
} }
@Test
void registerClassProxyWithTargetClassName() {
this.proxyHints.registerClassProxy(TypeReference.of(Properties.class.getName()), classProxyHint ->
classProxyHint.proxiedInterfaces(Serializable.class));
assertThat(this.proxyHints.classProxies()).singleElement().satisfies(classProxyHint -> {
assertThat(classProxyHint.getTargetClass()).isEqualTo(TypeReference.of(Properties.class));
assertThat(classProxyHint.getProxiedInterfaces()).containsOnly(TypeReference.of(Serializable.class));
});
}
@Test
void registerClassProxyWithTargetClass() {
this.proxyHints.registerClassProxy(Properties.class, classProxyHint ->
classProxyHint.proxiedInterfaces(Serializable.class));
assertThat(this.proxyHints.classProxies()).singleElement().satisfies(classProxyHint -> {
assertThat(classProxyHint.getTargetClass()).isEqualTo(TypeReference.of(Properties.class));
assertThat(classProxyHint.getProxiedInterfaces()).containsOnly(TypeReference.of(Serializable.class));
});
}
@Test
void registerClassProxyWithTargetInterface() {
assertThatIllegalArgumentException()
.isThrownBy(() -> this.proxyHints.registerClassProxy(Serializable.class, classProxyHint -> {}))
.withMessageContaining(Serializable.class.getName());
}
private static Consumer<JdkProxyHint.Builder> springProxy(String proxiedInterface) { private static Consumer<JdkProxyHint.Builder> springProxy(String proxiedInterface) {
return builder -> builder.proxiedInterfaces(toTypeReferences( return builder -> builder.proxiedInterfaces(toTypeReferences(
@ -140,6 +111,7 @@ class ProxyHintsTests {
sealed interface SealedInterface { sealed interface SealedInterface {
} }
@SuppressWarnings("unused")
static final class SealedClass implements SealedInterface { static final class SealedClass implements SealedInterface {
} }