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