Example usage for java.lang Class getDeclaredMethod

List of usage examples for java.lang Class getDeclaredMethod

Introduction

In this page you can find the example usage for java.lang Class getDeclaredMethod.

Prototype

@CallerSensitive
public Method getDeclaredMethod(String name, Class<?>... parameterTypes)
        throws NoSuchMethodException, SecurityException 

Source Link

Document

Returns a Method object that reflects the specified declared method of the class or interface represented by this Class object.

Usage

From source file:Main.java

static public Object invokeStaticMethod(Class<?> claxx, String methodName, Object... args) throws Exception {
    Class<?>[] argsClass = null;

    if (args != null && args.length > 0) {
        argsClass = new Class<?>[args.length];

        for (int i = 0, j = args.length; i < j; i++) {
            argsClass[i] = args[i].getClass();
            if (argsClass[i] == Integer.class) {
                argsClass[i] = int.class;
            } else if (argsClass[i] == Boolean.class) {
                argsClass[i] = boolean.class;
            }// w  w w. j a v  a2s . c  o  m
        }
    }

    Method method = claxx.getDeclaredMethod(methodName, argsClass);
    method.setAccessible(true);
    return method.invoke(null, args);
}

From source file:net.unicon.demetrius.fac.AbstractResourceFactory.java

public static IResourceFactory fromUrl(String url) throws DemetriusException {

    // Assertions.
    if (url == null) {
        String msg = "Argument 'url' cannot be null.";
        throw new IllegalArgumentException(msg);
    }//  w w  w.j  a va 2 s  .c  om

    IResourceFactory rslt = null;
    try {

        String[] tokens = url.split("/");
        String className = tokens[2];
        Class c = Class.forName(className);
        Method m = c.getDeclaredMethod("fromUrl", new Class[] { String.class });
        rslt = (IResourceFactory) m.invoke(null, new Object[] { url });

    } catch (Throwable t) {
        String msg = "Unable to evaluate the specified entity:  " + url;
        throw new RuntimeException(msg, t);
    }

    return rslt;

}

From source file:org.apache.cassandra.db.marshal.TypeParser.java

private static AbstractType<?> getAbstractType(String compareWith, TypeParser parser)
        throws SyntaxException, ConfigurationException {
    String className = compareWith.contains(".") ? compareWith
            : "org.apache.cassandra.db.marshal." + compareWith;
    Class<? extends AbstractType<?>> typeClass = FBUtilities.<AbstractType<?>>classForName(className,
            "abstract-type");
    try {//from w  w  w .j  av a2 s  . c  om
        Method method = typeClass.getDeclaredMethod("getInstance", TypeParser.class);
        return (AbstractType<?>) method.invoke(null, parser);
    } catch (NoSuchMethodException e) {
        // Trying to see if we have an instance field and apply the default parameter to it
        AbstractType<?> type = getRawAbstractType(typeClass);
        return AbstractType.parseDefaultParameters(type, parser);
    } catch (IllegalAccessException e) {
        // Trying to see if we have an instance field and apply the default parameter to it
        AbstractType<?> type = getRawAbstractType(typeClass);
        return AbstractType.parseDefaultParameters(type, parser);
    } catch (InvocationTargetException e) {
        ConfigurationException ex = new ConfigurationException(
                "Invalid definition for comparator " + typeClass.getName() + ".");
        ex.initCause(e.getTargetException());
        throw ex;
    }
}

From source file:com.xidu.framework.common.util.Utils.java

public static void setProperty(Object owner, String fieldName, String value) throws Exception {
    Class ownerClass = owner.getClass();
    String functionName = "set";
    if (fieldName.indexOf(0) > 97) {
        functionName += fieldName;/*from w  w w.ja v a2  s . co m*/
    } else {
        functionName += (fieldName.substring(0, 1).toUpperCase()) + fieldName.substring(1);
    }
    log.debug("function name" + functionName);

    Method method = ownerClass.getDeclaredMethod(functionName, java.lang.String.class);
    Object[] args = { value };
    method.invoke(owner, args);
}

From source file:net.unicon.demetrius.fac.AbstractResourceFactory.java

public static IResource resourceFromUrl(String url) throws DemetriusException {

    // Assertions.
    if (url == null) {
        String msg = "Argument 'url' cannot be null.";
        throw new IllegalArgumentException(msg);
    }//from   w  ww.  j a  v  a  2s. c om

    IResource rslt = null;
    try {

        String[] tokens = url.split(DELIMITER, 4);
        String className = tokens[2];
        Class c = Class.forName(className);
        Method m = c.getDeclaredMethod("fromUrl", new Class[] { String.class });

        if (url.matches(".*\\(FSA://.*\\).*")) { // Shared resource url
            tokens = url.split("\\)////", 2);
            tokens[0] = tokens[0] + ")";
        } else { // Non-shared resource url
            tokens = url.split("////", 2);
        }

        String facUrl = tokens[0];
        IResourceFactory fac = (IResourceFactory) m.invoke(null, new Object[] { facUrl });
        String path = "";
        if (tokens.length == 2) {
            path = tokens[1];
        }

        rslt = fac.getResource(path);

    } catch (Throwable t) {
        String msg = "Unable to evaluate the specified entity:  " + url;
        throw new RuntimeException(msg, t);
    }

    if (rslt != null) {
        return rslt;
    } else {
        throw new DemetriusException("Resource with the given url was not found. " + url);
    }

}

From source file:com.asakusafw.workflow.executor.TaskExecutors.java

static Method findMethod(String className, String methodName, Class<?>... parameterTypes) {
    try {//  ww w  . j av  a  2  s .c  o  m
        Class<?> aClass = Class.forName(className, false, TaskExecutors.class.getClassLoader());
        Method method = aClass.getDeclaredMethod(methodName, parameterTypes);
        method.setAccessible(true);
        return method;
    } catch (Exception e) {
        LOG.trace("failed to activate method: {}#{}", className, methodName, e);
        return null;
    }
}

From source file:com.vk.sdk.payments.VKPaymentsServerSender.java

@Nullable
private static String getDeviceId(Context ctx) {
    try {/*from w w w .  j  a  va2s  .co  m*/
        Class<?> advertisingIdClientClass = Class
                .forName("com.google.android.gms.ads.identifier.AdvertisingIdClient");
        Class<?> advertisingIdClientClassInfo = Class
                .forName("com.google.android.gms.ads.identifier.AdvertisingIdClient$Info");
        Method methodGetInfo = advertisingIdClientClass.getDeclaredMethod("getAdvertisingIdInfo",
                Context.class);
        Method methodGetId = advertisingIdClientClassInfo.getMethod("getId");

        Object info = methodGetInfo.invoke(advertisingIdClientClass, ctx);
        return (String) methodGetId.invoke(info);
    } catch (Exception e) {
        Log.e("vk", "error", e);
        return null;
    }
}

From source file:com.sun.faces.el.impl.BeanInfoManager.java

/**
 * If the given class is public and has a Method that declares the
 * same name and arguments as the given method, then that method is
 * returned.  Otherwise the superclass and interfaces are searched
 * recursively./* w  w  w  .  j  a  v  a  2s.  c o  m*/
 */
static Method getPublicMethod(Class pClass, Method pMethod) {
    // See if this is a public class declaring the method
    if (Modifier.isPublic(pClass.getModifiers())) {
        try {
            Method m;
            try {
                m = pClass.getDeclaredMethod(pMethod.getName(), pMethod.getParameterTypes());
            } catch (java.security.AccessControlException ex) {
                // kludge to accommodate J2EE RI's default settings
                // TODO: see if we can simply replace
                //       getDeclaredMethod() with getMethod() ...?
                m = pClass.getMethod(pMethod.getName(), pMethod.getParameterTypes());
            }
            if (Modifier.isPublic(m.getModifiers())) {
                return m;
            }
        } catch (NoSuchMethodException exc) {
        }
    }

    // Search the interfaces
    {
        Class[] interfaces = pClass.getInterfaces();
        if (interfaces != null) {
            for (int i = 0; i < interfaces.length; i++) {
                Method m = getPublicMethod(interfaces[i], pMethod);
                if (m != null) {
                    return m;
                }
            }
        }
    }

    // Search the superclass
    {
        Class superclass = pClass.getSuperclass();
        if (superclass != null) {
            Method m = getPublicMethod(superclass, pMethod);
            if (m != null) {
                return m;
            }
        }
    }

    return null;
}

From source file:net.pms.external.ExternalFactory.java

private static void postInstall(Class<?> clazz) {
    Method postInstall;// w w w. jav  a  2s.  c om
    try {
        postInstall = clazz.getDeclaredMethod("postInstall", (Class<?>[]) null);

        if (Modifier.isStatic(postInstall.getModifiers())) {
            postInstall.invoke((Object[]) null, (Object[]) null);
        }
    }

    // Ignore all errors
    catch (SecurityException | NoSuchMethodException | IllegalArgumentException | IllegalAccessException
            | InvocationTargetException e) {
    }
}

From source file:co.carlosjimenez.android.currencyalerts.app.MenuTint.java

/**
 * Set the menu to show MenuItem icons in the overflow window.
 *
 * @param menu the menu to force icons to show
 *///from  w ww.j ava 2s .c o  m
public static void forceMenuIcons(Menu menu) {
    try {
        Class<?> MenuBuilder = menu.getClass();
        Method setOptionalIconsVisible = MenuBuilder.getDeclaredMethod("setOptionalIconsVisible",
                boolean.class);
        if (!setOptionalIconsVisible.isAccessible()) {
            setOptionalIconsVisible.setAccessible(true);
        }
        setOptionalIconsVisible.invoke(menu, true);
    } catch (Exception ignored) {
    }
}