5

I have to complete a project in C# to find number of methods per java class.

I could find all methods in the .java file using c# regular expression, but what I want is to find the number of methods per each and every class, including inner classes. Can any one help me.

string[] lines = File.ReadAllLines(file);

int countLine = 0;
int AllCount = 0;

foreach (string line in lines)
{

    countLine = MethodsCount(line);
    AllCount = AllCount + countLine;

}

label5.Text = AllCount.ToString();

Here's the method-counting method.

private int MethodsCount (string LineOperator)
{
    int count = 0;

    string[] words = LineOperator.Split('{');

    foreach (string word in words)
    {
        if (Regex.IsMatch(word, @"(static\s|final\s)?(public|private|internal)(\sstatic|\sfinal)?\s(int|boolean|void|double|String|long|String\[\]|String\[\]\[\])?\s([a-z]|[A-Z]+[a-z]+|[a-z]+[A-Z]+)+(\s)*\((\s|\n|\r)*"))
        {
            count = count + 1;
        }
    }

    return count;
}

if we consider a class

public class vehicle {

    public method1() {
       ---------
    }

    public method2() {
       ------
    }

    public class car extends vehicle {
        public method3() {
            -------
        }
    }
}

I want to get the output there are this number of methods in vehicle class,this number of methods in car class like wise.

Chris Taylor
  • 52,623
  • 10
  • 78
  • 89
Randi
  • 639
  • 2
  • 6
  • 23
  • including inner class also I want to find number of methods – Randi Sep 03 '11 at 18:53
  • Wow, I certainly would not want to try to do that through a single regex, or even a bunch of regexes. Just one more thing that could go wrong: comments, I say. Never mind functions that return a custom generic type of a custom type. Or any of a number of other perfectly reasonable situations. I think you are opening a whole barrel of worms, here. – user Sep 03 '11 at 20:09
  • to add information or clarify your question, please edit the question (see the "edit" link just below the text) rather than adding a comment or answer. – user Sep 03 '11 at 20:13
  • If you can do it in Java, you could perhaps reflect upon the compiled bytecode and count the functions that are present (I don't know if Java reflection is "as strong" as C# reflection. I know you can do it in C# against .NET assemblies)... Or you could use http://download.oracle.com/javase/1,5.0/docs/tooldocs/windows/javap.html to disassemble the code and get the function list :-) The comments will be stripped and the format will be uniform. – xanatos Sep 04 '11 at 09:03
  • I'll note that this question is a semi-duplicate of http://stackoverflow.com/questions/4829243/get-the-available-functions-methods-of-java-package-in-c – xanatos Sep 04 '11 at 09:09

1 Answers1

4

Parsing a Java source file with just regex is flaky at best. There are so many different ways to format or annotate the code that you'll easily miss valid method declarations. The regex in your code sample does not handle line breaks, generic methods, the throws clause, arbitrary return types, the synchronized modifier or annotated arguments, two method declarations on the same line...

You'd have to drop regex and build (or reuse) a full parser to be sure you get everything. Fortunately, if you have access to a JDK you can take a shortcut.

First, compile the Java source code. This will give you an output directory full of .class files (inner classes get their own file). Scan the output directory to collect all the class names you need to analyze.

Then, for each class, run the javap command, which will give you this kind of output:

barend@TURMINDER-XUSS /tmp$ javap java.util.Iterator
Compiled from "Iterator.java"
public interface java.util.Iterator{
    public abstract boolean hasNext();
    public abstract java.lang.Object next();
    public abstract void remove();
}

This is much easier to parse than a full Java source file. You can just count all lines containing \s[a-zA-Z][a-zA-Z0-9_]*\( and you have your method count. Use the 'compiled from' information to get to the method count per source file.

(edit) NOTE: javap doesn't print private methods by default. pass the -private argument to include these in the output.

Barend
  • 17,296
  • 2
  • 61
  • 80