Example usage for javax.persistence AccessType FIELD

List of usage examples for javax.persistence AccessType FIELD

Introduction

In this page you can find the example usage for javax.persistence AccessType FIELD.

Prototype

AccessType FIELD

To view the source code for javax.persistence AccessType FIELD.

Click Source Link

Document

Field-based access is used.

Usage

From source file:jp.gr.java_conf.ka_ka_xyz.processor.JPA20AnnotationProcessor.java

public JPA20AnnotationProcessor(Class<?> clazz) {
    if (clazz != null) {
        Annotation classAnnotation = clazz.getAnnotation(Access.class);
        Access access = null;/* w  w  w .jav  a2s .  c  o m*/
        if (classAnnotation instanceof Access) {
            access = (Access) classAnnotation;
        }
        boolean isFieldAccess = (access == null || AccessType.FIELD == access.value());
        Field[] fields = clazz.getDeclaredFields();
        if (isFieldAccess) {
            for (Field field : fields) {
                registerFieldAnnotation(field);
            }
        } else {
            for (Field field : fields) {
                registerMethodAnnotation(field, clazz);
            }
        }
    }
}

From source file:org.caratarse.auth.model.po.Resource.java

@Temporal(javax.persistence.TemporalType.TIMESTAMP)
@Column(updatable = false)
@Access(AccessType.FIELD)
public Date getCreationDate() {
    return creationDate;
}

From source file:org.caratarse.auth.model.po.Resource.java

@Temporal(javax.persistence.TemporalType.TIMESTAMP)
@Access(AccessType.FIELD)
public Date getUpdatedDate() {
    return updatedDate;
}

From source file:org.apache.openjpa.persistence.AnnotationPersistenceMetaDataParser.java

/**
 * Set the explicit access type, if specified.
 *///  w w  w . j a v  a2  s.  c om
private void parseAccess(ClassMetaData meta, Access access) {
    if (access != null) {
        meta.setAccessType(AccessCode.EXPLICIT
                | (access.value() == AccessType.FIELD ? AccessCode.FIELD : AccessCode.PROPERTY));
    }
}

From source file:org.apache.openjpa.persistence.AnnotationPersistenceMetaDataParser.java

/**
 * Gets the explicit access for the class, if any.
 * Explicit access type specification does not affect the access type of
 * other entity classes or mapped super classes in the entity hierarchy.
 *///from  ww w .  j ava2s .c  o m
private int getAccessCode(Class<?> cls) {
    int accessCode = AccessCode.UNKNOWN;
    Access access = AccessController.doPrivileged(J2DoPrivHelper.getAnnotationAction(cls, Access.class));
    if (access != null) {
        accessCode |= AccessCode.EXPLICIT
                | (access.value() == AccessType.FIELD ? AccessCode.FIELD : AccessCode.PROPERTY);
    }
    return accessCode;
}

From source file:org.apache.openjpa.persistence.AnnotationPersistenceMetaDataParser.java

/**
 * Set the explicit access type, if specified.
 *///from   w w  w .  j  a va2  s.  c  om
private void parseAccess(FieldMetaData meta, Access access) {
    if (access != null) {
        meta.setAccessType(AccessCode.EXPLICIT
                | (access.value() == AccessType.FIELD ? AccessCode.FIELD : AccessCode.PROPERTY));
    }
}

From source file:org.apache.openjpa.persistence.PersistenceMetaDataDefaults.java

/**
 * Explicit access type, if any, is generally detected by the parser. This
 * is only used for metadata of an embeddable type which is encountered
 * as a field during some other owning entity.
 * //from  w  w w  .j  ava 2  s  .  c  o  m
 * @see ValueMetaData#addEmbeddedMetaData()
 */
private int determineExplicitAccessType(Class<?> cls) {
    Access access = cls.getAnnotation(Access.class);
    return access == null ? AccessCode.UNKNOWN
            : ((access.value() == AccessType.FIELD ? AccessCode.FIELD : AccessCode.PROPERTY)
                    | AccessCode.EXPLICIT);
}

From source file:org.apache.openjpa.persistence.PersistenceMetaDataDefaults.java

protected boolean isDefaultPersistent(ClassMetaData meta, Member member, String name, boolean ignoreTransient) {
    int mods = member.getModifiers();
    if (Modifier.isTransient(mods))
        return false;
    int access = meta.getAccessType();

    if (member instanceof Field) {
        // If mixed or unknown, default property access, keep explicit 
        // field members
        if (AccessCode.isProperty(access)) {
            if (!isAnnotatedAccess(member, AccessType.FIELD))
                return false;
        }// w ww  .  j  ava2 s  .c  o m
    } else if (member instanceof Method) {
        // If mixed or unknown, field default access, keep explicit property
        // members
        if (AccessCode.isField(access)) {
            if (!isAnnotatedAccess(member, AccessType.PROPERTY))
                return false;
        }
        try {
            // check for setters for methods
            Method setter = (Method) AccessController.doPrivileged(J2DoPrivHelper.getDeclaredMethodAction(
                    meta.getDescribedType(), "set" + StringUtils.capitalize(name),
                    new Class[] { ((Method) member).getReturnType() }));
            if (setter == null && !isAnnotatedTransient(member)) {
                logNoSetter(meta, name, null);
                return false;
            }
        } catch (Exception e) {
            // e.g., NoSuchMethodException
            if (!isAnnotatedTransient(member))
                logNoSetter(meta, name, e);
            return false;
        }
    }

    PersistenceStrategy strat = getPersistenceStrategy(null, member, ignoreTransient);
    if (strat == null) {
        warn(meta, _loc.get("no-pers-strat", name));
        return false;
    } else if (strat == PersistenceStrategy.TRANSIENT) {
        return false;
    } else {
        return true;
    }
}

From source file:org.apache.openjpa.persistence.PersistenceMetaDataDefaults.java

/**
 * Gets either the instance field or the getter method depending upon the 
 * access style of the given meta-data./*w  w  w  .  j  a v a 2 s.c  o m*/
 */
public Member getMemberByProperty(ClassMetaData meta, String property, int access, boolean applyDefaultRule) {
    Class<?> cls = meta.getDescribedType();
    Field field = Reflection.findField(cls, property, false);
    ;
    Method getter = Reflection.findGetter(cls, property, false);
    Method setter = Reflection.findSetter(cls, property, false);
    int accessCode = AccessCode.isUnknown(access) ? meta.getAccessType() : access;
    if (field == null && getter == null)
        error(meta, _loc.get("access-no-property", cls, property));
    if ((isNotTransient(getter) && isAnnotated(getter)) && isNotTransient(field) && isAnnotated(field))
        throw new IllegalStateException(_loc.get("access-duplicate", field, getter).toString());

    if (AccessCode.isField(accessCode)) {
        if (isAnnotatedAccess(getter, AccessType.PROPERTY)) {
            meta.setAccessType(AccessCode.MIXED | meta.getAccessType());
            return getter;
        }
        return field == null ? getter : field;
    } else if (AccessCode.isProperty(accessCode)) {
        if (isAnnotatedAccess(field, AccessType.FIELD)) {
            meta.setAccessType(AccessCode.MIXED | meta.getAccessType());
            return field;
        }
        return getter == null ? field : getter;
    } else if (AccessCode.isUnknown(accessCode)) {
        if (isAnnotated(field)) {
            meta.setAccessType(AccessCode.FIELD);
            return field;
        } else if (isAnnotated(getter)) {
            meta.setAccessType(AccessCode.PROPERTY);
            return getter;
        } else {
            warn(meta, _loc.get("access-none", meta, property));
            throw new IllegalStateException(_loc.get("access-none", meta, property).toString());
        }
    } else {
        throw new InternalException(meta + " " + AccessCode.toClassString(meta.getAccessType()));
    }
}

From source file:org.batoo.jpa.parser.impl.metadata.type.ManagedTypeMetadatImpl.java

/**
 * Infers and returns the access type based on all persistence annotations being on fields or methods and parent parent access type.
 * /*from www . j av a 2  s  .  co  m*/
 * @param parentAccessType
 *            the parent access type
 * @return the inferred access type
 * 
 * @since 2.0.0
 */
private AccessType inferAccessType(AccessType parentAccessType) {
    boolean methodsHasAnnotations = false;
    boolean fieldsHasAnnotations = false;

    final List<String> alternated = Lists.newArrayList();

    final Field[] fields = this.clazz.getDeclaredFields();
    final Method[] methods = this.clazz.getDeclaredMethods();

    // find the alternated ones with @Access
    for (final Method m : methods) {
        // skip static and private methods.
        final int mods = m.getModifiers();
        if (Modifier.isStatic(mods) || !Modifier.isPublic(mods) || m.isBridge() || m.isSynthetic()) {
            continue;
        }

        if ((m.getParameterTypes().length != 0) || (m.getReturnType() == null)) {
            continue;
        }

        final Access access = m.getAnnotation(Access.class);
        if (access != null) {
            final String name = m.getName();
            if ((m.getReturnType() == boolean.class) && name.startsWith("is")) {
                alternated.add(StringUtils.capitalize(name.substring(2)));
            } else if (name.startsWith("get")) {
                alternated.add(StringUtils.capitalize(name.substring(3)));
            }
        }
    }

    for (final Field f : fields) {
        final Access access = f.getAnnotation(Access.class);

        if (access != null) {
            alternated.add(StringUtils.capitalize(f.getName()));
        }
    }

    // check methods
    for (final Method m : methods) {
        for (final Annotation a : m.getAnnotations()) {
            // ignore @Access(PROPERTY)
            if (a instanceof Access) {
                if (((Access) a).value() != AccessType.PROPERTY) {
                    continue;
                }
            }

            // ignore @Transient
            if (a instanceof Transient) {
                continue;
            }

            if ((m.getReturnType() == null) || (m.getParameterTypes().length > 0)) {
                continue;
            }

            String name = a.annotationType().getName();
            // ignore the listener annotations
            if (name.startsWith("javax.persistence.Post") || name.startsWith("javax.persistence.Pre")) {
                continue;
            }

            if (name.startsWith("javax.persistence") || name.startsWith("org.batoo.jpa.annotation")) {
                name = m.getName();

                if ((boolean.class == m.getReturnType()) || name.startsWith("is")) {
                    name = name.substring(2);
                } else if (name.startsWith("get")) {
                    name = name.substring(3);
                }

                if (alternated.contains(StringUtils.capitalize(name))) {
                    continue;
                }

                methodsHasAnnotations = true;
                break;
            }
        }
    }

    // check fields
    for (final Field f : fields) {
        for (final Annotation a : f.getAnnotations()) {
            // ignore @Access(FIELD)
            if (a instanceof Access) {
                if (((Access) a).value() != AccessType.FIELD) {
                    continue;
                }
            }

            // ignore @Transient
            if (a instanceof Transient) {
                continue;
            }

            final String name = a.annotationType().getName();
            if (name.startsWith("javax.persistence") || name.startsWith("org.batoo.jpa.annotation")) {
                if (alternated.contains(StringUtils.capitalize(f.getName()))) {
                    continue;
                }

                fieldsHasAnnotations = true;
                break;
            }
        }
    }

    if (fieldsHasAnnotations && methodsHasAnnotations) {
        throw new PersistenceException(
                "At least one field and one method has persistence annotations: " + this.clazz.getName());
    }

    if (methodsHasAnnotations) {
        return AccessType.PROPERTY;
    }

    if (fieldsHasAnnotations) {
        return AccessType.FIELD;
    }

    if (parentAccessType != null) {
        return parentAccessType;
    }

    return AccessType.FIELD;
}