From 6bb9775309040ad8580d9c73823203ac68da8980 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Tue, 12 Dec 2023 12:39:52 +0100 Subject: [PATCH] Declare isStatic and releaseTarget as default methods on TargetSource Closes gh-31820 --- .../org/springframework/aop/TargetSource.java | 11 ++++++++--- .../AbstractBeanFactoryBasedTargetSource.java | 10 ---------- .../AbstractLazyCreationTargetSource.java | 10 ---------- .../aop/target/EmptyTargetSource.java | 7 ------- .../aop/target/HotSwappableTargetSource.java | 10 ---------- .../aop/target/SingletonTargetSource.java | 5 ----- .../AbstractRefreshableTargetSource.java | 17 +---------------- .../target/PrototypeBasedTargetSourceTests.java | 9 ++------- ...textAnnotationAutowireCandidateResolver.java | 11 ++--------- .../jndi/JndiObjectTargetSource.java | 6 +----- .../aop/framework/AbstractAopProxyTests.java | 16 ---------------- 11 files changed, 14 insertions(+), 98 deletions(-) diff --git a/spring-aop/src/main/java/org/springframework/aop/TargetSource.java b/spring-aop/src/main/java/org/springframework/aop/TargetSource.java index e894c5cb0a10..c19982f31916 100644 --- a/spring-aop/src/main/java/org/springframework/aop/TargetSource.java +++ b/spring-aop/src/main/java/org/springframework/aop/TargetSource.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2023 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. @@ -49,10 +49,13 @@ public interface TargetSource extends TargetClassAware { * Will all calls to {@link #getTarget()} return the same object? *

In that case, there will be no need to invoke {@link #releaseTarget(Object)}, * and the AOP framework can cache the return value of {@link #getTarget()}. + *

The default implementation returns {@code false}. * @return {@code true} if the target is immutable * @see #getTarget */ - boolean isStatic(); + default boolean isStatic() { + return false; + } /** * Return a target instance. Invoked immediately before the @@ -67,9 +70,11 @@ public interface TargetSource extends TargetClassAware { /** * Release the given target object obtained from the * {@link #getTarget()} method, if any. + *

The default implementation is empty. * @param target object obtained from a call to {@link #getTarget()} * @throws Exception if the object can't be released */ - void releaseTarget(Object target) throws Exception; + default void releaseTarget(Object target) throws Exception { + } } diff --git a/spring-aop/src/main/java/org/springframework/aop/target/AbstractBeanFactoryBasedTargetSource.java b/spring-aop/src/main/java/org/springframework/aop/target/AbstractBeanFactoryBasedTargetSource.java index 5bb7929b2dc1..6ab195c0a7bd 100644 --- a/spring-aop/src/main/java/org/springframework/aop/target/AbstractBeanFactoryBasedTargetSource.java +++ b/spring-aop/src/main/java/org/springframework/aop/target/AbstractBeanFactoryBasedTargetSource.java @@ -153,16 +153,6 @@ public Class getTargetClass() { } } - @Override - public boolean isStatic() { - return false; - } - - @Override - public void releaseTarget(Object target) throws Exception { - // Nothing to do here. - } - /** * Copy configuration from the other AbstractBeanFactoryBasedTargetSource object. diff --git a/spring-aop/src/main/java/org/springframework/aop/target/AbstractLazyCreationTargetSource.java b/spring-aop/src/main/java/org/springframework/aop/target/AbstractLazyCreationTargetSource.java index 8246e1e6268f..57838757015d 100644 --- a/spring-aop/src/main/java/org/springframework/aop/target/AbstractLazyCreationTargetSource.java +++ b/spring-aop/src/main/java/org/springframework/aop/target/AbstractLazyCreationTargetSource.java @@ -72,11 +72,6 @@ public synchronized Class getTargetClass() { return (this.lazyTarget != null ? this.lazyTarget.getClass() : null); } - @Override - public boolean isStatic() { - return false; - } - /** * Returns the lazy-initialized target object, * creating it on-the-fly if it doesn't exist already. @@ -91,11 +86,6 @@ public synchronized Object getTarget() throws Exception { return this.lazyTarget; } - @Override - public void releaseTarget(Object target) throws Exception { - // nothing to do - } - /** * Subclasses should implement this method to return the lazy initialized object. diff --git a/spring-aop/src/main/java/org/springframework/aop/target/EmptyTargetSource.java b/spring-aop/src/main/java/org/springframework/aop/target/EmptyTargetSource.java index 6b0fdfd49848..cfcb3b119fdf 100644 --- a/spring-aop/src/main/java/org/springframework/aop/target/EmptyTargetSource.java +++ b/spring-aop/src/main/java/org/springframework/aop/target/EmptyTargetSource.java @@ -116,13 +116,6 @@ public Object getTarget() { return null; } - /** - * Nothing to release. - */ - @Override - public void releaseTarget(Object target) { - } - /** * Returns the canonical instance on deserialization in case diff --git a/spring-aop/src/main/java/org/springframework/aop/target/HotSwappableTargetSource.java b/spring-aop/src/main/java/org/springframework/aop/target/HotSwappableTargetSource.java index 1a1a2fa7552a..fb5aceefcadf 100644 --- a/spring-aop/src/main/java/org/springframework/aop/target/HotSwappableTargetSource.java +++ b/spring-aop/src/main/java/org/springframework/aop/target/HotSwappableTargetSource.java @@ -66,21 +66,11 @@ public synchronized Class getTargetClass() { return this.target.getClass(); } - @Override - public final boolean isStatic() { - return false; - } - @Override public synchronized Object getTarget() { return this.target; } - @Override - public void releaseTarget(Object target) { - // nothing to do - } - /** * Swap the target, returning the old target object. diff --git a/spring-aop/src/main/java/org/springframework/aop/target/SingletonTargetSource.java b/spring-aop/src/main/java/org/springframework/aop/target/SingletonTargetSource.java index e1b2ae6a56c7..11e3a9e8fead 100644 --- a/spring-aop/src/main/java/org/springframework/aop/target/SingletonTargetSource.java +++ b/spring-aop/src/main/java/org/springframework/aop/target/SingletonTargetSource.java @@ -67,11 +67,6 @@ public Object getTarget() { return this.target; } - @Override - public void releaseTarget(Object target) { - // nothing to do - } - @Override public boolean isStatic() { return true; diff --git a/spring-aop/src/main/java/org/springframework/aop/target/dynamic/AbstractRefreshableTargetSource.java b/spring-aop/src/main/java/org/springframework/aop/target/dynamic/AbstractRefreshableTargetSource.java index 32c6ce1c928f..e6f386bf55e8 100644 --- a/spring-aop/src/main/java/org/springframework/aop/target/dynamic/AbstractRefreshableTargetSource.java +++ b/spring-aop/src/main/java/org/springframework/aop/target/dynamic/AbstractRefreshableTargetSource.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2023 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. @@ -73,14 +73,6 @@ public synchronized Class getTargetClass() { return this.targetObject.getClass(); } - /** - * Not static. - */ - @Override - public boolean isStatic() { - return false; - } - @Override @Nullable public final synchronized Object getTarget() { @@ -90,13 +82,6 @@ public final synchronized Object getTarget() { return this.targetObject; } - /** - * No need to release target. - */ - @Override - public void releaseTarget(Object object) { - } - @Override public final synchronized void refresh() { diff --git a/spring-aop/src/test/java/org/springframework/aop/target/PrototypeBasedTargetSourceTests.java b/spring-aop/src/test/java/org/springframework/aop/target/PrototypeBasedTargetSourceTests.java index 6e857ffe6b18..9d664ccc18e1 100644 --- a/spring-aop/src/test/java/org/springframework/aop/target/PrototypeBasedTargetSourceTests.java +++ b/spring-aop/src/test/java/org/springframework/aop/target/PrototypeBasedTargetSourceTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2022 the original author or authors. + * Copyright 2002-2023 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. @@ -75,14 +75,9 @@ private static class TestTargetSource extends AbstractPrototypeBasedTargetSource private TestBean thisFieldIsNotSerializable = new TestBean(); @Override - public Object getTarget() throws Exception { + public Object getTarget() { return newPrototypeInstance(); } - - @Override - public void releaseTarget(Object target) throws Exception { - // Do nothing - } } } diff --git a/spring-context/src/main/java/org/springframework/context/annotation/ContextAnnotationAutowireCandidateResolver.java b/spring-context/src/main/java/org/springframework/context/annotation/ContextAnnotationAutowireCandidateResolver.java index f50fdff6a1e8..1718ffd4de49 100644 --- a/spring-context/src/main/java/org/springframework/context/annotation/ContextAnnotationAutowireCandidateResolver.java +++ b/spring-context/src/main/java/org/springframework/context/annotation/ContextAnnotationAutowireCandidateResolver.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2022 the original author or authors. + * Copyright 2002-2023 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. @@ -85,7 +85,7 @@ protected Object buildLazyResolutionProxy(DependencyDescriptor descriptor, @Null } private Object buildLazyResolutionProxy( - final DependencyDescriptor descriptor, final @Nullable String beanName, boolean classOnly) { + final DependencyDescriptor descriptor, @Nullable final String beanName, boolean classOnly) { BeanFactory beanFactory = getBeanFactory(); Assert.state(beanFactory instanceof DefaultListableBeanFactory, @@ -98,10 +98,6 @@ public Class getTargetClass() { return descriptor.getDependencyType(); } @Override - public boolean isStatic() { - return false; - } - @Override public Object getTarget() { Set autowiredBeanNames = (beanName != null ? new LinkedHashSet<>(1) : null); Object target = dlbf.doResolveDependency(descriptor, beanName, autowiredBeanNames, null); @@ -128,9 +124,6 @@ else if (Set.class == type || Collection.class == type) { } return target; } - @Override - public void releaseTarget(Object target) { - } }; ProxyFactory pf = new ProxyFactory(); diff --git a/spring-context/src/main/java/org/springframework/jndi/JndiObjectTargetSource.java b/spring-context/src/main/java/org/springframework/jndi/JndiObjectTargetSource.java index 21b1e0cdae65..cc8e392a7778 100644 --- a/spring-context/src/main/java/org/springframework/jndi/JndiObjectTargetSource.java +++ b/spring-context/src/main/java/org/springframework/jndi/JndiObjectTargetSource.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2023 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. @@ -148,8 +148,4 @@ public Object getTarget() { } } - @Override - public void releaseTarget(Object target) { - } - } diff --git a/spring-context/src/test/java/org/springframework/aop/framework/AbstractAopProxyTests.java b/spring-context/src/test/java/org/springframework/aop/framework/AbstractAopProxyTests.java index a4003d98b852..28be283e4671 100644 --- a/spring-context/src/test/java/org/springframework/aop/framework/AbstractAopProxyTests.java +++ b/spring-context/src/test/java/org/springframework/aop/framework/AbstractAopProxyTests.java @@ -1143,17 +1143,10 @@ public Class getTargetClass() { return TestBean.class; } @Override - public boolean isStatic() { - return false; - } - @Override public Object getTarget() throws Exception { assertThat(AopContext.currentProxy()).isEqualTo(proxy); return target; } - @Override - public void releaseTarget(Object target) throws Exception { - } }); // Just test anything: it will fail if context wasn't found @@ -1903,15 +1896,6 @@ public void verify() { throw new RuntimeException("Expectation failed: " + gets + " gets and " + releases + " releases"); } } - - /** - * @see org.springframework.aop.TargetSource#isStatic() - */ - @Override - public boolean isStatic() { - return false; - } - }