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:MainMethodRunner.java

@Override
public void run() {
    try {/*from  w ww .j a va  2 s  .c o  m*/
        Class<?> mainClass = Thread.currentThread().getContextClassLoader().loadClass(this.mainClassName);
        Method mainMethod = mainClass.getDeclaredMethod("main", String[].class);
        if (mainMethod == null) {
            throw new IllegalStateException(this.mainClassName + " does not have a main method");
        }
        mainMethod.invoke(null, new Object[] { this.args });
    } catch (Exception ex) {
        ex.printStackTrace();
        System.exit(1);
    }
}

From source file:ninja.eivind.hotsreplayuploader.ClientTest.java

@Test
public void testClientIsMainClass() throws Exception {
    String className = parse.select("project > properties > mainClass").text();

    LOG.info("Loading class " + className);
    Class<?> mainClass = Class.forName(className);

    Method main = mainClass.getDeclaredMethod("main", String[].class);
    int modifiers = main.getModifiers();

    Class<?> returnType = main.getReturnType();

    assertEquals("Client is mainClass", Client.class, mainClass);
    assertSame("Main method returns void", returnType, Void.TYPE);
    assertTrue("Main method is static", Modifier.isStatic(modifiers));
    assertTrue("Main method is public", Modifier.isPublic(modifiers));
}

From source file:de.tuberlin.uebb.jbop.access.ConstructorBuilderTest.java

private Object invoke(final Class<?> clazz, final String methodName, final Object object) throws Exception {
    final Method method = clazz.getDeclaredMethod(methodName, new Class<?>[] {});
    method.setAccessible(true);//  w w w . j  av  a 2 s  .c  o m
    return method.invoke(object, new Object[] {});
}

From source file:org.springmodules.cache.config.CacheNamespaceHandlerTests.java

protected void setUp() throws Exception {
    Class target = AbstractCacheNamespaceHandler.class;

    Method getCacheModelParserMethod = target.getDeclaredMethod("getCacheModelParser", new Class[0]);

    Method getCacheProviderFacadeParserMethod = target.getDeclaredMethod("getCacheProviderFacadeParser",
            new Class[0]);

    Method[] methodsToMock = { getCacheModelParserMethod, getCacheProviderFacadeParserMethod };

    handlerControl = MockClassControl.createControl(target, new Class[0], new Object[0], methodsToMock);

    handler = (AbstractCacheNamespaceHandler) handlerControl.getMock();
}

From source file:com.intellij.plugins.firstspirit.languagesupport.FirstSpiritClassPathHack.java

public void addURL(UrlClassLoader classLoader, URL u) throws Exception {

    if (classLoader != null) {
        System.out.println("Classloader loaded successfully: " + classLoader);
    } else {//from w  ww.j  a va2  s .c o m
        System.err.println("Cannot load system classloader!");
    }
    Class sysclass = UrlClassLoader.class;

    try {
        Method method = sysclass.getDeclaredMethod("addURL", parameters);
        method.setAccessible(true);
        method.invoke(classLoader, new Object[] { u });
    } catch (Exception e) {
        System.err.println("Error, could not add URL " + u.getPath() + " to classloader!");
        e.printStackTrace();
        throw e;
    }

}

From source file:org.craftercms.commons.security.permissions.annotations.HasPermissionAnnotationHandler.java

protected Method getActualMethod(ProceedingJoinPoint pjp) {
    MethodSignature ms = (MethodSignature) pjp.getSignature();
    Method method = ms.getMethod();

    if (method.getDeclaringClass().isInterface()) {
        Class<?> targetClass = pjp.getTarget().getClass();
        try {/*  w ww. j  ava2  s  . c o m*/
            method = targetClass.getDeclaredMethod(method.getName(), method.getParameterTypes());
        } catch (NoSuchMethodException e) {
            // Should NEVER happen
            throw new RuntimeException(e);
        }
    }

    return method;
}

From source file:com.atwal.wakeup.battery.util.Utilities.java

@TargetApi(14)
public static boolean hasNavBar(Context context) {
    final String SHOW_NAV_BAR_RES_NAME = "config_showNavigationBar";
    String sNavBarOverride = null;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        try {//from  w  w w.  j  ava 2 s .c  o m
            Class c = Class.forName("android.os.SystemProperties");
            Method m = c.getDeclaredMethod("get", String.class);
            m.setAccessible(true);
            sNavBarOverride = (String) m.invoke(null, "qemu.hw.mainkeys");
        } catch (Throwable e) {
            sNavBarOverride = null;
        }
    }
    Resources res = context.getResources();
    int resourceId = res.getIdentifier(SHOW_NAV_BAR_RES_NAME, "bool", "android");
    if (resourceId != 0) {
        boolean hasNav = res.getBoolean(resourceId);
        // check override flag (see static block)
        if ("1".equals(sNavBarOverride)) {
            hasNav = false;
        } else if ("0".equals(sNavBarOverride)) {
            hasNav = true;
        }
        return hasNav;
    } else { // fallback
        return !ViewConfiguration.get(context).hasPermanentMenuKey();
    }
}

From source file:com.link_intersystems.lang.reflect.SerializableMethod.java

protected Method getMethod(Class<?> declaringClass, String methodName, Class<?>[] parameterTypes)
        throws NoSuchMethodException {
    Method method = declaringClass.getDeclaredMethod(methodName, parameterTypes);
    return method;
}

From source file:com.glaf.core.util.ReflectUtils.java

public static void setFieldValue(Object target, String name, Class<?> type, Object value) {
    if (target == null || StringUtils.isEmpty(name)
            || (value != null && !type.isAssignableFrom(value.getClass()))) {
        return;/* w w w. j  a  va2s.c  o  m*/
    }
    Class<?> clazz = target.getClass();
    try {
        Method method = clazz
                .getDeclaredMethod("set" + Character.toUpperCase(name.charAt(0)) + name.substring(1), type);
        if (!Modifier.isPublic(method.getModifiers())) {
            method.setAccessible(true);
        }
        method.invoke(target, value);
    } catch (Exception ex) {
        if (LogUtils.isDebug()) {
            logger.debug(ex);
        }
        try {
            Field field = clazz.getDeclaredField(name);
            if (!Modifier.isPublic(field.getModifiers())) {
                field.setAccessible(true);
            }
            field.set(target, value);
        } catch (Exception e) {
            if (LogUtils.isDebug()) {
                logger.debug(e);
            }
        }
    }
}

From source file:com.collaide.fileuploader.helper.TestHelper.java

protected Method invokePrivateMethod(Class targetClass, String methodName, Class... argClasses)
        throws NoSuchMethodException {
    Method method = targetClass.getDeclaredMethod(methodName, argClasses);
    method.setAccessible(true);// w ww  .  ja  va 2  s  . c o m
    return method;

}