use of org.springframework.roo.classpath.details.MethodMetadata in project spring-roo by spring-projects.
the class RepositoryJpaCustomImplMetadata method getFindByReferencedFieldsImpl.
/**
* Method that generates the findBy referenced fields implementation method
*
* @param the FieldMetadata of the referenced field
* @param method to implement
* @param referencedPathFieldName the String with the the referenced field name
* in "path" format.
* @param ids the entity id fields
* @param fields the entity fields to search for
*
* @return
*/
private MethodMetadata getFindByReferencedFieldsImpl(FieldMetadata referencedField, MethodMetadata method, String referencedPathFieldName, List<FieldMetadata> fields) {
// Define method name
JavaSymbolName methodName = method.getMethodName();
// Define method parameter types
List<AnnotatedJavaType> parameterTypes = method.getParameterTypes();
// Define method parameter names
List<JavaSymbolName> parameterNames = method.getParameterNames();
MethodMetadata existingMethod = getGovernorMethod(methodName, AnnotatedJavaType.convertFromAnnotatedJavaTypes(parameterTypes));
if (existingMethod != null) {
return existingMethod;
}
// Use provided findAllByReference method to generate its implementation
MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, method.getReturnType(), parameterTypes, parameterNames, null);
// Generate body
InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
// Getting variable name to use in the code
JavaSymbolName referencedFieldParamName = parameterNames.get(0);
JavaSymbolName globalSearch = parameterNames.get(1);
JavaSymbolName pageable = parameterNames.get(2);
String entity = this.entity.getSimpleTypeName();
String entityVariable = StringUtils.uncapitalize(entity);
// Types to import
JavaType projection = QUERYDSL_PROJECTIONS;
bodyBuilder.newLine();
// QEntity qEntity = QEntity.entity;
bodyBuilder.appendFormalLine(String.format("%1$s %2$s = %1$s.%2$s;", entityQtype.getNameIncludingTypeParameters(false, importResolver), entityVariable));
bodyBuilder.newLine();
// Construct query
buildQuery(bodyBuilder, entityVariable, globalSearch, referencedFieldParamName, referencedField, referencedPathFieldName, null, null, this.defaultReturnType, null, null);
bodyBuilder.newLine();
// AttributeMappingBuilder mapping = buildMapper()
StringBuffer mappingBuilderLine = new StringBuffer();
mappingBuilderLine.append(String.format("%s mapping = buildMapper()", SpringletsJavaType.SPRINGLETS_QUERYDSL_REPOSITORY_SUPPORT_ATTRIBUTE_BUILDER.getNameIncludingTypeParameters(false, this.importResolver)));
// .map(entiyVarName, varName) ...
if (!this.typesAreProjections.get(this.defaultReturnType)) {
// Return type is the same entity
Iterator<FieldMetadata> iterator = fields.iterator();
while (iterator.hasNext()) {
FieldMetadata field = iterator.next();
String entityFieldName = field.getFieldName().getSymbolName();
mappingBuilderLine.append(String.format("\n\t\t\t.map(%s, %s.%s)", getConstantForField(entityFieldName).getFieldName(), entityVariable, entityFieldName));
}
} else {
// Return type is a projection
List<Pair<String, String>> projectionFields = this.typesFieldMaps.get(this.defaultReturnType);
Iterator<Pair<String, String>> iterator = projectionFields.iterator();
while (iterator.hasNext()) {
Pair<String, String> entry = iterator.next();
mappingBuilderLine.append(String.format("\n\t\t\t.map(%s, %s)", getConstantForField(entry.getKey()).getFieldName(), entry.getValue()));
}
}
mappingBuilderLine.append(";");
bodyBuilder.appendFormalLine(mappingBuilderLine.toString());
bodyBuilder.newLine();
// applyPagination(pageable, query, mapping);
bodyBuilder.appendFormalLine(String.format("applyPagination(pageable, query, mapping);", pageable));
// applyOrderById(query);
bodyBuilder.appendFormalLine("applyOrderById(query);");
bodyBuilder.newLine();
buildQueryResult(bodyBuilder, pageable, entityVariable, projection, this.defaultReturnType);
// Sets body to generated method
methodBuilder.setBodyBuilder(bodyBuilder);
// Build and return a MethodMetadata
return methodBuilder.build();
// instance
}
use of org.springframework.roo.classpath.details.MethodMetadata in project spring-roo by spring-projects.
the class RepositoryJpaMetadata method getFindAllIteratorMethod.
private MethodMetadata getFindAllIteratorMethod(JavaType entity, FieldMetadata identifierFieldMetadata) {
// Define method parameter type and name
List<AnnotatedJavaType> parameterTypes = new ArrayList<AnnotatedJavaType>();
List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>();
parameterTypes.add(AnnotatedJavaType.convertFromJavaType(JavaType.iterableOf(identifierFieldMetadata.getFieldType())));
parameterNames.add(identifierFieldMetadata.getFieldName());
MethodMetadata existingMethod = getGovernorMethod(FIND_ALL_ITERATOR_METHOD_NAME, AnnotatedJavaType.convertFromAnnotatedJavaTypes(parameterTypes));
if (existingMethod != null) {
return existingMethod;
}
// Use the MethodMetadataBuilder for easy creation of MethodMetadata
return new MethodMetadataBuilder(getId(), Modifier.PUBLIC + Modifier.ABSTRACT, FIND_ALL_ITERATOR_METHOD_NAME, JavaType.listOf(entity), parameterTypes, parameterNames, null).build();
}
use of org.springframework.roo.classpath.details.MethodMetadata in project spring-roo by spring-projects.
the class RepositoryJpaMetadata method getSaveMethod.
private MethodMetadata getSaveMethod(JavaType entity) {
// Define method parameter type and name
List<AnnotatedJavaType> parameterTypes = new ArrayList<AnnotatedJavaType>();
List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>();
parameterTypes.add(AnnotatedJavaType.convertFromJavaType(entity));
parameterNames.add(new JavaSymbolName(StringUtils.uncapitalize(entity.getSimpleTypeName())));
MethodMetadata existingMethod = getGovernorMethod(SAVE_METHOD_NAME, AnnotatedJavaType.convertFromAnnotatedJavaTypes(parameterTypes));
if (existingMethod != null) {
return existingMethod;
}
// Use the MethodMetadataBuilder for easy creation of MethodMetadata
return new MethodMetadataBuilder(getId(), Modifier.PUBLIC + Modifier.ABSTRACT, SAVE_METHOD_NAME, entity, parameterTypes, parameterNames, null).build();
}
use of org.springframework.roo.classpath.details.MethodMetadata in project spring-roo by spring-projects.
the class SecurityAuthorizationsMetadataProviderImpl method getMetadata.
@Override
protected ItdTypeDetailsProvidingMetadataItem getMetadata(final String metadataIdentificationString, final JavaType aspectName, final PhysicalTypeMetadata governorPhysicalTypeMetadata, final String itdFilename) {
// Getting the annotated entity type
JavaType annotatedService = governorPhysicalTypeMetadata.getType();
// Getting the service details
MemberDetails serviceDetails = getMemberDetailsScanner().getMemberDetails(getClass().getName(), getTypeLocationService().getTypeDetails(annotatedService));
Map<MethodMetadata, String> preAuthorizationMethods = new LinkedHashMap<MethodMetadata, String>();
// Get methods defined in each annotation @RooSecurityAuthorization
AnnotationMetadata annotationAuthorizations = serviceDetails.getAnnotation(RooJavaType.ROO_SECURITY_AUTHORIZATIONS);
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();
// Get attributes from the annotation @RooSecurityAuthorization
String methodName = (String) authorizationAnnotation.getValue().getAttribute("method").getValue();
List<?> methodParameters = (List<?>) authorizationAnnotation.getValue().getAttribute("parameters").getValue();
List<MethodMetadata> methods = serviceDetails.getMethods(new JavaSymbolName(methodName));
for (MethodMetadata method : methods) {
// check the parameters to get corresponding method
if (checkParameters(method, methodParameters)) {
String roles = null;
if (authorizationAnnotation.getValue().getAttribute("roles") != null) {
roles = (String) authorizationAnnotation.getValue().getAttribute("roles").getValue();
}
String usernames = null;
if (authorizationAnnotation.getValue().getAttribute("usernames") != null) {
usernames = (String) authorizationAnnotation.getValue().getAttribute("usernames").getValue();
}
// Create the content based on defined roles and
// usernames to include it in the security annotation
String secAnnotationValue = getSecurityOperations().getSpringSecurityAnnotationValue(roles, usernames);
preAuthorizationMethods.put(method, secAnnotationValue);
break;
}
}
}
}
return new SecurityAuthorizationsMetadata(metadataIdentificationString, aspectName, governorPhysicalTypeMetadata, preAuthorizationMethods);
}
use of org.springframework.roo.classpath.details.MethodMetadata in project spring-roo by spring-projects.
the class PushInCommands method getAllPossibleMethods.
/**
* Method that returns all defined methods for provided class on --class parameter.
*
* @param context context
* ShellContext used to obtain specified parameters
* @return List with available methods. Empty List if class has not been specified.
*/
@CliOptionAutocompleteIndicator(command = "push-in", param = "method", validate = false, help = "Provides possible methods names if some class parameter has been specified")
public List<String> getAllPossibleMethods(ShellContext context) {
List<String> allPossibleMethods = new ArrayList<String>();
// Getting all introduces parameters
Map<String, String> specifiedParameters = context.getParameters();
String specifiedClass = specifiedParameters.get("class");
// Check if class parameter has been specified
if (StringUtils.isNotEmpty(specifiedClass)) {
JavaType klass = getJavaTypeConverter().convertFromText(specifiedClass, JavaType.class, PROJECT);
// TODO: Class details should be cached to prevent load MemberDetails everytime.
// The problem is that if some element is cached, and then, new method is added
// to .aj file, this parameter will not autocomplete it.
MemberDetails klassDetails = memberDetailsScanner.getMemberDetails(getClass().getName(), typeLocationService.getTypeDetails(klass));
if (klassDetails != null) {
List<MethodMetadata> definedMethods = klassDetails.getMethods();
for (MethodMetadata method : definedMethods) {
// Check if method has been defined on current class and check
// if current method has been pushed before.
String declaredByMetadataID = method.getDeclaredByMetadataId();
if (StringUtils.isNotBlank(declaredByMetadataID) && declaredByMetadataID.split("\\?").length > 1 && declaredByMetadataID.split("\\#").length > 0 && !declaredByMetadataID.split("\\#")[0].equals("MID:org.springframework.roo.classpath.PhysicalTypeIdentifier") && declaredByMetadataID.split("\\?")[1].equals(klass.getFullyQualifiedTypeName())) {
String methodName = method.getMethodName().getSymbolName();
List<AnnotatedJavaType> parameterTypes = method.getParameterTypes();
methodName = methodName.concat("(");
for (int i = 0; i < parameterTypes.size(); i++) {
String paramType = parameterTypes.get(i).getJavaType().getSimpleTypeName();
methodName = methodName.concat(paramType).concat(",");
}
if (!parameterTypes.isEmpty()) {
methodName = methodName.substring(0, methodName.length() - 1).concat(")");
} else {
methodName = methodName.concat(")");
}
allPossibleMethods.add(methodName);
}
}
}
}
return allPossibleMethods;
}
Aggregations