Access a file and extract only annotated methods thereby processing them further

时间:2017-07-12 08:06:28

标签: java file methods annotations

I have the following situation:

File1.java, File2.java .... Filen.java: I have different java files that has say different methods method1, method2 ... methodn. Now some of the methods within those files are annotated with annotation @annotated_method.

Now I want to create a java file which access all the files in a particular directory for File1.java, File2.java .... Filen.java and I need to profile(or do some processing) only to the methods that has been annotated with @annotated_method annotation. Profiling(or processing of that method) must be done separately for each and every annotated method.

eg.,

  File1.java:


    @annotated_method
    method 1{
    ...
    }

    method 2{
    ...
    }

    .
    .
    .
    @annotated_method
    method 13{
    ...
    }

    .
    .
    .

    method n
    {
    ...
    }




    File2.java:


    @annotated_method
    method x1{
    ...
    }

    method x2{
    ...
    }

    .
    .
    .
    @annotated_method
    method x13{
    ...
    }

    .
    .
    .

    @annotated_method
    method xn
    {
    ...
    }

As given above I need to profile(or process) only the methods 1,13,x1,x13,xn.

It will be great if I get to know how to get the annotated part of a code to be processed further according to various requirements.

I have gone through the usage of getAnnotations(); isAnnotationpresent();

1 个答案:

答案 0 :(得分:0)

这可能会对您有所帮助:

orginal_df[['Code','DateTime','Year','Month','Day','Hour','Reading']]

示例输出:

import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


public class TimeMeasurer {
    public static void gettimes(Class c) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException
    {
        List<Method> methods=Arrays.asList(c.getMethods());
        for(Method m:methods)
        {
            List<Annotation> ans= Arrays.asList(m.getAnnotations());
            List<Class> classes = new ArrayList<>();
            for(Annotation a:ans)
            {
                classes.add(a.getClass());
            }
            //if(classes.contains(Deprecated.class))

            {
                if (m.getParameterCount()!=0)
                {
                    System.err.println("The method "+m.getName()+" could not be executed as it has parameters. Use a method with example values instead!");
                }
                else
                {
                    System.out.println("Executing Method: "+m.getName());
                    long stamp = System.nanoTime();
                    Object result=m.invoke(c);
                    long timeconsumed=System.nanoTime()-stamp;
                    System.out.println("Result of Method: "+result);

                    System.out.println("Time needed: "+timeconsumed);
                }
            }
        }
    }
    public static void main(String[] args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
        gettimes(EndloseFormel.class);
    }
}