1 /*
2 * Copyright (c) 1997, 2018 Oracle and/or its affiliates. All rights reserved.
3 *
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Distribution License v. 1.0, which is available at
6 * http://www.eclipse.org/org/documents/edl-v10.php.
7 *
8 * SPDX-License-Identifier: BSD-3-Clause
9 */
10
11 package com.sun.xml.bind;
12
13 import java.lang.reflect.Field;
14 import java.lang.reflect.Method;
15
16 import javax.xml.bind.JAXBException;
17
18 import com.sun.xml.bind.v2.runtime.reflect.Accessor;
19
20 public class AccessorFactoryImpl implements InternalAccessorFactory {
21
22 private static AccessorFactoryImpl instance = new AccessorFactoryImpl();
23 private AccessorFactoryImpl(){}
24
25 public static AccessorFactoryImpl getInstance(){
26 return instance;
27 }
28
29 /**
30 * Access a field of the class.
31 *
32 * @param bean the class to be processed.
33 * @param field the field within the class to be accessed.
34 * @param readOnly the isStatic value of the field's modifier.
35 * @return Accessor the accessor for this field
36 */
37 public Accessor createFieldAccessor(Class bean, Field field, boolean readOnly) {
38 return readOnly
39 ? new Accessor.ReadOnlyFieldReflection(field)
40 : new Accessor.FieldReflection(field);
41 }
42
43 /**
44 * Access a field of the class.
45 *
46 * @param bean the class to be processed.
47 * @param field the field within the class to be accessed.
48 * @param readOnly the isStatic value of the field's modifier.
49 * @param supressWarning supress security warning about accessing fields through reflection
50 * @return Accessor the accessor for this field
51 */
52 public Accessor createFieldAccessor(Class bean, Field field, boolean readOnly, boolean supressWarning) {
53 return readOnly
54 ? new Accessor.ReadOnlyFieldReflection(field, supressWarning)
55 : new Accessor.FieldReflection(field, supressWarning);
56 }
57
58 /**
59 * Access a property of the class.
60 *
61 * @param bean the class to be processed
62 * @param getter the getter method to be accessed. The value can be null.
63 * @param setter the setter method to be accessed. The value can be null.
64 * @return Accessor the accessor for these methods
65 */
66 public Accessor createPropertyAccessor(Class bean, Method getter, Method setter) {
67 if (getter == null) {
68 return new Accessor.SetterOnlyReflection(setter);
69 }
70 if (setter == null) {
71 return new Accessor.GetterOnlyReflection(getter);
72 }
73 return new Accessor.GetterSetterReflection(getter, setter);
74 }
75 }
76