001/*
002 * Copyright 2002-2018 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.aop.target;
018
019import java.io.Serializable;
020
021import org.springframework.aop.TargetSource;
022import org.springframework.util.Assert;
023import org.springframework.util.ObjectUtils;
024
025/**
026 * Implementation of the {@link org.springframework.aop.TargetSource} interface
027 * that holds a given object. This is the default implementation of the TargetSource
028 * interface, as used by the Spring AOP framework. There is usually no need to
029 * create objects of this class in application code.
030 *
031 * <p>This class is serializable. However, the actual serializability of a
032 * SingletonTargetSource will depend on whether the target is serializable.
033 *
034 * @author Rod Johnson
035 * @author Juergen Hoeller
036 * @see org.springframework.aop.framework.AdvisedSupport#setTarget(Object)
037 */
038public class SingletonTargetSource implements TargetSource, Serializable {
039
040        /** use serialVersionUID from Spring 1.2 for interoperability. */
041        private static final long serialVersionUID = 9031246629662423738L;
042
043
044        /** Target cached and invoked using reflection. */
045        private final Object target;
046
047
048        /**
049         * Create a new SingletonTargetSource for the given target.
050         * @param target the target object
051         */
052        public SingletonTargetSource(Object target) {
053                Assert.notNull(target, "Target object must not be null");
054                this.target = target;
055        }
056
057
058        @Override
059        public Class<?> getTargetClass() {
060                return this.target.getClass();
061        }
062
063        @Override
064        public Object getTarget() {
065                return this.target;
066        }
067
068        @Override
069        public void releaseTarget(Object target) {
070                // nothing to do
071        }
072
073        @Override
074        public boolean isStatic() {
075                return true;
076        }
077
078
079        /**
080         * Two invoker interceptors are equal if they have the same target or if the
081         * targets or the targets are equal.
082         */
083        @Override
084        public boolean equals(Object other) {
085                if (this == other) {
086                        return true;
087                }
088                if (!(other instanceof SingletonTargetSource)) {
089                        return false;
090                }
091                SingletonTargetSource otherTargetSource = (SingletonTargetSource) other;
092                return this.target.equals(otherTargetSource.target);
093        }
094
095        /**
096         * SingletonTargetSource uses the hash code of the target object.
097         */
098        @Override
099        public int hashCode() {
100                return this.target.hashCode();
101        }
102
103        @Override
104        public String toString() {
105                return "SingletonTargetSource for target object [" + ObjectUtils.identityToString(this.target) + "]";
106        }
107
108}