Tue, 06 Mar 2012 16:09:35 -0800
7150322: Stop using drop source bundles in jaxws
Reviewed-by: darcy, ohrstrom
ohair@286 | 1 | /* |
ohair@286 | 2 | * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. |
ohair@286 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
ohair@286 | 4 | * |
ohair@286 | 5 | * This code is free software; you can redistribute it and/or modify it |
ohair@286 | 6 | * under the terms of the GNU General Public License version 2 only, as |
ohair@286 | 7 | * published by the Free Software Foundation. Oracle designates this |
ohair@286 | 8 | * particular file as subject to the "Classpath" exception as provided |
ohair@286 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
ohair@286 | 10 | * |
ohair@286 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
ohair@286 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
ohair@286 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
ohair@286 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
ohair@286 | 15 | * accompanied this code). |
ohair@286 | 16 | * |
ohair@286 | 17 | * You should have received a copy of the GNU General Public License version |
ohair@286 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
ohair@286 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
ohair@286 | 20 | * |
ohair@286 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
ohair@286 | 22 | * or visit www.oracle.com if you need additional information or have any |
ohair@286 | 23 | * questions. |
ohair@286 | 24 | */ |
ohair@286 | 25 | |
ohair@286 | 26 | package com.sun.xml.internal.bind.v2.runtime.reflect; |
ohair@286 | 27 | |
ohair@286 | 28 | import java.lang.reflect.Field; |
ohair@286 | 29 | import java.lang.reflect.InvocationTargetException; |
ohair@286 | 30 | import java.lang.reflect.Method; |
ohair@286 | 31 | import java.lang.reflect.Modifier; |
ohair@286 | 32 | import java.lang.reflect.Type; |
ohair@286 | 33 | import java.util.Arrays; |
ohair@286 | 34 | import java.util.HashMap; |
ohair@286 | 35 | import java.util.List; |
ohair@286 | 36 | import java.util.Map; |
ohair@286 | 37 | import java.util.List; |
ohair@286 | 38 | import java.util.Arrays; |
ohair@286 | 39 | import java.util.logging.Level; |
ohair@286 | 40 | import java.util.logging.Logger; |
ohair@286 | 41 | |
ohair@286 | 42 | import javax.xml.bind.JAXBElement; |
ohair@286 | 43 | import javax.xml.bind.annotation.adapters.XmlAdapter; |
ohair@286 | 44 | |
ohair@286 | 45 | import com.sun.istack.internal.Nullable; |
ohair@286 | 46 | import com.sun.xml.internal.bind.Util; |
ohair@286 | 47 | import com.sun.xml.internal.bind.api.AccessorException; |
ohair@286 | 48 | import com.sun.xml.internal.bind.api.JAXBRIContext; |
ohair@286 | 49 | import com.sun.xml.internal.bind.v2.model.core.Adapter; |
ohair@286 | 50 | import com.sun.xml.internal.bind.v2.model.impl.RuntimeModelBuilder; |
ohair@286 | 51 | import com.sun.xml.internal.bind.v2.model.nav.Navigator; |
ohair@286 | 52 | import com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl; |
ohair@286 | 53 | import com.sun.xml.internal.bind.v2.runtime.reflect.opt.OptimizedAccessorFactory; |
ohair@286 | 54 | import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader; |
ohair@286 | 55 | import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Receiver; |
ohair@286 | 56 | import com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext; |
ohair@286 | 57 | import com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl; |
ohair@286 | 58 | import com.sun.istack.internal.Nullable; |
ohair@286 | 59 | |
ohair@286 | 60 | import org.xml.sax.SAXException; |
ohair@286 | 61 | |
ohair@286 | 62 | /** |
ohair@286 | 63 | * Accesses a particular property of a bean. |
ohair@286 | 64 | * <p/> |
ohair@286 | 65 | * <p/> |
ohair@286 | 66 | * This interface encapsulates the access to the actual data store. |
ohair@286 | 67 | * The intention is to generate implementations for a particular bean |
ohair@286 | 68 | * and a property to improve the performance. |
ohair@286 | 69 | * <p/> |
ohair@286 | 70 | * <p/> |
ohair@286 | 71 | * Accessor can be used as a receiver. Upon receiving an object |
ohair@286 | 72 | * it sets that to the field. |
ohair@286 | 73 | * |
ohair@286 | 74 | * @author Kohsuke Kawaguchi (kk@kohsuke.org) |
ohair@286 | 75 | * @see Accessor.FieldReflection |
ohair@286 | 76 | * @see TransducedAccessor |
ohair@286 | 77 | */ |
ohair@286 | 78 | public abstract class Accessor<BeanT, ValueT> implements Receiver { |
ohair@286 | 79 | |
ohair@286 | 80 | public final Class<ValueT> valueType; |
ohair@286 | 81 | |
ohair@286 | 82 | public Class<ValueT> getValueType() { |
ohair@286 | 83 | return valueType; |
ohair@286 | 84 | } |
ohair@286 | 85 | |
ohair@286 | 86 | protected Accessor(Class<ValueT> valueType) { |
ohair@286 | 87 | this.valueType = valueType; |
ohair@286 | 88 | } |
ohair@286 | 89 | |
ohair@286 | 90 | /** |
ohair@286 | 91 | * Returns the optimized version of the same accessor. |
ohair@286 | 92 | * |
ohair@286 | 93 | * @param context The {@link JAXBContextImpl} that owns the whole thing. |
ohair@286 | 94 | * (See {@link RuntimeModelBuilder#context}.) |
ohair@286 | 95 | * @return At least the implementation can return <tt>this</tt>. |
ohair@286 | 96 | */ |
ohair@286 | 97 | public Accessor<BeanT, ValueT> optimize(@Nullable JAXBContextImpl context) { |
ohair@286 | 98 | return this; |
ohair@286 | 99 | } |
ohair@286 | 100 | |
ohair@286 | 101 | |
ohair@286 | 102 | /** |
ohair@286 | 103 | * Gets the value of the property of the given bean object. |
ohair@286 | 104 | * |
ohair@286 | 105 | * @param bean must not be null. |
ohair@286 | 106 | * @throws AccessorException if failed to set a value. For example, the getter method |
ohair@286 | 107 | * may throw an exception. |
ohair@286 | 108 | * @since 2.0 EA1 |
ohair@286 | 109 | */ |
ohair@286 | 110 | public abstract ValueT get(BeanT bean) throws AccessorException; |
ohair@286 | 111 | |
ohair@286 | 112 | /** |
ohair@286 | 113 | * Sets the value of the property of the given bean object. |
ohair@286 | 114 | * |
ohair@286 | 115 | * @param bean must not be null. |
ohair@286 | 116 | * @param value the value to be set. Setting value to null means resetting |
ohair@286 | 117 | * to the VM default value (even for primitive properties.) |
ohair@286 | 118 | * @throws AccessorException if failed to set a value. For example, the setter method |
ohair@286 | 119 | * may throw an exception. |
ohair@286 | 120 | * @since 2.0 EA1 |
ohair@286 | 121 | */ |
ohair@286 | 122 | public abstract void set(BeanT bean, ValueT value) throws AccessorException; |
ohair@286 | 123 | |
ohair@286 | 124 | |
ohair@286 | 125 | /** |
ohair@286 | 126 | * Sets the value without adapting the value. |
ohair@286 | 127 | * <p/> |
ohair@286 | 128 | * This ugly entry point is only used by JAX-WS. |
ohair@286 | 129 | * See {@link JAXBRIContext#getElementPropertyAccessor} |
ohair@286 | 130 | */ |
ohair@286 | 131 | public Object getUnadapted(BeanT bean) throws AccessorException { |
ohair@286 | 132 | return get(bean); |
ohair@286 | 133 | } |
ohair@286 | 134 | |
ohair@286 | 135 | /** |
ohair@286 | 136 | * Returns true if this accessor wraps an adapter. |
ohair@286 | 137 | * <p/> |
ohair@286 | 138 | * This method needs to be used with care, but it helps some optimization. |
ohair@286 | 139 | */ |
ohair@286 | 140 | public boolean isAdapted() { |
ohair@286 | 141 | return false; |
ohair@286 | 142 | } |
ohair@286 | 143 | |
ohair@286 | 144 | /** |
ohair@286 | 145 | * Sets the value without adapting the value. |
ohair@286 | 146 | * <p/> |
ohair@286 | 147 | * This ugly entry point is only used by JAX-WS. |
ohair@286 | 148 | * See {@link JAXBRIContext#getElementPropertyAccessor} |
ohair@286 | 149 | */ |
ohair@286 | 150 | public void setUnadapted(BeanT bean, Object value) throws AccessorException { |
ohair@286 | 151 | set(bean, (ValueT) value); |
ohair@286 | 152 | } |
ohair@286 | 153 | |
ohair@286 | 154 | public void receive(UnmarshallingContext.State state, Object o) throws SAXException { |
ohair@286 | 155 | try { |
ohair@286 | 156 | set((BeanT) state.target, (ValueT) o); |
ohair@286 | 157 | } catch (AccessorException e) { |
ohair@286 | 158 | Loader.handleGenericException(e, true); |
ohair@286 | 159 | } catch (IllegalAccessError iae) { |
ohair@286 | 160 | // throw UnmarshalException instead IllegalAccesssError | Issue 475 |
ohair@286 | 161 | Loader.handleGenericError(iae); |
ohair@286 | 162 | } |
ohair@286 | 163 | } |
ohair@286 | 164 | |
ohair@286 | 165 | private static List<Class> nonAbstractableClasses = Arrays.asList(new Class[]{ |
ohair@286 | 166 | Object.class, |
ohair@286 | 167 | java.util.Calendar.class, |
ohair@286 | 168 | javax.xml.datatype.Duration.class, |
ohair@286 | 169 | javax.xml.datatype.XMLGregorianCalendar.class, |
ohair@286 | 170 | java.awt.Image.class, |
ohair@286 | 171 | javax.activation.DataHandler.class, |
ohair@286 | 172 | javax.xml.transform.Source.class, |
ohair@286 | 173 | java.util.Date.class, |
ohair@286 | 174 | java.io.File.class, |
ohair@286 | 175 | java.net.URI.class, |
ohair@286 | 176 | java.net.URL.class, |
ohair@286 | 177 | Class.class, |
ohair@286 | 178 | String.class, |
ohair@286 | 179 | javax.xml.transform.Source.class} |
ohair@286 | 180 | ); |
ohair@286 | 181 | |
ohair@286 | 182 | public boolean isValueTypeAbstractable() { |
ohair@286 | 183 | return !nonAbstractableClasses.contains(getValueType()); |
ohair@286 | 184 | } |
ohair@286 | 185 | |
ohair@286 | 186 | /** |
ohair@286 | 187 | * Wraps this {@link Accessor} into another {@link Accessor} |
ohair@286 | 188 | * and performs the type adaption as necessary. |
ohair@286 | 189 | */ |
ohair@286 | 190 | public final <T> Accessor<BeanT, T> adapt(Class<T> targetType, final Class<? extends XmlAdapter<T, ValueT>> adapter) { |
ohair@286 | 191 | return new AdaptedAccessor<BeanT, ValueT, T>(targetType, this, adapter); |
ohair@286 | 192 | } |
ohair@286 | 193 | |
ohair@286 | 194 | public final <T> Accessor<BeanT, T> adapt(Adapter<Type, Class> adapter) { |
ohair@286 | 195 | return new AdaptedAccessor<BeanT, ValueT, T>( |
ohair@286 | 196 | (Class<T>) Navigator.REFLECTION.erasure(adapter.defaultType), |
ohair@286 | 197 | this, |
ohair@286 | 198 | adapter.adapterType); |
ohair@286 | 199 | } |
ohair@286 | 200 | |
ohair@286 | 201 | /** |
ohair@286 | 202 | * Flag that will be set to true after issueing a warning |
ohair@286 | 203 | * about the lack of permission to access non-public fields. |
ohair@286 | 204 | */ |
ohair@286 | 205 | private static boolean accessWarned = false; |
ohair@286 | 206 | |
ohair@286 | 207 | |
ohair@286 | 208 | /** |
ohair@286 | 209 | * {@link Accessor} that uses Java reflection to access a field. |
ohair@286 | 210 | */ |
ohair@286 | 211 | public static class FieldReflection<BeanT, ValueT> extends Accessor<BeanT, ValueT> { |
ohair@286 | 212 | public final Field f; |
ohair@286 | 213 | |
ohair@286 | 214 | private static final Logger logger = Util.getClassLogger(); |
ohair@286 | 215 | |
ohair@286 | 216 | public FieldReflection(Field f) { |
ohair@286 | 217 | this(f, false); |
ohair@286 | 218 | } |
ohair@286 | 219 | |
ohair@286 | 220 | public FieldReflection(Field f, boolean supressAccessorWarnings) { |
ohair@286 | 221 | super((Class<ValueT>) f.getType()); |
ohair@286 | 222 | this.f = f; |
ohair@286 | 223 | |
ohair@286 | 224 | int mod = f.getModifiers(); |
ohair@286 | 225 | if (!Modifier.isPublic(mod) || Modifier.isFinal(mod) || !Modifier.isPublic(f.getDeclaringClass().getModifiers())) { |
ohair@286 | 226 | try { |
ohair@286 | 227 | // attempt to make it accessible, but do so in the security context of the calling application. |
ohair@286 | 228 | // don't do this in the doPrivilege block, as that would create a security hole for anyone |
ohair@286 | 229 | // to make any field accessible. |
ohair@286 | 230 | f.setAccessible(true); |
ohair@286 | 231 | } catch (SecurityException e) { |
ohair@286 | 232 | if ((!accessWarned) && (!supressAccessorWarnings)) { |
ohair@286 | 233 | // this happens when we don't have enough permission. |
ohair@286 | 234 | logger.log(Level.WARNING, Messages.UNABLE_TO_ACCESS_NON_PUBLIC_FIELD.format( |
ohair@286 | 235 | f.getDeclaringClass().getName(), |
ohair@286 | 236 | f.getName()), |
ohair@286 | 237 | e); |
ohair@286 | 238 | } |
ohair@286 | 239 | accessWarned = true; |
ohair@286 | 240 | } |
ohair@286 | 241 | } |
ohair@286 | 242 | } |
ohair@286 | 243 | |
ohair@286 | 244 | public ValueT get(BeanT bean) { |
ohair@286 | 245 | try { |
ohair@286 | 246 | return (ValueT) f.get(bean); |
ohair@286 | 247 | } catch (IllegalAccessException e) { |
ohair@286 | 248 | throw new IllegalAccessError(e.getMessage()); |
ohair@286 | 249 | } |
ohair@286 | 250 | } |
ohair@286 | 251 | |
ohair@286 | 252 | public void set(BeanT bean, ValueT value) { |
ohair@286 | 253 | try { |
ohair@286 | 254 | if (value == null) |
ohair@286 | 255 | value = (ValueT) uninitializedValues.get(valueType); |
ohair@286 | 256 | f.set(bean, value); |
ohair@286 | 257 | } catch (IllegalAccessException e) { |
ohair@286 | 258 | throw new IllegalAccessError(e.getMessage()); |
ohair@286 | 259 | } |
ohair@286 | 260 | } |
ohair@286 | 261 | |
ohair@286 | 262 | @Override |
ohair@286 | 263 | public Accessor<BeanT, ValueT> optimize(JAXBContextImpl context) { |
ohair@286 | 264 | if (context != null && context.fastBoot) |
ohair@286 | 265 | // let's not waste time on doing this for the sake of faster boot. |
ohair@286 | 266 | return this; |
ohair@286 | 267 | Accessor<BeanT, ValueT> acc = OptimizedAccessorFactory.get(f); |
ohair@286 | 268 | if (acc != null) |
ohair@286 | 269 | return acc; |
ohair@286 | 270 | else |
ohair@286 | 271 | return this; |
ohair@286 | 272 | } |
ohair@286 | 273 | } |
ohair@286 | 274 | |
ohair@286 | 275 | /** |
ohair@286 | 276 | * Read-only access to {@link Field}. Used to handle a static field. |
ohair@286 | 277 | */ |
ohair@286 | 278 | public static final class ReadOnlyFieldReflection<BeanT, ValueT> extends FieldReflection<BeanT, ValueT> { |
ohair@286 | 279 | public ReadOnlyFieldReflection(Field f, boolean supressAccessorWarnings) { |
ohair@286 | 280 | super(f, supressAccessorWarnings); |
ohair@286 | 281 | } |
ohair@286 | 282 | public ReadOnlyFieldReflection(Field f) { |
ohair@286 | 283 | super(f); |
ohair@286 | 284 | } |
ohair@286 | 285 | |
ohair@286 | 286 | @Override |
ohair@286 | 287 | public void set(BeanT bean, ValueT value) { |
ohair@286 | 288 | // noop |
ohair@286 | 289 | } |
ohair@286 | 290 | |
ohair@286 | 291 | @Override |
ohair@286 | 292 | public Accessor<BeanT, ValueT> optimize(JAXBContextImpl context) { |
ohair@286 | 293 | return this; |
ohair@286 | 294 | } |
ohair@286 | 295 | } |
ohair@286 | 296 | |
ohair@286 | 297 | |
ohair@286 | 298 | /** |
ohair@286 | 299 | * {@link Accessor} that uses Java reflection to access a getter and a setter. |
ohair@286 | 300 | */ |
ohair@286 | 301 | public static class GetterSetterReflection<BeanT, ValueT> extends Accessor<BeanT, ValueT> { |
ohair@286 | 302 | public final Method getter; |
ohair@286 | 303 | public final Method setter; |
ohair@286 | 304 | |
ohair@286 | 305 | private static final Logger logger = Util.getClassLogger(); |
ohair@286 | 306 | |
ohair@286 | 307 | public GetterSetterReflection(Method getter, Method setter) { |
ohair@286 | 308 | super( |
ohair@286 | 309 | (Class<ValueT>) (getter != null ? |
ohair@286 | 310 | getter.getReturnType() : |
ohair@286 | 311 | setter.getParameterTypes()[0])); |
ohair@286 | 312 | this.getter = getter; |
ohair@286 | 313 | this.setter = setter; |
ohair@286 | 314 | |
ohair@286 | 315 | if (getter != null) |
ohair@286 | 316 | makeAccessible(getter); |
ohair@286 | 317 | if (setter != null) |
ohair@286 | 318 | makeAccessible(setter); |
ohair@286 | 319 | } |
ohair@286 | 320 | |
ohair@286 | 321 | private void makeAccessible(Method m) { |
ohair@286 | 322 | if (!Modifier.isPublic(m.getModifiers()) || !Modifier.isPublic(m.getDeclaringClass().getModifiers())) { |
ohair@286 | 323 | try { |
ohair@286 | 324 | m.setAccessible(true); |
ohair@286 | 325 | } catch (SecurityException e) { |
ohair@286 | 326 | if (!accessWarned) |
ohair@286 | 327 | // this happens when we don't have enough permission. |
ohair@286 | 328 | logger.log(Level.WARNING, Messages.UNABLE_TO_ACCESS_NON_PUBLIC_FIELD.format( |
ohair@286 | 329 | m.getDeclaringClass().getName(), |
ohair@286 | 330 | m.getName()), |
ohair@286 | 331 | e); |
ohair@286 | 332 | accessWarned = true; |
ohair@286 | 333 | } |
ohair@286 | 334 | } |
ohair@286 | 335 | } |
ohair@286 | 336 | |
ohair@286 | 337 | public ValueT get(BeanT bean) throws AccessorException { |
ohair@286 | 338 | try { |
ohair@286 | 339 | return (ValueT) getter.invoke(bean); |
ohair@286 | 340 | } catch (IllegalAccessException e) { |
ohair@286 | 341 | throw new IllegalAccessError(e.getMessage()); |
ohair@286 | 342 | } catch (InvocationTargetException e) { |
ohair@286 | 343 | throw handleInvocationTargetException(e); |
ohair@286 | 344 | } |
ohair@286 | 345 | } |
ohair@286 | 346 | |
ohair@286 | 347 | public void set(BeanT bean, ValueT value) throws AccessorException { |
ohair@286 | 348 | try { |
ohair@286 | 349 | if (value == null) |
ohair@286 | 350 | value = (ValueT) uninitializedValues.get(valueType); |
ohair@286 | 351 | setter.invoke(bean, value); |
ohair@286 | 352 | } catch (IllegalAccessException e) { |
ohair@286 | 353 | throw new IllegalAccessError(e.getMessage()); |
ohair@286 | 354 | } catch (InvocationTargetException e) { |
ohair@286 | 355 | throw handleInvocationTargetException(e); |
ohair@286 | 356 | } |
ohair@286 | 357 | } |
ohair@286 | 358 | |
ohair@286 | 359 | private AccessorException handleInvocationTargetException(InvocationTargetException e) { |
ohair@286 | 360 | // don't block a problem in the user code |
ohair@286 | 361 | Throwable t = e.getTargetException(); |
ohair@286 | 362 | if (t instanceof RuntimeException) |
ohair@286 | 363 | throw (RuntimeException) t; |
ohair@286 | 364 | if (t instanceof Error) |
ohair@286 | 365 | throw (Error) t; |
ohair@286 | 366 | |
ohair@286 | 367 | // otherwise it's a checked exception. |
ohair@286 | 368 | // I'm not sure how to handle this. |
ohair@286 | 369 | // we can throw a checked exception from here, |
ohair@286 | 370 | // but because get/set would be called from so many different places, |
ohair@286 | 371 | // the handling would be tedious. |
ohair@286 | 372 | return new AccessorException(t); |
ohair@286 | 373 | } |
ohair@286 | 374 | |
ohair@286 | 375 | @Override |
ohair@286 | 376 | public Accessor<BeanT, ValueT> optimize(JAXBContextImpl context) { |
ohair@286 | 377 | if (getter == null || setter == null) |
ohair@286 | 378 | // if we aren't complete, OptimizedAccessor won't always work |
ohair@286 | 379 | return this; |
ohair@286 | 380 | if (context != null && context.fastBoot) |
ohair@286 | 381 | // let's not waste time on doing this for the sake of faster boot. |
ohair@286 | 382 | return this; |
ohair@286 | 383 | |
ohair@286 | 384 | Accessor<BeanT, ValueT> acc = OptimizedAccessorFactory.get(getter, setter); |
ohair@286 | 385 | if (acc != null) |
ohair@286 | 386 | return acc; |
ohair@286 | 387 | else |
ohair@286 | 388 | return this; |
ohair@286 | 389 | } |
ohair@286 | 390 | } |
ohair@286 | 391 | |
ohair@286 | 392 | /** |
ohair@286 | 393 | * A version of {@link GetterSetterReflection} that doesn't have any setter. |
ohair@286 | 394 | * <p/> |
ohair@286 | 395 | * <p/> |
ohair@286 | 396 | * This provides a user-friendly error message. |
ohair@286 | 397 | */ |
ohair@286 | 398 | public static class GetterOnlyReflection<BeanT, ValueT> extends GetterSetterReflection<BeanT, ValueT> { |
ohair@286 | 399 | public GetterOnlyReflection(Method getter) { |
ohair@286 | 400 | super(getter, null); |
ohair@286 | 401 | } |
ohair@286 | 402 | |
ohair@286 | 403 | @Override |
ohair@286 | 404 | public void set(BeanT bean, ValueT value) throws AccessorException { |
ohair@286 | 405 | throw new AccessorException(Messages.NO_SETTER.format(getter.toString())); |
ohair@286 | 406 | } |
ohair@286 | 407 | } |
ohair@286 | 408 | |
ohair@286 | 409 | /** |
ohair@286 | 410 | * A version of {@link GetterSetterReflection} thaat doesn't have any getter. |
ohair@286 | 411 | * <p/> |
ohair@286 | 412 | * <p/> |
ohair@286 | 413 | * This provides a user-friendly error message. |
ohair@286 | 414 | */ |
ohair@286 | 415 | public static class SetterOnlyReflection<BeanT, ValueT> extends GetterSetterReflection<BeanT, ValueT> { |
ohair@286 | 416 | public SetterOnlyReflection(Method setter) { |
ohair@286 | 417 | super(null, setter); |
ohair@286 | 418 | } |
ohair@286 | 419 | |
ohair@286 | 420 | @Override |
ohair@286 | 421 | public ValueT get(BeanT bean) throws AccessorException { |
ohair@286 | 422 | throw new AccessorException(Messages.NO_GETTER.format(setter.toString())); |
ohair@286 | 423 | } |
ohair@286 | 424 | } |
ohair@286 | 425 | |
ohair@286 | 426 | /** |
ohair@286 | 427 | * Gets the special {@link Accessor} used to recover from errors. |
ohair@286 | 428 | */ |
ohair@286 | 429 | @SuppressWarnings("unchecked") |
ohair@286 | 430 | public static <A, B> Accessor<A, B> getErrorInstance() { |
ohair@286 | 431 | return ERROR; |
ohair@286 | 432 | } |
ohair@286 | 433 | |
ohair@286 | 434 | private static final Accessor ERROR = new Accessor<Object, Object>(Object.class) { |
ohair@286 | 435 | public Object get(Object o) { |
ohair@286 | 436 | return null; |
ohair@286 | 437 | } |
ohair@286 | 438 | |
ohair@286 | 439 | public void set(Object o, Object o1) { |
ohair@286 | 440 | } |
ohair@286 | 441 | }; |
ohair@286 | 442 | |
ohair@286 | 443 | /** |
ohair@286 | 444 | * {@link Accessor} for {@link JAXBElement#getValue()}. |
ohair@286 | 445 | */ |
ohair@286 | 446 | public static final Accessor<JAXBElement, Object> JAXB_ELEMENT_VALUE = new Accessor<JAXBElement, Object>(Object.class) { |
ohair@286 | 447 | public Object get(JAXBElement jaxbElement) { |
ohair@286 | 448 | return jaxbElement.getValue(); |
ohair@286 | 449 | } |
ohair@286 | 450 | |
ohair@286 | 451 | public void set(JAXBElement jaxbElement, Object o) { |
ohair@286 | 452 | jaxbElement.setValue(o); |
ohair@286 | 453 | } |
ohair@286 | 454 | }; |
ohair@286 | 455 | |
ohair@286 | 456 | /** |
ohair@286 | 457 | * Uninitialized map keyed by their classes. |
ohair@286 | 458 | */ |
ohair@286 | 459 | private static final Map<Class, Object> uninitializedValues = new HashMap<Class, Object>(); |
ohair@286 | 460 | |
ohair@286 | 461 | static { |
ohair@286 | 462 | /* |
ohair@286 | 463 | static byte default_value_byte = 0; |
ohair@286 | 464 | static boolean default_value_boolean = false; |
ohair@286 | 465 | static char default_value_char = 0; |
ohair@286 | 466 | static float default_value_float = 0; |
ohair@286 | 467 | static double default_value_double = 0; |
ohair@286 | 468 | static int default_value_int = 0; |
ohair@286 | 469 | static long default_value_long = 0; |
ohair@286 | 470 | static short default_value_short = 0; |
ohair@286 | 471 | */ |
ohair@286 | 472 | uninitializedValues.put(byte.class, Byte.valueOf((byte) 0)); |
ohair@286 | 473 | uninitializedValues.put(boolean.class, false); |
ohair@286 | 474 | uninitializedValues.put(char.class, Character.valueOf((char) 0)); |
ohair@286 | 475 | uninitializedValues.put(float.class, Float.valueOf(0)); |
ohair@286 | 476 | uninitializedValues.put(double.class, Double.valueOf(0)); |
ohair@286 | 477 | uninitializedValues.put(int.class, Integer.valueOf(0)); |
ohair@286 | 478 | uninitializedValues.put(long.class, Long.valueOf(0)); |
ohair@286 | 479 | uninitializedValues.put(short.class, Short.valueOf((short) 0)); |
ohair@286 | 480 | } |
ohair@286 | 481 | |
ohair@286 | 482 | } |