use of org.springframework.roo.model.JavaType in project spring-roo by spring-projects.
the class JavaBeanMetadata method getEntityCollectionMutatorBody.
private InvocableMemberBodyBuilder getEntityCollectionMutatorBody(final FieldMetadata field, final JavaSymbolName entityIdsFieldName) {
final String entityCollectionName = field.getFieldName().getSymbolName();
final String entityIdsName = entityIdsFieldName.getSymbolName();
final JavaType collectionElementType = field.getFieldType().getParameters().get(0);
final String localEnitiesName = "local" + StringUtils.capitalize(entityCollectionName);
JavaType collectionType = field.getFieldType();
builder.getImportRegistrationResolver().addImport(collectionType);
final String collectionName = field.getFieldType().getNameIncludingTypeParameters().replace(field.getFieldType().getPackage().getFullyQualifiedPackageName() + ".", "");
String instantiableCollection = collectionName;
if (collectionType.getFullyQualifiedTypeName().equals(LIST.getFullyQualifiedTypeName())) {
collectionType = new JavaType(ARRAY_LIST.getFullyQualifiedTypeName(), 0, DataType.TYPE, null, collectionType.getParameters());
instantiableCollection = collectionType.getNameIncludingTypeParameters().replace(collectionType.getPackage().getFullyQualifiedPackageName() + ".", "");
} else if (collectionType.getFullyQualifiedTypeName().equals(SET.getFullyQualifiedTypeName())) {
collectionType = new JavaType(HASH_SET.getFullyQualifiedTypeName(), 0, DataType.TYPE, null, collectionType.getParameters());
instantiableCollection = collectionType.getNameIncludingTypeParameters().replace(collectionType.getPackage().getFullyQualifiedPackageName() + ".", "");
}
builder.getImportRegistrationResolver().addImports(collectionType, LIST, ARRAY_LIST);
final String identifierMethodName = getIdentifierMethodName(field).getSymbolName();
final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
bodyBuilder.appendFormalLine(collectionName + " " + localEnitiesName + " = new " + instantiableCollection + "();");
bodyBuilder.appendFormalLine("List<Long> longIds = new ArrayList<Long>();");
bodyBuilder.appendFormalLine("for (Key key : " + entityIdsName + ") {");
bodyBuilder.indent();
bodyBuilder.appendFormalLine("if (!longIds.contains(key.getId())) {");
bodyBuilder.indent();
bodyBuilder.appendFormalLine("longIds.add(key.getId());");
bodyBuilder.indentRemove();
bodyBuilder.appendFormalLine("}");
bodyBuilder.indentRemove();
bodyBuilder.appendFormalLine("}");
bodyBuilder.appendFormalLine("for (" + collectionElementType.getSimpleTypeName() + " entity : " + entityCollectionName + ") {");
bodyBuilder.indent();
bodyBuilder.appendFormalLine("if (!longIds.contains(entity." + identifierMethodName + "())) {");
bodyBuilder.indent();
bodyBuilder.appendFormalLine("longIds.add(entity." + identifierMethodName + "());");
bodyBuilder.appendFormalLine(entityIdsName + ".add(KeyFactory.createKey(" + collectionElementType.getSimpleTypeName() + ".class.getName(), entity." + identifierMethodName + "()));");
bodyBuilder.indentRemove();
bodyBuilder.appendFormalLine("}");
bodyBuilder.appendFormalLine(localEnitiesName + ".add(entity);");
bodyBuilder.indentRemove();
bodyBuilder.appendFormalLine("}");
bodyBuilder.appendFormalLine("this." + entityCollectionName + " = " + localEnitiesName + ";");
return bodyBuilder;
}
use of org.springframework.roo.model.JavaType in project spring-roo by spring-projects.
the class JavaBeanMetadataProviderImpl method getMetadata.
@Override
protected ItdTypeDetailsProvidingMetadataItem getMetadata(final String metadataIdentificationString, final JavaType aspectName, final PhysicalTypeMetadata governorPhysicalTypeMetadata, final String itdFilename) {
final JavaBeanAnnotationValues annotationValues = new JavaBeanAnnotationValues(governorPhysicalTypeMetadata);
if (!annotationValues.isAnnotationFound()) {
return null;
}
ClassOrInterfaceTypeDetails currentClassDetails = governorPhysicalTypeMetadata.getMemberHoldingTypeDetails();
final Map<FieldMetadata, JavaSymbolName> declaredFields = new LinkedHashMap<FieldMetadata, JavaSymbolName>();
final List<FieldMetadata> declaredFieldsList = new ArrayList<FieldMetadata>();
for (final FieldMetadata field : currentClassDetails.getDeclaredFields()) {
declaredFields.put(field, getIdentifierAccessorMethodName(field, metadataIdentificationString));
declaredFieldsList.add(field);
}
// In order to handle switching between GAE and JPA produced MIDs need
// to be remembered so they can be regenerated on JPA <-> GAE switch
producedMids.add(metadataIdentificationString);
// Getting implements
List<JavaType> interfaces = currentClassDetails.getImplementsTypes();
List<? extends MethodMetadata> interfaceMethods = null;
if (!interfaces.isEmpty()) {
for (JavaType currentInterface : interfaces) {
ClassOrInterfaceTypeDetails currentInterfaceDetails = getTypeLocationService().getTypeDetails(currentInterface);
if (currentInterfaceDetails != null) {
interfaceMethods = currentInterfaceDetails.getDeclaredMethods();
}
}
}
final JavaType target = governorPhysicalTypeMetadata.getType();
// Prepare Equals
final EqualsAnnotationValues equalsAnnotationValues = new EqualsAnnotationValues(governorPhysicalTypeMetadata);
List<FieldMetadata> equalsFields = null;
FieldMetadata identifier = null;
if (equalsAnnotationValues.isAnnotationFound()) {
EqualsMetadataProvider equalsProvider = getEqualsMetadataProvider();
equalsFields = equalsProvider.locateFields(target, equalsAnnotationValues.getExcludeFields(), declaredFieldsList, metadataIdentificationString);
identifier = equalsProvider.getIdentifier(governorPhysicalTypeMetadata);
}
// Prepare ToString
final ToStringAnnotationValues toStringAnnotationValues = new ToStringAnnotationValues(governorPhysicalTypeMetadata);
List<FieldMetadata> toStringFields = null;
if (toStringAnnotationValues.isAnnotationFound()) {
ToStringMetadataProvider toStringProvider = getToStringMetadataProvider();
toStringFields = toStringProvider.getToStringFields(governorPhysicalTypeMetadata, declaredFieldsList);
}
return new JavaBeanMetadata(metadataIdentificationString, aspectName, governorPhysicalTypeMetadata, annotationValues, declaredFields, interfaceMethods, getMemberDetailsScanner(), equalsAnnotationValues, equalsFields, identifier, toStringAnnotationValues, toStringFields);
}
use of org.springframework.roo.model.JavaType in project spring-roo by spring-projects.
the class JavaBeanMetadataProviderImpl method getIdentifierAccessorMethodName.
private JavaSymbolName getIdentifierAccessorMethodName(final FieldMetadata field, final String metadataIdentificationString) {
if (projectOperations == null) {
projectOperations = getProjectOperations();
}
Validate.notNull(projectOperations, "ProjectOperations is required");
final LogicalPath path = PhysicalTypeIdentifier.getPath(field.getDeclaredByMetadataId());
final String moduleNme = path.getModule();
if (projectOperations.isProjectAvailable(moduleNme) || !projectOperations.isFeatureInstalled(FeatureNames.GAE)) {
return null;
}
// @javax.persistence.Transient
for (final AnnotationMetadata annotationMetadata : field.getAnnotations()) {
if (annotationMetadata.getAnnotationType().equals(TRANSIENT)) {
return null;
}
}
JavaType fieldType = field.getFieldType();
// type
if (fieldType.isCommonCollectionType()) {
if (fieldType.getParameters().isEmpty()) {
return null;
}
fieldType = fieldType.getParameters().get(0);
}
final MethodMetadata identifierAccessor = getPersistenceMemberLocator().getIdentifierAccessor(fieldType);
if (identifierAccessor != null) {
getMetadataDependencyRegistry().registerDependency(identifierAccessor.getDeclaredByMetadataId(), metadataIdentificationString);
return identifierAccessor.getMethodName();
}
return null;
}
use of org.springframework.roo.model.JavaType in project spring-roo by spring-projects.
the class JavaBeanMetadataProviderImpl method getGovernorPhysicalTypeIdentifier.
@Override
protected String getGovernorPhysicalTypeIdentifier(final String metadataIdentificationString) {
final JavaType javaType = JavaBeanMetadata.getJavaType(metadataIdentificationString);
final LogicalPath path = JavaBeanMetadata.getPath(metadataIdentificationString);
return PhysicalTypeIdentifier.createIdentifier(javaType, path);
}
use of org.springframework.roo.model.JavaType in project spring-roo by spring-projects.
the class SerializableMetadataProviderImpl method getGovernorPhysicalTypeIdentifier.
@Override
protected String getGovernorPhysicalTypeIdentifier(final String metadataIdentificationString) {
final JavaType javaType = SerializableMetadata.getJavaType(metadataIdentificationString);
final LogicalPath path = SerializableMetadata.getPath(metadataIdentificationString);
return PhysicalTypeIdentifier.createIdentifier(javaType, path);
}
Aggregations