Get class, method and Line of Code in Android Apps

 







 

This short article is to share a method to get a string containing the class name (possibly you can include the package), the method and the line of code in which we are when the method was invoked.

This string, which you can format also as you want, it is very useful to be used in Log messages , but it would be used also in others ways!

To create this string is convenient to use a static method in a final class that then can be imported into our projects and so it could be reused. Let's define and analyze this class!

 

Class DebugInfo

The class DebugInfo was declared final so that it can not be extended and has a constructor that simply raises an AssertionError that returns a string to indicate that this class is not done to have instances, but only to contain static utility methods.

Anyway, here's the code:

 

 

package com.example.android.apps.nomeApp.nomeSottoPackageUtility;
/**
 * This is a final class that we use to manage the constants of our application
 * 
 * @author Luca Bortolotti
 * @version 1.0 - Agosto 2014
 *
 */
public final class DebugInfo {
	private DebugInfo() {
		throw new AssertionError(
				"Never instantiate me! I'm only a Debug Info class!");
	}
	/**
	 * This static method return Class, Method and Line information
	 * 
	 * @param stack
	 *            as Throwable
	 * @return infoString as String formatted in this way:
	 *         "[Class].[Method].[line]: "
	 */
	public static String getClassMethodLine(Throwable stack) {
		StackTraceElement[] trace = stack.getStackTrace();
		// Get the class name with all the package
		String className = trace[0].getClassName();
		// Split and save only the class name
		className = className.substring(className.lastIndexOf('.') + 1);
		//return the infoString
		return className + "." + trace[0].getMethodName() + "."
				+ trace[0].getLineNumber() + ": ";
	}
	/**
	 * This static method return Class, Method and Line information
	 * 
	 * @param stack
	 *            as Throwable
	 * @return infoString as String formatted in this way: "[Class].[Method]: "
	 */
	public static String getClassMethod(Throwable stack) {
		StackTraceElement[] trace = stack.getStackTrace();
		// Get the class name with all the package
		String className = trace[0].getClassName();
		// Split and save only the class name
		className = className.substring(className.lastIndexOf('.') + 1);
		// return the infoString
		return className + "." + trace[0].getMethodName() + ": ";
	}
}

 

 

 

As you can see, the methods using the Throwable stack object to obtain a StackTraceElement array called trace which contains:

  • The class name including the package that I preferred to truncate to the SimpleName of the class for reasons of space in the log, but you can keep for extended if you want
  • The name of the method who raised the Throwable
  • The number of the Line Of Code where this Throwable was raised; in the second method this value does not appear as it may not always be necessary

 

 

Using the methods

 

Here I'll show you an example of Log message in which I used these methods. It's only needed to recall one of the methods showed above and pass to it a Throwable just created with new keyword:

 

Log.i("TAG_LOG_MSG",DebugInfo.getClassMethodLine(new Throwable) + 
"qui possiamo scrivere il messaggio di Log localizzato dal metodo precedentemente 
invocato");

 

 

With the line of code above we'll get in the Logcat the following result (assuming you entered the message Log in MainActivity and more specifically in the OnCreate() method at line 20):

 

 

MainActivity.OnCreate.20:  qui possiamo scrivere il messaggio di Log localizzato dal metodo precedentemente invocato

 

 





If you enjoyed this article, share it as you prefer or leave a comment!!

Parliamo di come si può essere sicuri che i rimedi si acquista on-line sono sicuri. Senza dubbio, uno dei luoghi emozionante, dove le persone possono acquistare farmaci è Internet. Ci sono farmaci variante per ogni malattie. Kamagra è un farmaco prescritto per il trattamento di diverse denunce. Cosa ne pensi "http://journal-info.net/buy/levitra-online.html "? Dove è possibile avere informazioni dettagliate su "buy levitra online "? Le questioni, come "buy levitra ", si riferiscono a vari tipi di problemi medici. Vivere con la disfunzione erettile può essere un vero problema. Ci sono farmaci variante per risolvere la disfunzione sessuale maschile. Il farmaco è per voi. Mai dare ad altre persone, anche se i loro reclami siano uguali ai suoi.