Search in sources :

Example 1 with InvocationTargetException

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);
    }
}
Also used : WebSocketException(org.eclipse.jetty.websocket.api.WebSocketException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 2 with InvocationTargetException

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;
    }
}
Also used : Streamable(org.elasticsearch.common.io.stream.Streamable) ElasticsearchException(org.elasticsearch.ElasticsearchException) SearchPhaseExecutionException(org.elasticsearch.action.search.SearchPhaseExecutionException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ClusterBlockException(org.elasticsearch.cluster.block.ClusterBlockException) IOException(java.io.IOException) ShardOperationFailedException(org.elasticsearch.action.ShardOperationFailedException)

Example 3 with InvocationTargetException

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;
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) ParamConverter(javax.ws.rs.ext.ParamConverter) InvocationTargetException(java.lang.reflect.InvocationTargetException) ProcessingException(javax.ws.rs.ProcessingException) WebApplicationException(javax.ws.rs.WebApplicationException) ExtractorException(org.glassfish.jersey.internal.inject.ExtractorException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ExtractorException(org.glassfish.jersey.internal.inject.ExtractorException) ProcessingException(javax.ws.rs.ProcessingException)

Example 4 with InvocationTargetException

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()));
    }
}
Also used : ReflectionException(javax.management.ReflectionException) AttributeNotFoundException(javax.management.AttributeNotFoundException) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) AttributeNotFoundException(javax.management.AttributeNotFoundException) ReflectionException(javax.management.ReflectionException) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvalidAttributeValueException(javax.management.InvalidAttributeValueException) MBeanException(javax.management.MBeanException) ObjectName(javax.management.ObjectName) Collection(java.util.Collection) ManagedObject(org.eclipse.jetty.util.annotation.ManagedObject) ManagedObject(org.eclipse.jetty.util.annotation.ManagedObject)

Example 5 with InvocationTargetException

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);
    }
}
Also used : ReflectionException(javax.management.ReflectionException) MBeanException(javax.management.MBeanException) ManagedObject(org.eclipse.jetty.util.annotation.ManagedObject) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) AttributeNotFoundException(javax.management.AttributeNotFoundException) ReflectionException(javax.management.ReflectionException) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvalidAttributeValueException(javax.management.InvalidAttributeValueException) MBeanException(javax.management.MBeanException)

Aggregations

InvocationTargetException (java.lang.reflect.InvocationTargetException)4722 Method (java.lang.reflect.Method)1999 IOException (java.io.IOException)541 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)485 IRunnableWithProgress (org.eclipse.jface.operation.IRunnableWithProgress)401 ArrayList (java.util.ArrayList)399 List (java.util.List)248 CoreException (org.eclipse.core.runtime.CoreException)241 File (java.io.File)236 ProgressMonitorDialog (org.eclipse.jface.dialogs.ProgressMonitorDialog)232 Constructor (java.lang.reflect.Constructor)229 Field (java.lang.reflect.Field)218 Test (org.junit.Test)207 Map (java.util.Map)201 HashMap (java.util.HashMap)200 DBException (org.jkiss.dbeaver.DBException)169 IStatus (org.eclipse.core.runtime.IStatus)136 DBRProgressMonitor (org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)97 Arrays (java.util.Arrays)96 Status (org.eclipse.core.runtime.Status)94