001 /* 002 * Copyright 2002-2004 The Apache Software Foundation 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.apache.commons.clazz.reflect.extended; 017 018 import java.lang.reflect.Method; 019 import java.util.Map; 020 021 import org.apache.commons.clazz.Clazz; 022 import org.apache.commons.clazz.reflect.common.ReflectedAccessorPairProperty; 023 024 /** 025 * 026 * @author <a href="mailto:dmitri@apache.org">Dmitri Plotnikov</a> 027 * @version $Id: ReflectedMappedProperty.java 155436 2005-02-26 13:17:48Z dirkv $ 028 */ 029 public class ReflectedMappedProperty extends ReflectedAccessorPairProperty { 030 031 private Class keyType; 032 private Class contentType; 033 private Method sizeMethod; 034 private Method getMethod; 035 private Method putMethod; 036 private Method removeMethod; 037 private Method keySetMethod; 038 039 /** 040 * Constructor for ReflectedMappedProperty. 041 * @param declaringClazz 042 * @param name 043 */ 044 public ReflectedMappedProperty(Clazz declaringClazz, String name) { 045 super(declaringClazz, name); 046 } 047 048 /** 049 * @see org.apache.commons.clazz.ClazzProperty#isMap() 050 */ 051 public boolean isMap() { 052 return true; 053 } 054 055 /** 056 * Returns the keyType. 057 * @return Class 058 */ 059 public Class getKeyType() { 060 return keyType; 061 } 062 063 /** 064 * Sets the keyType. 065 * @param keyType The keyType to set 066 */ 067 public void setKeyType(Class keyType) { 068 this.keyType = keyType; 069 } 070 071 /** 072 * Returns the contentType. 073 * @return Class 074 */ 075 public Class getContentType() { 076 return contentType; 077 } 078 079 /** 080 * Sets the contentType. 081 * @param contentType The contentType to set 082 */ 083 public void setContentType(Class valueType) { 084 this.contentType = valueType; 085 } 086 087 /** 088 * Returns the getMethod. 089 * @return Method 090 */ 091 public Method getGetMethod() { 092 return getMethod; 093 } 094 095 /** 096 * Sets the getMethod. 097 * @param getMethod The getMethod to set 098 */ 099 public void setGetMethod(Method getMethod) { 100 this.getMethod = getMethod; 101 } 102 103 /** 104 * Returns the putMethod. 105 * @return Method 106 */ 107 public Method getPutMethod() { 108 return putMethod; 109 } 110 111 /** 112 * Sets the putMethod. 113 * @param putMethod The putMethod to set 114 */ 115 public void setPutMethod(Method putMethod) { 116 this.putMethod = putMethod; 117 } 118 119 /** 120 * Returns the removeMethod. 121 * @return Method 122 */ 123 public Method getRemoveMethod() { 124 return removeMethod; 125 } 126 127 /** 128 * Sets the removeMethod. 129 * @param removeMethod The removeMethod to set 130 */ 131 public void setRemoveMethod(Method removeMethod) { 132 this.removeMethod = removeMethod; 133 } 134 135 /** 136 * Returns the keySetMethod. 137 * @return Method 138 */ 139 public Method getKeySetMethod() { 140 return keySetMethod; 141 } 142 143 /** 144 * Sets the keySetMethod. 145 * @param keySetMethod The keySetMethod to set 146 */ 147 public void setKeySetMethod(Method keySetMethod) { 148 this.keySetMethod = keySetMethod; 149 } 150 151 public String toString() { 152 StringBuffer buffer = new StringBuffer("[ReflectedMappedProperty "); 153 if (getType() != null) { 154 buffer.append(getType().getName()); 155 buffer.append(" "); 156 } 157 if (getKeyType() != null) { 158 buffer.append("[key: "); 159 buffer.append(getKeyType().getName()); 160 buffer.append("] "); 161 } 162 if (getContentType() != null) { 163 buffer.append("[content: "); 164 buffer.append(getContentType().getName()); 165 buffer.append("] "); 166 } 167 buffer.append(getName()); 168 if (getReadMethod() != null) { 169 buffer.append("\n [read method] "); 170 buffer.append(getReadMethod()); 171 } 172 if (getWriteMethod() != null) { 173 buffer.append("\n [write method] "); 174 buffer.append(getWriteMethod()); 175 } 176 if (getGetMethod() != null) { 177 buffer.append("\n [get method] "); 178 buffer.append(getGetMethod()); 179 } 180 if (getPutMethod() != null) { 181 buffer.append("\n [put method] "); 182 buffer.append(getPutMethod()); 183 } 184 if (getRemoveMethod() != null) { 185 buffer.append("\n [remove method] "); 186 buffer.append(getRemoveMethod()); 187 } 188 if (getKeySetMethod() != null) { 189 buffer.append("\n [keySet method] "); 190 buffer.append(getKeySetMethod()); 191 } 192 buffer.append("]"); 193 return buffer.toString(); 194 } 195 /** 196 * @see org.apache.commons.clazz.ClazzProperty#get(java.lang.Object) 197 */ 198 public Object get(Object instance) { 199 return new ReflectedMap(instance, this); 200 } 201 202 public Map getMap(Object instance) { 203 if (getReadMethod() == null) { 204 return null; 205 } 206 207 return (Map) super.get(instance); 208 } 209 210 /** 211 * @see org.apache.commons.clazz.ClazzProperty#set(Object, Object) 212 */ 213 public void set(Object instance, Object value) { 214 super.set(instance, value); 215 } 216 }