Java, method name via reflection for logging - java

Working on a quick logging utility method. Normally I would use Aspects for this but working in the Android SDK where there isnt support for that.
How would I go about grabbing a method name in a generic way using reflection?
Also any performance hit in java with reflection ?

Something like this will probably work:
Thread.currentThread().getStackTrace()[0].getMethodName()

Related

Access a method in same project but different package

Is it possible to access a method in different package but same project without making the method public or inheritance? I'm looking for something similar to friendly assemblies in C#.
Is it possible to access a method in different package but same project without making the method public or inheritance?
Sortof, yes - you can use reflection to get the method that you're after, call setAccessible(true) on the method to suppress the access checks, then invoke it from wherever you like.
But...
I'm looking for something similar to friendly assemblies in C#.
This isn't it, a similar feature simply doesn't exist in the Java language. The above reflection trick should really be treated as nothing more than a hack, and certainly not something you'd want to use in production code unless there really was no other feasible option.

Is it possible to override files from android sdk?

For example i want customize BluetoothGatt class. Is it possible to create android.bluetooth package and put own version of this class?
BluetoothGatt uses android interface files shown in here. Can i access these files and use it in own version of BluetoothGatt?
Yes as long as you match the package and class name, when you reference it in your code, you will be able invoke your custom behaviour / modified contract
Android classes are simply Java. You can modify the support SDK, as it is simply a Java file. However, this is only applying to your app, or it would be possible for apps to change the entire SDK, which is defeating the point of sandboxing. If you want to extend the class, that is done just the same as in Java, as the class is just Java. However, this specific class is final, so you can't, at least not without hacks. However, the support library is usually a wrapper, so you may as well write your own. If the reason you want to do this is to access a private method, use reflection!
EDIT: To pedantically answer your question, you have already accessed said files, and copy-and-paste is always your friend!
Hope I helped!

What does a striked through method means in Eclipse?

I'm using Eclipse to develop a Java program. But in my program I have some methods in Eclipse which are striked through. What does it mean ?
It means that your method are deprecated. You may find another way to do the same thing
All those methods/class which eclipse found start with #Deprecated annotation is displayed with a strike through.
A method is made deprecated to discourage the user/client of the method not to use it. Because this method might be remover from the later release of the API/package. In this case the there may be an alternative method to use. A good java doc should contains what to use in alternate of the deprecated method.
It is a deprecated method. From the documentation:
A program element annotated #Deprecated is one that programmers are
discouraged from using, typically because it is dangerous, or because
a better alternative exists.

How to mock a static call on a class from a third-party library

I am wondering whether it is possible with PowerMock or any other library to mock out a static call in a class from a library. The class in that third-party library makes a call to one of the classes in the rt.jar and I want a return a custom implementation of that returned Type. I am trying to do this outside of test code.
Otherwise, I had to extend a few classes from that library and replace some implementation. Has someone done anything like this before?
Yes it is possible, e.g. using PowerMock.mockStatic() or PowerMock.mockStaticPartial() if you want to mock one method, only.
There is a quite good tutorial on the homepage of PowerMock.
I posted an example on SO.
[EDIT] IF you want to do such magic in production code, however, I would recommend NOT to use a Testing-Framework. Maybe Groovy can help you with that - it is possible with Groovy to manipulate classes (even JDK classes) at runtime, and it is (I think) a thinner layer on top of the JVM.
Generally speaking it's really bad idea to do such hardcore stubbing (it's not mocking what you describe). I'd go with building tiny abstraction over this library so that you can have full control on what you want to return. It is possible to use some features of PowerMock outside tests but this not piece of cake. Here you have rough description that can help if you really want: https://groups.google.com/d/msg/powermock/SMDMe-y6fLg/1HF0TsGOqTIJ
Anyway I would not recommend doing that.

passing precompile directives to javac?

I am writing a desktop and j2me application. I have debug statements in program. Currently to avoid those getting compiled i use as following. We are doing this to reduce size of jar. (Specifically for mobile)
ConstantFile.java
Boolean ConstantFile.DebugEnabled = false;
if(ConstantFile.DebugEnabled) {
log.debug("msg");
}
But this is sort of hard coding. Is there an alternative like C where we have pre-compiled directives. Can Annotation help here ? Or something else i should look for ?
No, there is no precompile in Java, but instead of using the constants file like you are currently using, try Log4j or some other logging package that supports an isDebugEnabled() operation. This will avoid the hardcoding in favor of a config file or VM argument.
Annotations could work, or you could look at AOP. AOP would allow you to automatically insert code into your methods at specific points. Either way should get you what you want.
I got exact answer i wanted. Preprocessor. Similar to hatchetman82 suggested, There is wtkpreprocess task available. And its specifically developed for mobile. (Though it can be used for other java part also.) And it works perfectly. It comments out line so byte code remains lighter.

Categories

Resources