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.support; 018 019import java.io.IOException; 020import java.io.ObjectInputStream; 021import java.io.Serializable; 022import java.lang.reflect.Method; 023import java.util.LinkedHashSet; 024import java.util.Map; 025import java.util.Set; 026import java.util.concurrent.ConcurrentHashMap; 027 028import org.aopalliance.intercept.MethodInvocation; 029 030import org.springframework.aop.IntroductionInfo; 031import org.springframework.util.ClassUtils; 032 033/** 034 * Support for implementations of {@link org.springframework.aop.IntroductionInfo}. 035 * 036 * <p>Allows subclasses to conveniently add all interfaces from a given object, 037 * and to suppress interfaces that should not be added. Also allows for querying 038 * all introduced interfaces. 039 * 040 * @author Rod Johnson 041 * @author Juergen Hoeller 042 */ 043@SuppressWarnings("serial") 044public class IntroductionInfoSupport implements IntroductionInfo, Serializable { 045 046 protected final Set<Class<?>> publishedInterfaces = new LinkedHashSet<Class<?>>(); 047 048 private transient Map<Method, Boolean> rememberedMethods = new ConcurrentHashMap<Method, Boolean>(32); 049 050 051 /** 052 * Suppress the specified interface, which may have been autodetected 053 * due to the delegate implementing it. Call this method to exclude 054 * internal interfaces from being visible at the proxy level. 055 * <p>Does nothing if the interface is not implemented by the delegate. 056 * @param ifc the interface to suppress 057 */ 058 public void suppressInterface(Class<?> ifc) { 059 this.publishedInterfaces.remove(ifc); 060 } 061 062 @Override 063 public Class<?>[] getInterfaces() { 064 return ClassUtils.toClassArray(this.publishedInterfaces); 065 } 066 067 /** 068 * Check whether the specified interfaces is a published introduction interface. 069 * @param ifc the interface to check 070 * @return whether the interface is part of this introduction 071 */ 072 public boolean implementsInterface(Class<?> ifc) { 073 for (Class<?> pubIfc : this.publishedInterfaces) { 074 if (ifc.isInterface() && ifc.isAssignableFrom(pubIfc)) { 075 return true; 076 } 077 } 078 return false; 079 } 080 081 /** 082 * Publish all interfaces that the given delegate implements at the proxy level. 083 * @param delegate the delegate object 084 */ 085 protected void implementInterfacesOnObject(Object delegate) { 086 this.publishedInterfaces.addAll(ClassUtils.getAllInterfacesAsSet(delegate)); 087 } 088 089 /** 090 * Is this method on an introduced interface? 091 * @param mi the method invocation 092 * @return whether the invoked method is on an introduced interface 093 */ 094 protected final boolean isMethodOnIntroducedInterface(MethodInvocation mi) { 095 Boolean rememberedResult = this.rememberedMethods.get(mi.getMethod()); 096 if (rememberedResult != null) { 097 return rememberedResult; 098 } 099 else { 100 // Work it out and cache it. 101 boolean result = implementsInterface(mi.getMethod().getDeclaringClass()); 102 this.rememberedMethods.put(mi.getMethod(), result); 103 return result; 104 } 105 } 106 107 108 //--------------------------------------------------------------------- 109 // Serialization support 110 //--------------------------------------------------------------------- 111 112 /** 113 * This method is implemented only to restore the logger. 114 * We don't make the logger static as that would mean that subclasses 115 * would use this class's log category. 116 */ 117 private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException { 118 // Rely on default serialization; just initialize state after deserialization. 119 ois.defaultReadObject(); 120 // Initialize transient fields. 121 this.rememberedMethods = new ConcurrentHashMap<Method, Boolean>(32); 122 } 123 124}