001/*<
002 * Copyright 2002-2017 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;
018
019/**
020 * A {@code TargetSource} is used to obtain the current "target" of
021 * an AOP invocation, which will be invoked via reflection if no around
022 * advice chooses to end the interceptor chain itself.
023 *
024 * <p>If a {@code TargetSource} is "static", it will always return
025 * the same target, allowing optimizations in the AOP framework. Dynamic
026 * target sources can support pooling, hot swapping, etc.
027 *
028 * <p>Application developers don't usually need to work with
029 * {@code TargetSources} directly: this is an AOP framework interface.
030 *
031 * @author Rod Johnson
032 */
033public interface TargetSource extends TargetClassAware {
034
035        /**
036         * Return the type of targets returned by this {@link TargetSource}.
037         * <p>Can return {@code null}, although certain usages of a {@code TargetSource}
038         * might just work with a predetermined target class.
039         * @return the type of targets returned by this {@link TargetSource}
040         */
041        @Override
042        Class<?> getTargetClass();
043
044        /**
045         * Will all calls to {@link #getTarget()} return the same object?
046         * <p>In that case, there will be no need to invoke {@link #releaseTarget(Object)},
047         * and the AOP framework can cache the return value of {@link #getTarget()}.
048         * @return {@code true} if the target is immutable
049         * @see #getTarget
050         */
051        boolean isStatic();
052
053        /**
054         * Return a target instance. Invoked immediately before the
055         * AOP framework calls the "target" of an AOP method invocation.
056         * @return the target object which contains the joinpoint,
057         * or {@code null} if there is no actual target instance
058         * @throws Exception if the target object can't be resolved
059         */
060        Object getTarget() throws Exception;
061
062        /**
063         * Release the given target object obtained from the
064         * {@link #getTarget()} method, if any.
065         * @param target object obtained from a call to {@link #getTarget()}
066         * @throws Exception if the object can't be released
067         */
068        void releaseTarget(Object target) throws Exception;
069
070}