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

org.azolla.l.ling.lang.Class0 Maven / Gradle / Ivy

There is a newer version: 2.0.3
Show newest version
/*
 * @(#)Class0.java		Created at 2013-10-31
 * 
 * Copyright (c) 2011-2013 azolla.org All rights reserved.
 * Azolla PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. 
 */
package org.azolla.l.ling.lang;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

import javax.annotation.Nullable;

import org.azolla.l.ling.exception.code.AzollaCode;
import org.azolla.l.ling.text.Fmt0;
import org.azolla.l.ling.util.KV;
import org.azolla.l.ling.util.Log0;

import com.google.common.base.Function;
import com.google.common.collect.Lists;

/**
 * The coder is very lazy, nothing to write for this Class0 class
 *
 * @author 	[email protected]
 * @since 	ADK1.0
 */
public class Class0
{
    private Class            clazz    = null;

    private Object              instance = null;

    public Class0(String className, Object... args) throws Exception
    {
        this(className, null, args);
    }

    @SuppressWarnings("rawtypes")
    public Class0(String className, @Nullable Class[] argTypes, Object... args) throws Exception
    {
        // clazz = ClassLoader.getSystemClassLoader().loadClass(className);
        clazz = loadClass(className);
        if(argTypes == null)
        {
            instance = clazz.getConstructor(Lists.transform(Lists.newArrayList(args), new Function()
            {
                @Override
                public Class apply(Object input)
                {
                    return input.getClass();
                }
            }).toArray(new Class[args.length])).newInstance(args);
        }
        else
        {
            instance = clazz.getConstructor(argTypes).newInstance(args);
        }
    }

    public  T execMethod(Class returnType, String methodName, Object... args) throws Exception
    {
        return execMethod(returnType, methodName, null, args);
    }

    @SuppressWarnings({"unchecked", "rawtypes"})
    public  T execMethod(Class returnType, String methodName, @Nullable Class[] argTypes, Object... args) throws Exception
    {
        Method m = null;
        if(argTypes == null)
        {
            m = clazz.getMethod(methodName, Lists.transform(Lists.newArrayList(args), new Function()
            {
                @Override
                public Class apply(Object input)
                {
                    return input.getClass();
                }
            }).toArray(new Class[args.length]));

        }
        else
        {
            m = clazz.getMethod(methodName, argTypes);
        }
        return (T) m.invoke(Modifier.isStatic(m.getModifiers()) ? null : instance, args);
    }

    public Class loadClass(String className) throws Exception
    {
        return Class.forName(className);
    }

    public static boolean exist(String className)
    {
        boolean rtnBoolean = true;
        try
        {
            Class.forName(className);
        }
        catch(ClassNotFoundException e)
        {
            Log0.warn(Class0.class, Fmt0.LOG_EC_P_M, AzollaCode.CLASSNOTFOUNDEXCEPTION, KV.ins("className", className), e);
            rtnBoolean = false;
        }
        return rtnBoolean;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy