use of org.springframework.roo.classpath.details.annotations.AnnotatedJavaType in project spring-roo by spring-projects.
the class ItdSourceFileComposer method writeInnerTypeConstructors.
private void writeInnerTypeConstructors(final JavaType innerType, final List<? extends ConstructorMetadata> constructors, final boolean defineTarget, final boolean isInterfaceMethod) {
for (final ConstructorMetadata constructor : constructors) {
Validate.isTrue(constructor.getParameterTypes().size() == constructor.getParameterNames().size(), "One constructor has mismatched parameter names against parameter types");
// Append annotations
for (final AnnotationMetadata annotation : constructor.getAnnotations()) {
appendIndent();
outputAnnotation(annotation);
this.newLine(false);
}
// Append "<modifier> <methodName>" portion
appendIndent();
if (constructor.getModifier() != 0) {
append(Modifier.toString(constructor.getModifier()));
append(" ");
}
append(innerType.getSimpleTypeName());
// Append parameter types and names
append("(");
final List<AnnotatedJavaType> parameterTypes = constructor.getParameterTypes();
final List<JavaSymbolName> parameterNames = constructor.getParameterNames();
for (int i = 0; i < parameterTypes.size(); i++) {
final AnnotatedJavaType paramType = parameterTypes.get(i);
final JavaSymbolName paramName = parameterNames.get(i);
for (final AnnotationMetadata methodParameterAnnotation : paramType.getAnnotations()) {
outputAnnotation(methodParameterAnnotation);
append(" ");
}
append(paramType.getJavaType().getNameIncludingTypeParameters(false, resolver));
append(" ");
append(paramName.getSymbolName());
if (i < parameterTypes.size() - 1) {
append(", ");
}
}
// Add exceptions to be thrown
final List<JavaType> throwsTypes = constructor.getThrowsTypes();
if (throwsTypes.size() > 0) {
append(") throws ");
for (int i = 0; i < throwsTypes.size(); i++) {
append(throwsTypes.get(i).getNameIncludingTypeParameters(false, resolver));
if (throwsTypes.size() > i + 1) {
append(", ");
}
}
} else {
append(")");
}
if (isInterfaceMethod) {
append(";");
} else {
append(" {");
this.newLine(false);
// Add body
indent();
append(constructor.getBody());
indentRemove();
appendFormalLine("}");
}
this.newLine();
}
}
use of org.springframework.roo.classpath.details.annotations.AnnotatedJavaType in project spring-roo by spring-projects.
the class ItdSourceFileComposer method writeMethods.
private void writeMethods(final List<? extends MethodMetadata> methods, final boolean defineTarget, final boolean isInterfaceMethod) {
for (final MethodMetadata method : methods) {
Validate.isTrue(method.getParameterTypes().size() == method.getParameterNames().size(), "Method %s has mismatched parameter names against parameter types", method.getMethodName().getSymbolName());
// ROO-3447: Append comments if exists
CommentStructure commentStructure = method.getCommentStructure();
if (commentStructure != null && commentStructure.getBeginComments() != null) {
List<AbstractComment> comments = commentStructure.getBeginComments();
String commentString = "";
boolean missingComponentsAdded = false;
for (AbstractComment comment : comments) {
// Join all JavadocComment's
if (comment instanceof JavadocComment) {
// Add JavaDoc missing components
if (!missingComponentsAdded) {
// Check params info
if (!method.getParameterNames().isEmpty() && ((JavadocComment) comment).getParamsInfo() == null) {
List<String> paramsInfo = new ArrayList<String>();
for (JavaSymbolName name : method.getParameterNames()) {
paramsInfo.add(name.getSymbolName());
}
((JavadocComment) comment).setParamsInfo(paramsInfo);
}
// Check return info
if (!method.getReturnType().equals(JavaType.VOID_OBJECT) && !method.getReturnType().equals(JavaType.VOID_PRIMITIVE) && ((JavadocComment) comment).getReturnInfo() == null) {
((JavadocComment) comment).setReturnInfo(method.getReturnType().getSimpleTypeName());
}
// Check throws info
if (!method.getThrowsTypes().isEmpty() && ((JavadocComment) comment).getThrowsInfo() == null) {
List<String> throwsInfo = new ArrayList<String>();
for (JavaType throwsType : method.getThrowsTypes()) {
throwsInfo.add(throwsType.getSimpleTypeName());
}
((JavadocComment) comment).setThrowsInfo(throwsInfo);
}
missingComponentsAdded = true;
}
commentString = commentString.concat(comment.getComment()).concat(IOUtils.LINE_SEPARATOR);
} else {
// Not JavadocComment, write comment as it is, unchanged
appendFormalLine(comment.getComment());
}
}
// Write JavadocComment's to ITD, in a single JavadocComment instance
String[] commentLines = commentString.split(IOUtils.LINE_SEPARATOR);
for (String commentLine : commentLines) {
appendFormalLine(commentLine);
}
} else {
// ROO-3834: Append default Javadoc if not exists a comment structure,
// including params, return and throws
CommentStructure defaultCommentStructure = new CommentStructure();
List<String> parameterNames = new ArrayList<String>();
for (JavaSymbolName name : method.getParameterNames()) {
parameterNames.add(name.getSymbolName());
}
List<String> throwsTypesNames = new ArrayList<String>();
for (JavaType type : method.getThrowsTypes()) {
throwsTypesNames.add(type.getSimpleTypeName());
}
String returnInfo = null;
JavaType returnType = method.getReturnType();
if (!returnType.equals(JavaType.VOID_OBJECT) && !returnType.equals(JavaType.VOID_PRIMITIVE)) {
returnInfo = returnType.getSimpleTypeName();
}
JavadocComment javadocComment = new JavadocComment("TODO Auto-generated method documentation", parameterNames, returnInfo, throwsTypesNames);
defaultCommentStructure.addComment(javadocComment, CommentLocation.BEGINNING);
method.setCommentStructure(defaultCommentStructure);
// Now lines should be formatted, so write them
String[] comment = javadocComment.getComment().split(IOUtils.LINE_SEPARATOR);
for (String line : comment) {
appendFormalLine(line);
}
}
// Append annotations
for (final AnnotationMetadata annotation : method.getAnnotations()) {
appendIndent();
outputAnnotation(annotation);
this.newLine(false);
}
// Append "<modifier> <genericDefinition> <returnType> <methodName>" portion
appendIndent();
// modifier
if (method.getModifier() != 0) {
append(Modifier.toString(method.getModifier()));
append(" ");
}
// ROO-3648: genericDefinition
if (method.getGenericDefinition() != null && !method.getGenericDefinition().trim().equals("")) {
append("<".concat(method.getGenericDefinition()).concat(">"));
append(" ");
}
// return type
append(method.getReturnType().getNameIncludingTypeParameters(false, resolver));
append(" ");
if (defineTarget) {
append(introductionTo.getSimpleTypeName());
append(".");
}
append(method.getMethodName().getSymbolName());
// Append parameter types and names
append("(");
final List<AnnotatedJavaType> parameterTypes = method.getParameterTypes();
final List<JavaSymbolName> parameterNames = method.getParameterNames();
for (int i = 0; i < parameterTypes.size(); i++) {
final AnnotatedJavaType paramType = parameterTypes.get(i);
final JavaSymbolName paramName = parameterNames.get(i);
for (final AnnotationMetadata methodParameterAnnotation : paramType.getAnnotations()) {
outputAnnotation(methodParameterAnnotation);
append(" ");
}
append(paramType.getJavaType().getNameIncludingTypeParameters(false, resolver));
append(" ");
append(paramName.getSymbolName());
if (i < parameterTypes.size() - 1) {
append(", ");
}
}
// Add exceptions to be thrown
final List<JavaType> throwsTypes = method.getThrowsTypes();
if (throwsTypes.size() > 0) {
append(") throws ");
for (int i = 0; i < throwsTypes.size(); i++) {
append(throwsTypes.get(i).getNameIncludingTypeParameters(false, resolver));
if (throwsTypes.size() > i + 1) {
append(", ");
}
}
} else {
append(")");
}
if (isInterfaceMethod) {
append(";");
this.newLine(false);
} else {
append(" {");
this.newLine(false);
// Add body
indent();
append(method.getBody());
indentRemove();
appendFormalLine("}");
}
this.newLine();
}
}
use of org.springframework.roo.classpath.details.annotations.AnnotatedJavaType in project spring-roo by spring-projects.
the class JmsOperationsImpl method createReceiverJmsService.
private void createReceiverJmsService(JavaType service, String destinationProperty) {
// Create new service class
final String serviceClassIdentifier = getPathResolver().getCanonicalPath(service.getModule(), Path.SRC_MAIN_JAVA, service);
final String mid = PhysicalTypeIdentifier.createIdentifier(service, getPathResolver().getPath(serviceClassIdentifier));
ClassOrInterfaceTypeDetailsBuilder cidBuilder = new ClassOrInterfaceTypeDetailsBuilder(mid, Modifier.PUBLIC, service, PhysicalTypeCategory.CLASS);
// Create new @Service annotation
AnnotationMetadataBuilder serviceAnnotation = new AnnotationMetadataBuilder(SpringJavaType.SERVICE);
cidBuilder.addAnnotation(serviceAnnotation);
// Add method receiveJmsMessage
// @JmsListener(destination =
// "${application.jms.queue.plaintext.jndi-name}")
// public void receiveJmsMessage(String msg) {
//
// }
// Define methodName
final JavaSymbolName methodName = new JavaSymbolName("receiveJmsMessage");
// Define parameters
List<AnnotatedJavaType> parameterTypes = new ArrayList<AnnotatedJavaType>();
parameterTypes.add(new AnnotatedJavaType(JavaType.STRING));
final List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>();
parameterNames.add(new JavaSymbolName("msg"));
// Adding annotations
final List<AnnotationMetadataBuilder> annotations = new ArrayList<AnnotationMetadataBuilder>();
// Adding @JmsListener annotation
AnnotationMetadataBuilder jmsListenerAnnotation = new AnnotationMetadataBuilder(SpringJavaType.JMS_LISTENER);
jmsListenerAnnotation.addStringAttribute("destination", "${".concat(destinationProperty).concat("}"));
annotations.add(jmsListenerAnnotation);
// Generate body
InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
bodyBuilder.newLine();
bodyBuilder.appendFormalLine(" // To be implemented");
MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(mid, Modifier.PUBLIC, methodName, JavaType.VOID_PRIMITIVE, parameterTypes, parameterNames, bodyBuilder);
methodBuilder.setAnnotations(annotations);
cidBuilder.addMethod(methodBuilder);
getTypeManagementService().createOrUpdateTypeOnDisk(cidBuilder.build());
}
use of org.springframework.roo.classpath.details.annotations.AnnotatedJavaType in project spring-roo by spring-projects.
the class SecurityOperationsImpl method generateAuthorizeAnnotations.
@Override
public void generateAuthorizeAnnotations(JavaType klass, String methodName, String roles, String usernames) {
Validate.notNull(klass, "ERROR: klass parameter is mandatory on 'generateAuthorizeAnnotations' method");
Validate.notNull(methodName, "ERROR: method parameter is mandatory on 'generateAuthorizeAnnotations' method");
// Get methods to annotate.
// With the last parameter to false, we avoid that push in action occurs.
List<Object> pushedElements = getPushInOperations().pushIn(klass.getPackage(), klass, methodName, false);
List<AnnotationAttributeValue<?>> rooSecurityAuthorizationsToAdd = new ArrayList<AnnotationAttributeValue<?>>();
for (Object pushedElement : pushedElements) {
if (pushedElement instanceof DefaultMethodMetadata) {
DefaultMethodMetadata method = (DefaultMethodMetadata) pushedElement;
// Get parameters
List<AnnotationAttributeValue<?>> lstParamTypes = new ArrayList<AnnotationAttributeValue<?>>();
List<AnnotatedJavaType> parameterTypes = method.getParameterTypes();
Iterator<AnnotatedJavaType> iterParamTypes = parameterTypes.iterator();
while (iterParamTypes.hasNext()) {
ClassAttributeValue parameterAttributeValue = new ClassAttributeValue(new JavaSymbolName("value"), iterParamTypes.next().getJavaType());
lstParamTypes.add(parameterAttributeValue);
}
// Generate new annotations @RooSecurityAuthorization
NestedAnnotationAttributeValue newFilter = new NestedAnnotationAttributeValue(new JavaSymbolName("value"), getRooSecurityAuthorizationsAnnotation(method.getMethodName().getSymbolName(), lstParamTypes, roles, usernames).build());
rooSecurityAuthorizationsToAdd.add(newFilter);
}
}
// Get actual values of @RooSecurityAuthorizations
ClassOrInterfaceTypeDetails serviceDetails = getTypeLocationService().getTypeDetails(klass);
ClassOrInterfaceTypeDetailsBuilder cidBuilder = new ClassOrInterfaceTypeDetailsBuilder(serviceDetails);
// Check annotation @RooSecurityAuthorizations to delete defined annotations
// that will be redefined
AnnotationMetadata annotationAuthorizations = serviceDetails.getAnnotation(RooJavaType.ROO_SECURITY_AUTHORIZATIONS);
AnnotationMetadataBuilder annotationAuthorizationsMetadataBuilder;
if (annotationAuthorizations != null) {
// Getting authorizations from annotation
AnnotationAttributeValue<?> attributeAuthorizations = annotationAuthorizations.getAttribute("authorizations");
List<?> values = (List<?>) attributeAuthorizations.getValue();
if (values != null && !values.isEmpty()) {
Iterator<?> valuesIt = values.iterator();
while (valuesIt.hasNext()) {
NestedAnnotationAttributeValue authorizationAnnotation = (NestedAnnotationAttributeValue) valuesIt.next();
if (checkRooSecurityAuthorizationMaintainAnnotation(rooSecurityAuthorizationsToAdd, authorizationAnnotation)) {
// Maintain annotation if 'method' or 'parameters' are different
rooSecurityAuthorizationsToAdd.add(authorizationAnnotation);
}
}
}
annotationAuthorizationsMetadataBuilder = new AnnotationMetadataBuilder(annotationAuthorizations);
// remove annotation
cidBuilder.removeAnnotation(RooJavaType.ROO_SECURITY_AUTHORIZATIONS);
} else {
// Doesn't exist @RooSecurityAuthorizations, create it
annotationAuthorizationsMetadataBuilder = new AnnotationMetadataBuilder(RooJavaType.ROO_SECURITY_AUTHORIZATIONS);
}
// Add authorizations attribute
ArrayAttributeValue<AnnotationAttributeValue<?>> newAuthorizations = new ArrayAttributeValue<AnnotationAttributeValue<?>>(new JavaSymbolName("authorizations"), rooSecurityAuthorizationsToAdd);
annotationAuthorizationsMetadataBuilder.addAttribute(newAuthorizations);
// Include new @RooSecurityAuthorizations annotation
cidBuilder.addAnnotation(annotationAuthorizationsMetadataBuilder);
// Write on disk
getTypeManagementService().createOrUpdateTypeOnDisk(cidBuilder.build());
// Add Spring Security dependency
getProjectOperations().addDependency(klass.getModule(), SPRING_SECURITY_CORE, false);
}
use of org.springframework.roo.classpath.details.annotations.AnnotatedJavaType in project spring-roo by spring-projects.
the class SecurityOperationsImpl method generateFilterAnnotations.
public void generateFilterAnnotations(JavaType klass, String methodName, String roles, String usernames, String when) {
// Get methods to annotate.
// With the last parameter to false, we avoid that push in action occurs.
List<Object> pushedElements = getPushInOperations().pushIn(klass.getPackage(), klass, methodName, false);
List<AnnotationAttributeValue<?>> rooSecurityFiltersToAdd = new ArrayList<AnnotationAttributeValue<?>>();
for (Object pushedElement : pushedElements) {
if (pushedElement instanceof DefaultMethodMetadata) {
DefaultMethodMetadata method = (DefaultMethodMetadata) pushedElement;
// Get parameters
List<AnnotationAttributeValue<?>> lstParamTypes = new ArrayList<AnnotationAttributeValue<?>>();
List<AnnotatedJavaType> parameterTypes = method.getParameterTypes();
Iterator<AnnotatedJavaType> iterParamTypes = parameterTypes.iterator();
while (iterParamTypes.hasNext()) {
ClassAttributeValue parameterAttributeValue = new ClassAttributeValue(new JavaSymbolName("value"), iterParamTypes.next().getJavaType());
lstParamTypes.add(parameterAttributeValue);
}
// Generate new annotations @RooSecurityFilter
NestedAnnotationAttributeValue newFilter = new NestedAnnotationAttributeValue(new JavaSymbolName("value"), getRooSecurityFilterAnnotation(method.getMethodName().getSymbolName(), lstParamTypes, roles, usernames, when).build());
rooSecurityFiltersToAdd.add(newFilter);
}
}
// Get actual values of @RooSecurityFilters
ClassOrInterfaceTypeDetails serviceDetails = getTypeLocationService().getTypeDetails(klass);
ClassOrInterfaceTypeDetailsBuilder cidBuilder = new ClassOrInterfaceTypeDetailsBuilder(serviceDetails);
// Check annotation @RooSecurityFilters to delete defined annotations
// that will be redefined
AnnotationMetadata annotationFilters = serviceDetails.getAnnotation(RooJavaType.ROO_SECURITY_FILTERS);
AnnotationMetadataBuilder annotationFiltersMetadataBuilder;
if (annotationFilters != null) {
// Getting filters from annotation
AnnotationAttributeValue<?> attributeFilters = annotationFilters.getAttribute("filters");
List<?> values = (List<?>) attributeFilters.getValue();
if (values != null && !values.isEmpty()) {
Iterator<?> valuesIt = values.iterator();
while (valuesIt.hasNext()) {
NestedAnnotationAttributeValue filterAnnotation = (NestedAnnotationAttributeValue) valuesIt.next();
if (checkRooSecurityFilterMaintainAnnotation(rooSecurityFiltersToAdd, filterAnnotation)) {
// Maintain annotation if 'method', 'parameters' or 'when' are different
rooSecurityFiltersToAdd.add(filterAnnotation);
}
}
}
annotationFiltersMetadataBuilder = new AnnotationMetadataBuilder(annotationFilters);
// remove annotation
cidBuilder.removeAnnotation(RooJavaType.ROO_SECURITY_FILTERS);
} else {
// Doesn't exist @RooSecurityFilters, create it
annotationFiltersMetadataBuilder = new AnnotationMetadataBuilder(RooJavaType.ROO_SECURITY_FILTERS);
}
// Add filters attribute
ArrayAttributeValue<AnnotationAttributeValue<?>> newFilters = new ArrayAttributeValue<AnnotationAttributeValue<?>>(new JavaSymbolName("filters"), rooSecurityFiltersToAdd);
annotationFiltersMetadataBuilder.addAttribute(newFilters);
// Include new @RooSecurityFilters annotation
cidBuilder.addAnnotation(annotationFiltersMetadataBuilder);
// Write on disk
getTypeManagementService().createOrUpdateTypeOnDisk(cidBuilder.build());
// Add Spring Security dependency
getProjectOperations().addDependency(klass.getModule(), SPRING_SECURITY_CORE, false);
}
Aggregations