List of usage examples for java.lang Class getDeclaredMethod
@CallerSensitive public Method getDeclaredMethod(String name, Class<?>... parameterTypes) throws NoSuchMethodException, SecurityException
From source file:Main.java
public static void main(String[] args) throws Exception { Class c = Class.forName("MyClass"); Method m = c.getDeclaredMethod("say", new Class[] { String.class, String.class }); Object i = c.newInstance();/*from w w w . j a v a2s .co m*/ Object r = m.invoke(i, new Object[] { "Hello", "World" }); }
From source file:Main.java
public static void main(String[] args) { Main cls = new Main(); Class c = cls.getClass(); try {// w w w . ja v a 2 s . c o m Method m = c.getDeclaredMethod("show", null); System.out.println("method = " + m.toString()); // method Integer Class[] cArg = new Class[1]; cArg[0] = Integer.class; Method lMethod = c.getDeclaredMethod("showInteger", cArg); System.out.println("method = " + lMethod.toString()); } catch (NoSuchMethodException e) { System.out.println(e.toString()); } }
From source file:MethodTroubleReturns.java
public static void main(String... args) { try {//from w w w .j ava2 s .c om MethodTroubleReturns mtr = new MethodTroubleReturns(); Class<?> c = mtr.getClass(); Method m = c.getDeclaredMethod("drinkMe", int.class); m.invoke(mtr, -1); // production code should handle these exceptions more gracefully } catch (InvocationTargetException x) { Throwable cause = x.getCause(); System.err.format("drinkMe() failed: %s%n", cause.getMessage()); } catch (Exception x) { x.printStackTrace(); } }
From source file:Main.java
public static void main(String... args) { try {/*w ww. j av a 2 s .com*/ Class<?> c = Class.forName(args[0]); Class[] argTypes = new Class[] { String[].class }; Method main = c.getDeclaredMethod("main", argTypes); String[] mainArgs = Arrays.copyOfRange(args, 1, args.length); System.out.format("invoking %s.main()%n", c.getName()); main.invoke(null, (Object) mainArgs); // production code should handle these exceptions more gracefully } catch (ClassNotFoundException x) { x.printStackTrace(); } catch (NoSuchMethodException x) { x.printStackTrace(); } catch (IllegalAccessException x) { x.printStackTrace(); } catch (InvocationTargetException x) { x.printStackTrace(); } }
From source file:org.apache.commons.net.examples.Main.java
/** * Helper application for example classes. * Lists available classes, and provides shorthand invocation. * For example:<br>/*w w w .j a va 2 s .co m*/ * <code>java -jar commons-net-examples-m.n.jar FTPClientExample -l host user password</code> * * @param args the first argument is used to name the class; remaining arguments * are passed to the target class. * @throws Throwable if an error occurs */ public static void main(String[] args) throws Throwable { final Properties fp = new Properties(); final InputStream ras = Main.class.getResourceAsStream("examples.properties"); if (ras != null) { fp.load(ras); } else { System.err.println("[Cannot find examples.properties file, so aliases cannot be used]"); } if (args.length == 0) { if (Thread.currentThread().getStackTrace().length > 2) { // called by Maven System.out.println("Usage: mvn -q exec:java -Dexec.arguments=<alias or" + " exampleClass>,<exampleClass parameters> (comma-separated, no spaces)"); System.out.println("Or : mvn -q exec:java -Dexec.args=\"<alias" + " or exampleClass> <exampleClass parameters>\" (space separated)"); } else { if (fromJar()) { System.out.println( "Usage: java -jar commons-net-examples-m.n.jar <alias or exampleClass> <exampleClass parameters>"); } else { System.out.println( "Usage: java -cp target/classes examples/Main <alias or exampleClass> <exampleClass parameters>"); } } @SuppressWarnings("unchecked") // property names are Strings List<String> l = (List<String>) Collections.list(fp.propertyNames()); if (l.isEmpty()) { return; } Collections.sort(l); System.out.println("\nAliases and their classes:"); for (String s : l) { System.out.printf("%-25s %s%n", s, fp.getProperty(s)); } return; } String shortName = args[0]; String fullName = fp.getProperty(shortName); if (fullName == null) { fullName = shortName; } fullName = fullName.replace('/', '.'); try { Class<?> clazz = Class.forName(fullName); Method m = clazz.getDeclaredMethod("main", new Class[] { args.getClass() }); String[] args2 = new String[args.length - 1]; System.arraycopy(args, 1, args2, 0, args2.length); try { m.invoke(null, (Object) args2); } catch (InvocationTargetException ite) { Throwable cause = ite.getCause(); if (cause != null) { throw cause; } else { throw ite; } } } catch (ClassNotFoundException e) { System.out.println(e); } }
From source file:it.doqui.index.ecmengine.test.AllTests.java
public static void main(String[] args) { Log logger = LogFactory.getLog(ECMENGINE_TEST_LOG_CATEGORY); logger.debug("[AllTests::main] BEGIN"); String runner;//www. j av a 2s . c o m int i; String arguments[]; Class cls; runner = null; for (i = 0; (i < args.length) && (null == runner); i++) { if (args[i].equalsIgnoreCase("-text")) runner = "junit.textui.TestRunner"; else if (args[i].equalsIgnoreCase("-awt")) runner = "junit.awtui.TestRunner"; else if (args[i].equalsIgnoreCase("-swing")) runner = "junit.swingui.TestRunner"; } if (null != runner) { // remove it from the arguments arguments = new String[args.length - 1]; System.arraycopy(args, 0, arguments, 0, i - 1); System.arraycopy(args, i, arguments, i - 1, args.length - i); args = arguments; } else runner = "junit.swingui.TestRunner"; // append the test class arguments = new String[args.length + 1]; System.arraycopy(args, 0, arguments, 0, args.length); arguments[args.length] = "it.doqui.index.ecmengine.test.AllTests"; // invoke main() of the test runner try { cls = Class.forName(runner); java.lang.reflect.Method method = cls.getDeclaredMethod("main", new Class[] { String[].class }); method.invoke(null, new Object[] { arguments }); } catch (Throwable t) { logger.debug("[AllTests::main] Problem in test execution : " + t); } finally { logger.debug("[AllTests::main] BEGIN"); } }
From source file:Main.java
public static boolean objectHasEquals(Class clazz) { try {/*from w ww . j a v a 2s.com*/ clazz.getDeclaredMethod("equals", Object.class); return true; } catch (NoSuchMethodException e) { } //If no exception thrown, then the method exists! return false; }
From source file:Main.java
public static Method getPrivateMethod(Object target, String methodName, Class<?>... parameterTypes) throws NoSuchMethodException { Class targetClass = target.getClass(); Method method = targetClass.getDeclaredMethod(methodName, parameterTypes); method.setAccessible(true);/*from w w w. ja va 2 s . c o m*/ return method; }
From source file:Main.java
public static boolean hasMethod(String className, String methodName, Class<?>... parameterTypes) { try {/*w w w.j a v a2s . c om*/ Class<?> klass = Class.forName(className); klass.getDeclaredMethod(methodName, parameterTypes); return true; } catch (Throwable th) { return false; } }
From source file:Main.java
@SuppressWarnings("unchecked") public static Method getStaticMethod(Class clazz, String methodName, Class args[]) { try {// w w w. j a va 2 s. c o m Method method = clazz.getDeclaredMethod(methodName, args); if ((method.getModifiers() & Modifier.STATIC) != 0) return method; } catch (NoSuchMethodException ex) { } return null; }