I've been using this instruction to get the name of a method which is currently being used.
currentThread().getStackTrace()[1].getMethodName()
It works fine when I developed Spring Framework or other simple programmings.
However, It seems it works differently in Android Studio. I get time instead of the name of a method.
The reason why I try to use this instruction is for debugging. I used to use
Log.d(TAG, "Method()");
However, The problem when I use this... I should type its method name each. So, To implement this instruction in all the different methods with the same instruction. I found out using currentTrace(). But when I use this on Android Studio. I just get 'geStackTrace' instead of the current method name.
Is there any way to implement it correctly or better way for debugging?
Try this
StackTraceElement[] stacktraceObj = Thread.currentThread().getStackTrace();
stacktraceObj[1].getMethodName();
For further reference, checkout the links
https://www.badprog.com/android-api-getting-the-current-method-name-with-stacktraceelement
https://developer.android.com/reference/java/lang/Throwable.html#getStackTrace()
If typing it out every-time is the issue use the default live templates for log statements.
Use cmd+j(ctrl + j in windows) and look for logd, logi, logw or loge.
By default it prints the method and a colon. You can customize it in preferences.
There's logt for creating a TAG too.
Related
First of all I must say I'm new to both Android dev and Java.
I'm trying to find a list of the tags that are used for logging in Android studio.
The examples I've been researching include using:
Log.i(tag:"Info","message");
Log.i(tag:"Values","another message");
Log.i(tag:"Seekbar changed", "and another message");
I tried for the past couple of hours to find a document online, that has a table to describe the reserved tags for View objects, any help will be appreciated.
There is no fixed list of "reserved tags" one can use for logging in Android. You decide for yourself which tags you want to use and what additional information about the state of your objects or primitive types you want to display.
The Log class has six different log levels (debug, error, info, verbose, warn and wtf [What a Terrible Failure]) and corresponding (static) methods (Log.d, Log.e, Log.i, Log.v, Log.w and Log.wtf) each of which you call with two string parameters, one string parameter and one Throwable or two string parameters and one Throwable.
The most commonly used is probably the variant with two string parameters, one parameter for a tag (chosen by you) and one parameter for a message (also chosen by you). See this post for information about which level to choose.
During debugging I often use commands like this one:
Log.e(String.valueOf(myIntVariable), String.valueOf(myOtherVariable));
Let me explain the reason for using the Log class like this. I use the error level because it will give you red entries in the LogCat output (inside an IDE, e.g. Android Studio), and the same IDE will also let you filter out all logs below the error level. However, this is for debugging only; make sure to get rid of those log commands before your app enters production.
Instead of using logs in the way I do, you can also use breakpoints in the debug mode. I guess it is mainly a question of taste if you prefer one or the other. Toasts would be third option (with more boilerplate though).
If you use logs a lot in your code, it makes sense to use real tags. Either you define a string called TAG (or something else) in your class, or you put the name of the containing method as the first parameter. This will give you a sense of the order by which your methods are being called. You can also use other tags as well, and it doesn't have to follow a specific convention either (though you should have a system for it to make sense of it).
I'm trying to write a Java plugin that does a custom refactoring that involves inserting new assignment statements, and I'm not sure how to create a PsiAssignmentExpression.
I have a PsiElementFactory, but while I see PsiElementFactory#createIdentifier and PsiElementFactory#createVariableDeclarationStatement, I don't see how to do an assignment.
I tried looking for the extract variable refactoring in the base source code to try and find an example but wasn't able to find it yet.
P.S. I looked at IntelliJ IDEA plugin development: how to modify the Psi tree?, which recommended creating PsiElements by creating a PsiFile and then extracting the element from it back in, but I am wondering if that's specific to creating a custom language that doesn't have the Java api.
Use PsiElementFactory#createExpressionFromText and pass in the text of the assignment you want to create. For example "s = \"Hello World\"".
I'm working on an Eclipse plugin that enables traceability. I am implementing a notification system that tells the user whenever a traced item changes (is removed, renamed or edited) and for that purpose I implemented an IResourceChangeListener, but that doesn't give me all the support that I want for Java elements.
For example, when I rename a Java method inside a .java file, it only tells me which file has been edited, but I would like to have the info about the method as well. I know that this can be achieved with implementing the IElementChangedListener, but is there any way around it? Do I really have to implement two listeners (ResourceListener for other files and ElementChangedListener just for java elements) or can I somehow get the IJavaElementDelta (normally obtained from the ElementChangedListener) from the IResourceDelta? Thanks!
These two deltas are completely unrelated. You need to use both listeners.
Try to check this link example 5. There is some method with this description:
Converts an IResourceDelta and its children into
the corresponding IJavaElementDeltas.
Return whether the delta corresponds to a resource on the classpath.
If it is not a resource on the classpath, it will be added as a non-java
resource by the sender of this method.
So I suppose it could be possible.
The links leads here which should you check ass well. Method public void processJavaDelta(IJavaElementDelta delta)
I'm using android to do some testing, and would like to give the screen shots a name other than the time they were taken. Looking at the code, there is a method that can be used to do this by passing a String name into the method, but when I try to do this, I get an error. Does anyone know why this might be?
This is the method I use:
solo.takeScreenshot("FileName");
or:
String fileName = "FileName";
solo.takeScreenshot(filename);
Neither work. Any ideas why?
Thanks!
From looking at the javadocs here: http://code.google.com/p/robotium/downloads/detail?name=robotium-solo-3.3-javadoc.jar
I do not see any takeScreenshot() methods that take arguments. So, without knowing more about what version you are using and what error you are running into, I am not sure I can help.
However, if you want to extend the Solo class to add the method you want, try looking at this question: Android, Robotium - Issue taking screenshot
I want to hook the method System.out.print in Java and have the ability to read/change the variables used in the method before the part of the method is called that actually adds the string to whatever the output stream is.
In C++ I would just detour the function, or set an int3 instruction so I could access the registers but in java I have no idea how to accomplish something similar.
You can rewrite the byte code of the methods, and in the process capture/change the local variables. It is not trivial. See some notes here.
Maybe what you really want is a java debugger? You can connect a debugger to a remote process, add a breakpoint, and capture/change the local variables pretty easily using eclipse.
What is the real problem you are trying to solve?
Have a look at this link.
He sneakily defines a static anonymous class so that System.out points to something different, and therefore print and println will route through that object.
You can reassign System.out (and System.err) to another object which does what you want to do with it. Said object usually gets the old System.out value so that output can be made in the end.
This is usually done in main() and influences the whole JVM.
We use this to have automatic wrapping at 130 columns in a very peculiar setting where longer lines are truncated.
Since JDK 1.1, the System.setOut and System.setErr methods are added to enable applications to hook the streams.
Link : http://docs.oracle.com/javase/7/docs/api/java/lang/System.html#setOut(java.io.PrintStream)
http://docs.oracle.com/javase/7/docs/api/java/lang/System.html#setErr(java.io.PrintStream)
#Nowayz Some time before i too had the same problem with me.
After some research i came to know About AOP. AOP i.e. AspectJ provides a facility to intercept the java APIs by applying the pointcuts before,after, around. So have a look at it .You can refer my question on stack .it may help you.