Example usage for java.lang.reflect Method getName

List of usage examples for java.lang.reflect Method getName

Introduction

In this page you can find the example usage for java.lang.reflect Method getName.

Prototype

@Override
public String getName() 

Source Link

Document

Returns the name of the method represented by this Method object, as a String .

Usage

From source file:com.pinterest.terrapin.tools.TerrapinAdmin.java

public static void main(String[] args) throws Exception {
    PropertiesConfiguration configuration = TerrapinUtil
            .readPropertiesExitOnFailure(System.getProperties().getProperty("terrapin.config"));
    TerrapinAdmin admin = new TerrapinAdmin(configuration);
    String action = args[0];/* w  ww  .  j  a v a2  s . c om*/
    Method[] methods = admin.getClass().getMethods();
    boolean done = false;
    for (Method method : methods) {
        if (method.getName().equals(action) && !Modifier.isStatic(method.getModifiers())) {
            method.invoke(admin, (Object) args);
            done = true;
        }
    }
    if (!done) {
        LOG.error("Could not find a function call for " + args[0]);
        System.exit(1);
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {

    Class cls = java.lang.String.class;
    Method method = cls.getMethods()[0];
    Field field = cls.getFields()[0];
    Constructor constructor = cls.getConstructors()[0];
    String name;/*ww  w  . ja  va  2  s.c o m*/

    name = cls.getName();
    System.out.println(name);
    name = cls.getName() + "." + field.getName();
    System.out.println(name);
    name = constructor.getName();
    System.out.println(name);
    name = cls.getName() + "." + method.getName();
    System.out.println(name);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {

    Class cls = java.lang.String.class;
    Method method = cls.getMethods()[0];
    Field field = cls.getFields()[0];
    Constructor constructor = cls.getConstructors()[0];
    String name;//from   w  w  w .j  ava2  s.c  om

    name = cls.getName().substring(cls.getPackage().getName().length() + 1);
    System.out.println(name);
    name = field.getName();
    System.out.println(name);
    name = constructor.getName().substring(cls.getPackage().getName().length() + 1);
    System.out.println(name);
    name = method.getName();
    System.out.println(name);

}

From source file:MethodInspector.java

public static void main(String[] arguments) {
    Class inspect;/*from   w  ww.  ja va2 s. c o  m*/
    try {
        if (arguments.length > 0)
            inspect = Class.forName(arguments[0]);
        else
            inspect = Class.forName("MethodInspector");
        Method[] methods = inspect.getDeclaredMethods();
        for (int i = 0; i < methods.length; i++) {
            Method methVal = methods[i];
            Class returnVal = methVal.getReturnType();
            int mods = methVal.getModifiers();
            String modVal = Modifier.toString(mods);
            Class[] paramVal = methVal.getParameterTypes();
            StringBuffer params = new StringBuffer();
            for (int j = 0; j < paramVal.length; j++) {
                if (j > 0)
                    params.append(", ");
                params.append(paramVal[j].getName());
            }
            System.out.println("Method: " + methVal.getName() + "()");
            System.out.println("Modifiers: " + modVal);
            System.out.println("Return Type: " + returnVal.getName());
            System.out.println("Parameters: " + params + "\n");
        }
    } catch (ClassNotFoundException c) {
        System.out.println(c.toString());
    }
}

From source file:net.dontdrinkandroot.lastfm.api.CheckImplementationStatus.java

public static void main(final String[] args) throws DocumentException, IOException {

    CheckImplementationStatus.xmlReader = new Parser();
    CheckImplementationStatus.saxReader = new SAXReader(CheckImplementationStatus.xmlReader);

    final String packagePrefix = "net.dontdrinkandroot.lastfm.api.model.";

    final Map<String, Map<String, URL>> packages = CheckImplementationStatus.parseOverview();

    final StringBuffer html = new StringBuffer();
    html.append("<html>\n");
    html.append("<head>\n");
    html.append("<title>Implementation Status</title>\n");
    html.append("</head>\n");
    html.append("<body>\n");
    html.append("<h1>Implementation Status</h1>\n");

    final StringBuffer wiki = new StringBuffer();

    int numImplemented = 0;
    int numTested = 0;
    int numMethods = 0;

    final List<String> packageList = new ArrayList<String>(packages.keySet());
    Collections.sort(packageList);

    for (final String pkg : packageList) {

        System.out.println("Parsing " + pkg);
        html.append("<h2>" + pkg + "</h2>\n");
        wiki.append("\n===== " + pkg + " =====\n\n");

        Class<?> modelClass = null;
        final String className = packagePrefix + pkg;
        try {// w ww.  ja v a2  s. c  om
            modelClass = Class.forName(className);
            System.out.println("\tClass " + modelClass.getName() + " exists");
        } catch (final ClassNotFoundException e) {
            // e.printStackTrace();
            System.out.println("\t" + className + ": DOES NOT exist");
        }

        Class<?> testClass = null;
        final String testClassName = packagePrefix + pkg + "Test";
        try {
            testClass = Class.forName(testClassName);
            System.out.println("\tTestClass " + testClass.getName() + " exists");
        } catch (final ClassNotFoundException e) {
            // e.printStackTrace();
            System.out.println("\t" + testClassName + ": TestClass for DOES NOT exist");
        }

        final List<String> methods = new ArrayList<String>(packages.get(pkg).keySet());
        Collections.sort(methods);

        final Method[] classMethods = modelClass.getMethods();
        final Method[] testMethods = testClass.getMethods();

        html.append("<table>\n");
        html.append("<tr><th>Method</th><th>Implemented</th><th>Tested</th></tr>\n");
        wiki.append("^ Method  ^ Implemented  ^ Tested  ^\n");

        numMethods += methods.size();

        for (final String method : methods) {

            System.out.println("\t\t parsing " + method);

            html.append("<tr>\n");
            html.append("<td>" + method + "</td>\n");
            wiki.append("| " + method + "  ");

            boolean classMethodFound = false;
            for (final Method classMethod : classMethods) {
                if (classMethod.getName().equals(method)) {
                    classMethodFound = true;
                    break;
                }
            }

            if (classMethodFound) {
                System.out.println("\t\t\tMethod " + method + " found");
                html.append("<td style=\"background-color: green\">true</td>\n");
                wiki.append("| yes  ");
                numImplemented++;
            } else {
                System.out.println("\t\t\t" + method + " NOT found");
                html.append("<td style=\"background-color: red\">false</td>\n");
                wiki.append("| **no**  ");
            }

            boolean testMethodFound = false;
            final String testMethodName = "test" + StringUtils.capitalize(method);
            for (final Method testMethod : testMethods) {
                if (testMethod.getName().equals(testMethodName)) {
                    testMethodFound = true;
                    break;
                }
            }

            if (testMethodFound) {
                System.out.println("\t\t\tTestMethod " + method + " found");
                html.append("<td style=\"background-color: green\">true</td>\n");
                wiki.append("| yes  |\n");
                numTested++;
            } else {
                System.out.println("\t\t\t" + testMethodName + " NOT found");
                html.append("<td style=\"background-color: red\">false</td>\n");
                wiki.append("| **no**  |\n");
            }

            html.append("</tr>\n");

        }

        html.append("</table>\n");

        // for (String methodName : methods) {
        // URL url = pkg.getValue().get(methodName);
        // System.out.println("PARSING: " + pkg.getKey() + "." + methodName + ": " + url);
        // String html = loadIntoString(url);
        // String description = null;
        // Matcher descMatcher = descriptionPattern.matcher(html);
        // if (descMatcher.find()) {
        // description = descMatcher.group(1).trim();
        // }
        // boolean post = false;
        // Matcher postMatcher = postPattern.matcher(html);
        // if (postMatcher.find()) {
        // post = true;
        // }
        // Matcher paramsMatcher = paramsPattern.matcher(html);
        // List<String[]> params = new ArrayList<String[]>();
        // boolean authenticated = false;
        // if (paramsMatcher.find()) {
        // String paramsString = paramsMatcher.group(1);
        // Matcher paramMatcher = paramPattern.matcher(paramsString);
        // while (paramMatcher.find()) {
        // String[] param = new String[3];
        // param[0] = paramMatcher.group(1);
        // param[1] = paramMatcher.group(3);
        // param[2] = paramMatcher.group(5);
        // // System.out.println(paramMatcher.group(1) + "|" + paramMatcher.group(3) + "|" +
        // paramMatcher.group(5));
        // if (param[0].equals("")) {
        // /* DO NOTHING */
        // } else if (param[0].equals("api_key")) {
        // /* DO NOTHING */
        // } else if (param[0].equals("api_sig")) {
        // authenticated = true;
        // } else {
        // params.add(param);
        // }
        // }
        // }
        // }
        // count++;
        //
    }

    html.append("<hr />");
    html.append("<p>" + numImplemented + "/" + numMethods + " implemented (" + numImplemented * 100 / numMethods
            + "%)</p>");
    html.append("<p>" + numTested + "/" + numMethods + " tested (" + numTested * 100 / numMethods + "%)</p>");

    html.append("</body>\n");
    html.append("</html>\n");

    FileOutputStream out = new FileOutputStream(new File(FileUtils.getTempDirectory(), "apistatus.html"));
    IOUtils.write(html, out);
    IOUtils.closeQuietly(out);

    out = new FileOutputStream(new File(FileUtils.getTempDirectory(), "apistatus.wiki.txt"));
    IOUtils.write(wiki, out);
    IOUtils.closeQuietly(out);
}

From source file:net.openhft.chronicle.wire.benchmarks.ComparisonMain.java

public static void main(String... args) throws Exception {
    Affinity.setAffinity(2);// w  w w  . j ava  2 s  . c o m
    if (Jvm.isDebug()) {
        ComparisonMain main = new ComparisonMain();
        for (Method m : ComparisonMain.class.getMethods()) {
            main.s = null;
            main.sb.setLength(0);
            main.mhe.wrap(main.directBuffer, 0).blockLength(0);
            main.buf = null;

            if (m.getAnnotation(Benchmark.class) != null) {
                m.invoke(main);
                String s = main.s;
                if (s != null) {
                    System.out.println("Test " + m.getName() + " used " + s.length() + " chars.");
                    System.out.println(s);
                } else if (main.sb.length() > 0) {
                    System.out.println("Test " + m.getName() + " used " + main.sb.length() + " chars.");
                    System.out.println(main.sb);
                } else if (main.mhd.wrap(main.directBuffer, 0).blockLength() > 0) {
                    int len = main.mhd.wrap(main.directBuffer, 0).blockLength() + main.mhd.encodedLength();
                    System.out.println("Test " + m.getName() + " used " + len + " chars.");
                    System.out.println(new NativeBytesStore<>(main.directBuffer.addressOffset(), len)
                            .bytesForRead().toHexString());
                } else if (main.buf != null) {
                    System.out.println("Test " + m.getName() + " used " + main.buf.length + " chars.");
                    System.out.println(Bytes.wrapForRead(main.buf).toHexString());
                } else if (main.bytes.writePosition() > 0) {
                    main.bytes.readPosition(0);
                    System.out
                            .println("Test " + m.getName() + " used " + main.bytes.readRemaining() + " chars.");
                    System.out.println(main.bytes.toHexString());
                }
            }
        }
    } else {
        int time = Boolean.getBoolean("longTest") ? 30 : 2;
        System.out.println("measurementTime: " + time + " secs");
        Options opt = new OptionsBuilder().include(ComparisonMain.class.getSimpleName())
                //                    .warmupIterations(5)
                .measurementIterations(5).forks(10).mode(Mode.SampleTime)
                .measurementTime(TimeValue.seconds(time)).timeUnit(TimeUnit.NANOSECONDS).build();

        new Runner(opt).run();
    }
}

From source file:com.alkacon.opencms.registration.CmsRegistrationFormHandler.java

/**
 * As test case.<p>//from   ww w. jav  a  2  s .  c  o  m
 * 
 * @param args not used
 */
public static void main(String[] args) {

    CmsUser user = new CmsUser(null, "/mylongouname/m.moossen@alkacon.com", "", "", "", "", 0, 0, 0, null);
    String code = getActivationCode(user);
    System.out.println(code);
    System.out.println(getUserName(code));

    CmsMacroResolver macroResolver = CmsMacroResolver.newInstance();
    macroResolver.setKeepEmptyMacros(true);
    // create macros for getters 
    Method[] methods = CmsUser.class.getDeclaredMethods();
    for (int i = 0; i < methods.length; i++) {
        Method method = methods[i];
        if (method.getReturnType() != String.class) {
            continue;
        }
        if (method.getParameterTypes().length > 0) {
            continue;
        }
        if (!method.getName().startsWith("get") || (method.getName().length() < 4)
                || method.getName().equals("getPassword")) {
            continue;
        }
        String label = ("" + method.getName().charAt(3)).toLowerCase();
        if (method.getName().length() > 4) {
            label += method.getName().substring(4);
        }
        try {
            Object value = method.invoke(user, new Object[] {});
            if (value == null) {
                value = "";
            }
            macroResolver.addMacro(label, value.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    // add addinfo values as macros
    Iterator itFields = user.getAdditionalInfo().entrySet().iterator();
    while (itFields.hasNext()) {
        Map.Entry entry = (Map.Entry) itFields.next();
        if ((entry.getValue() instanceof String) && (entry.getKey() instanceof String)) {
            macroResolver.addMacro(entry.getKey().toString(), entry.getValue().toString());
        }
    }
    // add login
    macroResolver.addMacro(FIELD_LOGIN, user.getSimpleName());

}

From source file:Main.java

public static String getFieldName(Method handlerMethod) {
    return handlerMethod.getName().substring(3, handlerMethod.getName().length() - 7);
}

From source file:Main.java

static public Method isMethodImplemented(Object obj, String methodName) {
    Method[] methods = obj.getClass().getDeclaredMethods();
    for (Method m : methods) {
        if (m.getName().contains(methodName)) {
            return m;
        }/*  www .  j  ava2  s  .com*/
    }
    return null;
}

From source file:Main.java

private static String getMethodNameMinusGet(Method aMethod) {
    String result = aMethod.getName();
    if (result.startsWith(fGET)) {
        result = result.substring(fGET.length());
    }// ww w . ja  va 2 s  . c o  m
    return result;
}