List of usage examples for java.lang Class getDeclaredMethod
@CallerSensitive public Method getDeclaredMethod(String name, Class<?>... parameterTypes) throws NoSuchMethodException, SecurityException
From source file:org.opendaylight.nemo.renderer.cli.physicalnetwork.PhysicalResourceLoaderTest.java
@Test public void testBuildPortAttributes() throws Exception { Class<PhysicalResourceLoader> class1 = PhysicalResourceLoader.class; Method method = class1.getDeclaredMethod("buildPortAttributes", new Class[] { JsonNode.class }); method.setAccessible(true);// w w w. j a va 2 s. co m List<Attribute> attributeList; JsonNode attributes = mock(JsonNode.class); JsonNode portAttribute = mock(JsonNode.class); JsonNode jsonNode_temp = mock(JsonNode.class); when(attributes.size()).thenReturn(1); when(attributes.get(any(Integer.class))).thenReturn(portAttribute); //get into method "buildPortAttribute" when(portAttribute.path(any(String.class))).thenReturn(jsonNode_temp); when(jsonNode_temp.asText()).thenReturn(new String(""))//branch null .thenReturn(new String("zm")); attributeList = (List<Attribute>) method.invoke(physicalResourceLoader, attributes); Assert.assertTrue(attributeList.size() == 0); //new AttributeName("zm"); attributeList = (List<Attribute>) method.invoke(physicalResourceLoader, attributes); Assert.assertTrue(attributeList.size() == 1); verify(portAttribute, times(3)).path(any(String.class)); verify(jsonNode_temp, times(3)).asText(); }
From source file:org.opendaylight.nemo.renderer.cli.physicalnetwork.PhysicalResourceLoaderTest.java
@Test public void testBuildLinks() throws Exception { List<PhysicalLink> list; Class<PhysicalResourceLoader> class1 = PhysicalResourceLoader.class; Method method = class1.getDeclaredMethod("buildLinks", new Class[] { JsonNode.class }); method.setAccessible(true);//from w ww . jav a 2 s . c o m JsonNode linksRoot = mock(JsonNode.class); JsonNode links = mock(JsonNode.class); JsonNode link = mock(JsonNode.class); JsonNode link_temp_buildlink = mock(JsonNode.class); when(linksRoot.path(any(String.class))).thenReturn(links); when(links.size()).thenReturn(1); when(links.get(any(Integer.class))).thenReturn(link); //get into method "build link" args(link) when(link.get(any(String.class))).thenReturn(link_temp_buildlink); when(link_temp_buildlink.asText()).thenReturn(new String("1"))//new PhysicalLinkId(strLinkId) .thenReturn(new String("2"))//new PhysicalNodeId .thenReturn(new String("3"))//new PhysicalPortId .thenReturn(new String("4"))//new PhysicalNodeId .thenReturn(new String("5"))//new PhysicalPortId .thenReturn(new String(""));//linkNode.get("link-bandwidth").asText().equals("") when(link_temp_buildlink.asLong()).thenReturn((long) 1); list = (List<PhysicalLink>) method.invoke(physicalResourceLoader, linksRoot); Assert.assertTrue(list.size() == 1); verify(link_temp_buildlink, times(6)).asText(); }
From source file:InlineTestStringsProvider.java
@SuppressWarnings("unchecked") @Override//w ww .jav a 2 s. c o m void provideStrings() throws Exception { // Setup errors are set by the constructor - if any. if (!isBlank(getErrors()) || !isSetupValid()) return; String testItemsGeneratorClassName = getClassName(); String testItemsGeneratorMethod = getMethodName(); // Invoke a method in an 'inline' items provide class // This code is here by design in order to keep the inline test item provider with no dependency on any of the main classes (such as TestItem) try { Class c = loadInlineClassIfNeeded(testItemsGeneratorClassName); try { Object o = c.newInstance(); ArrayList<String[]> testData = new ArrayList<>(); Method m = c.getDeclaredMethod(testItemsGeneratorMethod, testData.getClass()); m.setAccessible(true); // Invoke the method on the instance with testData as a parameter to be created and returned m.invoke(o, testData); stringifyTestItems(testData); } catch (InvocationTargetException e) { throw (new Exception( "InvocationTargetException generated by invoking Inline Items Generator Class: " + sq(testItemsGeneratorClassName) + " / Method: " + sq(testItemsGeneratorMethod))); } catch (Exception e) { throw (new Exception("Exception generated by invoking Inline Items Generator Class: " + sq(testItemsGeneratorClassName) + " / Method: " + sq(testItemsGeneratorMethod))); } } catch (Exception e) { System.out.println("failed to get class for class named: " + sq(testItemsGeneratorClassName)); } }
From source file:BrowserLauncher.java
/** * Called by a static initializer to load any classes, fields, and methods required at runtime * to locate the user's web browser./*ww w . j av a 2 s. c om*/ * @return <code>true</code> if all intialization succeeded * <code>false</code> if any portion of the initialization failed */ private static boolean loadClasses() { switch (jvm) { case MRJ_2_0: try { Class aeTargetClass = Class.forName("com.apple.MacOS.AETarget"); Class osUtilsClass = Class.forName("com.apple.MacOS.OSUtils"); Class appleEventClass = Class.forName("com.apple.MacOS.AppleEvent"); Class aeClass = Class.forName("com.apple.MacOS.ae"); aeDescClass = Class.forName("com.apple.MacOS.AEDesc"); aeTargetConstructor = aeTargetClass.getDeclaredConstructor(new Class[] { int.class }); appleEventConstructor = appleEventClass.getDeclaredConstructor( new Class[] { int.class, int.class, aeTargetClass, int.class, int.class }); aeDescConstructor = aeDescClass.getDeclaredConstructor(new Class[] { String.class }); makeOSType = osUtilsClass.getDeclaredMethod("makeOSType", new Class[] { String.class }); putParameter = appleEventClass.getDeclaredMethod("putParameter", new Class[] { int.class, aeDescClass }); sendNoReply = appleEventClass.getDeclaredMethod("sendNoReply", new Class[] {}); Field keyDirectObjectField = aeClass.getDeclaredField("keyDirectObject"); keyDirectObject = (Integer) keyDirectObjectField.get(null); Field autoGenerateReturnIDField = appleEventClass.getDeclaredField("kAutoGenerateReturnID"); kAutoGenerateReturnID = (Integer) autoGenerateReturnIDField.get(null); Field anyTransactionIDField = appleEventClass.getDeclaredField("kAnyTransactionID"); kAnyTransactionID = (Integer) anyTransactionIDField.get(null); } catch (ClassNotFoundException cnfe) { errorMessage = cnfe.getMessage(); return false; } catch (NoSuchMethodException nsme) { errorMessage = nsme.getMessage(); return false; } catch (NoSuchFieldException nsfe) { errorMessage = nsfe.getMessage(); return false; } catch (IllegalAccessException iae) { errorMessage = iae.getMessage(); return false; } break; case MRJ_2_1: try { mrjFileUtilsClass = Class.forName("com.apple.mrj.MRJFileUtils"); mrjOSTypeClass = Class.forName("com.apple.mrj.MRJOSType"); Field systemFolderField = mrjFileUtilsClass.getDeclaredField("kSystemFolderType"); kSystemFolderType = systemFolderField.get(null); findFolder = mrjFileUtilsClass.getDeclaredMethod("findFolder", new Class[] { mrjOSTypeClass }); getFileCreator = mrjFileUtilsClass.getDeclaredMethod("getFileCreator", new Class[] { File.class }); getFileType = mrjFileUtilsClass.getDeclaredMethod("getFileType", new Class[] { File.class }); } catch (ClassNotFoundException cnfe) { errorMessage = cnfe.getMessage(); return false; } catch (NoSuchFieldException nsfe) { errorMessage = nsfe.getMessage(); return false; } catch (NoSuchMethodException nsme) { errorMessage = nsme.getMessage(); return false; } catch (SecurityException se) { errorMessage = se.getMessage(); return false; } catch (IllegalAccessException iae) { errorMessage = iae.getMessage(); return false; } break; case MRJ_3_0: try { Class linker = Class.forName("com.apple.mrj.jdirect.Linker"); Constructor constructor = linker.getConstructor(new Class[] { Class.class }); linkage = constructor.newInstance(new Object[] { BrowserLauncher.class }); } catch (ClassNotFoundException cnfe) { errorMessage = cnfe.getMessage(); return false; } catch (NoSuchMethodException nsme) { errorMessage = nsme.getMessage(); return false; } catch (InvocationTargetException ite) { errorMessage = ite.getMessage(); return false; } catch (InstantiationException ie) { errorMessage = ie.getMessage(); return false; } catch (IllegalAccessException iae) { errorMessage = iae.getMessage(); return false; } break; case MRJ_3_1: try { mrjFileUtilsClass = Class.forName("com.apple.mrj.MRJFileUtils"); openURL = mrjFileUtilsClass.getDeclaredMethod("openURL", new Class[] { String.class }); } catch (ClassNotFoundException cnfe) { errorMessage = cnfe.getMessage(); return false; } catch (NoSuchMethodException nsme) { errorMessage = nsme.getMessage(); return false; } break; default: break; } return true; }
From source file:net.sf.excelutils.tags.CallTag.java
private Method findMethod(Object service, String methodName, Class[] types) { Class clazz; if (service instanceof Class) { clazz = (Class) service;//from ww w . j av a2 s . com } else { clazz = service.getClass(); } Method method = null; try { method = clazz.getDeclaredMethod(methodName, types); } catch (Exception e) { } return method; }
From source file:org.springmodules.cache.config.MetadataAttributesParserTests.java
protected void afterSetUp() throws Exception { Class targetClass = AbstractMetadataAttributesParser.class; Method registerCustomBeansMethod = targetClass.getDeclaredMethod("registerCustomBeans", new Class[] { BeanDefinitionRegistry.class }); Method configureCachingInterceptorMethod = targetClass.getDeclaredMethod("configureCachingInterceptor", new Class[] { MutablePropertyValues.class, BeanDefinitionRegistry.class }); Method configureFlushingInterceptorMethod = targetClass.getDeclaredMethod("configureFlushingInterceptor", new Class[] { MutablePropertyValues.class, BeanDefinitionRegistry.class }); Method[] methodsToMock = new Method[] { configureCachingInterceptorMethod, configureFlushingInterceptorMethod, registerCustomBeansMethod }; parserControl = MockClassControl.createControl(targetClass, null, null, methodsToMock); parser = (AbstractMetadataAttributesParser) parserControl.getMock(); }
From source file:org.opendaylight.nemo.renderer.cli.physicalnetwork.PhysicalResourceLoaderTest.java
@Test public void testBuildHosts() throws Exception { Class<PhysicalResourceLoader> class1 = PhysicalResourceLoader.class; Method method = class1.getDeclaredMethod("buildHosts", new Class[] { JsonNode.class }); method.setAccessible(true);//ww w.j av a 2 s.c om JsonNode hostsNode = mock(JsonNode.class); JsonNode hosts = mock(JsonNode.class); JsonNode host = mock(JsonNode.class); JsonNode host_temp = mock(JsonNode.class); JsonNode host_temp1 = mock(JsonNode.class); JsonNode ipaddrs = mock(JsonNode.class); JsonNode ipaddr = mock(JsonNode.class); List<PhysicalHost> list; when(hostsNode.path(any(String.class))).thenReturn(hosts); when(hosts.size()).thenReturn(1); when(hosts.get(any(Integer.class))).thenReturn(host); //get into method "buildhost" when(host.get(any(String.class))).thenReturn(host_temp); when(host_temp.asText()).thenReturn(new String("00001111-0000-0000-0000-000011112222")) //HOST_ID .thenReturn(new String("hostName")) //HOST_NAME .thenReturn(new String("00:11:22:33:44:55")) //MAC_ADDRESS .thenReturn(new String("nodeId")) //NODE_ID .thenReturn(new String("connetionId"));//PhysicalPortId when(host.path(any(String.class))).thenReturn(ipaddrs); when(ipaddrs.size()).thenReturn(1); when(ipaddrs.get(any(Integer.class))).thenReturn(ipaddr); when(ipaddr.get(any(String.class))).thenReturn(host_temp1); when(host_temp1.asText()).thenReturn(new String("192.168.1.1"));//ipv4_address list = (List<PhysicalHost>) method.invoke(physicalResourceLoader, hostsNode); Assert.assertTrue(list.size() == 1); }
From source file:com.egreen.tesla.server.api.component.Component.java
public void LoadClass() throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { URLClassLoader sysLoader = (URLClassLoader) ClassLoader.getSystemClassLoader(); Class sysClass = URLClassLoader.class; Method sysMethod = sysClass.getDeclaredMethod("addURL", new Class[] { URL.class }); sysMethod.setAccessible(true);/* w ww. j ava 2s . co m*/ sysMethod.invoke(sysLoader, new Object[] { jarFile }); // for (Object object : classeNames) { // Class classFromName = sysLoader.loadClass(object + ""); // LOGGER.info(classFromName.getSimpleName()); // component.setControllers(object + "", sysClass); // } }
From source file:org.opendaylight.nemo.renderer.openflow.physicalnetwork.PhyConfigLoaderTest.java
@Test public void testGetPhysicalNode() throws Exception { PhysicalNodeId physicalNodeId = mock(PhysicalNodeId.class); PhysicalNode physicalNode;/*from ww w .jav a 2 s.c om*/ Class<PhyConfigLoader> class1 = PhyConfigLoader.class; Method method = class1.getDeclaredMethod("getPhysicalNode", new Class[] { PhysicalNodeId.class }); method.setAccessible(true); physicalNode = (PhysicalNode) method.invoke(phyConfigLoader, physicalNodeId); Assert.assertTrue(physicalNode == null); }
From source file:org.opendaylight.nemo.renderer.openflow.physicalnetwork.PhyConfigLoaderTest.java
@Test public void testGetPhysicalPort() throws Exception { PhysicalPortId physicalPortId = mock(PhysicalPortId.class); PhysicalPort physicalPort;// w w w . j a va 2 s .c om Class<PhyConfigLoader> class1 = PhyConfigLoader.class; Method method = class1.getDeclaredMethod("getPhysicalPort", new Class[] { PhysicalPortId.class }); method.setAccessible(true); physicalPort = (PhysicalPort) method.invoke(phyConfigLoader, physicalPortId); Assert.assertTrue(physicalPort == null); }