use of org.springframework.roo.classpath.details.annotations.AnnotatedJavaType in project spring-roo by spring-projects.
the class EntityDeserializerMetadata method getDeserializeMethod.
private MethodMetadata getDeserializeMethod() {
// Define methodName
final JavaSymbolName methodName = new JavaSymbolName("deserializeObject");
// Adding parameter types
List<AnnotatedJavaType> parameterTypes = new ArrayList<AnnotatedJavaType>();
parameterTypes.add(AnnotatedJavaType.convertFromJavaType(JSON_PARSER));
parameterTypes.add(AnnotatedJavaType.convertFromJavaType(DESERIALIZATION_CONTEXT));
parameterTypes.add(AnnotatedJavaType.convertFromJavaType(OBJECT_CODEC));
parameterTypes.add(AnnotatedJavaType.convertFromJavaType(JSON_NODE));
MethodMetadata existingMethod = getGovernorMethod(methodName, AnnotatedJavaType.convertFromAnnotatedJavaTypes(parameterTypes));
if (existingMethod != null) {
return existingMethod;
}
// Adding parameter names
final List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>();
parameterNames.add(new JavaSymbolName("jsonParser"));
parameterNames.add(new JavaSymbolName("context"));
parameterNames.add(new JavaSymbolName("codec"));
parameterNames.add(new JavaSymbolName("tree"));
// Adding annotations
final List<AnnotationMetadataBuilder> annotations = new ArrayList<AnnotationMetadataBuilder>();
// Generate body
InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
final JavaType idType = getTypeToUseAsIdentifier(entityMetadata.getCurrentIndentifierField().getFieldType());
// String idText = tree.asText();
bodyBuilder.appendFormalLine("String idText = tree.asText();");
// Long id = conversionService.convert(idText, Long.class);
bodyBuilder.appendFormalLine("%s %s = %s.convert(idText, %s.class);", getNameOfJavaType(idType), entityMetadata.getCurrentIndentifierField().getFieldName(), conversionServiceField.getFieldName(), getNameOfJavaType(idType));
final String entityItemName = StringUtils.uncapitalize(entityMetadata.getDestination().getSimpleTypeName());
// Product product = productSercie.findOne(id);
bodyBuilder.appendFormalLine("%s %s = %s.%s(%s);", getNameOfJavaType(entityMetadata.getDestination()), entityItemName, serviceField.getFieldName(), serviceMetadata.getCurrentFindOneMethod().getMethodName(), entityMetadata.getCurrentIndentifierField().getFieldName());
// if (product == null) {
bodyBuilder.appendFormalLine("if (%s == null) {", entityItemName);
// throw new NotFoundException("Product not found");
bodyBuilder.indent();
bodyBuilder.appendFormalLine("throw new %s(\"%s not found\");", getNameOfJavaType(SpringletsJavaType.SPRINGLETS_NOT_FOUND_EXCEPTION), entityMetadata.getDestination().getSimpleTypeName());
bodyBuilder.indentRemove();
// }
bodyBuilder.appendFormalLine("}");
// return product;
bodyBuilder.appendFormalLine("return %s;", entityItemName);
MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, entityMetadata.getDestination(), parameterTypes, parameterNames, bodyBuilder);
methodBuilder.setAnnotations(annotations);
methodBuilder.addThrowsType(IO_EXCEPTION);
return methodBuilder.build();
}
use of org.springframework.roo.classpath.details.annotations.AnnotatedJavaType in project spring-roo by spring-projects.
the class ExceptionsMetadata method getHandlerExceptionMethod.
private MethodMetadata getHandlerExceptionMethod(JavaType exception, ClassOrInterfaceTypeDetails exceptionDetails) {
// Define annotations
List<AnnotationMetadataBuilder> annotationTypes = new ArrayList<AnnotationMetadataBuilder>();
AnnotationMetadataBuilder responsebody = new AnnotationMetadataBuilder(SpringJavaType.RESPONSE_BODY);
AnnotationMetadataBuilder exceptionHandler = new AnnotationMetadataBuilder(SpringJavaType.EXCEPTION_HANDLER);
exceptionHandler.addClassAttribute("value", exception);
annotationTypes.add(responsebody);
annotationTypes.add(exceptionHandler);
// Define method parameter types
List<AnnotatedJavaType> parameterTypes = new ArrayList<AnnotatedJavaType>();
parameterTypes.add(AnnotatedJavaType.convertFromJavaType(HTTP_SERVLET_REQUEST));
parameterTypes.add(AnnotatedJavaType.convertFromJavaType(JdkJavaType.EXCEPTION));
parameterTypes.add(AnnotatedJavaType.convertFromJavaType(JdkJavaType.LOCALE));
// Define method parameter names
List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>();
parameterNames.add(new JavaSymbolName(HTTP_SERVLET_REQUEST_PARAM_NAME));
parameterNames.add(new JavaSymbolName(EXCEPTION_PARAM_NAME));
parameterNames.add(new JavaSymbolName(LOCALE_PARAM_NAME));
// Define method name
JavaSymbolName methodName = new JavaSymbolName("handle".concat(exception.getSimpleTypeName()));
MethodMetadata existingMethod = getGovernorMethod(methodName);
if (existingMethod != null) {
return existingMethod;
}
// Set throws types
List<JavaType> throwTypes = new ArrayList<JavaType>();
throwTypes.add(JdkJavaType.EXCEPTION);
// Generate body
InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
// String errorMessage =
bodyBuilder.appendFormalLine(String.format("%s errorMessage = ", getNameOfJavaType(JavaType.STRING)));
// this.messageSource.getMessage("label_my_exception", null, locale);
bodyBuilder.indent();
bodyBuilder.appendFormalLine(String.format("this.%s.getMessage(\"label_%s\", null, locale);", this.messageSourceField.getFieldName(), exception.getSimpleTypeName().toLowerCase()));
bodyBuilder.indentRemove();
// LOG.error(errorMessage, e);
bodyBuilder.appendFormalLine("LOG.error(errorMessage, e);");
// return ResponseEntity.status(HttpStatus.ERROR_CODE)
// .body(Collections.singletonMap("message", errorMessage));
bodyBuilder.appendFormalLine(String.format("return %s.status(%s)", getNameOfJavaType(SpringJavaType.RESPONSE_ENTITY), exceptionDetails.getAnnotation(SpringJavaType.RESPONSE_STATUS).getAttribute("value").getValue()));
bodyBuilder.indent();
bodyBuilder.appendFormalLine(String.format(".body(%s.singletonMap(\"message\", errorMessage));", getNameOfJavaType(new JavaType(Collections.class))));
bodyBuilder.indentRemove();
MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, SpringJavaType.RESPONSE_ENTITY, parameterTypes, parameterNames, bodyBuilder);
methodBuilder.setThrowsTypes(throwTypes);
methodBuilder.setAnnotations(annotationTypes);
return methodBuilder.build();
}
use of org.springframework.roo.classpath.details.annotations.AnnotatedJavaType in project spring-roo by spring-projects.
the class ExceptionsMetadata method getViewHandlerExceptionMethod.
private MethodMetadata getViewHandlerExceptionMethod(JavaType exception, String errorView) {
// Define annotations
List<AnnotationMetadataBuilder> annotationTypes = new ArrayList<AnnotationMetadataBuilder>();
AnnotationMetadataBuilder exceptionHandler = new AnnotationMetadataBuilder(SpringJavaType.EXCEPTION_HANDLER);
exceptionHandler.addClassAttribute("value", exception);
annotationTypes.add(exceptionHandler);
// Define method parameter types
List<AnnotatedJavaType> parameterTypes = new ArrayList<AnnotatedJavaType>();
parameterTypes.add(AnnotatedJavaType.convertFromJavaType(HTTP_SERVLET_REQUEST));
parameterTypes.add(AnnotatedJavaType.convertFromJavaType(JdkJavaType.EXCEPTION));
parameterTypes.add(AnnotatedJavaType.convertFromJavaType(JdkJavaType.LOCALE));
// Define method parameter names
List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>();
parameterNames.add(new JavaSymbolName(HTTP_SERVLET_REQUEST_PARAM_NAME));
parameterNames.add(new JavaSymbolName(EXCEPTION_PARAM_NAME));
parameterNames.add(new JavaSymbolName(LOCALE_PARAM_NAME));
// Define method name
JavaSymbolName methodName = new JavaSymbolName("handle".concat(exception.getSimpleTypeName()));
MethodMetadata existingMethod = getGovernorMethod(methodName);
if (existingMethod != null) {
return existingMethod;
}
// Set throws types
List<JavaType> throwTypes = new ArrayList<JavaType>();
throwTypes.add(JdkJavaType.EXCEPTION);
// Generate body
InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
// if (AnnotationUtils.findAnnotation(e.getClass(), ResponseStatus.class) != null) {
bodyBuilder.appendFormalLine(String.format("if (%s.findAnnotation(e.getClass(), %s.class) != null) {", getNameOfJavaType(SpringJavaType.ANNOTATION_UTILS), getNameOfJavaType(SpringJavaType.RESPONSE_STATUS)));
bodyBuilder.indent();
// throw e;
bodyBuilder.appendFormalLine("throw e;");
bodyBuilder.indentRemove();
bodyBuilder.appendFormalLine("}");
// String errorMessage =
bodyBuilder.appendFormalLine(String.format("%s errorMessage = ", getNameOfJavaType(JavaType.STRING)));
// this.messageSource.getMessage("label_my_exception", null, locale);
bodyBuilder.indent();
bodyBuilder.appendFormalLine(String.format("this.%s.getMessage(\"label_%s\", null, locale);", this.messageSourceField.getFieldName(), exception.getSimpleTypeName().toLowerCase()));
bodyBuilder.indentRemove();
// LOG.error(errorMessage, e);
bodyBuilder.appendFormalLine("LOG.error(errorMessage, e);");
// ModelAndView mav = new ModelAndView();
bodyBuilder.appendFormalLine(String.format("%s mav = new %s();", getNameOfJavaType(SpringJavaType.MODEL_AND_VIEW), getNameOfJavaType(SpringJavaType.MODEL_AND_VIEW)));
// mav.addObject("exception", e);
bodyBuilder.appendFormalLine("mav.addObject(\"exception\", e);");
// mav.addObject("message", errorMessage);
bodyBuilder.appendFormalLine("mav.addObject(\"message\", errorMessage);");
// mav.addObject("url", req.getRequestURL());
bodyBuilder.appendFormalLine("mav.addObject(\"url\", req.getRequestURL());");
// mav.setViewName("errorView");
bodyBuilder.appendFormalLine(String.format("mav.setViewName(\"%s\");", errorView));
// return mav;
bodyBuilder.appendFormalLine("return mav;");
MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, SpringJavaType.MODEL_AND_VIEW, parameterTypes, parameterNames, bodyBuilder);
methodBuilder.setThrowsTypes(throwTypes);
methodBuilder.setAnnotations(annotationTypes);
return methodBuilder.build();
}
use of org.springframework.roo.classpath.details.annotations.AnnotatedJavaType in project spring-roo by spring-projects.
the class SecurityAuthorizationsMetadataProviderImpl method checkParameters.
/**
* Check that the parameters of the method are equals of parameters list
*
* @param method Method to check
* @param methodParametersToCompare Parameters to compare
* @return true if are equals, false otherwise
*/
private boolean checkParameters(MethodMetadata method, List<?> methodParametersToCompare) {
boolean parametersAreEquals = true;
List<AnnotatedJavaType> parameterTypes = method.getParameterTypes();
if (methodParametersToCompare.size() != parameterTypes.size()) {
parametersAreEquals = false;
} else {
for (int i = 0; i < methodParametersToCompare.size(); i++) {
ClassAttributeValue methodParameterToCompare = (ClassAttributeValue) methodParametersToCompare.get(i);
AnnotatedJavaType parameterJavaType = parameterTypes.get(i);
if (!methodParameterToCompare.getValue().getSimpleTypeName().equals(parameterJavaType.getJavaType().getSimpleTypeName())) {
parametersAreEquals = false;
break;
}
}
}
return parametersAreEquals;
}
use of org.springframework.roo.classpath.details.annotations.AnnotatedJavaType in project spring-roo by spring-projects.
the class PushInOperationsImpl method pushInClass.
/**
* Makes push-in of all items defined on a provided class
*
* @param klass
* class to make the push-in operation
* @param weiteOnDisk
* indicates if pushed elements should be writed on .java file
* @param force
* if some operation will produce several changes, this parameter
* should be true.
*
* @return list of objects with all the pushed elements.
*/
public List<Object> pushInClass(JavaType klass, boolean writeOnDisk, boolean force) {
List<Object> pushedElements = new ArrayList<Object>();
// Check if current klass exists
Validate.notNull(klass, "ERROR: You must specify a valid class to continue with push-in action");
// Getting class details
ClassOrInterfaceTypeDetails classDetails = getTypeLocationService().getTypeDetails(klass);
Validate.notNull(klass, "ERROR: You must specify a valid class to continue with push-in action");
// String builder where changes will be registered
StringBuilder changesToApply = new StringBuilder();
// Getting member details
MemberDetails memberDetails = getMemberDetailsScanner().getMemberDetails(getClass().getName(), classDetails);
List<MemberHoldingTypeDetails> memberHoldingTypes = memberDetails.getDetails();
// Return if the class has not associated ITD's
if (memberHoldingTypes.size() == 1 && memberHoldingTypes.get(0).getPhysicalTypeCategory() != PhysicalTypeCategory.ITD) {
return pushedElements;
}
// Check if the provided class is a test to be able to select valid
// class path
Path path = classDetails.getAnnotation(RooJavaType.ROO_JPA_UNIT_TEST) == null ? Path.SRC_MAIN_JAVA : Path.SRC_TEST_JAVA;
// Getting current class .java file metadata ID
final String declaredByMetadataId = PhysicalTypeIdentifier.createIdentifier(klass, getPathResolver().getPath(klass.getModule(), path));
// Getting detailsBuilder
ClassOrInterfaceTypeDetailsBuilder detailsBuilder = new ClassOrInterfaceTypeDetailsBuilder(classDetails);
// Getting all details
for (final MemberHoldingTypeDetails memberHoldingTypeDetails : memberDetails.getDetails()) {
// this .java file
if (!memberHoldingTypeDetails.getType().equals(classDetails.getType())) {
continue;
}
// Getting all declared methods (including declared on ITDs
// and .java files)
List<MethodMetadata> allDeclaredMethods = memberHoldingTypeDetails.getMethods();
// Checking if is necessary to make push-in for all declared methods
for (MethodMetadata method : allDeclaredMethods) {
// If method exists on .aj file, add it!
if (method.getDeclaredByMetadataId().split("\\?").length > 1 && method.getDeclaredByMetadataId().split("\\?")[1].equals(klass.getFullyQualifiedTypeName()) && !method.getDeclaredByMetadataId().equals(declaredByMetadataId)) {
// Add method to .java file
MethodMetadata newMethod = getNewMethod(declaredByMetadataId, method);
detailsBuilder.addMethod(newMethod);
// Save changes on pushed elements list
pushedElements.add(newMethod);
changesToApply.append(String.format("Method '%s' will be pushed on '%s.java' class. \n", method.getMethodName(), klass.getSimpleTypeName()));
}
}
// Getting all declared fields (including declared on ITDs
// and .java files)
List<? extends FieldMetadata> allDeclaredFields = memberHoldingTypeDetails.getDeclaredFields();
// Checking if is necessary to make push-in for all declared fields
for (FieldMetadata field : allDeclaredFields) {
// If field exists on .aj file, add it!
if (field.getDeclaredByMetadataId().split("\\?").length > 1 && field.getDeclaredByMetadataId().split("\\?")[1].equals(klass.getFullyQualifiedTypeName()) && !field.getDeclaredByMetadataId().equals(declaredByMetadataId)) {
// Add field to .java file
FieldMetadata newField = getNewField(declaredByMetadataId, field);
detailsBuilder.addField(newField);
// Save changes on pushed elements list
pushedElements.add(newField);
changesToApply.append(String.format("Field '%s' will be pushed on '%s.java' class. \n", field.getFieldName(), klass.getSimpleTypeName()));
}
}
// Getting all declared constructors (including declared on ITDs and
// .java files)
List<? extends ConstructorMetadata> allDeclaredConstructors = memberHoldingTypeDetails.getDeclaredConstructors();
// constructors
for (ConstructorMetadata constructor : allDeclaredConstructors) {
// Check if current constructor exists on .java file
classDetails = getTypeLocationService().getTypeDetails(detailsBuilder.build().getType());
List<JavaType> parameterTypes = new ArrayList<JavaType>();
for (AnnotatedJavaType type : constructor.getParameterTypes()) {
parameterTypes.add(type.getJavaType());
}
ConstructorMetadata javaDeclaredConstructor = classDetails.getDeclaredConstructor(parameterTypes);
// If not exists, add it!
if (javaDeclaredConstructor == null) {
// Add constructor to .java file
detailsBuilder.addConstructor(constructor);
// Save changes on pushed elements list
pushedElements.add(constructor);
String constructorParametersNames = "";
for (JavaSymbolName paramName : constructor.getParameterNames()) {
constructorParametersNames = constructorParametersNames.concat(paramName.getSymbolName()).concat(", ");
changesToApply.append(String.format("Constructor with parameters '%s' will be pushed on '%s.java' class. \n", constructorParametersNames.substring(0, constructorParametersNames.length() - 2), klass.getSimpleTypeName()));
}
}
}
// Getting all declared annotations (including declared on ITDs
// and .java files)
List<AnnotationMetadata> allDeclaredAnnotations = memberHoldingTypeDetails.getAnnotations();
for (AnnotationMetadata annotation : allDeclaredAnnotations) {
// Check if current annotation exists on .java file
classDetails = getTypeLocationService().getTypeDetails(detailsBuilder.build().getType());
List<AnnotationMetadata> javaDeclaredAnnotations = classDetails.getAnnotations();
boolean annotationExists = false;
for (AnnotationMetadata javaAnnotation : javaDeclaredAnnotations) {
if (javaAnnotation.getAnnotationType().getFullyQualifiedTypeName().equals(annotation.getAnnotationType().getFullyQualifiedTypeName())) {
annotationExists = true;
}
}
// If not exists, add it!
if (!annotationExists) {
// Add annotation to .java file
detailsBuilder.addAnnotation(annotation);
// Save changes on pushed elements list
pushedElements.add(annotation);
changesToApply.append(String.format("Annotation '%s' will be pushed on '%s.java' class. \n", annotation.getAnnotationType().getSimpleTypeName(), klass.getSimpleTypeName()));
}
}
// Getting all extends registered on .aj file to move to .java file
List<JavaType> allExtendsTypes = memberHoldingTypeDetails.getExtendsTypes();
for (JavaType extendsType : allExtendsTypes) {
// If extends exists on .aj file, add it!
if (!detailsBuilder.getExtendsTypes().contains(extendsType)) {
detailsBuilder.addExtendsTypes(extendsType);
// Save changes on pushed elements list
pushedElements.add(extendsType);
changesToApply.append(String.format("Extends type '%s' will be pushed on '%s.java' class. \n", extendsType.getSimpleTypeName(), klass.getSimpleTypeName()));
}
}
// Getting all implements registered on .aj file to move to .java
// file
List<JavaType> allImplementsTypes = memberHoldingTypeDetails.getImplementsTypes();
for (JavaType implementsType : allImplementsTypes) {
if (!detailsBuilder.getImplementsTypes().contains(implementsType)) {
detailsBuilder.addImplementsType(implementsType);
// Save changes on pushed elements list
pushedElements.add(implementsType);
changesToApply.append(String.format("Implements type '%s' will be pushed on '%s.java' class. \n", implementsType.getSimpleTypeName(), klass.getSimpleTypeName()));
}
}
// Getting all imports registered on .aj file to move to .java file
Set<ImportMetadata> allRegisteredImports = memberHoldingTypeDetails.getImports();
detailsBuilder.addImports(allRegisteredImports);
// Save changes on pushed elements list
pushedElements.add(allRegisteredImports);
}
// Updating .java file
if (!force) {
// Show message to be able to know which changes will be applied
if (changesToApply.length() > 0) {
LOGGER.log(Level.INFO, changesToApply.toString());
}
} else if (writeOnDisk) {
getTypeManagementService().createOrUpdateTypeOnDisk(detailsBuilder.build());
}
return pushedElements;
}
Aggregations