1 /*
   2  * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.xml.internal.bind;
  27 
  28 import java.lang.reflect.Field;
  29 import java.lang.reflect.Method;
  30 
  31 import javax.xml.bind.JAXBException;
  32 
  33 import com.sun.xml.internal.bind.v2.runtime.reflect.Accessor;
  34 
  35 public class AccessorFactoryImpl implements InternalAccessorFactory {
  36 
  37     private static AccessorFactoryImpl instance = new AccessorFactoryImpl();
  38     private AccessorFactoryImpl(){}
  39 
  40     public static AccessorFactoryImpl getInstance(){
  41         return instance;
  42     }
  43 
  44     /**
  45      * Access a field of the class.
  46      *
  47      * @param bean the class to be processed.
  48      * @param field the field within the class to be accessed.
  49      * @param readOnly  the isStatic value of the field's modifier.
  50      * @return Accessor the accessor for this field
  51      *
  52      * @throws JAXBException reports failures of the method.
  53      */
  54     public Accessor createFieldAccessor(Class bean, Field field, boolean readOnly) {
  55         return readOnly
  56                 ? new Accessor.ReadOnlyFieldReflection(field)
  57                 : new Accessor.FieldReflection(field);
  58     }
  59 
  60     /**
  61      * Access a field of the class.
  62      *
  63      * @param bean the class to be processed.
  64      * @param field the field within the class to be accessed.
  65      * @param readOnly  the isStatic value of the field's modifier.
  66      * @param supressWarning supress security warning about accessing fields through reflection
  67      * @return Accessor the accessor for this field
  68      *
  69      * @throws JAXBException reports failures of the method.
  70      */
  71     public Accessor createFieldAccessor(Class bean, Field field, boolean readOnly, boolean supressWarning) {
  72         return readOnly
  73                 ? new Accessor.ReadOnlyFieldReflection(field, supressWarning)
  74                 : new Accessor.FieldReflection(field, supressWarning);
  75     }
  76 
  77     /**
  78      * Access a property of the class.
  79      *
  80      * @param bean the class to be processed
  81      * @param getter the getter method to be accessed. The value can be null.
  82      * @param setter the setter method to be accessed. The value can be null.
  83      * @return Accessor the accessor for these methods
  84      *
  85      * @throws JAXBException reports failures of the method.
  86      */
  87     public Accessor createPropertyAccessor(Class bean, Method getter, Method setter) {
  88         if (getter == null) {
  89             return new Accessor.SetterOnlyReflection(setter);
  90         }
  91         if (setter == null) {
  92             return new Accessor.GetterOnlyReflection(getter);
  93         }
  94         return new Accessor.GetterSetterReflection(getter, setter);
  95     }
  96 }