use of org.springframework.roo.classpath.details.annotations.AnnotatedJavaType 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.annotations.AnnotatedJavaType 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.annotations.AnnotatedJavaType 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.annotations.AnnotatedJavaType 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;
}
use of org.springframework.roo.classpath.details.annotations.AnnotatedJavaType in project spring-roo by spring-projects.
the class PushInOperationsImpl method methodMatch.
/**
* This method checks if the provided methodName matches with the provided
* regular expression
*
* @param methodName
* @param regEx
* @return
*/
private boolean methodMatch(MethodMetadata method, String regEx) {
// Create regular expression using provided text
Pattern pattern = Pattern.compile(regEx);
Matcher matcher = pattern.matcher(method.getMethodName().getSymbolName());
boolean matches = matcher.matches();
// manually
if (!matches && regEx.split("\\(").length > 1) {
// Getting method name and parameter types
String name = regEx.split("\\(")[0];
String[] parameterTypes = regEx.split("\\(")[1].replaceAll("\\)", "").split(",");
// Prevent errors with empty regular expressions
if (StringUtils.isEmpty(name)) {
return false;
}
if (method.getMethodName().equals(new JavaSymbolName(name))) {
List<AnnotatedJavaType> methodParams = method.getParameterTypes();
boolean sameParameterTypes = false;
if (methodParams.size() == parameterTypes.length) {
sameParameterTypes = true;
for (int i = 0; i < methodParams.size(); i++) {
if (!methodParams.get(i).getJavaType().getSimpleTypeName().equals(parameterTypes[i])) {
sameParameterTypes = false;
break;
}
}
}
// If the same method as provided, return true
if (sameParameterTypes) {
return true;
}
}
}
return matches;
}
Aggregations