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 020package org.apache.fulcrum.yaafi.interceptor.util; 021 022import java.lang.reflect.Method; 023 024/** 025 * Creates a string representation of java.lang.reflect.Method 026 * 027 * @author <a href="mailto:siegfried.goeschl@it20one.at">Siegfried Goeschl</a> 028 */ 029public class MethodToStringBuilderImpl implements InterceptorToStringBuilder 030{ 031 /** include the method return type */ 032 public static final int INCLUDE_RETURNTYPE = 0x1; 033 034 /** the default mode using class names and hashcode */ 035 private static int defaultMode = 0x01; 036 037 /** our current formatting mode */ 038 private int mode; 039 040 /** initial size for the StringBuilder */ 041 private static final int BUF_SIZE = 512; 042 043 /** the method we are dumping */ 044 private Method method; 045 046 /** 047 * Constructor 048 */ 049 public MethodToStringBuilderImpl() 050 { 051 this.mode = MethodToStringBuilderImpl.defaultMode; 052 } 053 054 /** 055 * Constructor 056 * 057 * @param method the method to print 058 */ 059 public MethodToStringBuilderImpl(Method method) 060 { 061 this.method = method; 062 this.mode = MethodToStringBuilderImpl.defaultMode; 063 } 064 065 /** 066 * Constructor 067 * 068 * @param method the method to print 069 * @param mode the formatting mode 070 */ 071 public MethodToStringBuilderImpl(Method method, int mode) 072 { 073 this.method = method; 074 this.mode = mode; 075 } 076 077 /** 078 * @see org.apache.fulcrum.yaafi.interceptor.util.InterceptorToStringBuilder#setMaxArgLength(int) 079 */ 080 public void setMaxArgLength(int maxArgLength) 081 { 082 // not supported 083 } 084 085 /** 086 * @see org.apache.fulcrum.yaafi.interceptor.util.InterceptorToStringBuilder#setMode(int) 087 */ 088 public void setMode(int mode) 089 { 090 this.mode = mode; 091 } 092 093 /** 094 * @see org.apache.fulcrum.yaafi.interceptor.util.InterceptorToStringBuilder#setTarget(java.lang.Object) 095 */ 096 public void setTarget(Object target) 097 { 098 this.method = (Method) target; 099 } 100 101 /** 102 * @see java.lang.Object#toString() 103 */ 104 public String toString() 105 { 106 try 107 { 108 StringBuilder buffer = new StringBuilder(BUF_SIZE); 109 110 Class<?> returnType = method.getReturnType(); 111 Class<?> declaringClass = method.getDeclaringClass(); 112 Class[] params = method.getParameterTypes(); 113 114 // print return type 115 116 if ((this.mode & INCLUDE_RETURNTYPE) == 1) 117 { 118 buffer.append( returnType.getSimpleName() ); 119 buffer.append( ' '); 120 } 121 122 // print class and method 123 124 buffer.append( declaringClass.getSimpleName() ) ; 125 buffer.append( '.'); 126 buffer.append( method.getName() ); 127 buffer.append( '('); 128 129 // print the argument list of the method 130 131 for (int i = 0; i < params.length; i++) 132 { 133 buffer.append( params[i].getSimpleName() ); 134 if (i < (params.length - 1)) 135 { 136 buffer.append(","); 137 } 138 } 139 140 buffer.append(")"); 141 142 return buffer.toString(); 143 } 144 catch (Throwable t) 145 { 146 return "<" + t.getClass().getSimpleName() + ">"; 147 } 148 } 149}