开发者

How to find the number of declared functions in a Java program

开发者 https://www.devze.com 2023-02-18 20:16 出处:网络
I am doing project in Core Java which identifies the similarity between two files, in that one part is to identify the declared functions length. I have tried the following code to find the declared m

I am doing project in Core Java which identifies the similarity between two files, in that one part is to identify the declared functions length. I have tried the following code to find the declared methods in a given class.

import java.lang.reflect.*;
import java.io.*;
import java.lang.String.*;
public class Method1 {
    private int f1(
    Object p, int x) throws NullPointerException
    {
        if (p == null)
        throw new NullPointerException();
        return x;
    }

    public static void main(String args[])throws Exception
    {
        try {
            Class cls = Class.forName("Anu");
            int a;
            Method methlist[]= cls.getDeclaredMethods();
            for (int i = 0; i < me开发者_开发问答thlist.length;i++) {
                Method m = methlist[i];
                System.out.println(methlist[i]);
                System.out.println("name = " + (m.getName()).length());

            }
        }
        catch (Throwable e) {
            System.err.println(e);
        }
    }
} 

But i have to find all the classes for a program. Shall i give input as a program since have to identify the declared methods in each class. Secondary it is working only if the given class is compiled, ie class file exist for given class. Can any one help me for identifying the declared methods in the given program.

And i have to identify the comment lines in the program, please help me for that too.


You need to write you program to read the original code as you cannot only find the comments there. You can parse the text yourself to find comments and method signatures.

You might be able to google for libraries wich help you do this.


Using JavaCompiler class, reading file as string and execute it as below:

public class SampleTestCase {

public static void main(String[] args) {
    String str = "public class sample {public static void doSomething() {System.out.println(\"Im here\");}}";
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();

    SimpleJavaFileObject obj = new SourceString("sample", str);

    Iterable<? extends JavaFileObject> compilationUnits = Arrays
            .asList(obj);
    CompilationTask task = compiler.getTask(null, null, diagnostics, null,
            null, compilationUnits);

    boolean success = task.call();
    if (success) {
        try {
            Method[] declaredMethods = Class.forName("sample")
                    .getDeclaredMethods();

            for (Method method : declaredMethods) {
                System.out.println(method.getName());
            }
        } catch (ClassNotFoundException e) {
            System.err.println("Class not found: " + e);
        } 
    }
}
}

class SourceString extends SimpleJavaFileObject {
final String code;

SourceString(String name, String code) {
    super(URI.create("string:///" + name.replace('.', '/')
            + Kind.SOURCE.extension), Kind.SOURCE);
    this.code = code;
}

@Override
public CharSequence getCharContent(boolean ignoreEncodingErrors) {
    return code;
}

}

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号