001 // Copyright 2005 The Apache Software Foundation 002 // 003 // Licensed under the Apache License, Version 2.0 (the "License"); 004 // you may not use this file except in compliance with the License. 005 // You may obtain a copy of the License at 006 // 007 // http://www.apache.org/licenses/LICENSE-2.0 008 // 009 // Unless required by applicable law or agreed to in writing, software 010 // distributed under the License is distributed on an "AS IS" BASIS, 011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 012 // See the License for the specific language governing permissions and 013 // limitations under the License. 014 015 package org.apache.tapestry.annotations; 016 017 import java.beans.Introspector; 018 import java.lang.reflect.Method; 019 020 import org.apache.hivemind.ApplicationRuntimeException; 021 022 /** 023 * @author Howard M. Lewis Ship 024 * @since 4.0 025 */ 026 027 public class AnnotationUtils 028 { 029 public static String getPropertyName(Method method) 030 { 031 String name = method.getName(); 032 033 if (name.startsWith("is")) 034 { 035 checkGetter(method); 036 return Introspector.decapitalize(name.substring(2)); 037 } 038 039 if (name.startsWith("get")) 040 { 041 checkGetter(method); 042 return Introspector.decapitalize(name.substring(3)); 043 } 044 045 if (name.startsWith("set")) 046 { 047 checkSetter(method); 048 return Introspector.decapitalize(name.substring(3)); 049 } 050 051 throw new ApplicationRuntimeException(AnnotationMessages.notAccessor(method)); 052 } 053 054 private static void checkGetter(Method method) 055 { 056 if (method.getParameterTypes().length > 0) 057 throw new ApplicationRuntimeException(AnnotationMessages.noParametersExpected(method)); 058 059 if (method.getReturnType().equals(void.class)) 060 throw new ApplicationRuntimeException(AnnotationMessages.voidAccessor(method)); 061 062 } 063 064 private static void checkSetter(Method method) 065 { 066 if (!method.getReturnType().equals(void.class)) 067 throw new ApplicationRuntimeException(AnnotationMessages.nonVoidMutator(method)); 068 069 if (method.getParameterTypes().length != 1) 070 throw new ApplicationRuntimeException(AnnotationMessages.wrongParameterCount(method)); 071 } 072 }