5

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;
    }
}
dskinner
  • 10,527
  • 3
  • 34
  • 43
  • 3
    That would have to be before compilation, comments are not retained in class files. – Mat Dec 27 '11 at 16:12
  • 1
    I don't think there is such a tool and I don't believe it would be such a good idea. Logs are meant to trace what the prorgam does at debugging stage (and usually those logs should be removed). Logs can also be used for some high level logging such as error reporting or help developpers use a tool. Comments are meant to explain difficult part of code. They don't have the same intent. * – Snicolas Dec 27 '11 at 16:16
  • 1
    Oh, and in android, it is more than recommended to remove every call to Logging before releasing a product. Usually through obfuscation – Snicolas Dec 27 '11 at 16:17
  • yes I'm well aware of recommendations, and regardless of if you feel it's a good idea or not isn't pertinent to answering the question. There are use-cases during development that such a thing could prove useful, such as tracking down temporary ANRs during android development in well commented code to provide a quick hint for where to start investigating. The kind of ANRs that like to cause adb to disconnect. – dskinner Dec 27 '11 at 16:28

3 Answers3

1

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.

chubbsondubs
  • 37,646
  • 24
  • 106
  • 138
  • this is interesting, have you ever done this before and if so, do you have any recommended reading on the subject in general? – dskinner Dec 27 '11 at 16:36
  • It's seem complex indeed. And not every comment can be transform to a log call in a straightforward manner. – user802421 Dec 27 '11 at 17:02
  • are you looking at something in particular? I did some brief searching a bit ago and came across Polyglot for java http://www.cs.cornell.edu/projects/polyglot/ but will have to research a bit more later on. – dskinner Dec 27 '11 at 17:41
  • No I've never done anything related to parsing Java per se, but I have created my own languages parsed with tools like ANTLR. The thing you'd need to do is extend Java's compiler so your extension could take the annotation and generate the code and insert that into the AST before passing it to the compiler to render to byte code. Using something like polygot seems like a better choice because its meant for that very purpose. – chubbsondubs Dec 27 '11 at 21:26
  • The general topic here is context free grammars (CFGs) which define all programming languages like Java, Javascript, Ruby, etc. ANTLR is a tool meant for designing new languages by defining a Backus–Naur Form (BNF) grammar. There are grammars already defined you can use to generate code that parses Java code. While you can parse the Java code into an abstract syntax tree (AST), you'd still need to translate the AST into byte code (ie class file) which is what the compiler does. ANTLR is a neat tool for parsing languages, but interpreting that language is still up to you. Look at polygot. – chubbsondubs Dec 27 '11 at 21:29
  • Thanks, this was very informative. I have a mini language of sorts im parsing by hand in cython and recently started work on eclipse integration with outlining, validation, and all that entails. This looks like a worthwhile experience for my interests and may bleed over to my other side projects. – dskinner Dec 28 '11 at 06:29
  • Ah well if you're writing eclipse integration for other languages ANTLR is definitely something you should know about. This can be used for validation, code completion, structure, etc. You might be interested in this question as well. http://stackoverflow.com/questions/1723275/tolerating-malformed-statements-with-antlr-e-g-for-code-completion – chubbsondubs Dec 28 '11 at 18:16
0

No this is not possible because comments are removed at runtime. The best I can think of is reading the javadoc at run time and logging those. But that doesn't include non-javadoc comments.

Amir Raminfar
  • 33,777
  • 7
  • 93
  • 123
0

What are you trying to achieve?

If you want to enable Logging on per-class basis, you can use some kind of a flag (static final int will do) that will disable or enable logging. Take a look at this thread: How do I enable/disable log levels in Android? for a few more tips on logging in Android.

Community
  • 1
  • 1
user1234567
  • 1,832
  • 1
  • 18
  • 25
  • I already have a static method for handling logs tied to my android application instance and based on ant build properties. The achievement is knowing how to accomplish such items either before or during java runtime. The issue that raised the question can be seen in a comment on main question, but a solution for that one issue is irrelevant to the question. – dskinner Dec 27 '11 at 16:32