jdk/src/java.base/share/classes/sun/reflect/UnsafeFieldAccessorFactory.java
changeset 37377 bd3b98a11a14
parent 37376 5daa5fbad3ce
parent 37374 ee1b8619eddb
child 37380 55c2549714af
equal deleted inserted replaced
37376:5daa5fbad3ce 37377:bd3b98a11a14
     1 /*
       
     2  * Copyright (c) 2001, 2011, 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 sun.reflect;
       
    27 
       
    28 import java.lang.reflect.Field;
       
    29 import java.lang.reflect.Modifier;
       
    30 
       
    31 class UnsafeFieldAccessorFactory {
       
    32     static FieldAccessor newFieldAccessor(Field field, boolean override) {
       
    33         Class<?> type = field.getType();
       
    34         boolean isStatic = Modifier.isStatic(field.getModifiers());
       
    35         boolean isFinal = Modifier.isFinal(field.getModifiers());
       
    36         boolean isVolatile = Modifier.isVolatile(field.getModifiers());
       
    37         boolean isQualified = isFinal || isVolatile;
       
    38         boolean isReadOnly = isFinal && (isStatic || !override);
       
    39         if (isStatic) {
       
    40             // This code path does not guarantee that the field's
       
    41             // declaring class has been initialized, but it must be
       
    42             // before performing reflective operations.
       
    43             UnsafeFieldAccessorImpl.unsafe.ensureClassInitialized(field.getDeclaringClass());
       
    44 
       
    45             if (!isQualified) {
       
    46                 if (type == Boolean.TYPE) {
       
    47                     return new UnsafeStaticBooleanFieldAccessorImpl(field);
       
    48                 } else if (type == Byte.TYPE) {
       
    49                     return new UnsafeStaticByteFieldAccessorImpl(field);
       
    50                 } else if (type == Short.TYPE) {
       
    51                     return new UnsafeStaticShortFieldAccessorImpl(field);
       
    52                 } else if (type == Character.TYPE) {
       
    53                     return new UnsafeStaticCharacterFieldAccessorImpl(field);
       
    54                 } else if (type == Integer.TYPE) {
       
    55                     return new UnsafeStaticIntegerFieldAccessorImpl(field);
       
    56                 } else if (type == Long.TYPE) {
       
    57                     return new UnsafeStaticLongFieldAccessorImpl(field);
       
    58                 } else if (type == Float.TYPE) {
       
    59                     return new UnsafeStaticFloatFieldAccessorImpl(field);
       
    60                 } else if (type == Double.TYPE) {
       
    61                     return new UnsafeStaticDoubleFieldAccessorImpl(field);
       
    62                 } else {
       
    63                     return new UnsafeStaticObjectFieldAccessorImpl(field);
       
    64                 }
       
    65             } else {
       
    66                 if (type == Boolean.TYPE) {
       
    67                     return new UnsafeQualifiedStaticBooleanFieldAccessorImpl(field, isReadOnly);
       
    68                 } else if (type == Byte.TYPE) {
       
    69                     return new UnsafeQualifiedStaticByteFieldAccessorImpl(field, isReadOnly);
       
    70                 } else if (type == Short.TYPE) {
       
    71                     return new UnsafeQualifiedStaticShortFieldAccessorImpl(field, isReadOnly);
       
    72                 } else if (type == Character.TYPE) {
       
    73                     return new UnsafeQualifiedStaticCharacterFieldAccessorImpl(field, isReadOnly);
       
    74                 } else if (type == Integer.TYPE) {
       
    75                     return new UnsafeQualifiedStaticIntegerFieldAccessorImpl(field, isReadOnly);
       
    76                 } else if (type == Long.TYPE) {
       
    77                     return new UnsafeQualifiedStaticLongFieldAccessorImpl(field, isReadOnly);
       
    78                 } else if (type == Float.TYPE) {
       
    79                     return new UnsafeQualifiedStaticFloatFieldAccessorImpl(field, isReadOnly);
       
    80                 } else if (type == Double.TYPE) {
       
    81                     return new UnsafeQualifiedStaticDoubleFieldAccessorImpl(field, isReadOnly);
       
    82                 } else {
       
    83                     return new UnsafeQualifiedStaticObjectFieldAccessorImpl(field, isReadOnly);
       
    84                 }
       
    85             }
       
    86         } else {
       
    87             if (!isQualified) {
       
    88                 if (type == Boolean.TYPE) {
       
    89                     return new UnsafeBooleanFieldAccessorImpl(field);
       
    90                 } else if (type == Byte.TYPE) {
       
    91                     return new UnsafeByteFieldAccessorImpl(field);
       
    92                 } else if (type == Short.TYPE) {
       
    93                     return new UnsafeShortFieldAccessorImpl(field);
       
    94                 } else if (type == Character.TYPE) {
       
    95                     return new UnsafeCharacterFieldAccessorImpl(field);
       
    96                 } else if (type == Integer.TYPE) {
       
    97                     return new UnsafeIntegerFieldAccessorImpl(field);
       
    98                 } else if (type == Long.TYPE) {
       
    99                     return new UnsafeLongFieldAccessorImpl(field);
       
   100                 } else if (type == Float.TYPE) {
       
   101                     return new UnsafeFloatFieldAccessorImpl(field);
       
   102                 } else if (type == Double.TYPE) {
       
   103                     return new UnsafeDoubleFieldAccessorImpl(field);
       
   104                 } else {
       
   105                     return new UnsafeObjectFieldAccessorImpl(field);
       
   106                 }
       
   107             } else {
       
   108                 if (type == Boolean.TYPE) {
       
   109                     return new UnsafeQualifiedBooleanFieldAccessorImpl(field, isReadOnly);
       
   110                 } else if (type == Byte.TYPE) {
       
   111                     return new UnsafeQualifiedByteFieldAccessorImpl(field, isReadOnly);
       
   112                 } else if (type == Short.TYPE) {
       
   113                     return new UnsafeQualifiedShortFieldAccessorImpl(field, isReadOnly);
       
   114                 } else if (type == Character.TYPE) {
       
   115                     return new UnsafeQualifiedCharacterFieldAccessorImpl(field, isReadOnly);
       
   116                 } else if (type == Integer.TYPE) {
       
   117                     return new UnsafeQualifiedIntegerFieldAccessorImpl(field, isReadOnly);
       
   118                 } else if (type == Long.TYPE) {
       
   119                     return new UnsafeQualifiedLongFieldAccessorImpl(field, isReadOnly);
       
   120                 } else if (type == Float.TYPE) {
       
   121                     return new UnsafeQualifiedFloatFieldAccessorImpl(field, isReadOnly);
       
   122                 } else if (type == Double.TYPE) {
       
   123                     return new UnsafeQualifiedDoubleFieldAccessorImpl(field, isReadOnly);
       
   124                 } else {
       
   125                     return new UnsafeQualifiedObjectFieldAccessorImpl(field, isReadOnly);
       
   126                 }
       
   127             }
       
   128         }
       
   129     }
       
   130 }