|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package com.cloud.utils.component; |
| 19 | + |
| 20 | +import java.lang.ref.WeakReference; |
| 21 | +import java.lang.reflect.Method; |
| 22 | +import java.util.WeakHashMap; |
| 23 | + |
| 24 | +public class ComponentMethodProxyCache { |
| 25 | + |
| 26 | + private static WeakHashMap<TargetKey, WeakReference<Method>> s_cache = new WeakHashMap<TargetKey, WeakReference<Method>>(); |
| 27 | + |
| 28 | + public ComponentMethodProxyCache() { |
| 29 | + } |
| 30 | + |
| 31 | + public static Method getTargetMethod(Method method, Object target) { |
| 32 | + synchronized(s_cache) { |
| 33 | + WeakReference<Method> targetMethod = s_cache.get(new TargetKey(method, target)); |
| 34 | + if(targetMethod != null && targetMethod.get() != null) |
| 35 | + return targetMethod.get(); |
| 36 | + |
| 37 | + Class<?> clazz = target.getClass(); |
| 38 | + for(Method m : clazz.getMethods()) { |
| 39 | + if(isMethodMatched(method, m)) { |
| 40 | + s_cache.put(new TargetKey(method, target), new WeakReference<Method>(m)); |
| 41 | + return m; |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + return method; |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + private static boolean isMethodMatched(Method m1, Method m2) { |
| 50 | + if(!m1.getName().equals(m2.getName())) |
| 51 | + return false; |
| 52 | + |
| 53 | + Class<?>[] params1 = m1.getParameterTypes(); |
| 54 | + Class<?>[] params2 = m2.getParameterTypes(); |
| 55 | + |
| 56 | + if(params1.length != params2.length) |
| 57 | + return false; |
| 58 | + |
| 59 | + for(int i = 0; i < params1.length; i++) { |
| 60 | + if(!params1[i].isAssignableFrom(params2[i])) |
| 61 | + return false; |
| 62 | + } |
| 63 | + |
| 64 | + return true; |
| 65 | + } |
| 66 | + |
| 67 | + public static class TargetKey { |
| 68 | + Method _method; |
| 69 | + Object _target; |
| 70 | + |
| 71 | + public TargetKey(Method method, Object target) { |
| 72 | + _method = method; |
| 73 | + _target = target; |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public boolean equals(Object obj) { |
| 78 | + if(!(obj instanceof TargetKey)) |
| 79 | + return false; |
| 80 | + |
| 81 | + // for target object, we just check the reference |
| 82 | + return _method.equals(((TargetKey)obj)._method) && |
| 83 | + _target == ((TargetKey)obj)._target; |
| 84 | + } |
| 85 | + |
| 86 | + public int hashCode() { |
| 87 | + return _target.hashCode() ^ _target.hashCode(); |
| 88 | + } |
| 89 | + } |
| 90 | +} |
0 commit comments