use of org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue in project spring-roo by spring-projects.
the class DbreMetadata method hasField.
private boolean hasField(final JavaSymbolName fieldName, final List<AnnotationMetadata> annotations) {
// Check governor for field
if (governorTypeDetails.getField(fieldName) != null) {
return true;
}
// Check @Column and @JoinColumn annotations on fields in governor with
// the same 'name' as the generated field
final List<FieldMetadata> governorFields = governorTypeDetails.getFieldsWithAnnotation(COLUMN);
governorFields.addAll(governorTypeDetails.getFieldsWithAnnotation(JOIN_COLUMN));
for (final FieldMetadata governorField : governorFields) {
governorFieldAnnotations: for (final AnnotationMetadata governorFieldAnnotation : governorField.getAnnotations()) {
if (governorFieldAnnotation.getAnnotationType().equals(COLUMN) || governorFieldAnnotation.getAnnotationType().equals(JOIN_COLUMN)) {
final AnnotationAttributeValue<?> name = governorFieldAnnotation.getAttribute(new JavaSymbolName(NAME));
if (name == null) {
continue governorFieldAnnotations;
}
for (final AnnotationMetadata annotationMetadata : annotations) {
final AnnotationAttributeValue<?> columnName = annotationMetadata.getAttribute(new JavaSymbolName(NAME));
if (columnName != null && columnName.equals(name)) {
return true;
}
}
}
}
}
return false;
}
use of org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue in project spring-roo by spring-projects.
the class DbreMetadata method excludeFieldsInToStringAnnotation.
// XXX DiSiD: Invoke this method when add non other fields to builder
// http://projects.disid.com/issues/7455
private void excludeFieldsInToStringAnnotation(final String fieldName) {
if (toStringAnnotation == null) {
return;
}
final List<AnnotationAttributeValue<?>> attributes = new ArrayList<AnnotationAttributeValue<?>>();
final List<StringAttributeValue> ignoreFields = new ArrayList<StringAttributeValue>();
// Copy the existing attributes, excluding the "ignoreFields" attribute
boolean alreadyAdded = false;
AnnotationAttributeValue<?> value = toStringAnnotation.getAttribute(new JavaSymbolName("excludeFields"));
if (value == null) {
value = new ArrayAttributeValue<StringAttributeValue>(new JavaSymbolName("excludeFields"), new ArrayList<StringAttributeValue>());
}
// Ensure we have an array of strings
final String errMsg = "@RooToString attribute 'excludeFields' must be an array of strings";
Validate.isInstanceOf(ArrayAttributeValue.class, value, errMsg);
final ArrayAttributeValue<?> arrayVal = (ArrayAttributeValue<?>) value;
for (final Object obj : arrayVal.getValue()) {
Validate.isInstanceOf(StringAttributeValue.class, obj, errMsg);
final StringAttributeValue sv = (StringAttributeValue) obj;
if (sv.getValue().equals(fieldName)) {
alreadyAdded = true;
}
ignoreFields.add(sv);
}
// Add the desired field to ignore to the end
if (!alreadyAdded) {
ignoreFields.add(new StringAttributeValue(new JavaSymbolName("ignored"), fieldName));
}
attributes.add(new ArrayAttributeValue<StringAttributeValue>(new JavaSymbolName("excludeFields"), ignoreFields));
final AnnotationMetadataBuilder toStringAnnotationBuilder = new AnnotationMetadataBuilder(ROO_TO_STRING, attributes);
updatedGovernorBuilder = new ClassOrInterfaceTypeDetailsBuilder(governorTypeDetails);
toStringAnnotation = toStringAnnotationBuilder.build();
updatedGovernorBuilder.updateTypeAnnotation(toStringAnnotation, new HashSet<JavaSymbolName>());
}
use of org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue in project spring-roo by spring-projects.
the class DbreMetadata method getJoinColumnsAnnotation.
private AnnotationMetadataBuilder getJoinColumnsAnnotation(final Set<Reference> references, final JavaType fieldType) {
final List<NestedAnnotationAttributeValue> arrayValues = new ArrayList<NestedAnnotationAttributeValue>();
// Nullable attribute will have same value for each
// If some column not required, all JoinColumn will be nullable
boolean nullable = false;
for (final Reference reference : references) {
if (!reference.getLocalColumn().isRequired()) {
nullable = true;
}
}
for (final Reference reference : references) {
final AnnotationMetadataBuilder joinColumnAnnotation = getJoinColumnAnnotation(reference, true, fieldType, nullable);
arrayValues.add(new NestedAnnotationAttributeValue(new JavaSymbolName(VALUE), joinColumnAnnotation.build()));
}
final List<AnnotationAttributeValue<?>> attributes = new ArrayList<AnnotationAttributeValue<?>>();
attributes.add(new ArrayAttributeValue<NestedAnnotationAttributeValue>(new JavaSymbolName(VALUE), arrayValues));
return new AnnotationMetadataBuilder(JOIN_COLUMNS, attributes);
}
use of org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue in project spring-roo by spring-projects.
the class FinderOperationsImpl method installFinder.
public void installFinder(final JavaType entity, final JavaSymbolName finderName, JavaType formBean, JavaType returnType) {
Validate.notNull(entity, "ERROR: Entity type required to generate finder.");
Validate.notNull(finderName, "ERROR: Finder name required to generate finder.");
final String id = getTypeLocationService().getPhysicalTypeIdentifier(entity);
if (id == null) {
LOGGER.warning("Cannot locate source for '" + entity.getFullyQualifiedTypeName() + "'");
return;
}
// Check if provided entity is annotated with @RooJpaEntity
ClassOrInterfaceTypeDetails entityDetails = getTypeLocationService().getTypeDetails(entity);
AnnotationMetadata entityAnnotation = entityDetails.getAnnotation(RooJavaType.ROO_JPA_ENTITY);
Validate.notNull(entityAnnotation, "ERROR: Provided entity must be annotated with @RooJpaEntity");
// Getting repository that manages current entity
ClassOrInterfaceTypeDetails repository = getRepositoryJpaLocator().getRepository(entity);
// Entity must have a repository that manages it, if not, shows an error
Validate.notNull(repository, "ERROR: You must generate a repository to the provided entity before to add new finder. You could use 'repository jpa' commands.");
// Check if provided formBean contains the field names and types indicated as finder params
if (formBean != null) {
MemberDetails entityMemberDetails = getMemberDetailsScanner().getMemberDetails(this.getClass().getName(), getTypeLocationService().getTypeDetails(entity));
PartTree finderPartTree = new PartTree(finderName.getSymbolName(), entityMemberDetails);
checkDtoFieldsForFinder(getTypeLocationService().getTypeDetails(formBean), finderPartTree, repository.getType());
}
// Get repository annotation
AnnotationMetadata repositoryAnnotation = repository.getAnnotation(RooJavaType.ROO_REPOSITORY_JPA);
AnnotationMetadataBuilder repositoryAnnotationBuilder = new AnnotationMetadataBuilder(repositoryAnnotation);
// Create list that will include finders to add
List<AnnotationAttributeValue<?>> finders = new ArrayList<AnnotationAttributeValue<?>>();
// Check if new finder name to be included already exists in @RooRepositoryJpa annotation
AnnotationAttributeValue<?> currentFinders = repositoryAnnotation.getAttribute("finders");
if (currentFinders != null) {
List<?> values = (List<?>) currentFinders.getValue();
Iterator<?> it = values.iterator();
while (it.hasNext()) {
NestedAnnotationAttributeValue finder = (NestedAnnotationAttributeValue) it.next();
if (finder.getValue() != null && finder.getValue().getAttribute("value") != null) {
if (finder.getValue().getAttribute("value").getValue().equals(finderName.getSymbolName())) {
LOGGER.log(Level.WARNING, String.format("ERROR: Finder '%s' already exists on entity '%s'", finderName.getSymbolName(), entity.getSimpleTypeName()));
return;
}
finders.add(finder);
}
}
}
// Create @RooFinder
AnnotationMetadataBuilder singleFinderAnnotation = new AnnotationMetadataBuilder(RooJavaType.ROO_FINDER);
// Add finder attribute
singleFinderAnnotation.addStringAttribute("value", finderName.getSymbolName());
// Add returnType attribute
singleFinderAnnotation.addClassAttribute("returnType", returnType);
// Prevent errors validating if the return type contains a valid module
if (returnType.getModule() != null) {
getProjectOperations().addModuleDependency(repository.getName().getModule(), returnType.getModule());
}
// Add formBean attribute
if (formBean != null) {
singleFinderAnnotation.addClassAttribute("formBean", formBean);
getProjectOperations().addModuleDependency(repository.getName().getModule(), formBean.getModule());
}
NestedAnnotationAttributeValue newFinder = new NestedAnnotationAttributeValue(new JavaSymbolName("value"), singleFinderAnnotation.build());
// If not exists current finder, include it
finders.add(newFinder);
// Add finder list to currentFinders
ArrayAttributeValue<AnnotationAttributeValue<?>> newFinders = new ArrayAttributeValue<AnnotationAttributeValue<?>>(new JavaSymbolName("finders"), finders);
// Include finder name to finders attribute
repositoryAnnotationBuilder.addAttribute(newFinders);
// Update @RooRepositoryJpa annotation
final ClassOrInterfaceTypeDetailsBuilder cidBuilder = new ClassOrInterfaceTypeDetailsBuilder(repository);
// Update annotation
cidBuilder.updateTypeAnnotation(repositoryAnnotationBuilder);
// Save changes on disk
getTypeManagementService().createOrUpdateTypeOnDisk(cidBuilder.build());
}
use of org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue in project spring-roo by spring-projects.
the class JspOperationsImpl method getHttpPostMethod.
private MethodMetadataBuilder getHttpPostMethod(final String declaredByMetadataId) {
final List<AnnotationMetadataBuilder> postMethodAnnotations = new ArrayList<AnnotationMetadataBuilder>();
final List<AnnotationAttributeValue<?>> postMethodAttributes = new ArrayList<AnnotationAttributeValue<?>>();
postMethodAttributes.add(new EnumAttributeValue(new JavaSymbolName("method"), new EnumDetails(REQUEST_METHOD, new JavaSymbolName("POST"))));
postMethodAttributes.add(new StringAttributeValue(new JavaSymbolName("value"), "{id}"));
postMethodAnnotations.add(new AnnotationMetadataBuilder(REQUEST_MAPPING, postMethodAttributes));
final List<AnnotatedJavaType> postParamTypes = new ArrayList<AnnotatedJavaType>();
final AnnotationMetadataBuilder idParamAnnotation = new AnnotationMetadataBuilder(PATH_VARIABLE);
postParamTypes.add(new AnnotatedJavaType(new JavaType("java.lang.Long"), idParamAnnotation.build()));
postParamTypes.add(new AnnotatedJavaType(MODEL_MAP));
postParamTypes.add(new AnnotatedJavaType(HTTP_SERVLET_REQUEST));
postParamTypes.add(new AnnotatedJavaType(HTTP_SERVLET_RESPONSE));
final List<JavaSymbolName> postParamNames = new ArrayList<JavaSymbolName>();
postParamNames.add(new JavaSymbolName("id"));
postParamNames.add(new JavaSymbolName("modelMap"));
postParamNames.add(new JavaSymbolName("request"));
postParamNames.add(new JavaSymbolName("response"));
final MethodMetadataBuilder postMethodBuilder = new MethodMetadataBuilder(declaredByMetadataId, Modifier.PUBLIC, new JavaSymbolName("post"), JavaType.VOID_PRIMITIVE, postParamTypes, postParamNames, new InvocableMemberBodyBuilder());
postMethodBuilder.setAnnotations(postMethodAnnotations);
return postMethodBuilder;
}
Aggregations