All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.exactpro.sf.aml.script.ActionNameRetriever Maven / Gradle / Ivy

There is a newer version: 3.4.260
Show newest version
/*******************************************************************************
 * Copyright 2009-2019 Exactpro (Exactpro Systems Limited)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package com.exactpro.sf.aml.script;

import static net.bytebuddy.implementation.InvocationHandlerAdapter.of;
import static net.bytebuddy.matcher.ElementMatchers.anyOf;
import static net.bytebuddy.matcher.ElementMatchers.noneOf;
import static org.jooq.lambda.Unchecked.function;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.jooq.lambda.fi.util.function.CheckedConsumer;

import com.exactpro.sf.scriptrunner.actionmanager.IActionCaller;
import com.exactpro.sf.scriptrunner.actionmanager.IActionCaller.ConsumerAction;
import com.exactpro.sf.scriptrunner.actionmanager.IActionCaller.ConsumerActionWithParameters;
import com.exactpro.sf.scriptrunner.actionmanager.IActionCaller.FunctionAction;
import com.exactpro.sf.scriptrunner.actionmanager.IActionCaller.FunctionActionWithParameters;
import com.exactpro.sf.scriptrunner.actionmanager.actioncontext.IActionContext;

import net.bytebuddy.ByteBuddy;

@SuppressWarnings("unchecked")
public class ActionNameRetriever {
    private static final ThreadLocal, IActionCallerProxy>> CACHE = ThreadLocal.withInitial(HashMap::new);

    public static  String getMethodName(Class clazz, ConsumerAction method, IActionContext actionContext) throws Exception {
        return getMethodName(clazz, caller -> method.accept((T)caller, actionContext));
    }

    public static  String getMethodName(Class clazz, FunctionAction method, IActionContext actionContext) throws Exception {
        return getMethodName(clazz, caller -> method.apply((T)caller, actionContext));
    }

    public static  String getMethodName(Class clazz, ConsumerActionWithParameters method, IActionContext actionContext, P parameters) throws Exception {
        return getMethodName(clazz, caller -> method.accept((T)caller, actionContext, parameters));
    }

    public static  String getMethodName(Class clazz, FunctionActionWithParameters method, IActionContext actionContext, P parameters) throws Exception {
        return getMethodName(clazz, caller -> method.apply((T)caller, actionContext, parameters));
    }

    private static String getMethodName(Class clazz, CheckedConsumer method) throws Exception {
        try {
            IActionCallerProxy proxy = getProxy(clazz);
            method.accept(proxy);
            return proxy.getMethodName();
        } catch (Exception e) {
            throw e;
        } catch (@SuppressWarnings("ProhibitedExceptionCaught") Throwable t) {
            return ExceptionUtils.rethrow(t);
        }
    }

    private static IActionCallerProxy getProxy(Class actionClass) {
        return CACHE.get().computeIfAbsent(actionClass, function(clazz -> (IActionCallerProxy)new ByteBuddy()
                .subclass(clazz)
                .implement(IActionCallerProxy.class)
                .defineProperty("methodName", String.class)
                .method(anyOf(clazz.getMethods()).and(noneOf(Object.class.getMethods())))
                .intercept(of((proxy, method, args) -> {
                    ((IActionCallerProxy)proxy).setMethodName(method.getName());
                    return null;
                }))
                .make()
                .load(clazz.getClassLoader())
                .getLoaded()
                .newInstance()));
    }

    protected interface IActionCallerProxy extends IActionCaller {
        String getMethodName();

        void setMethodName(String methodName);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy