Java Code Examples for java.lang.reflect.Modifier#isVolatile()

The following examples show how to use java.lang.reflect.Modifier#isVolatile() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JavassistProxyFactory.java    From joyrpc with Apache License 2.0 6 votes vote down vote up
/**
 * 添加修饰符
 *
 * @param mod     修饰符变量
 * @param builder 字符串构建器
 * @return 字符串构建器
 */
protected StringBuilder modifier(final int mod, final StringBuilder builder) {
    if (Modifier.isPublic(mod)) {
        builder.append("public");
    } else if (Modifier.isProtected(mod)) {
        builder.append("protected");
    } else if (Modifier.isPrivate(mod)) {
        builder.append("private");
    }
    if (Modifier.isStatic(mod)) {
        builder.append(" static");
    }
    if (Modifier.isVolatile(mod)) {
        builder.append(" volatile");
    }

    return builder;
}
 
Example 2
Source File: AtomicLongFieldUpdater.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
LockedUpdater(final Class<T> tclass, final String fieldName,
              final Class<?> caller) {
    Field field = null;
    int modifiers = 0;
    try {
        field = AccessController.doPrivileged(
            new PrivilegedExceptionAction<Field>() {
                public Field run() throws NoSuchFieldException {
                    return tclass.getDeclaredField(fieldName);
                }
            });
        modifiers = field.getModifiers();
        sun.reflect.misc.ReflectUtil.ensureMemberAccess(
            caller, tclass, null, modifiers);
        ClassLoader cl = tclass.getClassLoader();
        ClassLoader ccl = caller.getClassLoader();
        if ((ccl != null) && (ccl != cl) &&
            ((cl == null) || !isAncestor(cl, ccl))) {
          sun.reflect.misc.ReflectUtil.checkPackageAccess(tclass);
        }
    } catch (PrivilegedActionException pae) {
        throw new RuntimeException(pae.getException());
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }

    Class<?> fieldt = field.getType();
    if (fieldt != long.class)
        throw new IllegalArgumentException("Must be long type");

    if (!Modifier.isVolatile(modifiers))
        throw new IllegalArgumentException("Must be volatile type");

    this.cclass = (Modifier.isProtected(modifiers) &&
                   caller != tclass) ? caller : null;
    this.tclass = tclass;
    offset = unsafe.objectFieldOffset(field);
}
 
Example 3
Source File: UnsafeAtomicLongFieldUpdater.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
UnsafeAtomicLongFieldUpdater(Unsafe unsafe, Class<?> tClass, String fieldName) throws NoSuchFieldException {
    Field field = tClass.getDeclaredField(fieldName);
    if (!Modifier.isVolatile(field.getModifiers())) {
        throw new IllegalArgumentException("Must be volatile");
    }
    this.unsafe = unsafe;
    offset = unsafe.objectFieldOffset(field);
}
 
Example 4
Source File: AtomicLongFieldUpdater.java    From j2objc with Apache License 2.0 5 votes vote down vote up
CASUpdater(final Class<T> tclass, final String fieldName,
           final Class<?> caller) {
    final Field field;
    final int modifiers;
    try {
        field = tclass.getDeclaredField(fieldName); // android-changed
        modifiers = field.getModifiers();
        // BEGIN Android-removed
        // sun.reflect.misc.ReflectUtil.ensureMemberAccess(
        //     caller, tclass, null, modifiers);
        // ClassLoader cl = tclass.getClassLoader();
        // ClassLoader ccl = caller.getClassLoader();
        // if ((ccl != null) && (ccl != cl) &&
        //     ((cl == null) || !isAncestor(cl, ccl))) {
        //     sun.reflect.misc.ReflectUtil.checkPackageAccess(tclass);
        // }
        // END Android-removed
    // BEGIN Android-removed
    // } catch (PrivilegedActionException pae) {
    //     throw new RuntimeException(pae.getException());
    // END Android-removed
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }

    if (field.getType() != long.class)
        throw new IllegalArgumentException("Must be long type");

    if (!Modifier.isVolatile(modifiers))
        throw new IllegalArgumentException("Must be volatile type");

    this.cclass = (Modifier.isProtected(modifiers)) ? caller : tclass;
    this.tclass = tclass;
    this.offset = U.objectFieldOffset(field);
}
 
Example 5
Source File: UnsafeAtomicIntegerFieldUpdater.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
UnsafeAtomicIntegerFieldUpdater(Unsafe unsafe, Class<?> tClass, String fieldName) throws NoSuchFieldException {
    Field field = tClass.getDeclaredField(fieldName);
    if (!Modifier.isVolatile(field.getModifiers())) {
        throw new IllegalArgumentException("Must be volatile");
    }
    this.unsafe = unsafe;
    offset = unsafe.objectFieldOffset(field);
}
 
Example 6
Source File: AtomicIntegerFieldUpdater.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
AtomicIntegerFieldUpdaterImpl(final Class<T> tclass,
                              final String fieldName,
                              final Class<?> caller) {
    final Field field;
    final int modifiers;
    try {
        field = AccessController.doPrivileged(
            new PrivilegedExceptionAction<Field>() {
                public Field run() throws NoSuchFieldException {
                    return tclass.getDeclaredField(fieldName);
                }
            });
        modifiers = field.getModifiers();
        sun.reflect.misc.ReflectUtil.ensureMemberAccess(
            caller, tclass, null, modifiers);
        ClassLoader cl = tclass.getClassLoader();
        ClassLoader ccl = caller.getClassLoader();
        if ((ccl != null) && (ccl != cl) &&
            ((cl == null) || !isAncestor(cl, ccl))) {
          sun.reflect.misc.ReflectUtil.checkPackageAccess(tclass);
        }
    } catch (PrivilegedActionException pae) {
        throw new RuntimeException(pae.getException());
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }

    Class<?> fieldt = field.getType();
    if (fieldt != int.class)
        throw new IllegalArgumentException("Must be integer type");

    if (!Modifier.isVolatile(modifiers))
        throw new IllegalArgumentException("Must be volatile type");

    this.cclass = (Modifier.isProtected(modifiers) &&
                   caller != tclass) ? caller : null;
    this.tclass = tclass;
    offset = unsafe.objectFieldOffset(field);
}
 
Example 7
Source File: AtomicLongFieldUpdater.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
CASUpdater(final Class<T> tclass, final String fieldName,
           final Class<?> caller) {
    final Field field;
    final int modifiers;
    try {
        field = AccessController.doPrivileged(
            new PrivilegedExceptionAction<Field>() {
                public Field run() throws NoSuchFieldException {
                    return tclass.getDeclaredField(fieldName);
                }
            });
        modifiers = field.getModifiers();
        sun.reflect.misc.ReflectUtil.ensureMemberAccess(
            caller, tclass, null, modifiers);
        ClassLoader cl = tclass.getClassLoader();
        ClassLoader ccl = caller.getClassLoader();
        if ((ccl != null) && (ccl != cl) &&
            ((cl == null) || !isAncestor(cl, ccl))) {
          sun.reflect.misc.ReflectUtil.checkPackageAccess(tclass);
        }
    } catch (PrivilegedActionException pae) {
        throw new RuntimeException(pae.getException());
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }

    Class<?> fieldt = field.getType();
    if (fieldt != long.class)
        throw new IllegalArgumentException("Must be long type");

    if (!Modifier.isVolatile(modifiers))
        throw new IllegalArgumentException("Must be volatile type");

    this.cclass = (Modifier.isProtected(modifiers) &&
                   caller != tclass) ? caller : null;
    this.tclass = tclass;
    offset = unsafe.objectFieldOffset(field);
}
 
Example 8
Source File: FieldDocImpl.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Return true if this field is volatile
 */
public boolean isVolatile() {
    return Modifier.isVolatile(getModifiers());
}
 
Example 9
Source File: AtomicReferenceFieldUpdater.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
AtomicReferenceFieldUpdaterImpl(final Class<T> tclass,
                                final Class<V> vclass,
                                final String fieldName,
                                final Class<?> caller) {
    final Field field;
    final Class<?> fieldClass;
    final int modifiers;
    try {
        field = AccessController.doPrivileged(
            new PrivilegedExceptionAction<Field>() {
                public Field run() throws NoSuchFieldException {
                    return tclass.getDeclaredField(fieldName);
                }
            });
        modifiers = field.getModifiers();
        sun.reflect.misc.ReflectUtil.ensureMemberAccess(
            caller, tclass, null, modifiers);
        ClassLoader cl = tclass.getClassLoader();
        ClassLoader ccl = caller.getClassLoader();
        if ((ccl != null) && (ccl != cl) &&
            ((cl == null) || !isAncestor(cl, ccl))) {
            sun.reflect.misc.ReflectUtil.checkPackageAccess(tclass);
        }
        fieldClass = field.getType();
    } catch (PrivilegedActionException pae) {
        throw new RuntimeException(pae.getException());
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }

    if (vclass != fieldClass)
        throw new ClassCastException();
    if (vclass.isPrimitive())
        throw new IllegalArgumentException("Must be reference type");

    if (!Modifier.isVolatile(modifiers))
        throw new IllegalArgumentException("Must be volatile type");

    // Access to protected field members is restricted to receivers only
    // of the accessing class, or one of its subclasses, and the
    // accessing class must in turn be a subclass (or package sibling)
    // of the protected member's defining class.
    // If the updater refers to a protected field of a declaring class
    // outside the current package, the receiver argument will be
    // narrowed to the type of the accessing class.
    this.cclass = (Modifier.isProtected(modifiers) &&
                   tclass.isAssignableFrom(caller) &&
                   !isSamePackage(tclass, caller))
                  ? caller : tclass;
    this.tclass = tclass;
    this.vclass = vclass;
    this.offset = U.objectFieldOffset(field);
}
 
Example 10
Source File: ModifiersProvider.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * @see Modifier#isVolatile(int)
 */
default boolean isVolatile() {
    return Modifier.isVolatile(getModifiers());
}
 
Example 11
Source File: AtomicReferenceFieldUpdater.java    From Java8CN with Apache License 2.0 4 votes vote down vote up
AtomicReferenceFieldUpdaterImpl(final Class<T> tclass,
                                final Class<V> vclass,
                                final String fieldName,
                                final Class<?> caller) {
    final Field field;
    final Class<?> fieldClass;
    final int modifiers;
    try {
        field = AccessController.doPrivileged(
            new PrivilegedExceptionAction<Field>() {
                public Field run() throws NoSuchFieldException {
                    return tclass.getDeclaredField(fieldName);
                }
            });
        modifiers = field.getModifiers();
        sun.reflect.misc.ReflectUtil.ensureMemberAccess(
            caller, tclass, null, modifiers);
        ClassLoader cl = tclass.getClassLoader();
        ClassLoader ccl = caller.getClassLoader();
        if ((ccl != null) && (ccl != cl) &&
            ((cl == null) || !isAncestor(cl, ccl))) {
          sun.reflect.misc.ReflectUtil.checkPackageAccess(tclass);
        }
        fieldClass = field.getType();
    } catch (PrivilegedActionException pae) {
        throw new RuntimeException(pae.getException());
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }

    if (vclass != fieldClass)
        throw new ClassCastException();
    if (vclass.isPrimitive())
        throw new IllegalArgumentException("Must be reference type");

    if (!Modifier.isVolatile(modifiers))
        throw new IllegalArgumentException("Must be volatile type");

    this.cclass = (Modifier.isProtected(modifiers) &&
                   caller != tclass) ? caller : null;
    this.tclass = tclass;
    if (vclass == Object.class)
        this.vclass = null;
    else
        this.vclass = vclass;
    offset = unsafe.objectFieldOffset(field);
}
 
Example 12
Source File: UnsafeFieldAccessorFactory.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
static FieldAccessor newFieldAccessor(Field field, boolean override) {
    Class<?> type = field.getType();
    boolean isStatic = Modifier.isStatic(field.getModifiers());
    boolean isFinal = Modifier.isFinal(field.getModifiers());
    boolean isVolatile = Modifier.isVolatile(field.getModifiers());
    boolean isQualified = isFinal || isVolatile;
    boolean isReadOnly = isFinal && (isStatic || !override);
    if (isStatic) {
        // This code path does not guarantee that the field's
        // declaring class has been initialized, but it must be
        // before performing reflective operations.
        UnsafeFieldAccessorImpl.unsafe.ensureClassInitialized(field.getDeclaringClass());

        if (!isQualified) {
            if (type == Boolean.TYPE) {
                return new UnsafeStaticBooleanFieldAccessorImpl(field);
            } else if (type == Byte.TYPE) {
                return new UnsafeStaticByteFieldAccessorImpl(field);
            } else if (type == Short.TYPE) {
                return new UnsafeStaticShortFieldAccessorImpl(field);
            } else if (type == Character.TYPE) {
                return new UnsafeStaticCharacterFieldAccessorImpl(field);
            } else if (type == Integer.TYPE) {
                return new UnsafeStaticIntegerFieldAccessorImpl(field);
            } else if (type == Long.TYPE) {
                return new UnsafeStaticLongFieldAccessorImpl(field);
            } else if (type == Float.TYPE) {
                return new UnsafeStaticFloatFieldAccessorImpl(field);
            } else if (type == Double.TYPE) {
                return new UnsafeStaticDoubleFieldAccessorImpl(field);
            } else {
                return new UnsafeStaticObjectFieldAccessorImpl(field);
            }
        } else {
            if (type == Boolean.TYPE) {
                return new UnsafeQualifiedStaticBooleanFieldAccessorImpl(field, isReadOnly);
            } else if (type == Byte.TYPE) {
                return new UnsafeQualifiedStaticByteFieldAccessorImpl(field, isReadOnly);
            } else if (type == Short.TYPE) {
                return new UnsafeQualifiedStaticShortFieldAccessorImpl(field, isReadOnly);
            } else if (type == Character.TYPE) {
                return new UnsafeQualifiedStaticCharacterFieldAccessorImpl(field, isReadOnly);
            } else if (type == Integer.TYPE) {
                return new UnsafeQualifiedStaticIntegerFieldAccessorImpl(field, isReadOnly);
            } else if (type == Long.TYPE) {
                return new UnsafeQualifiedStaticLongFieldAccessorImpl(field, isReadOnly);
            } else if (type == Float.TYPE) {
                return new UnsafeQualifiedStaticFloatFieldAccessorImpl(field, isReadOnly);
            } else if (type == Double.TYPE) {
                return new UnsafeQualifiedStaticDoubleFieldAccessorImpl(field, isReadOnly);
            } else {
                return new UnsafeQualifiedStaticObjectFieldAccessorImpl(field, isReadOnly);
            }
        }
    } else {
        if (!isQualified) {
            if (type == Boolean.TYPE) {
                return new UnsafeBooleanFieldAccessorImpl(field);
            } else if (type == Byte.TYPE) {
                return new UnsafeByteFieldAccessorImpl(field);
            } else if (type == Short.TYPE) {
                return new UnsafeShortFieldAccessorImpl(field);
            } else if (type == Character.TYPE) {
                return new UnsafeCharacterFieldAccessorImpl(field);
            } else if (type == Integer.TYPE) {
                return new UnsafeIntegerFieldAccessorImpl(field);
            } else if (type == Long.TYPE) {
                return new UnsafeLongFieldAccessorImpl(field);
            } else if (type == Float.TYPE) {
                return new UnsafeFloatFieldAccessorImpl(field);
            } else if (type == Double.TYPE) {
                return new UnsafeDoubleFieldAccessorImpl(field);
            } else {
                return new UnsafeObjectFieldAccessorImpl(field);
            }
        } else {
            if (type == Boolean.TYPE) {
                return new UnsafeQualifiedBooleanFieldAccessorImpl(field, isReadOnly);
            } else if (type == Byte.TYPE) {
                return new UnsafeQualifiedByteFieldAccessorImpl(field, isReadOnly);
            } else if (type == Short.TYPE) {
                return new UnsafeQualifiedShortFieldAccessorImpl(field, isReadOnly);
            } else if (type == Character.TYPE) {
                return new UnsafeQualifiedCharacterFieldAccessorImpl(field, isReadOnly);
            } else if (type == Integer.TYPE) {
                return new UnsafeQualifiedIntegerFieldAccessorImpl(field, isReadOnly);
            } else if (type == Long.TYPE) {
                return new UnsafeQualifiedLongFieldAccessorImpl(field, isReadOnly);
            } else if (type == Float.TYPE) {
                return new UnsafeQualifiedFloatFieldAccessorImpl(field, isReadOnly);
            } else if (type == Double.TYPE) {
                return new UnsafeQualifiedDoubleFieldAccessorImpl(field, isReadOnly);
            } else {
                return new UnsafeQualifiedObjectFieldAccessorImpl(field, isReadOnly);
            }
        }
    }
}
 
Example 13
Source File: AtomicLongFieldUpdater.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
CASUpdater(final Class<T> tclass, final String fieldName,
           final Class<?> caller) {
    final Field field;
    final int modifiers;
    try {
        field = AccessController.doPrivileged(
            new PrivilegedExceptionAction<Field>() {
                public Field run() throws NoSuchFieldException {
                    return tclass.getDeclaredField(fieldName);
                }
            });
        modifiers = field.getModifiers();
        sun.reflect.misc.ReflectUtil.ensureMemberAccess(
            caller, tclass, null, modifiers);
        ClassLoader cl = tclass.getClassLoader();
        ClassLoader ccl = caller.getClassLoader();
        if ((ccl != null) && (ccl != cl) &&
            ((cl == null) || !isAncestor(cl, ccl))) {
            sun.reflect.misc.ReflectUtil.checkPackageAccess(tclass);
        }
    } catch (PrivilegedActionException pae) {
        throw new RuntimeException(pae.getException());
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }

    if (field.getType() != long.class)
        throw new IllegalArgumentException("Must be long type");

    if (!Modifier.isVolatile(modifiers))
        throw new IllegalArgumentException("Must be volatile type");

    // Access to protected field members is restricted to receivers only
    // of the accessing class, or one of its subclasses, and the
    // accessing class must in turn be a subclass (or package sibling)
    // of the protected member's defining class.
    // If the updater refers to a protected field of a declaring class
    // outside the current package, the receiver argument will be
    // narrowed to the type of the accessing class.
    this.cclass = (Modifier.isProtected(modifiers) &&
                   tclass.isAssignableFrom(caller) &&
                   !isSamePackage(tclass, caller))
                  ? caller : tclass;
    this.tclass = tclass;
    this.offset = U.objectFieldOffset(field);
}
 
Example 14
Source File: AtomicIntegerFieldUpdater.java    From JDKSourceCode1.8 with MIT License 4 votes vote down vote up
AtomicIntegerFieldUpdaterImpl(final Class<T> tclass,
                              final String fieldName,
                              final Class<?> caller) {
    final Field field;
    final int modifiers;
    try {
        field = AccessController.doPrivileged(
            new PrivilegedExceptionAction<Field>() {
                public Field run() throws NoSuchFieldException {
                    return tclass.getDeclaredField(fieldName);
                }
            });
        modifiers = field.getModifiers();
        sun.reflect.misc.ReflectUtil.ensureMemberAccess(
            caller, tclass, null, modifiers);
        ClassLoader cl = tclass.getClassLoader();
        ClassLoader ccl = caller.getClassLoader();
        if ((ccl != null) && (ccl != cl) &&
            ((cl == null) || !isAncestor(cl, ccl))) {
            sun.reflect.misc.ReflectUtil.checkPackageAccess(tclass);
        }
    } catch (PrivilegedActionException pae) {
        throw new RuntimeException(pae.getException());
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }

    if (field.getType() != int.class)
        throw new IllegalArgumentException("Must be integer type");

    if (!Modifier.isVolatile(modifiers))
        throw new IllegalArgumentException("Must be volatile type");

    // Access to protected field members is restricted to receivers only
    // of the accessing class, or one of its subclasses, and the
    // accessing class must in turn be a subclass (or package sibling)
    // of the protected member's defining class.
    // If the updater refers to a protected field of a declaring class
    // outside the current package, the receiver argument will be
    // narrowed to the type of the accessing class.
    this.cclass = (Modifier.isProtected(modifiers) &&
                   tclass.isAssignableFrom(caller) &&
                   !isSamePackage(tclass, caller))
                  ? caller : tclass;
    this.tclass = tclass;
    this.offset = U.objectFieldOffset(field);
}
 
Example 15
Source File: UnsafeFieldAccessorFactory.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
static FieldAccessor newFieldAccessor(Field field, boolean override) {
    Class<?> type = field.getType();
    boolean isStatic = Modifier.isStatic(field.getModifiers());
    boolean isFinal = Modifier.isFinal(field.getModifiers());
    boolean isVolatile = Modifier.isVolatile(field.getModifiers());
    boolean isQualified = isFinal || isVolatile;
    boolean isReadOnly = isFinal && (isStatic || !override);
    if (isStatic) {
        // This code path does not guarantee that the field's
        // declaring class has been initialized, but it must be
        // before performing reflective operations.
        UnsafeFieldAccessorImpl.unsafe.ensureClassInitialized(field.getDeclaringClass());

        if (!isQualified) {
            if (type == Boolean.TYPE) {
                return new UnsafeStaticBooleanFieldAccessorImpl(field);
            } else if (type == Byte.TYPE) {
                return new UnsafeStaticByteFieldAccessorImpl(field);
            } else if (type == Short.TYPE) {
                return new UnsafeStaticShortFieldAccessorImpl(field);
            } else if (type == Character.TYPE) {
                return new UnsafeStaticCharacterFieldAccessorImpl(field);
            } else if (type == Integer.TYPE) {
                return new UnsafeStaticIntegerFieldAccessorImpl(field);
            } else if (type == Long.TYPE) {
                return new UnsafeStaticLongFieldAccessorImpl(field);
            } else if (type == Float.TYPE) {
                return new UnsafeStaticFloatFieldAccessorImpl(field);
            } else if (type == Double.TYPE) {
                return new UnsafeStaticDoubleFieldAccessorImpl(field);
            } else {
                return new UnsafeStaticObjectFieldAccessorImpl(field);
            }
        } else {
            if (type == Boolean.TYPE) {
                return new UnsafeQualifiedStaticBooleanFieldAccessorImpl(field, isReadOnly);
            } else if (type == Byte.TYPE) {
                return new UnsafeQualifiedStaticByteFieldAccessorImpl(field, isReadOnly);
            } else if (type == Short.TYPE) {
                return new UnsafeQualifiedStaticShortFieldAccessorImpl(field, isReadOnly);
            } else if (type == Character.TYPE) {
                return new UnsafeQualifiedStaticCharacterFieldAccessorImpl(field, isReadOnly);
            } else if (type == Integer.TYPE) {
                return new UnsafeQualifiedStaticIntegerFieldAccessorImpl(field, isReadOnly);
            } else if (type == Long.TYPE) {
                return new UnsafeQualifiedStaticLongFieldAccessorImpl(field, isReadOnly);
            } else if (type == Float.TYPE) {
                return new UnsafeQualifiedStaticFloatFieldAccessorImpl(field, isReadOnly);
            } else if (type == Double.TYPE) {
                return new UnsafeQualifiedStaticDoubleFieldAccessorImpl(field, isReadOnly);
            } else {
                return new UnsafeQualifiedStaticObjectFieldAccessorImpl(field, isReadOnly);
            }
        }
    } else {
        if (!isQualified) {
            if (type == Boolean.TYPE) {
                return new UnsafeBooleanFieldAccessorImpl(field);
            } else if (type == Byte.TYPE) {
                return new UnsafeByteFieldAccessorImpl(field);
            } else if (type == Short.TYPE) {
                return new UnsafeShortFieldAccessorImpl(field);
            } else if (type == Character.TYPE) {
                return new UnsafeCharacterFieldAccessorImpl(field);
            } else if (type == Integer.TYPE) {
                return new UnsafeIntegerFieldAccessorImpl(field);
            } else if (type == Long.TYPE) {
                return new UnsafeLongFieldAccessorImpl(field);
            } else if (type == Float.TYPE) {
                return new UnsafeFloatFieldAccessorImpl(field);
            } else if (type == Double.TYPE) {
                return new UnsafeDoubleFieldAccessorImpl(field);
            } else {
                return new UnsafeObjectFieldAccessorImpl(field);
            }
        } else {
            if (type == Boolean.TYPE) {
                return new UnsafeQualifiedBooleanFieldAccessorImpl(field, isReadOnly);
            } else if (type == Byte.TYPE) {
                return new UnsafeQualifiedByteFieldAccessorImpl(field, isReadOnly);
            } else if (type == Short.TYPE) {
                return new UnsafeQualifiedShortFieldAccessorImpl(field, isReadOnly);
            } else if (type == Character.TYPE) {
                return new UnsafeQualifiedCharacterFieldAccessorImpl(field, isReadOnly);
            } else if (type == Integer.TYPE) {
                return new UnsafeQualifiedIntegerFieldAccessorImpl(field, isReadOnly);
            } else if (type == Long.TYPE) {
                return new UnsafeQualifiedLongFieldAccessorImpl(field, isReadOnly);
            } else if (type == Float.TYPE) {
                return new UnsafeQualifiedFloatFieldAccessorImpl(field, isReadOnly);
            } else if (type == Double.TYPE) {
                return new UnsafeQualifiedDoubleFieldAccessorImpl(field, isReadOnly);
            } else {
                return new UnsafeQualifiedObjectFieldAccessorImpl(field, isReadOnly);
            }
        }
    }
}
 
Example 16
Source File: MemberName.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
/** Utility method to query the modifier flags of this member. */
public boolean isVolatile() {
    return Modifier.isVolatile(flags);
}
 
Example 17
Source File: AtomicReferenceFieldUpdater.java    From jdk1.8-source-analysis with Apache License 2.0 4 votes vote down vote up
AtomicReferenceFieldUpdaterImpl(final Class<T> tclass,
                                final Class<V> vclass,
                                final String fieldName,
                                final Class<?> caller) {
    final Field field;
    final Class<?> fieldClass;
    final int modifiers;
    try {
        field = AccessController.doPrivileged(
            new PrivilegedExceptionAction<Field>() {
                public Field run() throws NoSuchFieldException {
                    return tclass.getDeclaredField(fieldName);
                }
            });
        modifiers = field.getModifiers();
        sun.reflect.misc.ReflectUtil.ensureMemberAccess(
            caller, tclass, null, modifiers);
        ClassLoader cl = tclass.getClassLoader();
        ClassLoader ccl = caller.getClassLoader();
        if ((ccl != null) && (ccl != cl) &&
            ((cl == null) || !isAncestor(cl, ccl))) {
            sun.reflect.misc.ReflectUtil.checkPackageAccess(tclass);
        }
        fieldClass = field.getType();
    } catch (PrivilegedActionException pae) {
        throw new RuntimeException(pae.getException());
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }

    if (vclass != fieldClass)
        throw new ClassCastException();
    if (vclass.isPrimitive())
        throw new IllegalArgumentException("Must be reference type");

    if (!Modifier.isVolatile(modifiers))
        throw new IllegalArgumentException("Must be volatile type");

    // Access to protected field members is restricted to receivers only
    // of the accessing class, or one of its subclasses, and the
    // accessing class must in turn be a subclass (or package sibling)
    // of the protected member's defining class.
    // If the updater refers to a protected field of a declaring class
    // outside the current package, the receiver argument will be
    // narrowed to the type of the accessing class.
    this.cclass = (Modifier.isProtected(modifiers) &&
                   tclass.isAssignableFrom(caller) &&
                   !isSamePackage(tclass, caller))
                  ? caller : tclass;
    this.tclass = tclass;
    this.vclass = vclass;
    this.offset = U.objectFieldOffset(field);
}
 
Example 18
Source File: FHIRSwaggerGenerator.java    From FHIR with Apache License 2.0 4 votes vote down vote up
private static void generateDefinition(Class<?> modelClass, JsonObjectBuilder definitions) throws Exception {
        if (!ModelSupport.isPrimitiveType(modelClass)) {
            JsonObjectBuilder definition = factory.createObjectBuilder();
            JsonObjectBuilder properties = factory.createObjectBuilder();
            JsonArrayBuilder required = factory.createArrayBuilder();

            StructureDefinition structureDefinition = getStructureDefinition(modelClass);

            if (structureDefinition == null) {
                System.err.println("Failed generateDefinition for: " + modelClass.getName());
                return;
            }

            if (Resource.class.isAssignableFrom(modelClass)) {
                // add the 'resourceType' property
                JsonObjectBuilder property = factory.createObjectBuilder();

                // Convert all the primitive types to json types.
                property.add("type", "string");
                if (Resource.class == modelClass) {
                    // TODO: when a filter was passed, limit this to just the resource types included in the filter
                    List<String> typeNames = Arrays.stream(ResourceType.ValueSet.values()).map(ResourceType.ValueSet::value).collect(Collectors.toList());
                    JsonArrayBuilder enumValues = factory.createArrayBuilder(typeNames);
                    property.add("enum", enumValues);
                    properties.add("resourceType", property.build());
                    required.add("resourceType");
                } else {
                    // TODO how to "overwrite" the Resource definition and say that the value is fixed?
                    // https://github.com/OAI/OpenAPI-Specification/issues/1313
//                    property.add("enum", modelClass.getSimpleName());
                }
            }

            for (Field field : modelClass.getDeclaredFields()) {
                if (!Modifier.isStatic(field.getModifiers()) && !Modifier.isVolatile(field.getModifiers())) {
                    if (!ModelSupport.isChoiceElement(modelClass, ModelSupport.getElementName(field)) && field.isAnnotationPresent(Required.class)) {
                        required.add(ModelSupport.getElementName(field));
                    }
                    generateProperties(structureDefinition, modelClass, field, properties);
                }
            }

            JsonArray requiredArray = required.build();

            Class<?> superClass = modelClass.getSuperclass();
            if (superClass != null
                    && superClass.getPackage().getName().startsWith("com.ibm.fhir.model")
                    && !superClass.equals(AbstractVisitable.class)) {
                JsonArrayBuilder allOf = factory.createArrayBuilder();

                JsonObjectBuilder ref = factory.createObjectBuilder();
                ref.add("$ref", "#/definitions/" + superClass.getSimpleName());
                allOf.add(ref);

                JsonObjectBuilder wrapper = factory.createObjectBuilder();
                wrapper.add("type", "object");
                wrapper.add("properties", properties);
                if (!requiredArray.isEmpty()) {
                    wrapper.add("required", requiredArray);
                }
                allOf.add(wrapper);

                definition.add("allOf", allOf);
            } else {
                definition.add("type", "object");
                if (Resource.class.equals(modelClass)) {
                    definition.add("discriminator", "resourceType");
                }
                definition.add("properties", properties);
                if (!requiredArray.isEmpty()) {
                    definition.add("required", requiredArray);
                }
            }

            if (Resource.class.isAssignableFrom(modelClass)) {
                FHIROpenApiGenerator.addExamples(modelClass, definition);
            }

            definitions.add(getSimpleNameWithEnclosingNames(modelClass), definition);
        }
    }
 
Example 19
Source File: UnsafeFieldAccessorFactory.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
static FieldAccessor newFieldAccessor(Field field, boolean override) {
    Class<?> type = field.getType();
    boolean isStatic = Modifier.isStatic(field.getModifiers());
    boolean isFinal = Modifier.isFinal(field.getModifiers());
    boolean isVolatile = Modifier.isVolatile(field.getModifiers());
    boolean isQualified = isFinal || isVolatile;
    boolean isReadOnly = isFinal && (isStatic || !override);
    if (isStatic) {
        // This code path does not guarantee that the field's
        // declaring class has been initialized, but it must be
        // before performing reflective operations.
        UnsafeFieldAccessorImpl.unsafe.ensureClassInitialized(field.getDeclaringClass());

        if (!isQualified) {
            if (type == Boolean.TYPE) {
                return new UnsafeStaticBooleanFieldAccessorImpl(field);
            } else if (type == Byte.TYPE) {
                return new UnsafeStaticByteFieldAccessorImpl(field);
            } else if (type == Short.TYPE) {
                return new UnsafeStaticShortFieldAccessorImpl(field);
            } else if (type == Character.TYPE) {
                return new UnsafeStaticCharacterFieldAccessorImpl(field);
            } else if (type == Integer.TYPE) {
                return new UnsafeStaticIntegerFieldAccessorImpl(field);
            } else if (type == Long.TYPE) {
                return new UnsafeStaticLongFieldAccessorImpl(field);
            } else if (type == Float.TYPE) {
                return new UnsafeStaticFloatFieldAccessorImpl(field);
            } else if (type == Double.TYPE) {
                return new UnsafeStaticDoubleFieldAccessorImpl(field);
            } else {
                return new UnsafeStaticObjectFieldAccessorImpl(field);
            }
        } else {
            if (type == Boolean.TYPE) {
                return new UnsafeQualifiedStaticBooleanFieldAccessorImpl(field, isReadOnly);
            } else if (type == Byte.TYPE) {
                return new UnsafeQualifiedStaticByteFieldAccessorImpl(field, isReadOnly);
            } else if (type == Short.TYPE) {
                return new UnsafeQualifiedStaticShortFieldAccessorImpl(field, isReadOnly);
            } else if (type == Character.TYPE) {
                return new UnsafeQualifiedStaticCharacterFieldAccessorImpl(field, isReadOnly);
            } else if (type == Integer.TYPE) {
                return new UnsafeQualifiedStaticIntegerFieldAccessorImpl(field, isReadOnly);
            } else if (type == Long.TYPE) {
                return new UnsafeQualifiedStaticLongFieldAccessorImpl(field, isReadOnly);
            } else if (type == Float.TYPE) {
                return new UnsafeQualifiedStaticFloatFieldAccessorImpl(field, isReadOnly);
            } else if (type == Double.TYPE) {
                return new UnsafeQualifiedStaticDoubleFieldAccessorImpl(field, isReadOnly);
            } else {
                return new UnsafeQualifiedStaticObjectFieldAccessorImpl(field, isReadOnly);
            }
        }
    } else {
        if (!isQualified) {
            if (type == Boolean.TYPE) {
                return new UnsafeBooleanFieldAccessorImpl(field);
            } else if (type == Byte.TYPE) {
                return new UnsafeByteFieldAccessorImpl(field);
            } else if (type == Short.TYPE) {
                return new UnsafeShortFieldAccessorImpl(field);
            } else if (type == Character.TYPE) {
                return new UnsafeCharacterFieldAccessorImpl(field);
            } else if (type == Integer.TYPE) {
                return new UnsafeIntegerFieldAccessorImpl(field);
            } else if (type == Long.TYPE) {
                return new UnsafeLongFieldAccessorImpl(field);
            } else if (type == Float.TYPE) {
                return new UnsafeFloatFieldAccessorImpl(field);
            } else if (type == Double.TYPE) {
                return new UnsafeDoubleFieldAccessorImpl(field);
            } else {
                return new UnsafeObjectFieldAccessorImpl(field);
            }
        } else {
            if (type == Boolean.TYPE) {
                return new UnsafeQualifiedBooleanFieldAccessorImpl(field, isReadOnly);
            } else if (type == Byte.TYPE) {
                return new UnsafeQualifiedByteFieldAccessorImpl(field, isReadOnly);
            } else if (type == Short.TYPE) {
                return new UnsafeQualifiedShortFieldAccessorImpl(field, isReadOnly);
            } else if (type == Character.TYPE) {
                return new UnsafeQualifiedCharacterFieldAccessorImpl(field, isReadOnly);
            } else if (type == Integer.TYPE) {
                return new UnsafeQualifiedIntegerFieldAccessorImpl(field, isReadOnly);
            } else if (type == Long.TYPE) {
                return new UnsafeQualifiedLongFieldAccessorImpl(field, isReadOnly);
            } else if (type == Float.TYPE) {
                return new UnsafeQualifiedFloatFieldAccessorImpl(field, isReadOnly);
            } else if (type == Double.TYPE) {
                return new UnsafeQualifiedDoubleFieldAccessorImpl(field, isReadOnly);
            } else {
                return new UnsafeQualifiedObjectFieldAccessorImpl(field, isReadOnly);
            }
        }
    }
}
 
Example 20
Source File: UnsafeFieldAccessorFactory.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
static FieldAccessor newFieldAccessor(Field field, boolean override) {
    Class type = field.getType();
    boolean isStatic = Modifier.isStatic(field.getModifiers());
    boolean isFinal = Modifier.isFinal(field.getModifiers());
    boolean isVolatile = Modifier.isVolatile(field.getModifiers());
    boolean isQualified = isFinal || isVolatile;
    boolean isReadOnly = isFinal && (isStatic || !override);
    if (isStatic) {
        // This code path does not guarantee that the field's
        // declaring class has been initialized, but it must be
        // before performing reflective operations.
        UnsafeFieldAccessorImpl.unsafe.ensureClassInitialized(field.getDeclaringClass());

        if (!isQualified) {
            if (type == Boolean.TYPE) {
                return new UnsafeStaticBooleanFieldAccessorImpl(field);
            } else if (type == Byte.TYPE) {
                return new UnsafeStaticByteFieldAccessorImpl(field);
            } else if (type == Short.TYPE) {
                return new UnsafeStaticShortFieldAccessorImpl(field);
            } else if (type == Character.TYPE) {
                return new UnsafeStaticCharacterFieldAccessorImpl(field);
            } else if (type == Integer.TYPE) {
                return new UnsafeStaticIntegerFieldAccessorImpl(field);
            } else if (type == Long.TYPE) {
                return new UnsafeStaticLongFieldAccessorImpl(field);
            } else if (type == Float.TYPE) {
                return new UnsafeStaticFloatFieldAccessorImpl(field);
            } else if (type == Double.TYPE) {
                return new UnsafeStaticDoubleFieldAccessorImpl(field);
            } else {
                return new UnsafeStaticObjectFieldAccessorImpl(field);
            }
        } else {
            if (type == Boolean.TYPE) {
                return new UnsafeQualifiedStaticBooleanFieldAccessorImpl(field, isReadOnly);
            } else if (type == Byte.TYPE) {
                return new UnsafeQualifiedStaticByteFieldAccessorImpl(field, isReadOnly);
            } else if (type == Short.TYPE) {
                return new UnsafeQualifiedStaticShortFieldAccessorImpl(field, isReadOnly);
            } else if (type == Character.TYPE) {
                return new UnsafeQualifiedStaticCharacterFieldAccessorImpl(field, isReadOnly);
            } else if (type == Integer.TYPE) {
                return new UnsafeQualifiedStaticIntegerFieldAccessorImpl(field, isReadOnly);
            } else if (type == Long.TYPE) {
                return new UnsafeQualifiedStaticLongFieldAccessorImpl(field, isReadOnly);
            } else if (type == Float.TYPE) {
                return new UnsafeQualifiedStaticFloatFieldAccessorImpl(field, isReadOnly);
            } else if (type == Double.TYPE) {
                return new UnsafeQualifiedStaticDoubleFieldAccessorImpl(field, isReadOnly);
            } else {
                return new UnsafeQualifiedStaticObjectFieldAccessorImpl(field, isReadOnly);
            }
        }
    } else {
        if (!isQualified) {
            if (type == Boolean.TYPE) {
                return new UnsafeBooleanFieldAccessorImpl(field);
            } else if (type == Byte.TYPE) {
                return new UnsafeByteFieldAccessorImpl(field);
            } else if (type == Short.TYPE) {
                return new UnsafeShortFieldAccessorImpl(field);
            } else if (type == Character.TYPE) {
                return new UnsafeCharacterFieldAccessorImpl(field);
            } else if (type == Integer.TYPE) {
                return new UnsafeIntegerFieldAccessorImpl(field);
            } else if (type == Long.TYPE) {
                return new UnsafeLongFieldAccessorImpl(field);
            } else if (type == Float.TYPE) {
                return new UnsafeFloatFieldAccessorImpl(field);
            } else if (type == Double.TYPE) {
                return new UnsafeDoubleFieldAccessorImpl(field);
            } else {
                return new UnsafeObjectFieldAccessorImpl(field);
            }
        } else {
            if (type == Boolean.TYPE) {
                return new UnsafeQualifiedBooleanFieldAccessorImpl(field, isReadOnly);
            } else if (type == Byte.TYPE) {
                return new UnsafeQualifiedByteFieldAccessorImpl(field, isReadOnly);
            } else if (type == Short.TYPE) {
                return new UnsafeQualifiedShortFieldAccessorImpl(field, isReadOnly);
            } else if (type == Character.TYPE) {
                return new UnsafeQualifiedCharacterFieldAccessorImpl(field, isReadOnly);
            } else if (type == Integer.TYPE) {
                return new UnsafeQualifiedIntegerFieldAccessorImpl(field, isReadOnly);
            } else if (type == Long.TYPE) {
                return new UnsafeQualifiedLongFieldAccessorImpl(field, isReadOnly);
            } else if (type == Float.TYPE) {
                return new UnsafeQualifiedFloatFieldAccessorImpl(field, isReadOnly);
            } else if (type == Double.TYPE) {
                return new UnsafeQualifiedDoubleFieldAccessorImpl(field, isReadOnly);
            } else {
                return new UnsafeQualifiedObjectFieldAccessorImpl(field, isReadOnly);
            }
        }
    }
}