emukiss
emukiss

Reputation: 1

How to copy methods signatures from one class to a file?

I have a Java class that contains all my methods that are reusable in other parts of a framework. There are lots of them. I cannot do much about it because of company policy.

I would like to copy all lines from that class that starts with "public" to a file or another class. Just to have methods signatures for easier search for specific functionality. These methods are being added, removed overtime so maybe a method to copy all of them which can be manually started when I need it? Maybe regular expressions can help but I don't know how to deal with it.

Upvotes: 0

Views: 336

Answers (2)

Federico klez Culloca
Federico klez Culloca

Reputation: 27119

You can use javadoc to generate the documentation for you, provided that you add javadoc comments to your method.

So for example, suppose your class has a User getUser(int id) method. Also suppose your class is in package com.example.myproject. Just before the method definition you should put something like this

/**
 * Returns a user given an id
 *
 * @param  id   the id the User has on database
 * @return      An instance of <code>User</code> corresponding to the given id
 */
 public User getUser(int userId) {

Notice that the comment starts with /** and not with /*.

Once you've done that you can invoke javadoc to generate the documentation html for your package.

javadoc com.example.yourproject

Here is the manual for javadoc with further instructions if you need to do anything fancier. And here are some suggestions on how to best write your documentation (dated, but still good).

Upvotes: 1

By reflection you can do something like :

import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.Arrays;
import java.util.stream.Collectors;

public class ReflectionCode {
    public static String reflect(Class<?> classToRead) {
        StringBuilder sb = new StringBuilder();
        for (Method clas : classToRead.getMethods()) {
            System.out.println(clas.getReturnType().getSimpleName() + " " +                                    
                clas.getName() + "(" + String.join(",", //
                    Arrays.stream(clas.getParameters())//
                        .map(ReflectionCode::parameterToString)//
                        .collect(Collectors.toList()))
                + ")");
        }
        return sb.toString();
    }

    public static String parameterToString(Parameter p) {
        return p.getType().getSimpleName() + " " + p.getName();
    }

    public static void main(String[] args) {
        System.out.println(reflect(ReflectionCode.class));
    }
}

Upvotes: 0

Related Questions