build method by name and parameters, like: method(type1,type2) - Android java.lang.reflect

Android examples for java.lang.reflect:Method

Description

build method by name and parameters, like: method(type1,type2)

Demo Code


//package com.java2s;

import java.io.PrintWriter;
import java.io.StringWriter;

public class Main {
    /**/*from   w  w w  .j a v  a  2s  .c  o m*/
     * build method by name and parameters, like: method(type1,type2)
     */
    public static String getMethodFullName(String name,
            Class<?>[] parameterTypes) {
        StringBuilder builder = new StringBuilder();
        builder.append(name);
        builder.append("(");
        if (parameterTypes != null && parameterTypes.length > 0) {
            boolean first = true;
            for (Class<?> type : parameterTypes) {
                if (type != null) {
                    if (first) {
                        first = false;
                    } else {
                        builder.append(",");
                    }
                    builder.append(type.getCanonicalName());
                }
            }
        }
        builder.append(")");
        return builder.toString();
    }

    /**
     * make throwable to string.
     */
    public static String toString(Throwable throwable) {
        StringWriter stringWriter = new StringWriter();
        PrintWriter printWriter = new PrintWriter(stringWriter);
        printWriter.print(throwable.getClass().getName() + ": ");
        if (throwable.getMessage() != null) {
            printWriter.print(throwable.getMessage() + "\n");
        }
        printWriter.println();
        try {
            throwable.printStackTrace(printWriter);
            return stringWriter.toString();
        } finally {
            printWriter.close();
        }
    }
}

Related Tutorials