001/* 002 * jPOS Project [http://jpos.org] 003 * Copyright (C) 2000-2026 jPOS Software SRL 004 * 005 * This program is free software: you can redistribute it and/or modify 006 * it under the terms of the GNU Affero General Public License as 007 * published by the Free Software Foundation, either version 3 of the 008 * License, or (at your option) any later version. 009 * 010 * This program is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 013 * GNU Affero General Public License for more details. 014 * 015 * You should have received a copy of the GNU Affero General Public License 016 * along with this program. If not, see <http://www.gnu.org/licenses/>. 017 */ 018 019package org.jpos.util; 020 021import java.util.regex.Matcher; 022import java.util.regex.Pattern; 023 024/** 025 * Return Caller's short class name, method and line number 026 */ 027public class Caller { 028 private static String JAVA_ID_PATTERN = "(\\p{javaJavaIdentifierStart}\\p{javaJavaIdentifierPart}*)\\.*"; 029 private static Pattern FQCN = Pattern.compile(JAVA_ID_PATTERN + "(\\." + JAVA_ID_PATTERN + ")*"); 030 public static String info() { 031 return info(1); 032 } 033 034 public static String info(int pos) { 035 return info (Thread.currentThread().getStackTrace()[2+pos]); 036 } 037 038 public static String info (StackTraceElement st) { 039 String clazz = st.getClassName(); 040 Matcher matcher = FQCN.matcher(clazz); 041 StringBuilder sb = new StringBuilder(); 042 while (matcher.find()) { 043 sb.append(matcher.hitEnd() ? matcher.group(1) : matcher.group(1).charAt(0)); 044 sb.append('.'); 045 } 046 return sb.append(st.getMethodName()) 047 .append(':') 048 .append(st.getLineNumber()) 049 .toString(); 050 } 051 052 053 public static String shortClassName(String clazz) { 054 Matcher matcher = FQCN.matcher(clazz); 055 StringBuilder sb = new StringBuilder(); 056 while (matcher.find()) { 057 if (matcher.hitEnd()) { 058 sb.append(matcher.group(1)); 059 break; 060 } 061 else { 062 sb.append(matcher.group(1).charAt(0)); 063 sb.append('.'); 064 } 065 } 066 return sb.toString(); 067 } 068}