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

com.landawn.abacus.util.ReflectASM Maven / Gradle / Ivy

Go to download

A general programming library in Java/Android. It's easy to learn and simple to use with concise and powerful APIs.

There is a newer version: 5.2.4
Show newest version
/*
 * Copyright (C) 2017 HaiYang Li
 *
 * 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.landawn.abacus.util;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.esotericsoftware.reflectasm.ConstructorAccess;
import com.esotericsoftware.reflectasm.FieldAccess;
import com.esotericsoftware.reflectasm.MethodAccess;

/**
 *
 * @author Haiyang Li
 * @param 
 * @since 0.8
 */
final class ReflectASM {

    @SuppressWarnings("rawtypes")
    static final Class[] EMPTY_CLASSES = {};

    static final Map, FieldAccess> clsFieldPool = new ConcurrentHashMap<>();

    static final Map, ConstructorAccess> clsConstructorPool = new ConcurrentHashMap<>();

    static final Map, MethodAccess> clsMethodPool = new ConcurrentHashMap<>();

    private final Class cls;

    private final T target;

    ReflectASM(Class cls, T target) {
        this.cls = cls;
        this.target = target;
    }

    /**
     *
     * @param 
     * @param clsName
     * @return
     */
    public static  ReflectASM on(String clsName) {
        return on((Class) ClassUtil.forClass(clsName));
    }

    /**
     *
     * @param 
     * @param cls
     * @return
     */
    public static  ReflectASM on(Class cls) {
        return new ReflectASM<>(cls, null);
    }

    /**
     *
     * @param 
     * @param target
     * @return
     */
    public static  ReflectASM on(T target) {
        return new ReflectASM<>((Class) target.getClass(), target);
    }

    /**
     * 
     *
     * @return 
     */
    public ReflectASM _new() { //NOSONAR
        return new ReflectASM<>(cls, getConstructorAccess(cls).newInstance());
    }

    /**
     *
     * @param  the value type
     * @param fieldName
     * @return
     */
    public  V get(String fieldName) {
        final FieldAccess fieldAccess = getFieldAccess();

        return (V) fieldAccess.get(target, fieldName);
    }

    /**
     *
     * @param fieldName
     * @param value
     * @return
     */
    public ReflectASM set(String fieldName, Object value) {
        final FieldAccess fieldAccess = getFieldAccess();

        fieldAccess.set(target, fieldName, value);

        return this;
    }

    /**
     *
     * @param  the value type
     * @param methodName
     * @param args
     * @return
     */
    @SafeVarargs
    public final  V invoke(String methodName, Object... args) {
        final MethodAccess methodAccess = getMethodAccess(cls);

        return (V) methodAccess.invoke(target, methodName, args);
    }

    /**
     *
     * @param methodName
     * @param args
     * @return
     */
    @SafeVarargs
    public final ReflectASM call(String methodName, Object... args) {
        invoke(methodName, args);

        return this;
    }

    /**
     * Gets the field access.
     *
     * @return
     */
    private FieldAccess getFieldAccess() {
        FieldAccess fieldAccess = clsFieldPool.get(cls);

        if (fieldAccess == null) {
            fieldAccess = FieldAccess.get(cls);
            clsFieldPool.put(cls, fieldAccess);
        }

        return fieldAccess;
    }

    /**
     * Gets the constructor access.
     *
     * @param cls
     * @return
     * @throws SecurityException the security exception
     */
    private ConstructorAccess getConstructorAccess(final Class cls) throws SecurityException {
        ConstructorAccess constructorAccess = clsConstructorPool.get(cls);

        if (constructorAccess == null) {
            constructorAccess = ConstructorAccess.get(cls);
            clsConstructorPool.put(cls, constructorAccess);
        }

        return (ConstructorAccess) constructorAccess;
    }

    /**
     * Gets the method access.
     *
     * @param cls
     * @return
     */
    private MethodAccess getMethodAccess(final Class cls) {
        MethodAccess methodAccess = clsMethodPool.get(cls);

        if (methodAccess == null) {
            methodAccess = MethodAccess.get(cls);
            clsMethodPool.put(cls, methodAccess);
        }

        return methodAccess;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy