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:com.velonuboso.made.core.common.MadePattern.java

public MadePattern(String label, String regexp, String weight, String condition) throws Exception {
    this.label = label;
    this.regexp = regexp;
    this.weight = weight;
    this.condition = condition;
    patt = Pattern.compile(regexp);

    ClassPool pool = ClassPool.getDefault();
    CtClass evalClass = pool.makeClass("Eval" + label);

    String weight2 = weight.replace("gaussian", "com.velonuboso.made.core.MadePattern.gaussian");

    evalClass.addMethod(CtNewMethod.make("public double getWeight(int p, int pm, int a, int am) {" + "return ("
            + condition + ")?((double)" + weight2 + "):0.0;" + "}", evalClass));
    Class clazz = evalClass.toClass();
    evaluator = clazz.newInstance();/*w  ww  .  j  a  va 2s .c  om*/

    Class[] formalParams = new Class[] { int.class, int.class, int.class, int.class };
    evaluatorMethod = clazz.getDeclaredMethod("getWeight", formalParams);
}

From source file:org.kuali.coeus.s2sgen.impl.generate.support.stylesheet.StylesheetValidationTest.java

private SchemaType getSchemaType(Class<? extends S2SFormGenerator> generatorClass) {
    final Class<?> returnType;
    try {//w  w  w . j  a va2 s .co m
        returnType = generatorClass
                .getDeclaredMethod("getFormObject", ProposalDevelopmentDocumentContract.class).getReturnType();
        return (SchemaType) returnType.getDeclaredField("type").get(null);
    } catch (NoSuchMethodException | IllegalAccessException | NoSuchFieldException e) {
        throw new RuntimeException(generatorClass.getName() + " cannot find SchemaType", e);
    }
}

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

protected void setUp() throws Exception {
    configElementBuilder = new ConfigElementBuilder();
    cacheManagerClass = AbstractCacheManagerFactoryBean.class;

    Class targetClass = AbstractCacheManagerAndProviderFacadeParser.class;

    Method getCacheManagerClassMethod = targetClass.getDeclaredMethod("getCacheManagerClass", new Class[0]);

    Method[] methodsToMock = { getCacheManagerClassMethod };

    parserControl = MockClassControl.createControl(targetClass, null, null, methodsToMock);
    parser = (AbstractCacheManagerAndProviderFacadeParser) parserControl.getMock();
    registry = new DefaultListableBeanFactory();

    cacheProviderFacade = new RootBeanDefinition(CacheProviderFacade.class);
    cacheProviderFacade.setPropertyValues(new MutablePropertyValues());
    registry.registerBeanDefinition(BeanName.CACHE_PROVIDER_FACADE, cacheProviderFacade);
}

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

private void setUpHelper() throws NoSuchMethodException {
    Class targetClass = BeanDefinitionParserDelegate.class;

    Method parseBeanDefinitionElementMethod = targetClass.getDeclaredMethod("parseBeanDefinitionElement",
            new Class[] { Element.class, BeanDefinition.class });

    Method[] methodsToMock = { parseBeanDefinitionElementMethod };

    helperControl = MockClassControl.createControl(targetClass, new Class[] { XmlReaderContext.class },
            new Object[] { new XmlReaderContext(null, null, null, null, null, null) }, methodsToMock);

    delegate = (BeanDefinitionParserDelegate) helperControl.getMock();
}

From source file:com.hybris.oms.rest.resources.TmallJSCMQ2TestEnvResource.java

@SuppressWarnings("unchecked")
@POST/*from ww w.  j a v a 2s  .  c  o  m*/
@Path("/sendmq2testenv")
@Secured({ "ROLE_admin" })
public String sendMQ2TestEnv(final String oid) {
    final List<Message> messageList = sendTmallMessageRelatedConfig.getTmallMessageList();

    // Mock Message

    Map<String, Object> rawMap = null;
    Map<String, String> contentMap = null;
    Message message = null;

    Class c;
    try {
        c = Class.forName("com.taobao.api.internal.tmc.Message");
        final Method methodSetRaw = c.getDeclaredMethod("setRaw", Map.class);
        methodSetRaw.setAccessible(true);
        final Object tmallMessageObj = c.newInstance();

        rawMap = new HashMap<String, Object>();
        contentMap = new HashMap<String, String>();
        contentMap.put("buyer_nick", "sandbox_cilai_c");
        contentMap.put("payment", "44.00");
        contentMap.put("tid", oid);
        contentMap.put("oid", oid);
        contentMap.put("seller_nick", "sandbox_c_1");
        contentMap.put("type", "guarantee_trade");

        rawMap.put("content", contentMap);
        rawMap.put("topic", "taobao_trade_TradeBuyerPay");
        rawMap.put("time", Calendar.getInstance().getTime());
        rawMap.put("id", "2130700002172846269");
        rawMap.put("nick", "sandbox_c_1");
        rawMap.put("userid", "2074082786");
        rawMap.put("dataid", "192559684481084");
        rawMap.put("publisher", "4272");
        rawMap.put("outtime", Calendar.getInstance().getTime());

        methodSetRaw.invoke(tmallMessageObj, rawMap);
        message = (Message) tmallMessageObj;

        message.setId(4160600490938325004L);
        message.setTopic("taobao_trade_TradeBuyerPay");
        message.setPubTime(Calendar.getInstance().getTime());
        message.setOutgoingTime(Calendar.getInstance().getTime());
        message.setUserId(911757567L);
        message.setContentMap(contentMap);

        messageList.add(message);
    } catch (final Exception e1) {
        logger.error("message reflec error ", e1);
    }

    try {
        // messageNum = messageList.size();

        // ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
        // String json = "";
        // json = ow.writeValueAsString(messageList.get(0).getRaw());

        final JSONSerializer jsonSerializer = new JSONSerializer();
        final String rawJson = jsonSerializer.deepSerialize(messageList.get(0).getRaw());

        // ClientResponse response = sendTmallMessageRelatedConfig.getPostmq2testenvWR().header("X-tenantid", "single")
        // .post(ClientResponse.class, rawJson);

        final ClientResponse response = sendTmallMessageRelatedConfig.getReceivemqWR()
                .type(MediaType.APPLICATION_JSON).header("X-tenantid", "single")
                .post(ClientResponse.class, rawJson);

        if (response.getStatus() != 200) {
            logger.debug("Failed : HTTP error code : " + response.getStatus());
        } else {
            messageList.clear();
        }

        logger.info("Output from test env .... \n");
        final String output = response.getEntity(String.class);
        logger.info("Receive " + output + " messages.");

    } catch (final Exception e) {
        logger.error("Fail to send to test env ", e);
    }

    // return messageNum + " messages are sent to test env.";
    return "1 messages are sent to test env.";

}

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

private void setUpParser() throws Exception {
    Class targetClass = AbstractCacheProviderFacadeParser.class;

    Method doParseMethod = targetClass.getDeclaredMethod("doParse",
            new Class[] { String.class, Element.class, BeanDefinitionRegistry.class });

    Method getCacheProviderFacadeClassMethod = targetClass.getDeclaredMethod("getCacheProviderFacadeClass",
            new Class[0]);

    Method[] methodsToMock = new Method[] { doParseMethod, getCacheProviderFacadeClassMethod };

    parserControl = MockClassControl.createControl(targetClass, null, null, methodsToMock);
    parser = (AbstractCacheProviderFacadeParser) parserControl.getMock();
}

From source file:org.opendaylight.nemo.renderer.cli.physicalnetwork.PhysicalResourceLoaderTest.java

@Test
public void testGetPhysicalNode() throws Exception {
    PhysicalNodeId physicalNodeId = mock(PhysicalNodeId.class);
    PhysicalNode physicalNode;/*from  w  w w  .j a v  a 2s  .  com*/

    Class<PhysicalResourceLoader> class1 = PhysicalResourceLoader.class;
    Method method = class1.getDeclaredMethod("getPhysicalNode", new Class[] { PhysicalNodeId.class });
    method.setAccessible(true);

    physicalNode = (PhysicalNode) method.invoke(physicalResourceLoader, physicalNodeId);

    Assert.assertTrue(physicalNode == null);
}

From source file:org.opendaylight.nemo.renderer.cli.physicalnetwork.PhysicalResourceLoaderTest.java

@Test
public void testGetPhysicalPort() throws Exception {
    PhysicalPortId physicalPortId = mock(PhysicalPortId.class);
    PhysicalPort physicalPort;/*from   w w w  . j av a 2  s  .  c  o m*/

    Class<PhysicalResourceLoader> class1 = PhysicalResourceLoader.class;
    Method method = class1.getDeclaredMethod("getPhysicalPort", new Class[] { PhysicalPortId.class });
    method.setAccessible(true);

    physicalPort = (PhysicalPort) method.invoke(physicalResourceLoader, physicalPortId);

    Assert.assertTrue(physicalPort == null);
}

From source file:org.opendaylight.nemo.renderer.cli.physicalnetwork.PhysicalResourceLoaderTest.java

@Test
public void testGetPhysicalLink() throws Exception {
    PhysicalLinkId physicalLinkId = mock(PhysicalLinkId.class);
    PhysicalLink physicalLink;/*from   ww  w  . j a v a  2 s  .co m*/

    Class<PhysicalResourceLoader> class1 = PhysicalResourceLoader.class;
    Method method = class1.getDeclaredMethod("getPhysicalLink", new Class[] { PhysicalLinkId.class });
    method.setAccessible(true);

    physicalLink = (PhysicalLink) method.invoke(physicalResourceLoader, physicalLinkId);

    Assert.assertTrue(physicalLink == null);
}

From source file:fr.cobaltians.cobalt.plugin.CobaltPluginManager.java

/****************************************************************************************************************************************
  * COBALT METHODS// www  . ja v  a2s.c  o m
  ****************************************************************************************************************************************/

public boolean onMessage(Context context, CobaltFragment fragment, JSONObject message) {
    try {
        String pluginName = message.getString(Cobalt.kJSPluginName);
        Class<? extends CobaltAbstractPlugin> pluginClass = mPluginsMap.get(pluginName);
        if (pluginClass != null) {
            try {
                Method pluginGetInstanceMethod = pluginClass.getDeclaredMethod(GET_INSTANCE_METHOD_NAME,
                        CobaltPluginWebContainer.class);
                try {
                    CobaltPluginWebContainer webContainer = new CobaltPluginWebContainer((Activity) context,
                            fragment);
                    CobaltAbstractPlugin plugin = (CobaltAbstractPlugin) pluginGetInstanceMethod.invoke(null,
                            webContainer);
                    plugin.onMessage(webContainer, message);
                    return true;
                } catch (NullPointerException exception) {
                    if (Cobalt.DEBUG) {
                        Log.e(TAG, "onMessage: " + pluginClass.getSimpleName()
                                + ".getInstance(CobaltPluginWebContainer) method must be static.");
                        exception.printStackTrace();
                    }
                } catch (IllegalAccessException exception) {
                    if (Cobalt.DEBUG)
                        exception.printStackTrace();
                } catch (InvocationTargetException exception) {
                    if (Cobalt.DEBUG) {
                        Log.e(TAG, "onMessage: exception thrown by " + pluginClass.getSimpleName()
                                + ".getInstance(CobaltPluginWebContainer) method.");
                        exception.printStackTrace();
                    }
                }
            } catch (NoSuchMethodException exception) {
                if (Cobalt.DEBUG) {
                    Log.e(TAG, "onMessage: no method found matching " + pluginClass.getSimpleName()
                            + ".getInstance(CobaltPluginWebContainer).");
                    exception.printStackTrace();
                }
            }
        } else if (Cobalt.DEBUG)
            Log.e(TAG, "onMessage: no plugin class found for name " + pluginName + ".");
    } catch (JSONException exception) {
        if (Cobalt.DEBUG) {
            Log.e(TAG, "onMessage: name field not found or not a String.");
            exception.printStackTrace();
        }
    }

    return false;
}