001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019 020 package org.apache.fulcrum.yaafi.interceptor.util; 021 022 import java.lang.reflect.Method; 023 024 import org.apache.fulcrum.yaafi.framework.util.StringUtils; 025 026 /** 027 * Creates a string representation of java.lang.reflect.Method 028 * 029 * @author <a href="mailto:siegfried.goeschl@it20one.at">Siegfried Goeschl</a> 030 */ 031 public class MethodToStringBuilderImpl implements InterceptorToStringBuilder 032 { 033 /** include the method return type */ 034 public static final int INCLUDE_RETURNTYPE = 0x1; 035 036 /** the default mode using class names and hashcode */ 037 private static int defaultMode = 0x01; 038 039 /** our current formatting mode */ 040 private int mode; 041 042 /** initial size for the StringBuffer */ 043 private static final int BUF_SIZE = 512; 044 045 /** the method we are dumping */ 046 private Method method; 047 048 /** 049 * Constructor 050 */ 051 public MethodToStringBuilderImpl() 052 { 053 this.mode = MethodToStringBuilderImpl.defaultMode; 054 } 055 056 /** 057 * Constructor 058 * 059 * @param method the method to print 060 */ 061 public MethodToStringBuilderImpl(Method method) 062 { 063 this.method = method; 064 this.mode = MethodToStringBuilderImpl.defaultMode; 065 } 066 067 /** 068 * Constructor 069 * 070 * @param method the method to print 071 * @param mode the formatting mode 072 */ 073 public MethodToStringBuilderImpl(Method method, int mode) 074 { 075 this.method = method; 076 this.mode = mode; 077 } 078 079 /** 080 * @see org.apache.fulcrum.yaafi.interceptor.util.InterceptorToStringBuilder#setMaxArgLength(int) 081 */ 082 public void setMaxArgLength(int maxArgLength) 083 { 084 // not supported 085 } 086 087 /** 088 * @see org.apache.fulcrum.yaafi.interceptor.util.InterceptorToStringBuilder#setMode(int) 089 */ 090 public void setMode(int mode) 091 { 092 this.mode = mode; 093 } 094 095 /** 096 * @see org.apache.fulcrum.yaafi.interceptor.util.InterceptorToStringBuilder#setTarget(java.lang.Object) 097 */ 098 public void setTarget(Object target) 099 { 100 this.method = (Method) target; 101 } 102 103 /** 104 * @see java.lang.Object#toString() 105 */ 106 public String toString() 107 { 108 try 109 { 110 StringBuffer buffer = new StringBuffer(BUF_SIZE); 111 112 Class returnType = method.getReturnType(); 113 Class declaringClass = method.getDeclaringClass(); 114 Class[] params = method.getParameterTypes(); 115 116 // print return type 117 118 if ((this.mode & INCLUDE_RETURNTYPE) == 1) 119 { 120 buffer.append( this.stripDefaultPackage(returnType.getName())); 121 buffer.append( ' '); 122 } 123 124 // print class and method 125 126 buffer.append( this.stripDefaultPackage(declaringClass.getName())); 127 buffer.append( '.'); 128 buffer.append( method.getName() ); 129 buffer.append( '('); 130 131 // print the argument list of the method 132 133 for (int i = 0; i < params.length; i++) 134 { 135 buffer.append( this.stripDefaultPackage(params[i].getName()) ); 136 if (i < (params.length - 1)) 137 { 138 buffer.append(","); 139 } 140 } 141 142 buffer.append(")"); 143 144 return buffer.toString(); 145 } 146 catch (Throwable t) 147 { 148 return "<" + t + ">"; 149 } 150 } 151 152 /** 153 * Strips "java.lang" from the argument other than arrays 154 */ 155 private String stripDefaultPackage( String arg ) 156 { 157 if( arg.charAt(0) == '[' ) 158 { 159 // this is an array 160 return StringUtils.replaceChars(arg,';','#'); 161 } 162 else 163 { 164 return StringUtils.replace( arg, "java.lang.", ""); 165 } 166 } 167 }