List of usage examples for java.lang Class getMethods
@CallerSensitive public Method[] getMethods() throws SecurityException
From source file:Main.java
public static void main(String[] args) throws Exception { Class<?> class1 = Class.forName("Rate"); Object obj = class1.newInstance(); for (Method m : class1.getMethods()) { if (m.getName().equals("myMethod")) { Class<?>[] parameterTypes = m.getParameterTypes(); System.out.println(Arrays.toString(m.getParameterTypes())); Object methodArgs[] = new Object[parameterTypes.length]; for (Class<?> parameterType : parameterTypes) { if (parameterType == Double.TYPE) { double value = 0.5; methodArgs[0] = value; }//from w w w. j av a 2s. c om } Rate rate = (Rate) m.invoke(obj, methodArgs); System.out.println(rate.getValue()); } } }
From source file:Main.java
public static void main(String[] args) throws Exception { Class myclass = Class.forName("java.lang.String"); Method[] methods = myclass.getMethods(); //Object object = myclass.newInstance(); for (int i = 0; i < methods.length; i++) { System.out.println(methods[i].getName()); //System.out.println(methods[i].invoke(object)); }/*from w ww . java2s .c o m*/ }
From source file:org.jetbrains.webdemo.executors.JunitExecutor.java
public static void main(String[] args) { try {//from www . j av a 2 s . c o m JUnitCore jUnitCore = new JUnitCore(); jUnitCore.addListener(new MyRunListener()); List<Class> classes = getAllClassesFromTheDir(new File(args[0])); for (Class cl : classes) { boolean hasTestMethods = false; for (Method method : cl.getMethods()) { if (method.isAnnotationPresent(Test.class)) { hasTestMethods = true; break; } } if (!hasTestMethods) continue; Request request = Request.aClass(cl); jUnitCore.run(request); } try { ObjectMapper objectMapper = new ObjectMapper(); SimpleModule module = new SimpleModule(); module.addSerializer(Throwable.class, new ThrowableSerializer()); module.addSerializer(junit.framework.ComparisonFailure.class, new JunitFrameworkComparisonFailureSerializer()); module.addSerializer(org.junit.ComparisonFailure.class, new OrgJunitComparisonFailureSerializer()); objectMapper.registerModule(module); System.setOut(standardOutput); Map<String, List<TestRunInfo>> groupedTestResults = new HashMap<>(); for (TestRunInfo testRunInfo : output) { if (!groupedTestResults.containsKey(testRunInfo.className)) { groupedTestResults.put(testRunInfo.className, new ArrayList<TestRunInfo>()); } groupedTestResults.get(testRunInfo.className).add(testRunInfo); } System.out.print(objectMapper.writeValueAsString(groupedTestResults)); } catch (IOException e) { e.printStackTrace(); } } catch (Throwable e) { System.setOut(standardOutput); System.out.print("[\""); e.printStackTrace(); System.out.print("\"]"); } }
From source file:com.cisco.dvbu.ps.deploytool.DeployManagerUtil.java
@SuppressWarnings("rawtypes") public static void main(String[] args) { if (logger.isDebugEnabled()) { logger.debug("Entering Main method"); }/*from w ww . j ava 2 s . co m*/ if (args == null || args.length <= 1) { throw new ValidationException("Invalid Arguments"); } initSpring(); logger.info("--------------------------------------------------------"); logger.info("------------------ DEPLOYMENT MANAGER ------------------"); logger.info("--------------------------------------------------------"); try { boolean validMethod = false; initAdapter(); DeployManager deployManager = getDeployManager(); Class deployManagerClass = deployManager.getClass(); Method[] methods = deployManagerClass.getMethods(); // Number of method arguments int numInputArgs = args.length - 1; for (int i = 0; i < methods.length; i++) { String method = methods[i].getName(); int numMethodArgs = methods[i].getParameterTypes().length; if (methods[i].getName().equals(args[0]) && numMethodArgs == numInputArgs) { validMethod = true; try { if (logger.isInfoEnabled()) { logger.info("Calling Action " + args[0]); } // Determine which parameter is the password parameter when invoking methods. // The list and parameter number are contained in DeployManager.methodList. int maskParamNum = CommonUtils.getMaskParamNum(args[0], numInputArgs, DeployManager.methodList); String[] methodArgs = new String[args.length - 1]; for (int j = 0; j < args.length; j++) { //Skip arg[0] - method name if (j > 0) { methodArgs[j - 1] = args[j]; String arg = ""; if (args[j] != null) { arg = args[j].trim(); } if (logger.isDebugEnabled()) { // This is a special case. If a method in the DeployManager.methodList is being invoked then it has the potential of // containing a password. If it does that password is blanked out on display with "********". if (maskParamNum != j) { logger.debug("arg[" + j + "]=" + arg); } else { if (arg.length() == 0) { logger.debug("arg[" + j + "]=" + arg); } else { logger.debug("arg[" + j + "]=********"); } } } } } // Invoke the action Object returnValue = methods[i].invoke(deployManager, (Object[]) methodArgs); if (logger.isDebugEnabled()) { logger.debug(" Result from invoking method " + args[0] + " " + returnValue); } } catch (IllegalArgumentException e) { logger.error("Error while invoking method " + args[0], e); throw new ValidationException(e.getMessage(), e); } catch (IllegalAccessException e) { logger.error("Error while invoking method " + args[0], e); throw new ValidationException(e.getMessage(), e); } catch (InvocationTargetException e) { logger.error("Error while invoking method " + args[0], e); throw new ValidationException(e.getMessage(), e); } catch (Throwable e) { throw new ValidationException(e.getMessage(), e); } } } if (!validMethod) { logger.error("Passed in method " + args[0] + " does not exist or does not match the number of required arguments."); throw new ValidationException("Passed in method " + args[0] + " does not exist or does not match the number of required arguments."); } } catch (CompositeException e) { logger.error("Error occured while executing ", e); throw e; } if (logger.isDebugEnabled()) { logger.debug("Leaving Main method"); } }
From source file:com.cisco.dvbu.ps.common.scriptutil.ScriptUtil.java
public static void main(String[] args) { if (args == null || args.length <= 1) { throw new ValidationException("Invalid Arguments"); }/*w w w . j a v a 2s .co m*/ try { boolean validMethod = false; Class<ScriptUtil> scriptUtilClass = ScriptUtil.class; Method[] methods = scriptUtilClass.getMethods(); for (int i = 0; i < methods.length; i++) { String name = methods[i].getName(); if (name.equals(args[0])) { validMethod = true; try { String[] methodArgs = new String[args.length - 1]; for (int j = 0; j < methodArgs.length; j++) { methodArgs[j] = args[j + 1]; } if (logger.isDebugEnabled()) { logger.debug(" Invoking method " + args[0] + " With args" + methodArgs); } Object returnValue = methods[i].invoke(scriptUtilClass, (Object[]) methodArgs); if (logger.isDebugEnabled()) { logger.debug(" Result from invoking method " + args[0] + " " + returnValue); } } catch (IllegalArgumentException e) { logger.error("Error while invoking method " + args[0], e); throw new ValidationException(e.getMessage(), e); } catch (IllegalAccessException e) { logger.error("Error while invoking method " + args[0], e); throw new ValidationException(e.getMessage(), e); } catch (InvocationTargetException e) { logger.error("Error while invoking method " + args[0], e); throw new ValidationException(e.getMessage(), e); } catch (Throwable e) { throw new ValidationException(e.getMessage(), e); } } } if (!validMethod) { logger.error("Passed in method " + args[0] + " does not exist"); throw new ValidationException("Passed in method " + args[0] + " does not exist "); } } catch (CompositeException e) { logger.error("Error occured while executing ", e); throw e; } }
From source file:ReflectionDemo1.java
public static void main(String args[]) { try {/*from w w w . ja v a 2 s . c o m*/ Class c = Class.forName("java.awt.Dimension"); System.out.println("Constructors:"); Constructor constructors[] = c.getConstructors(); for (int i = 0; i < constructors.length; i++) { System.out.println(" " + constructors[i]); } System.out.println("Fields:"); Field fields[] = c.getFields(); for (int i = 0; i < fields.length; i++) { System.out.println(" " + fields[i]); } System.out.println("Methods:"); Method methods[] = c.getMethods(); for (int i = 0; i < methods.length; i++) { System.out.println(" " + methods[i]); } } catch (Exception e) { System.out.println("Exception: " + e); } }
From source file:net.dontdrinkandroot.lastfm.api.CheckImplementationStatus.java
public static void main(final String[] args) throws DocumentException, IOException { CheckImplementationStatus.xmlReader = new Parser(); CheckImplementationStatus.saxReader = new SAXReader(CheckImplementationStatus.xmlReader); final String packagePrefix = "net.dontdrinkandroot.lastfm.api.model."; final Map<String, Map<String, URL>> packages = CheckImplementationStatus.parseOverview(); final StringBuffer html = new StringBuffer(); html.append("<html>\n"); html.append("<head>\n"); html.append("<title>Implementation Status</title>\n"); html.append("</head>\n"); html.append("<body>\n"); html.append("<h1>Implementation Status</h1>\n"); final StringBuffer wiki = new StringBuffer(); int numImplemented = 0; int numTested = 0; int numMethods = 0; final List<String> packageList = new ArrayList<String>(packages.keySet()); Collections.sort(packageList); for (final String pkg : packageList) { System.out.println("Parsing " + pkg); html.append("<h2>" + pkg + "</h2>\n"); wiki.append("\n===== " + pkg + " =====\n\n"); Class<?> modelClass = null; final String className = packagePrefix + pkg; try {// w ww .ja v a 2 s . co m modelClass = Class.forName(className); System.out.println("\tClass " + modelClass.getName() + " exists"); } catch (final ClassNotFoundException e) { // e.printStackTrace(); System.out.println("\t" + className + ": DOES NOT exist"); } Class<?> testClass = null; final String testClassName = packagePrefix + pkg + "Test"; try { testClass = Class.forName(testClassName); System.out.println("\tTestClass " + testClass.getName() + " exists"); } catch (final ClassNotFoundException e) { // e.printStackTrace(); System.out.println("\t" + testClassName + ": TestClass for DOES NOT exist"); } final List<String> methods = new ArrayList<String>(packages.get(pkg).keySet()); Collections.sort(methods); final Method[] classMethods = modelClass.getMethods(); final Method[] testMethods = testClass.getMethods(); html.append("<table>\n"); html.append("<tr><th>Method</th><th>Implemented</th><th>Tested</th></tr>\n"); wiki.append("^ Method ^ Implemented ^ Tested ^\n"); numMethods += methods.size(); for (final String method : methods) { System.out.println("\t\t parsing " + method); html.append("<tr>\n"); html.append("<td>" + method + "</td>\n"); wiki.append("| " + method + " "); boolean classMethodFound = false; for (final Method classMethod : classMethods) { if (classMethod.getName().equals(method)) { classMethodFound = true; break; } } if (classMethodFound) { System.out.println("\t\t\tMethod " + method + " found"); html.append("<td style=\"background-color: green\">true</td>\n"); wiki.append("| yes "); numImplemented++; } else { System.out.println("\t\t\t" + method + " NOT found"); html.append("<td style=\"background-color: red\">false</td>\n"); wiki.append("| **no** "); } boolean testMethodFound = false; final String testMethodName = "test" + StringUtils.capitalize(method); for (final Method testMethod : testMethods) { if (testMethod.getName().equals(testMethodName)) { testMethodFound = true; break; } } if (testMethodFound) { System.out.println("\t\t\tTestMethod " + method + " found"); html.append("<td style=\"background-color: green\">true</td>\n"); wiki.append("| yes |\n"); numTested++; } else { System.out.println("\t\t\t" + testMethodName + " NOT found"); html.append("<td style=\"background-color: red\">false</td>\n"); wiki.append("| **no** |\n"); } html.append("</tr>\n"); } html.append("</table>\n"); // for (String methodName : methods) { // URL url = pkg.getValue().get(methodName); // System.out.println("PARSING: " + pkg.getKey() + "." + methodName + ": " + url); // String html = loadIntoString(url); // String description = null; // Matcher descMatcher = descriptionPattern.matcher(html); // if (descMatcher.find()) { // description = descMatcher.group(1).trim(); // } // boolean post = false; // Matcher postMatcher = postPattern.matcher(html); // if (postMatcher.find()) { // post = true; // } // Matcher paramsMatcher = paramsPattern.matcher(html); // List<String[]> params = new ArrayList<String[]>(); // boolean authenticated = false; // if (paramsMatcher.find()) { // String paramsString = paramsMatcher.group(1); // Matcher paramMatcher = paramPattern.matcher(paramsString); // while (paramMatcher.find()) { // String[] param = new String[3]; // param[0] = paramMatcher.group(1); // param[1] = paramMatcher.group(3); // param[2] = paramMatcher.group(5); // // System.out.println(paramMatcher.group(1) + "|" + paramMatcher.group(3) + "|" + // paramMatcher.group(5)); // if (param[0].equals("")) { // /* DO NOTHING */ // } else if (param[0].equals("api_key")) { // /* DO NOTHING */ // } else if (param[0].equals("api_sig")) { // authenticated = true; // } else { // params.add(param); // } // } // } // } // count++; // } html.append("<hr />"); html.append("<p>" + numImplemented + "/" + numMethods + " implemented (" + numImplemented * 100 / numMethods + "%)</p>"); html.append("<p>" + numTested + "/" + numMethods + " tested (" + numTested * 100 / numMethods + "%)</p>"); html.append("</body>\n"); html.append("</html>\n"); FileOutputStream out = new FileOutputStream(new File(FileUtils.getTempDirectory(), "apistatus.html")); IOUtils.write(html, out); IOUtils.closeQuietly(out); out = new FileOutputStream(new File(FileUtils.getTempDirectory(), "apistatus.wiki.txt")); IOUtils.write(wiki, out); IOUtils.closeQuietly(out); }
From source file:SampleMethod.java
static void showMethods(Object o) { Class c = o.getClass(); Method[] theMethods = c.getMethods(); for (int i = 0; i < theMethods.length; i++) { String methodString = theMethods[i].getName(); System.out.println("Name: " + methodString); String returnString = theMethods[i].getReturnType().getName(); System.out.println(" Return Type: " + returnString); Class[] parameterTypes = theMethods[i].getParameterTypes(); System.out.print(" Parameter Types:"); for (int k = 0; k < parameterTypes.length; k++) { String parameterString = parameterTypes[k].getName(); System.out.print(" " + parameterString); }//from w ww . j a v a2 s . c o m System.out.println(); } }
From source file:Main.java
public static Method[] getMethodsByName(Class<?> clazz, String name) { Method[] methods = clazz.getMethods(); List<Method> list = new ArrayList<Method>(); for (Method method : methods) { if (method.getName().equals(name)) list.add(method);// ww w .j av a2s .c o m } return list.toArray(new Method[list.size()]); }
From source file:Main.java
public static Method getXMLBeansValueMethod(Class<?> wrapperType) { for (Method method : wrapperType.getMethods()) { if (method.getName().startsWith("addNew")) { return method; }//w w w . j a v a2 s. c o m } return null; }