001/**
002 * Copyright (c) 2015-2022, Michael Yang 杨福海 (fuhai999@gmail.com).
003 * <p>
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 * <p>
008 * http://www.apache.org/licenses/LICENSE-2.0
009 * <p>
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 */
016package io.jboot.aop.javassist;
017
018import com.jfinal.aop.Interceptor;
019import com.jfinal.aop.InterceptorManager;
020import com.jfinal.aop.Invocation;
021import io.jboot.aop.InterceptorBuilderManager;
022import io.jboot.aop.InterceptorCache;
023import javassist.util.proxy.MethodHandler;
024
025import java.lang.reflect.Method;
026import java.util.HashSet;
027import java.util.Set;
028
029
030public class JbootJavassistHandler implements MethodHandler {
031
032    private static final Set<String> excludedMethodName = buildExcludedMethodName();
033    private static final InterceptorManager interManager = InterceptorManager.me();
034    private static final InterceptorBuilderManager builderManager = InterceptorBuilderManager.me();
035
036
037    @Override
038    public Object invoke(Object self, Method originalMethod, Method proxyMethod, Object[] args) throws Throwable {
039
040        if (excludedMethodName.contains(originalMethod.getName())) {
041            return proxyMethod.invoke(self, args);
042        }
043
044        Class<?> targetClass = self.getClass().getSuperclass();
045        //ClassUtil.getUsefulClass(self.getClass());
046
047        InterceptorCache.MethodKey key = InterceptorCache.getMethodKey(targetClass, originalMethod);
048        Interceptor[] inters = InterceptorCache.get(key);
049        if (inters == null) {
050            inters = interManager.buildServiceMethodInterceptor(targetClass, originalMethod);
051            inters = builderManager.build(targetClass, originalMethod, inters);
052
053            InterceptorCache.put(key, inters);
054        }
055
056        if (inters.length == 0) {
057            return proxyMethod.invoke(self, args);
058        } else {
059            Invocation invocation = new Invocation(self, originalMethod, inters,
060                    x -> proxyMethod.invoke(self, x), args);
061            invocation.invoke();
062            return invocation.getReturnValue();
063        }
064    }
065
066
067    private static Set<String> buildExcludedMethodName() {
068        Set<String> excludedMethodName = new HashSet<String>(64, 0.25F);
069        Method[] methods = Object.class.getDeclaredMethods();
070        for (Method m : methods) {
071            excludedMethodName.add(m.getName());
072        }
073        // getClass() registerNatives() can not be enhanced
074        // excludedMethodName.remove("getClass");
075        // excludedMethodName.remove("registerNatives");
076        return excludedMethodName;
077    }
078
079
080}
081
082