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

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

/*
 * 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;

/**
 * 
 * @since 0.8
 * 
 * @author Haiyang Li
 */
final class ReflectASM {
    @SuppressWarnings("rawtypes")
    static final Class[] EMPTY_CLASSES = new Class[0];

    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;
    }

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

    public static  ReflectASM on(Class cls) {
        return new ReflectASM(cls, null);
    }

    public static  ReflectASM on(T target) {
        return new ReflectASM((Class) target.getClass(), target);
    }

    public ReflectASM _new() {
        return new ReflectASM(cls, getConstructorAccess(cls).newInstance());
    }

    public  V get(String fieldName) {
        final FieldAccess fieldAccess = getFieldAccess(fieldName);

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

    public ReflectASM set(String fieldName, Object value) {
        final FieldAccess fieldAccess = getFieldAccess(fieldName);

        fieldAccess.set(target, fieldName, value);

        return this;
    }

    @SafeVarargs
    public final  V invoke(String methodName, Object... args) {
        final MethodAccess methodAccess = getMethodAccess(cls);

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

    @SafeVarargs
    public final ReflectASM invokke(String methodName, Object... args) {
        invoke(methodName, args);

        return this;
    }

    private FieldAccess getFieldAccess(String fieldName) {
        FieldAccess fieldAccess = clsFieldPool.get(cls);

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

        return fieldAccess;
    }

    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;
    }

    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