0

I have a question about this topic. I want to get the names of methods and parameters. I used the Reflection API. If the class which I need to get names of the methods for is a Java Class, it works, but I have a C file. I couldn't do that. How do I reach this C file and get names of the methods and parameters?

package odev1;
import java.io.*;
import java.lang.reflect.Method;

public class Odev1 {    
    public static void main(String[] args) throws FileNotFoundException, ClassNotFoundException, IOException {
        try {
            Class cls = Odev1.class;
            for (Method m : cls.getDeclaredMethods()) {
                m.getName();
                m.getReturnType();
                m.getParameterCount();
                System.out.println(" Name of Method : " + m.getName() + " \n"
                    + " Return Type of Method : " + m.getReturnType() + " \n"
                    + " Count of Parametres : " + m.getParameterCount());
            } 
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

This is the C file that will be read for parameters and methods:

#include "stdio.h" 
#include "stdlib.h" 

void DiziYazdir(int *p,int uzunluk)
{   
    int i=0;  
    for(;i<uzunluk;i++) 
    printf("%d ",p[i]); 
} 

int DiziTopla(int *p,int uzunluk)
{ 
    int i=0;  int toplam=0;  
    for(;i<uzunluk;i++) 
    toplam += p[i];  
    return toplam; 
}

int main()
{ 
    int x,y;  
    printf("x:");  
    scanf("%d",&x);  
    printf("y:");  
    scanf("%d",&y);  
    int sonuc = x + y;  
    printf("Sonuc:%d\n\n",sonuc);  
    int *dizi = malloc(3*sizeof(int));  
    dizi[0]=x;  
    dizi[1]=y;  
    dizi[2]=sonuc;  
    DiziYazdir(dizi,3);  
    printf("\n\nToplam Deger:%d",DiziTopla(dizi,3));  
    free(dizi);  
    return 0; 
}

How can I get the names of methods and parameters in the C file?

Lauren Rutledge
  • 1,195
  • 5
  • 18
  • 27
  • 2
    You'll need to write (or find) a parser to extract function names from the C source. The language itself has no builtin support for reflection. – user3386109 Feb 26 '19 at 22:10

2 Answers2

0

As already mentioned you need a C parser for Java - have a look at this answer: Eclipse CDT project

Markus Fried
  • 96
  • 1
  • 10
0

You have several approaches here:

  • As it has been already answered in the comments to the question by @user3386109, you can scan the source file and extract the names of the functions included. This requires you to preprocess the file (with cpp(1)) because the actual function names can be hidden in macro constructions, and then extract the function definition names (this will require a parser or not, depending on the restrictions you impose to the source file you want to scan). Anyway, this will be a complex task, the more the less restrictions you assume to the format in the source file.
  • You can dlopen(3) the object file (the program or the shared library involved) and scan for function identifiers. This will be tedious also, as it forces you to a binary format (mainly ELF) to be used as the target architecture of your program. It depends on executable format and will not always be possible.
  • A simpler approach is to tag those identifiers in the source (like annotations in java) with a TAG macro that expands to nothing, and mark the places where a function identifier is going to happen. For example:

    #define TAG
    void TAG myFunc(int a, int b, int c)
    

    Then you can search the file for TAG and extract the identifier that comes next.

Java language requires the identifiers to be included in the executable in string format, so you can apply introspection to get that info, but this is not a requirement in C/C++. And all the classes hierarchy structure is also included in the executable as a static data structure, so you are able to navigate and discover all the java types hierarchy. This is not possible in C, as the primary intent of C programming is efficiency. Having all the type information included in a program forces a lot of memory to be used for describing the data definitions, and so, it is not included in C/C++.

Anyway, you can compile a list yourself with the name identifiers you physically see in the files, so probably you don't even need to automatically process your files at build time do that.

Another simple approach to automatically do what you ask for is to define a macro:

#define FUNCTION_NAME(typ, name) char name##_string[] = #name; typ name

and use it as

FUNCTION_NAME(void, myFunc)(int par1, char *par2, int par3)
{
    ...
}

that expands to

char myFunc_string[] = "myFunc";
void myFunc(int par1, char *par2, int par3)
{
    ...
}

so for each function x, you will have a static string x_string with the name as a string and the function header free of declaring whatever you want (even a variadic func)

By the way, there's an implicit macro defined inside the body of a function that expands to a string containing the function name __func__. But this has the problem of being usable only inside a function body.

Luis Colorado
  • 10,974
  • 1
  • 16
  • 31