MethodTracer 取得class名称、方法、行号

package com.segsec.gisap.webservice;

import java.util.Properties;

public class MethodTracer {
	    private StackTraceElement elements[] ;	    
	    private int a[] = new int[2];
	    private int b;
	    
	    public MethodTracer(){
	    	
	    }

	    public String getTrueMethod() {
	    	elements = new Throwable().getStackTrace();
/*	        try {
	            b = a[4];
	        } catch (ArrayIndexOutOfBoundsException aioobe) {
	        	
	            elements = aioobe.getStackTrace();
	            for(StackTraceElement element : elements){
	            	System.out.println(element.getClassName() 
	            				+ "\t" + element.getFileName() 
	            				+ "\t" + element.getLineNumber()
	            				+ "\t" + element.getMethodName());
	            }            
	        }
*/	        
			//String path=elements[1].getClass();
			System.out.println("System.getProperty(\"java.class.path\"):"+this.getClass().getClassLoader().getResource("").getPath());

	    	Properties props=System.getProperties();
	    	System.out.println("用户的当前工作目录:"+props.getProperty("user.dir"));
	    	return elements[1].getClassName() + "." + elements[1].getMethodName()
	        	+ "\t" + elements[1].getFileName() +":"+ elements[1].getLineNumber() ;
	    }
	    
	    public static void main(String[] args){
	    	new MethodTracer().mymethod();
	    }
	    
	    public void mymethod(){
	    	log("123");
	    }
	    
	    public void log(String s){
	    	System.out.println(new MethodTracer().getTrueMethod() + "[INFO]: " + s);
	    }

}

你可能感兴趣的:(MethodTracer 取得class名称、方法、行号)