use of org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder in project spring-roo by spring-projects.
the class RepositoryJpaIntegrationTestMetadata method getCountTestMethod.
/**
* Builds a method to test count method.
*
* @return {@link MethodMetadata}
*/
private MethodMetadata getCountTestMethod() {
MethodMetadata method = getGovernorMethod(COUNT_TEST_METHOD_NAME);
if (method != null) {
return method;
}
InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
// Verify
bodyBuilder.appendFormalLine("// Verify");
// assertThat(repository.count()).as("Check there are available 'Pet' entries").isGreaterThan(0);
bodyBuilder.appendFormalLine("%s(%s().count()).as(\"Check there are available '%s' entries\").isGreaterThan(0);", getNameOfJavaType(ASSERT_THAT), getAccessorMethod(this.repositoryField).getMethodName(), getNameOfJavaType(this.entity));
MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, COUNT_TEST_METHOD_NAME, JavaType.VOID_PRIMITIVE, bodyBuilder);
// Add @Test
methodBuilder.addAnnotation(new AnnotationMetadataBuilder(TEST));
return methodBuilder.build();
}
use of org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder in project spring-roo by spring-projects.
the class RepositoryJpaIntegrationTestMetadata method getFindOneTestMethod.
/**
* Builds a method to test find one method.
*
* @return {@link MethodMetadata}
*/
private MethodMetadata getFindOneTestMethod() {
final JavaSymbolName methodName = new JavaSymbolName(String.format("findOneShouldReturnExisting%s", this.entity.getSimpleTypeName()));
MethodMetadata method = getGovernorMethod(methodName);
if (method != null) {
return method;
}
InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
// Setup
bodyBuilder.appendFormalLine("// Setup");
// Long id = getRandomPetId();
bodyBuilder.appendFormalLine("%s id = %s();", getNameOfJavaType(this.identifierType), this.getRandomIdMethodName);
// Exercise
bodyBuilder.newLine();
bodyBuilder.appendFormalLine("// Exercise");
// Pet pet = repository.findOne(id);
bodyBuilder.appendFormalLine("%s %s = %s().findOne(id);", getNameOfJavaType(this.entity), entityVar, getAccessorMethod(this.repositoryField).getMethodName());
// Verify
bodyBuilder.newLine();
bodyBuilder.appendFormalLine("// Verify");
// assertThat(pet).as("Check that findOne illegally returned null for id %s", id).isNotNull();
bodyBuilder.appendFormalLine("%s(%s).as(\"Check that findOne illegally returned null for id %s\", id).isNotNull();", getNameOfJavaType(ASSERT_THAT), this.entityVar, "%s");
// assertThat(id).as("Check the identifier of the found 'Pet' is the same used to look for it")
bodyBuilder.appendFormalLine("%s(id).as(\"Check the identifier of the found '%s' is the same used to look for it\")", getNameOfJavaType(ASSERT_THAT), getNameOfJavaType(this.entity));
// .isEqualTo(pet.getId());
bodyBuilder.indent();
bodyBuilder.appendFormalLine(".isEqualTo(%s.%s());", this.entityVar, this.identifierAccessorMethodName);
bodyBuilder.reset();
MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, JavaType.VOID_PRIMITIVE, bodyBuilder);
// Add @Test
methodBuilder.addAnnotation(new AnnotationMetadataBuilder(TEST));
return methodBuilder.build();
}
use of org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder in project spring-roo by spring-projects.
the class RepositoryJpaIntegrationTestMetadata method getImportAnnotation.
/**
* Builds and returns `@Import` annotation
*
* @return {@link AnnotationMetadataBuilder}
*/
private AnnotationMetadataBuilder getImportAnnotation() {
AnnotationMetadataBuilder annotationBuilder = new AnnotationMetadataBuilder(SpringJavaType.ANNOTATION_IMPORT);
// Create List of ClassAttributeValue
List<ClassAttributeValue> typesToImport = new ArrayList<ClassAttributeValue>();
typesToImport.add(new ClassAttributeValue(new JavaSymbolName("value"), this.annotationValues.getDodConfigurationClass()));
typesToImport.add(new ClassAttributeValue(new JavaSymbolName("value"), this.jpaDetachableRepositoryClass));
// Convert List to ArrayAttributeValue
ArrayAttributeValue<ClassAttributeValue> importAttr = new ArrayAttributeValue<ClassAttributeValue>(new JavaSymbolName("value"), typesToImport);
// Add annotation attribute
annotationBuilder.addAttribute(importAttr);
return annotationBuilder;
}
use of org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder in project spring-roo by spring-projects.
the class ServiceImplMetadata method getMethod.
/**
* Method that generates implementation of provided method with specified
* body
*
* @param methodToBeImplemented
* @param isTransactional
* @param bodyBuilder
* @return
*/
private MethodMetadata getMethod(final MethodMetadata methodToBeImplemented, boolean isTransactional, InvocableMemberBodyBuilder bodyBuilder) {
List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>(methodToBeImplemented.getParameterNames());
List<AnnotatedJavaType> parameterTypes = new ArrayList<AnnotatedJavaType>(methodToBeImplemented.getParameterTypes());
MethodMetadata existingMethod = getGovernorMethod(methodToBeImplemented.getMethodName(), AnnotatedJavaType.convertFromAnnotatedJavaTypes(parameterTypes));
if (existingMethod != null) {
return existingMethod;
}
// Use the MethodMetadataBuilder for easy creation of MethodMetadata
MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodToBeImplemented.getMethodName(), methodToBeImplemented.getReturnType(), parameterTypes, parameterNames, bodyBuilder);
// Adding @Transactional
if (isTransactional) {
AnnotationMetadataBuilder transactionalAnnotation = new AnnotationMetadataBuilder(SpringJavaType.TRANSACTIONAL);
methodBuilder.addAnnotation(transactionalAnnotation);
}
// Build and return a MethodMetadata instance
return methodBuilder.build();
}
use of org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder in project spring-roo by spring-projects.
the class ServiceOperationsImpl method createServiceImplementation.
/**
* Method that creates the service implementation
*
* @param interfaceType
* @param implType
* @param domainType
*/
private void createServiceImplementation(final JavaType interfaceType, JavaType implType, ClassOrInterfaceTypeDetails repository, JavaType domainType) {
Validate.notNull(interfaceType, "ERROR: Interface should be provided to be able to generate its implementation");
Validate.notNull(interfaceType.getModule(), "ERROR: Interface module is required");
Validate.notNull(domainType, "ERROR: Domain type required to generate service");
// Generating implementation JavaType if needed
if (implType == null) {
implType = new JavaType(String.format("%sImpl", interfaceType.getFullyQualifiedTypeName()), interfaceType.getModule());
}
Validate.notNull(implType.getModule(), "ERROR: Implementation module is required");
// Checks if new service interface already exists.
final String implIdentifier = pathResolver.getCanonicalPath(implType.getModule(), Path.SRC_MAIN_JAVA, implType);
if (fileManager.exists(implIdentifier)) {
// Type already exists - nothing to do
return;
}
// Generating @RooServiceImpl annotation
final AnnotationMetadataBuilder implAnnotationMetadata = new AnnotationMetadataBuilder(ROO_SERVICE_IMPL);
implAnnotationMetadata.addAttribute(new ClassAttributeValue(new JavaSymbolName("service"), interfaceType));
// Creating class builder
final String implMid = PhysicalTypeIdentifier.createIdentifier(implType, pathResolver.getPath(implIdentifier));
final ClassOrInterfaceTypeDetailsBuilder implTypeBuilder = new ClassOrInterfaceTypeDetailsBuilder(implMid, PUBLIC, implType, PhysicalTypeCategory.CLASS);
// Adding @RooService annotation to current interface
implTypeBuilder.addAnnotation(implAnnotationMetadata.build());
// Adding implements
implTypeBuilder.addImplementsType(interfaceType);
final String declaredByMetadataId = PhysicalTypeIdentifier.createIdentifier(implTypeBuilder.build().getType(), pathResolver.getPath(implType.getModule(), Path.SRC_MAIN_JAVA));
// Write service implementation on disk
typeManagementService.createOrUpdateTypeOnDisk(implTypeBuilder.build());
// Add dependencies between modules
projectOperations.addModuleDependency(implType.getModule(), interfaceType.getModule());
projectOperations.addModuleDependency(implType.getModule(), repository.getName().getModule());
projectOperations.addModuleDependency(implType.getModule(), domainType.getModule());
// ROO-3799 Included dependency spring-tx if it's a multimodule project
if (projectOperations.isMultimoduleProject()) {
projectOperations.addDependency(implType.getModule(), new Dependency("org.springframework", "spring-tx", "", DependencyType.JAR, DependencyScope.COMPILE));
}
}
Aggregations