use of java.lang.reflect.InvocationTargetException in project jetty.project by eclipse.
the class EventMethod method call.
public void call(Object obj, Object... args) {
if ((this.pojo == null) || (this.method == null)) {
LOG.warn("Cannot execute call: pojo={}, method={}", pojo, method);
// no call event method determined
return;
}
if (obj == null) {
LOG.warn("Cannot call {} on null object", this.method);
return;
}
if (args.length > paramTypes.length) {
Object[] trimArgs = dropFirstArg(args);
call(obj, trimArgs);
return;
}
if (args.length < paramTypes.length) {
throw new IllegalArgumentException("Call arguments length [" + args.length + "] must always be greater than or equal to captured args length [" + paramTypes.length + "]");
}
try {
this.method.invoke(obj, args);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
String err = String.format("Cannot call method %s on %s with args: %s", method, pojo, QuoteUtil.join(args, ","));
throw new WebSocketException(err, e);
}
}
use of java.lang.reflect.InvocationTargetException in project elasticsearch by elastic.
the class ElasticsearchAssertions method tryCreateNewInstance.
private static Streamable tryCreateNewInstance(Streamable streamable) throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
try {
Class<? extends Streamable> clazz = streamable.getClass();
Constructor<? extends Streamable> constructor = clazz.getConstructor();
assertThat(constructor, Matchers.notNullValue());
Streamable newInstance = constructor.newInstance();
return newInstance;
} catch (Exception e) {
return null;
}
}
use of java.lang.reflect.InvocationTargetException in project dropwizard by dropwizard.
the class AbstractParamConverterProvider method getConverter.
@Override
public <T> ParamConverter<T> getConverter(Class<T> rawType, Type genericType, Annotation[] annotations) {
if (AbstractParam.class.isAssignableFrom(rawType)) {
final String parameterName = JerseyParameterNameProvider.getParameterNameFromAnnotations(annotations).orElse("Parameter");
final Constructor<T> constructor;
try {
constructor = rawType.getConstructor(String.class, String.class);
} catch (NoSuchMethodException ignored) {
// leaving Jersey to handle these parameters as it normally would.
return null;
}
return new ParamConverter<T>() {
@Override
@SuppressWarnings("unchecked")
public T fromString(String value) {
if (rawType != NonEmptyStringParam.class && Strings.isNullOrEmpty(value)) {
return null;
}
try {
return _fromString(value);
} catch (InvocationTargetException ex) {
final Throwable cause = ex.getCause();
if (cause instanceof WebApplicationException) {
throw (WebApplicationException) cause;
} else {
throw new ExtractorException(cause);
}
} catch (final Exception ex) {
throw new ProcessingException(ex);
}
}
protected T _fromString(String value) throws Exception {
return constructor.newInstance(value, parameterName);
}
@Override
public String toString(T value) throws IllegalArgumentException {
if (value == null) {
throw new IllegalArgumentException(LocalizationMessages.METHOD_PARAMETER_CANNOT_BE_NULL("value"));
}
return value.toString();
}
};
}
return null;
}
use of java.lang.reflect.InvocationTargetException in project jetty.project by eclipse.
the class ObjectMBean method getAttribute.
/* ------------------------------------------------------------ */
public Object getAttribute(String name) throws AttributeNotFoundException, MBeanException, ReflectionException {
Method getter = (Method) _getters.get(name);
if (getter == null) {
throw new AttributeNotFoundException(name);
}
try {
Object o = _managed;
if (getter.getDeclaringClass().isInstance(this))
// mbean method
o = this;
// get the attribute
Object r = getter.invoke(o, (java.lang.Object[]) null);
// convert to ObjectName if the type has the @ManagedObject annotation
if (r != null) {
if (r.getClass().isArray()) {
if (r.getClass().getComponentType().isAnnotationPresent(ManagedObject.class)) {
ObjectName[] on = new ObjectName[Array.getLength(r)];
for (int i = 0; i < on.length; i++) {
on[i] = _mbeanContainer.findMBean(Array.get(r, i));
}
r = on;
}
} else if (r instanceof Collection<?>) {
@SuppressWarnings("unchecked") Collection<Object> c = (Collection<Object>) r;
if (!c.isEmpty() && c.iterator().next().getClass().isAnnotationPresent(ManagedObject.class)) {
// check the first thing out
ObjectName[] on = new ObjectName[c.size()];
int i = 0;
for (Object obj : c) {
on[i++] = _mbeanContainer.findMBean(obj);
}
r = on;
}
} else {
Class<?> clazz = r.getClass();
while (clazz != null) {
if (clazz.isAnnotationPresent(ManagedObject.class)) {
ObjectName mbean = _mbeanContainer.findMBean(r);
if (mbean != null) {
return mbean;
} else {
return null;
}
}
clazz = clazz.getSuperclass();
}
}
}
return r;
} catch (IllegalAccessException e) {
LOG.warn(Log.EXCEPTION, e);
throw new AttributeNotFoundException(e.toString());
} catch (InvocationTargetException e) {
LOG.warn(Log.EXCEPTION, e);
throw new ReflectionException(new Exception(e.getCause()));
}
}
use of java.lang.reflect.InvocationTargetException in project jetty.project by eclipse.
the class ObjectMBean method invoke.
/* ------------------------------------------------------------ */
public Object invoke(String name, Object[] params, String[] signature) throws MBeanException, ReflectionException {
if (LOG.isDebugEnabled())
LOG.debug("ObjectMBean:invoke " + name);
String methodKey = name + "(";
if (signature != null)
for (int i = 0; i < signature.length; i++) methodKey += (i > 0 ? "," : "") + signature[i];
methodKey += ")";
ClassLoader old_loader = Thread.currentThread().getContextClassLoader();
try {
Thread.currentThread().setContextClassLoader(_loader);
Method method = (Method) _methods.get(methodKey);
if (method == null)
throw new NoSuchMethodException(methodKey);
Object o = _managed;
if (method.getDeclaringClass().isInstance(this)) {
o = this;
}
return method.invoke(o, params);
} catch (NoSuchMethodException e) {
LOG.warn(Log.EXCEPTION, e);
throw new ReflectionException(e);
} catch (IllegalAccessException e) {
LOG.warn(Log.EXCEPTION, e);
throw new MBeanException(e);
} catch (InvocationTargetException e) {
LOG.warn(Log.EXCEPTION, e);
throw new ReflectionException(new Exception(e.getCause()));
} finally {
Thread.currentThread().setContextClassLoader(old_loader);
}
}
Aggregations