/** * Consistent with hasAnnotation() */ private static String inferTypeName(CtClass ctClass, String attributeName ) { AccessType classAccessType = getAccessTypeOrNull( ctClass ); CtField field = findFieldOrNull( ctClass, attributeName ); CtMethod getter = findGetterOrNull( ctClass, attributeName ); if ( classAccessType == AccessType.FIELD || ( field != null && getAccessTypeOrNull( field ) == AccessType.FIELD ) ) { return field == null ? null : inferFieldTypeName( field ); } if ( classAccessType == AccessType.PROPERTY || ( getter != null && getAccessTypeOrNull( getter ) == AccessType.PROPERTY ) ) { return getter == null ? null : inferMethodTypeName( getter ); } String found = ( getter == null ? null : inferMethodTypeName( getter ) ); if ( found == null && field != null ) { return inferFieldTypeName( field ); } return found; }
public static boolean hasAnnotation(CtField attribute, Class<? extends Annotation> annotation) { return getAnnotation( attribute, annotation ) != null; }
public static <T extends Annotation> T getAnnotation(CtClass ctClass, String attributeName, Class<T> annotation) { AccessType classAccessType = getAccessTypeOrNull( ctClass ); CtField field = findFieldOrNull( ctClass, attributeName ); CtMethod getter = findGetterOrNull( ctClass, attributeName ); if ( classAccessType == AccessType.FIELD || ( field != null && getAccessTypeOrNull( field ) == AccessType.FIELD ) ) { return field == null ? null : getAnnotationOrNull( field, annotation ); } if ( classAccessType == AccessType.PROPERTY || ( getter != null && getAccessTypeOrNull( getter ) == AccessType.PROPERTY ) ) { return getter == null ? null : getAnnotationOrNull( getter, annotation ); } T found = ( getter == null ? null : getAnnotationOrNull( getter, annotation ) ); if ( found == null && field != null ) { return getAnnotationOrNull( field, annotation ); } return found; }
public static String getMappedBy(CtField persistentField, CtClass targetEntity, JavassistEnhancementContext context) throws NotFoundException { final String local = getMappedByFromAnnotation( persistentField ); return local.isEmpty() ? getMappedByFromTargetEntity( persistentField, targetEntity, context ) : local; }
private void handleBiDirectionalAssociation(CtClass managedCtClass, CtField persistentField, CtMethod fieldWriter) throws NotFoundException, CannotCompileException { if ( !PersistentAttributesHelper.isPossibleBiDirectionalAssociation( persistentField ) ) { return; final CtClass targetEntity = PersistentAttributesHelper.getTargetEntityClass( managedCtClass, persistentField ); if ( targetEntity == null ) { log.infof( final String mappedBy = PersistentAttributesHelper.getMappedBy( persistentField, targetEntity, enhancementContext ); if ( mappedBy == null || mappedBy.isEmpty() ) { log.infof( if ( PersistentAttributesHelper.hasAnnotation( persistentField, OneToOne.class ) ) { if ( PersistentAttributesHelper.hasAnnotation( persistentField, OneToMany.class ) ) { boolean isMap = PersistentAttributesHelper.isAssignable( persistentField.getType(), Map.class.getName() ); String toArrayMethod = isMap ? "values().toArray()" : "toArray()"; if ( PersistentAttributesHelper.hasAnnotation( persistentField, ManyToOne.class ) ) { fieldWriter.insertBefore( String.format( if ( PersistentAttributesHelper.hasAnnotation( persistentField, ManyToMany.class ) ) { if ( PersistentAttributesHelper.isAssignable( persistentField.getType(), Map.class.getName() ) || PersistentAttributesHelper.isAssignable( targetEntity.getField( mappedBy ).getType(), Map.class.getName() ) ) { log.infof( "Bi-directional association for field [%s#%s] not managed: @ManyToMany in java.util.Map attribute not supported ",
private static String getMappedByFromTargetEntity( CtField persistentField, CtClass targetEntity, JavassistEnhancementContext context) throws NotFoundException { // get mappedBy value by searching in the fields of the target entity class for ( CtField f : targetEntity.getDeclaredFields() ) { if ( context.isPersistentField( f ) && getMappedByFromAnnotation( f ).equals( persistentField.getName() ) && isAssignable( persistentField.getDeclaringClass(), inferFieldTypeName( f ) ) ) { log.debugf( "mappedBy association for field [%s#%s] is [%s#%s]", persistentField.getDeclaringClass().getName(), persistentField.getName(), targetEntity.getName(), f.getName() ); return f.getName(); } } return ""; }
return null; CtMethod method = getterOrNull( ctClass, propertyName ); if ( method != null ) { return method; method = findGetterOrNull( ctClass.getSuperclass(), propertyName ); if ( method != null ) { return method; method = getterOrNull( interfaceCtClass, propertyName ); if ( method != null ) { return method;
public static CtClass getTargetEntityClass(CtClass managedCtClass, CtField persistentField) throws NotFoundException { // get targetEntity defined in the annotation try { OneToOne oto = PersistentAttributesHelper.getAnnotation( persistentField, OneToOne.class ); OneToMany otm = PersistentAttributesHelper.getAnnotation( persistentField, OneToMany.class ); ManyToOne mto = PersistentAttributesHelper.getAnnotation( persistentField, ManyToOne.class ); ManyToMany mtm = PersistentAttributesHelper.getAnnotation( persistentField, ManyToMany.class ); Class<?> targetClass = null; if ( oto != null ) { targetClass = oto.targetEntity(); } if ( otm != null ) { targetClass = otm.targetEntity(); } if ( mto != null ) { targetClass = mto.targetEntity(); } if ( mtm != null ) { targetClass = mtm.targetEntity(); } if ( targetClass != null && targetClass != void.class ) { return managedCtClass.getClassPool().get( targetClass.getName() ); } } catch (NotFoundException ignore) { } // infer targetEntity from generic type signature String inferredTypeName = inferTypeName( managedCtClass, persistentField.getName() ); return inferredTypeName == null ? null : managedCtClass.getClassPool().get( inferredTypeName ); }
private static AccessType getAccessTypeOrNull(CtMember ctMember) { Access access = getAnnotationOrNull( ctMember, Access.class ); return access == null ? null : access.value(); }
private static AccessType getAccessTypeOrNull(CtClass ctClass) { try { if ( ctClass.hasAnnotation( Access.class ) ) { return ( (Access) ctClass.getAnnotation( Access.class ) ).value(); } else { CtClass extendsClass = ctClass.getSuperclass(); return extendsClass == null ? null : getAccessTypeOrNull( extendsClass ); } } catch (ClassNotFoundException e) { return null; } catch (NotFoundException e) { return null; } }
/** * duplicated here to take CtClass instead of Class * @see org.hibernate.internal.util.ReflectHelper#locateField */ private static CtField findFieldOrNull(CtClass ctClass, String propertyName) { if ( ctClass == null ) { return null; } try { return ctClass.getField( propertyName ); } catch ( NotFoundException nsfe ) { try { return findFieldOrNull( ctClass.getSuperclass(), propertyName ); } catch (NotFoundException e) { return null; } } }
private void handleBiDirectionalAssociation(CtClass managedCtClass, CtField persistentField, CtMethod fieldWriter) throws NotFoundException, CannotCompileException { if ( !PersistentAttributesHelper.isPossibleBiDirectionalAssociation( persistentField ) ) { return; final CtClass targetEntity = PersistentAttributesHelper.getTargetEntityClass( managedCtClass, persistentField ); if ( targetEntity == null ) { log.infof( final String mappedBy = PersistentAttributesHelper.getMappedBy( persistentField, targetEntity, enhancementContext ); if ( mappedBy == null || mappedBy.isEmpty() ) { log.infof( if ( PersistentAttributesHelper.hasAnnotation( persistentField, OneToOne.class ) ) { if ( PersistentAttributesHelper.hasAnnotation( persistentField, OneToMany.class ) ) { boolean isMap = PersistentAttributesHelper.isAssignable( persistentField.getType(), Map.class.getName() ); String toArrayMethod = isMap ? "values().toArray()" : "toArray()"; if ( PersistentAttributesHelper.hasAnnotation( persistentField, ManyToOne.class ) ) { fieldWriter.insertBefore( String.format( if ( PersistentAttributesHelper.hasAnnotation( persistentField, ManyToMany.class ) ) { if ( PersistentAttributesHelper.isAssignable( persistentField.getType(), Map.class.getName() ) || PersistentAttributesHelper.isAssignable( targetEntity.getField( mappedBy ).getType(), Map.class.getName() ) ) { log.infof( "Bi-directional association for field [%s#%s] not managed: @ManyToMany in java.util.Map attribute not supported ",
private static String getMappedByFromTargetEntity( CtField persistentField, CtClass targetEntity, JavassistEnhancementContext context) throws NotFoundException { // get mappedBy value by searching in the fields of the target entity class for ( CtField f : targetEntity.getDeclaredFields() ) { if ( context.isPersistentField( f ) && getMappedByFromAnnotation( f ).equals( persistentField.getName() ) && isAssignable( persistentField.getDeclaringClass(), inferFieldTypeName( f ) ) ) { log.debugf( "mappedBy association for field [%s#%s] is [%s#%s]", persistentField.getDeclaringClass().getName(), persistentField.getName(), targetEntity.getName(), f.getName() ); return f.getName(); } } return ""; }
public static String getMappedBy(CtField persistentField, CtClass targetEntity, JavassistEnhancementContext context) throws NotFoundException { final String local = getMappedByFromAnnotation( persistentField ); return local.isEmpty() ? getMappedByFromTargetEntity( persistentField, targetEntity, context ) : local; }
return null; CtMethod method = getterOrNull( ctClass, propertyName ); if ( method != null ) { return method; method = findGetterOrNull( ctClass.getSuperclass(), propertyName ); if ( method != null ) { return method; method = getterOrNull( interfaceCtClass, propertyName ); if ( method != null ) { return method;
public static CtClass getTargetEntityClass(CtClass managedCtClass, CtField persistentField) throws NotFoundException { // get targetEntity defined in the annotation try { OneToOne oto = PersistentAttributesHelper.getAnnotation( persistentField, OneToOne.class ); OneToMany otm = PersistentAttributesHelper.getAnnotation( persistentField, OneToMany.class ); ManyToOne mto = PersistentAttributesHelper.getAnnotation( persistentField, ManyToOne.class ); ManyToMany mtm = PersistentAttributesHelper.getAnnotation( persistentField, ManyToMany.class ); Class<?> targetClass = null; if ( oto != null ) { targetClass = oto.targetEntity(); } if ( otm != null ) { targetClass = otm.targetEntity(); } if ( mto != null ) { targetClass = mto.targetEntity(); } if ( mtm != null ) { targetClass = mtm.targetEntity(); } if ( targetClass != null && targetClass != void.class ) { return managedCtClass.getClassPool().get( targetClass.getName() ); } } catch (NotFoundException ignore) { } // infer targetEntity from generic type signature String inferredTypeName = inferTypeName( managedCtClass, persistentField.getName() ); return inferredTypeName == null ? null : managedCtClass.getClassPool().get( inferredTypeName ); }
private static AccessType getAccessTypeOrNull(CtMember ctMember) { Access access = getAnnotationOrNull( ctMember, Access.class ); return access == null ? null : access.value(); }
private static AccessType getAccessTypeOrNull(CtClass ctClass) { try { if ( ctClass.hasAnnotation( Access.class ) ) { return ( (Access) ctClass.getAnnotation( Access.class ) ).value(); } else { CtClass extendsClass = ctClass.getSuperclass(); return extendsClass == null ? null : getAccessTypeOrNull( extendsClass ); } } catch (ClassNotFoundException e) { return null; } catch (NotFoundException e) { return null; } }
/** * duplicated here to take CtClass instead of Class * @see org.hibernate.internal.util.ReflectHelper#locateField */ private static CtField findFieldOrNull(CtClass ctClass, String propertyName) { if ( ctClass == null ) { return null; } try { return ctClass.getField( propertyName ); } catch ( NotFoundException nsfe ) { try { return findFieldOrNull( ctClass.getSuperclass(), propertyName ); } catch (NotFoundException e) { return null; } } }
/** * Consistent with hasAnnotation() */ private static String inferTypeName(CtClass ctClass, String attributeName ) { AccessType classAccessType = getAccessTypeOrNull( ctClass ); CtField field = findFieldOrNull( ctClass, attributeName ); CtMethod getter = findGetterOrNull( ctClass, attributeName ); if ( classAccessType == AccessType.FIELD || ( field != null && getAccessTypeOrNull( field ) == AccessType.FIELD ) ) { return field == null ? null : inferFieldTypeName( field ); } if ( classAccessType == AccessType.PROPERTY || ( getter != null && getAccessTypeOrNull( getter ) == AccessType.PROPERTY ) ) { return getter == null ? null : inferMethodTypeName( getter ); } String found = ( getter == null ? null : inferMethodTypeName( getter ) ); if ( found == null && field != null ) { return inferFieldTypeName( field ); } return found; }