view src/PrintPublicMethods.java @ 0:29318fe8d6d0

2012-01-04 Pavel Tisnovsky <ptisnovs@redhat.com> * adding .classpath: * adding .project: * adding .settings/org.eclipse.jdt.core.prefs: * adding AUTHORS: * adding ChangeLog: * adding LICENSE: * adding Makefile: * adding NEWS: * adding README: * adding TODO: * adding class_list.txt: * adding path_to_rt_jar.txt: * adding src/PrintClassList.java: * adding src/PrintPublicMethods.java: * adding src/PrintTestCoverage.java: * adding src/ReportGenerator.java: * adding src/index.html: * adding src/style.css: * adding test_directory.txt: Initial push to this project.
author Pavel Tisnovsky <ptisnovs@redhat.com>
date Wed, 04 Jan 2012 11:20:55 +0100
parents
children 61f453c6b172
line wrap: on
line source

/*
  Test coverage tool.

   Copyright (C) 2012 Red Hat

This tool is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

This tool is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with this tool; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.

Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version.
*/

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Set;
import java.util.TreeSet;

/**
 * This class generates a list containing all public methods and its argument
 * types and return type for all classes specified in a file "class_list.txt"
 * or in a file specified by a command line parameter.
 * 
 * @author Pavel Tisnovsky
 */
public class PrintPublicMethods
{
    /**
     * Get a Class instance for a given class name or null if something goes
     * wrong.
     * 
     * @param className
     *            name of a class (including package name)
     * @return Class instance
     */
    @SuppressWarnings("unchecked")
    private static Class getClass(String className) {
        Class clazz = null;
        try {
            clazz = Class.forName(className);
            if (!clazz.isInterface() && Modifier.isPublic(clazz.getModifiers())) {
                return clazz;
            }
        }
        catch (ClassNotFoundException e) {
            return null;
        }
        catch (UnsatisfiedLinkError e) {
            return null;
        }
        catch (ExceptionInInitializerError e) {
            return null;
        }
        catch (NoClassDefFoundError e) {
            return null;
        }
        return null;
    }

    /**
     * Remove "public", "static", "final", "synchronized" and "native" prefixes
     * from full method name;
     * 
     * @param methodName
     *            method name with all prefixes
     * @return method name without prefixes
     */
    private static String acquireMethodName(String methodName) {
        final String[] prefixes = new String[] {"public", "final", "native", "synchronized", "static"};
        String methodNameString = methodName;
        for (String prefix : prefixes) {
            methodNameString = removePrefix(methodNameString, prefix);
        }
        return removeThrowsFromDeclaration(methodNameString);
    }

    /**
     * Remove one given prefix from method name.
     * 
     * @param methodName
     *            method name that could contains prefix.
     * @param prefix
     *            prefix to be removed.
     * @return method name without prefixes
     */
    private static String removePrefix(String methodName, String prefix) {
        String prefixStr = prefix + " ";
        if (methodName.startsWith(prefixStr)) {
            return methodName.substring(prefixStr.length());
        }
        return methodName;
    }

    /**
     * Removes throws declaration from method name, i.e.:
     * 
     * <pre>
     * void java.lang.Object.wait() throws java.lang.InterruptedException
     * </pre>
     * 
     * is changed to:
     * 
     * <pre>
     * void java.lang.Object.wait() throws java.lang.InterruptedException
     * </pre>
     * 
     * @param methodName
     *            method name which could contain throws declaration
     * @return method name without throws declaration
     */
    private static String removeThrowsFromDeclaration(String methodName) {
        int throwDeclarationIndex = methodName.indexOf(" throws ");
        if (throwDeclarationIndex > 1) {
            return methodName.substring(0, throwDeclarationIndex);
        }
        return methodName;
    }

    /**
     * Print all public method from given class name (if such class exists).
     * 
     * @param className
     *            name of a class (including package name)
     */
    @SuppressWarnings("unchecked")
    private static Set<String> getAllPublicMethodsForClass(String className) {
        Set<String> out = new TreeSet<String>();
        Class clazz = getClass(className);
        if (clazz == null) {
            return out;
        }
        Method[] methods = clazz.getDeclaredMethods();
        for (Method method : methods) {
            if (Modifier.isPublic(method.getModifiers())) {
                String methodName = acquireMethodName(method.toString());
                out.add(methodName);
            }
        }
        return out;
    }

    @SuppressWarnings("unchecked")
    private static Set<String> getAllConstructors(String className) {
        Set<String> out = new TreeSet<String>();
        Class clazz = getClass(className);
        if (clazz == null) {
            return out;
        }
        Constructor[] constructors = clazz.getConstructors();
        for (Constructor constructor : constructors) {
            if (Modifier.isPublic(constructor.getModifiers())) {
                String methodName = acquireMethodName(constructor.toString());
                out.add("<init> " + methodName);
            }
        }
        return out;
    }

    private static void printAllPublicMethodsAndConstructors(String className)
    {
        for (String methodSignature : getAllConstructors(className))
        {
            System.out.println(methodSignature);
        }
        for (String methodSignature : getAllPublicMethodsForClass(className))
        {
            System.out.println(methodSignature);
        }
    }

    /**
     * Entry point to this tool.
     * 
     * @param args
     *            first argument could contains path to file containing class
     *            list. 
     */
    public static void main(String[] args) {
        if (args.length == 1)
        {
            printAllPublicMethodsAndConstructors(args[0].trim());
        }
        else
        {
            System.err.println("Usage java PrintPublicMethods package.className");
        }
    }

}