Example usage for java.lang.reflect InvocationTargetException getClass

List of usage examples for java.lang.reflect InvocationTargetException getClass

Introduction

In this page you can find the example usage for java.lang.reflect InvocationTargetException getClass.

Prototype

@HotSpotIntrinsicCandidate
public final native Class<?> getClass();

Source Link

Document

Returns the runtime class of this Object .

Usage

From source file:org.jetbrains.webdemo.executors.JavaExecutor.java

public static void main(String[] args) {
    PrintStream defaultOutputStream = System.out;
    try {//from   w ww. j a v a 2 s .  c  o m
        System.setOut(new PrintStream(standardOutputStream));
        System.setErr(new PrintStream(errorOutputStream));

        RunOutput outputObj = new RunOutput();
        String className;
        if (args.length > 0) {
            className = args[0];
            try {
                Method mainMethod = Class.forName(className).getMethod("main", String[].class);
                mainMethod.invoke(null, (Object) Arrays.copyOfRange(args, 1, args.length));
            } catch (InvocationTargetException e) {
                outputObj.exception = e.getCause();
            } catch (NoSuchMethodException e) {
                System.err.println("No main method found in project.");
            } catch (ClassNotFoundException e) {
                System.err.println("No main method found in project.");
            }
        } else {
            System.err.println("No main method found in project.");
        }

        System.out.flush();
        System.err.flush();
        System.setOut(defaultOutputStream);
        outputObj.text = outputStream.toString().replaceAll("</errStream><errStream>", "")
                .replaceAll("</outStream><outStream>", "");
        ObjectMapper objectMapper = new ObjectMapper();
        SimpleModule module = new SimpleModule();
        module.addSerializer(Throwable.class, new ThrowableSerializer());
        objectMapper.registerModule(module);
        System.out.print(objectMapper.writeValueAsString(outputObj));
    } catch (Throwable e) {
        System.setOut(defaultOutputStream);
        System.out.println("{\"text\":\"<errStream>" + e.getClass().getName() + ": " + e.getMessage());
        System.out.print("</errStream>\"}");
    }

}

From source file:ai.general.net.MethodHandler.java

/**
 * Handles the request by calling the method represented by this handler.
 * Request parameters are converted to match the method signature if possible. If no conversion
 * is possible, the method is not called.
 *
 * @param request The request to handle.
 *//* w  w w .j a  v a 2s  .co  m*/
public void handle(Request request) {
    log.entry(request.getUri().toString());
    try {
        Object[] raw_args = request.getArguments().toArray();
        if (raw_args.length != parameter_types_.length) {
            request.getResult()
                    .addError(new Result.Error("invalid number of method arguments", "got " + raw_args.length
                            + " arguments for method with " + parameter_types_.length + " arguments"));
            log.exit("invalid number of arguments");
            return;
        }
        Object[] args = new Object[raw_args.length];
        for (int i = 0; i < raw_args.length; i++) {
            args[i] = json_parser_.convertValue(raw_args[i], parameter_types_[i]);
        }
        Object result = method_.invoke(instance_, args);
        if (result != null) {
            request.getResult().addValue(result);
        }
    } catch (InvocationTargetException e) {
        log.catching(Level.TRACE, e);
        Throwable cause = e.getCause();
        if (cause != null) {
            if (cause instanceof RpcException) {
                request.getResult()
                        .addError(new Result.Error(cause.getMessage(), ((RpcException) cause).getDetails()));
            } else {
                request.getResult().addError(new Result.Error(cause.getClass().getName(), cause.getMessage()));
            }
        } else {
            request.getResult().addError(new Result.Error("unspecified exception thrown by RPC method", null));
        }
    } catch (ReflectiveOperationException e) {
        log.catching(Level.TRACE, e);
        request.getResult().addError(new Result.Error("cannot call method with specified arguments", null));
    } catch (Exception e) {
        log.catching(Level.TRACE, e);
        request.getResult().addError(new Result.Error(e.getClass().getName(), e.getMessage()));
    }
    log.exit();
}

From source file:org.hyperic.snmp.SNMPSessionCache.java

public Object invoke(Object proxy, Method method, Object[] args) throws SNMPException {
    SNMPCacheObject cacheVal = null;//  w w  w .  ja v  a 2 s  . c o  m

    HashMap cache = null;

    Object cacheKey = null;
    Object retval;

    String name = method.getName();

    long timeNow = 0;

    // Perhaps more later...
    if (name.equals("getBulk")) {
        cache = this.bulkCache;

        cacheKey = args[0];
    } else if (name.equals("getTable")) {
        cache = this.tableCache;

        cacheKey = new Integer(args[0].hashCode() ^ args[1].hashCode());
    } else if (name.equals("getColumn")) {
        cache = this.columnCache;

        cacheKey = args[0];
    }

    if (cache != null) {
        timeNow = System.currentTimeMillis();

        cacheVal = getFromCache(timeNow, cache, name, cacheKey);

        if (cacheVal.value != null) {
            return cacheVal.value;
        }
    }

    try {
        retval = method.invoke(this.session, args);
    } catch (InvocationTargetException e) {
        Throwable t = ((InvocationTargetException) e).getTargetException();

        String msg;

        if (t instanceof MIBLookupException) {
            throw (MIBLookupException) t;
        }

        if (t instanceof SNMPException) {
            msg = "";
        } else {
            msg = t.getClass().getName() + ": ";
        }

        msg += t.getMessage() + " invoking: " + invokerToString(name, args, cacheKey);

        throw new SNMPException(msg, t);
    } catch (Exception e) {
        String msg = e.getClass().getName() + ": " + e.getMessage() + " invoking: "
                + invokerToString(name, args, cacheKey);

        throw new SNMPException(msg, e);
    }

    if (cacheVal != null) {
        cacheVal.value = retval;
        cacheVal.timestamp = timeNow;

        if (log.isDebugEnabled()) {
            log.debug(invokerToString(name, args, cacheKey) + " took: " + new StopWatch(timeNow));
        }
    }

    return retval;
}