private AnnotationInstance overrideSchemaCatalogByDefault( AnnotationInstance annotationInstance, EntityMappingsMocker.Default defaults) { List<AnnotationValue> newAnnotationValueList = new ArrayList<AnnotationValue>(); newAnnotationValueList.addAll(annotationInstance.values()); boolean schemaDefined = false; boolean catalogDefined = false; if (annotationInstance.value("schema") != null) { schemaDefined = true; } if (annotationInstance.value("catalog") != null) { catalogDefined = true; } if (schemaDefined && catalogDefined) { return annotationInstance; } if (!catalogDefined && StringHelper.isNotEmpty(defaults.getCatalog())) { newAnnotationValueList.add( AnnotationValue.createStringValue("catalog", defaults.getCatalog())); } if (!schemaDefined && StringHelper.isNotEmpty(defaults.getSchema())) { newAnnotationValueList.add(AnnotationValue.createStringValue("schema", defaults.getSchema())); } return MockHelper.create( annotationInstance.name(), annotationInstance.target(), MockHelper.toArray(newAnnotationValueList)); }
private void addCascadePersistIfNotExist( DotName annName, Map<DotName, List<AnnotationInstance>> indexedAnnotationMap) { List<AnnotationInstance> annotationInstanceList = indexedAnnotationMap.get(annName); if (annotationInstanceList == null || annotationInstanceList.isEmpty()) { return; } List<AnnotationInstance> newAnnotationInstanceList = new ArrayList<AnnotationInstance>(annotationInstanceList.size()); for (AnnotationInstance annotationInstance : annotationInstanceList) { AnnotationValue cascadeValue = annotationInstance.value("cascade"); List<AnnotationValue> newAnnotationValueList = new ArrayList<AnnotationValue>(); newAnnotationValueList.addAll(annotationInstance.values()); if (cascadeValue == null) { AnnotationValue temp = AnnotationValue.createEnumValue("", JPADotNames.CASCADE_TYPE, "PERSIST"); cascadeValue = AnnotationValue.createArrayValue("cascade", new AnnotationValue[] {temp}); } else { newAnnotationValueList.remove(cascadeValue); String[] cascadeTypes = cascadeValue.asEnumArray(); boolean hasPersistDefined = false; for (String type : cascadeTypes) { if ("PERSIST".equals(type)) { hasPersistDefined = true; continue; } } if (hasPersistDefined) { newAnnotationInstanceList.add(annotationInstance); continue; } String[] newCascadeTypes = new String[cascadeTypes.length + 1]; newCascadeTypes[0] = "PERSIST"; System.arraycopy(cascadeTypes, 0, newCascadeTypes, 1, cascadeTypes.length); AnnotationValue[] cascades = new AnnotationValue[newCascadeTypes.length]; for (int i = 0; i < newCascadeTypes.length; i++) { cascades[i] = AnnotationValue.createEnumValue("", JPADotNames.CASCADE_TYPE, newCascadeTypes[i]); } cascadeValue = AnnotationValue.createArrayValue("cascade", cascades); } newAnnotationValueList.add(cascadeValue); AnnotationInstance newAnnotationInstance = MockHelper.create( annotationInstance.name(), annotationInstance.target(), MockHelper.toArray(newAnnotationValueList)); newAnnotationInstanceList.add(newAnnotationInstance); } indexedAnnotationMap.put(annName, newAnnotationInstanceList); }
/** * Get {@link javax.persistence.AccessType } from {@link javax.persistence.Access @Access} on the * attribute of the given class */ static XMLAccessType getAccessFromAttributeAnnotation( DotName className, String attributeName, IndexBuilder indexBuilder) { Map<DotName, List<AnnotationInstance>> indexedAnnotations = indexBuilder.getIndexedAnnotations(className); if (indexedAnnotations != null && indexedAnnotations.containsKey(ACCESS)) { List<AnnotationInstance> annotationInstances = indexedAnnotations.get(ACCESS); if (MockHelper.isNotEmpty(annotationInstances)) { for (AnnotationInstance annotationInstance : annotationInstances) { AnnotationTarget indexedPropertyTarget = annotationInstance.target(); if (indexedPropertyTarget == null) { continue; } if (JandexHelper.getPropertyName(indexedPropertyTarget).equals(attributeName)) { XMLAccessType accessType = JandexHelper.getValueAsEnum(annotationInstance, "value", XMLAccessType.class); /** here we ignore @Access(FIELD) on property (getter) and @Access(PROPERTY) on field */ XMLAccessType targetAccessType = annotationTargetToAccessType(indexedPropertyTarget); if (accessType.equals(targetAccessType)) { return targetAccessType; } else { LOG.warn( String.format( "%s.%s has @Access on %s, but it tries to assign the access type to %s, this is not allowed by JPA spec, and will be ignored.", className, attributeName, targetAccessType, accessType)); } } } } } return null; }
void collectGlobalConfigurationFromIndex(GlobalAnnotations globalAnnotations) { for (DotName annName : DefaultConfigurationHelper.GLOBAL_ANNOTATIONS) { List<AnnotationInstance> annotationInstanceList = index.getAnnotations(annName); if (MockHelper.isNotEmpty(annotationInstanceList)) { globalAnnotations.addIndexedAnnotationInstance(annotationInstanceList); } } globalAnnotations.filterIndexedAnnotations(); }
private static XMLAccessType getAccessFromIdPosition( Map<DotName, List<AnnotationInstance>> annotations) { if (annotations == null || annotations.isEmpty() || !(annotations.containsKey(ID))) { return null; } List<AnnotationInstance> idAnnotationInstances = annotations.get(ID); if (MockHelper.isNotEmpty(idAnnotationInstances)) { return processIdAnnotations(idAnnotationInstances); } return null; }
private void applyDefaultsToEntityObject( EntityObject entityObject, EntityMappingsMocker.Default defaults) { if (defaults == null) { return; } String className = MockHelper.buildSafeClassName(entityObject.getClazz(), defaults.getPackageName()); entityObject.setClazz(className); if (entityObject.isMetadataComplete() == null) { entityObject.setMetadataComplete(defaults.isMetadataComplete()); } LOG.debugf("Adding XML overriding information for %s", className); }
// @Table, @CollectionTable, @JoinTable, @SecondaryTable private void overrideSchemaCatalogByDefault( DotName annName, Map<DotName, List<AnnotationInstance>> indexedAnnotationMap, EntityMappingsMocker.Default defaults) { List<AnnotationInstance> annotationInstanceList = indexedAnnotationMap.get(annName); if (annotationInstanceList == null || annotationInstanceList.isEmpty()) { return; } List<AnnotationInstance> newAnnotationInstanceList = new ArrayList<AnnotationInstance>(annotationInstanceList.size()); for (AnnotationInstance annotationInstance : annotationInstanceList) { if (annName.equals(IndexedAnnotationFilter.SECONDARY_TABLES)) { AnnotationInstance[] secondaryTableAnnotationInstanceArray = annotationInstance.value().asNestedArray(); AnnotationValue[] newAnnotationValueArray = new AnnotationValue[secondaryTableAnnotationInstanceArray.length]; for (int i = 0; i < secondaryTableAnnotationInstanceArray.length; i++) { newAnnotationValueArray[i] = MockHelper.nestedAnnotationValue( "", overrideSchemaCatalogByDefault( secondaryTableAnnotationInstanceArray[i], defaults)); } AnnotationInstance secondaryTablesAnnotationInstance = MockHelper.create( annName, annotationInstance.target(), new AnnotationValue[] { AnnotationValue.createArrayValue("value", newAnnotationValueArray) }); newAnnotationInstanceList.add(secondaryTablesAnnotationInstance); } else { newAnnotationInstanceList.add(overrideSchemaCatalogByDefault(annotationInstance, defaults)); } } indexedAnnotationMap.put(annName, newAnnotationInstanceList); }
private static XMLAccessType getAccess(Map<DotName, List<AnnotationInstance>> annotations) { if (annotations == null || annotations.isEmpty() || !isEntityObject(annotations)) { return null; } List<AnnotationInstance> accessAnnotationInstances = annotations.get(JPADotNames.ACCESS); if (MockHelper.isNotEmpty(accessAnnotationInstances)) { for (AnnotationInstance annotationInstance : accessAnnotationInstances) { if (annotationInstance.target() != null && annotationInstance.target() instanceof ClassInfo) { return JandexHelper.getValueAsEnum(annotationInstance, "value", XMLAccessType.class); } } } return null; }
private void mockTableIfNonExist( Map<DotName, List<AnnotationInstance>> annotationsMap, DotName annName) { if (annName == JPADotNames.TABLE && !annotationsMap.containsKey(JPADotNames.TABLE) && annotationsMap.containsKey(JPADotNames.ENTITY)) { // if an entity doesn't have a @Table, we create one here AnnotationInstance entity = JandexHelper.getSingleAnnotation(annotationsMap, JPADotNames.ENTITY); AnnotationInstance table = MockHelper.create( JPADotNames.TABLE, entity.target(), MockHelper.EMPTY_ANNOTATION_VALUE_ARRAY); List<AnnotationInstance> annotationInstanceList = new ArrayList<AnnotationInstance>(1); annotationInstanceList.add(table); annotationsMap.put(JPADotNames.TABLE, annotationInstanceList); } }