Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace java comment with logging during or before runtime

I'm curious if it's possible to use an annotation on a class or method that, during or before runtime, replaces comments with logging of the comment string. For example, if on android:

@LogComments
class MyActivity extends Activity {
    @Override public void onCreate(Bundle b) {
        super.onCreate(b);
        // set some local vars
        int a = 1;
        int b = 2;
    }
}

would translate to something like

class MyActivity extends Activity {
    @Override public void onCreate(Bundle b) {
        super.onCreate(b);
        Log.d("TAG", "set some local vars");
        int a = 1;
        int b = 2;
    }
}
like image 988
dskinner Avatar asked Nov 13 '22 12:11

dskinner


1 Answers

Out of the box. No there is nothing that exists to allow this.

Now if you wanted to build this yourself you might be able to create some sort of compiler extension that would allow you to parse the file and translate the comments into log expressions depending on the annotation being present. But this would have to occur as a step of the compile phase because comments aren't apart of the class file so there would be no way for it to occur at runtime.

But, you're talking about a pretty large side project to create something like this. Plus there are limitations to this approach like writing out variable values at runtime wouldn't be possible without adding some sort of expression language to the comments. In the end you'll be creating something complex vs something that already exists, is robust, and used millions of times over by using the API.

like image 110
chubbsondubs Avatar answered Nov 16 '22 02:11

chubbsondubs