List of usage examples for org.apache.commons.lang3.reflect MethodUtils invokeStaticMethod
public static Object invokeStaticMethod(final Class<?> cls, final String methodName, Object... args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException
Invokes a named static method whose parameter type matches the object type.
This method delegates the method search to #getMatchingAccessibleMethod(Class,String,Class[]) .
This method supports calls to methods taking primitive parameters via passing in wrapping classes.
From source file:com.google.caliper.maven.CaliperBenchmark.java
public CaliperBenchmark(ClassLoader loader, String benchmarkClass) throws ClassNotFoundException { Class caliperBenchmark = loadClass(loader, "com.google.caliper.maven.CaliperBenchmarkHarness", "Please report this issue to bug tracker"); try {/*w w w . j av a2 s . c o m*/ benchmark = MethodUtils.invokeStaticMethod(caliperBenchmark, "create", benchmarkClass); } catch (Exception e) { throw new RuntimeException("Please report this problem to caliper-maven-plugin bug tracker", e); } }
From source file:alfio.config.SpringBootLauncher.java
private static void launchHsqlGUI() { Class<?> cls;//w ww . j a va2 s .com try { cls = ClassUtils.getClass("org.hsqldb.util.DatabaseManagerSwing"); MethodUtils.invokeStaticMethod(cls, "main", new Object[] { new String[] { "--url", "jdbc:hsqldb:mem:alfio", "--noexit" } }); } catch (ReflectiveOperationException e) { log.warn("error starting db manager", e); } }
From source file:com.offbynull.coroutines.instrumenter.InstrumenterTest.java
@Test public void mustNotFailWithVerifierErrorWhenRunningAsPerOfAnActor() throws Exception { try (URLClassLoader classLoader = loadClassesInZipResourceAndInstrument(PEERNETIC_FAILURE_TEST + ".zip")) { Class<?> cls = classLoader.loadClass(PEERNETIC_FAILURE_TEST); MethodUtils.invokeStaticMethod(cls, "main", new Object[] { new String[0] }); }// w w w. ja va2 s . com }
From source file:com.thinkbiganalytics.policy.BasePolicyAnnotationTransformer.java
private P createClass(BaseUiPolicyRule rule) throws ClassNotFoundException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException { P domainPolicy = null;/*from ww w . j a v a 2 s . c o m*/ String classType = rule.getObjectClassType(); Class<P> domainPolicyClass = (Class<P>) Class.forName(classType); Constructor constructor = null; Object[] paramValues = null; boolean hasConstructor = false; for (Constructor con : domainPolicyClass.getConstructors()) { hasConstructor = true; int parameterSize = con.getParameterTypes().length; paramValues = new Object[parameterSize]; for (int p = 0; p < parameterSize; p++) { Annotation[] annotations = con.getParameterAnnotations()[p]; Object paramValue = null; for (Annotation a : annotations) { if (a instanceof PolicyPropertyRef) { // this is the one we want if (constructor == null) { constructor = con; } //find the value associated to this property paramValue = getPropertyValue(rule, domainPolicyClass, (PolicyPropertyRef) a); } } paramValues[p] = paramValue; } if (constructor != null) { //exit once we find a constructor with @PropertyRef break; } } if (constructor != null) { //call that constructor try { domainPolicy = ConstructorUtils.invokeConstructor(domainPolicyClass, paramValues); } catch (NoSuchMethodException e) { domainPolicy = domainPolicyClass.newInstance(); } } else { //if the class has no public constructor then attempt to call the static instance method if (!hasConstructor) { //if the class has a static "instance" method on it then call that try { domainPolicy = (P) MethodUtils.invokeStaticMethod(domainPolicyClass, "instance", null); } catch (NoSuchMethodException | SecurityException | InvocationTargetException e) { domainPolicy = domainPolicyClass.newInstance(); } } else { //attempt to create a new instance domainPolicy = domainPolicyClass.newInstance(); } } return domainPolicy; }
From source file:org.grouplens.lenskit.cli.Main.java
private static void registerClass(Subparsers subparsers, Class<? extends Command> cls) { CommandSpec spec = cls.getAnnotation(CommandSpec.class); if (spec == null) { throw new IllegalArgumentException(cls + " has no @CommandSpec annotation"); }// w w w. j a v a 2 s . c om Subparser parser = subparsers.addParser(spec.name()).help(spec.help()).setDefault("command", cls); try { MethodUtils.invokeStaticMethod(cls, "configureArguments", parser); } catch (NoSuchMethodException e) { throw new RuntimeException("cannot configure command " + cls, e); } catch (IllegalAccessException e) { throw new RuntimeException("cannot configure command " + cls, e); } catch (InvocationTargetException e) { throw new RuntimeException("cannot configure command " + cls, e); } }
From source file:org.lenskit.specs.SpecUtils.java
/** * Build an object from a specification. On its own, this method does very little. It depends on support from * objects in order to work.//from ww w . j a va 2s . c om * * It operates as follows: * * 1. Load the {@link SpecHandler}s using {@link java.util.ServiceLoader}. * 2. Query each spec handler, in turn, to try to find one that can build an object of type `type` from the spec. * 3. If no such handler is found, try to invoke a static `fromSpec(AbstractSpec)` method on `type`. * 4. If no such method is found, or it returns `null`, throw {@link NoSpecHandlerFound}. * * @param type The type of object to build. * @param spec The specification to use. * @param cl The class loader to search. * @param <T> The built object type. * @return The built object. Will be `null` if and only if `spec` is `null`. * @throws NoSpecHandlerFound if no spec handler or `fromSpec` method can be found. */ @Nullable public static <T> T buildObject(@Nonnull Class<T> type, @Nullable AbstractSpec spec, ClassLoader cl) { if (spec == null) { return null; } ServiceLoader<SpecHandler> loader; if (cl == null) { loader = ServiceLoader.load(SpecHandler.class); } else { loader = ServiceLoader.load(SpecHandler.class, cl); } for (SpecHandler h : loader) { T obj = h.build(type, spec); if (obj != null) { return obj; } } try { T obj = type.cast(MethodUtils.invokeStaticMethod(type, "fromSpec", spec)); if (obj == null) { throw new NoSpecHandlerFound("No spec handler to build " + type + " from " + spec); } else { return obj; } } catch (NoSuchMethodException e) { throw new NoSpecHandlerFound("No spec handler to build " + type + " from " + spec); } catch (IllegalAccessException e) { throw new RuntimeException("access error invoking fromSpec on " + type, e); } catch (InvocationTargetException e) { throw new RuntimeException("Error occurred in fromSpec on " + type, e); } }