parameterTypes.add(new AnnotatedJavaType(this.entity, ANN_METADATA_MODEL_ATTRIBUTE)); parameterTypes.add(MODEL_PARAM); AnnotatedJavaType.convertFromAnnotatedJavaTypes(parameterTypes)); if (existingMethod != null) { return existingMethod; parameterNames.add(new JavaSymbolName(entityItemName)); parameterNames.add(MODEL_PARAM_NAME); final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder(); bodyBuilder.appendFormalLine("%s(model);", populateFormMethod.getMethodName()); bodyBuilder.newLine(); bodyBuilder.appendFormalLine("model.addAttribute(\"%s\", %s);", entityItemName, entityItemName); bodyBuilder.appendFormalLine("return new %s(\"%s/edit\");", getNameOfJavaType(SpringJavaType.MODEL_AND_VIEW), viewsPath); new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, SpringJavaType.MODEL_AND_VIEW, parameterTypes, parameterNames, bodyBuilder); methodBuilder.setAnnotations(annotations); return methodBuilder.build();
InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder(); bodyBuilder.appendFormalLine("// Setup"); bodyBuilder.appendFormalLine("%s id = %s();", getNameOfJavaType(this.identifierType), this.getRandomIdMethodName); bodyBuilder.newLine(); bodyBuilder.appendFormalLine("// Exercise"); bodyBuilder.appendFormalLine("%s %s = %s().findOne(id);", getNameOfJavaType(this.entity), entityVar, getAccessorMethod(this.repositoryField).getMethodName()); bodyBuilder.newLine(); bodyBuilder.appendFormalLine("// Verify"); bodyBuilder.appendFormalLine( "%s(%s).as(\"Check that findOne illegally returned null for id %s\", id).isNotNull();", getNameOfJavaType(ASSERT_THAT), this.entityVar, "%s"); bodyBuilder.appendFormalLine( "%s(id).as(\"Check the identifier of the found '%s' is the same used to look for it\")", getNameOfJavaType(ASSERT_THAT), getNameOfJavaType(this.entity)); bodyBuilder.indent(); bodyBuilder.appendFormalLine(".isEqualTo(%s.%s());", this.entityVar, this.identifierAccessorMethodName); bodyBuilder.reset();
final JavaSymbolName methodName = new JavaSymbolName("javascriptTemplates"); parameterNames.add(new JavaSymbolName("template")); AnnotatedJavaType.convertFromAnnotatedJavaTypes(parameterTypes)); if (existingMethod != null) { return existingMethod; InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder(); bodyBuilder.appendFormalLine("if (%s.hasLength(template)) {", getNameOfJavaType(SpringJavaType.STRING_UTILS)); bodyBuilder.indent(); bodyBuilder.appendFormalLine("return template.concat(\".js\");"); bodyBuilder.indentRemove(); bodyBuilder.appendFormalLine("}"); bodyBuilder.appendFormalLine("throw new %s(\"File not found\");", getNameOfJavaType(SpringletsJavaType.SPRINGLETS_NOT_FOUND_EXCEPTION)); new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, JavaType.STRING, parameterTypes, parameterNames, bodyBuilder); methodBuilder.setAnnotations(annotations); return methodBuilder.build();
private InvocableMemberBodyBuilder getInterfaceMethodBody(JavaType returnType) { final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder(); bodyBuilder.appendFormalLine("// Interface Implementation"); bodyBuilder.appendFormalLine(RETURN_0); return bodyBuilder; }
/** * Prints the message, after adding indents and returns to a new line. This * is the most commonly used method. */ public InvocableMemberBodyBuilder appendFormalLine(final String message) { appendIndent(); if (message != null && !"".equals(message)) { stringBuilder.append(message); } return newLine(false); }
protected MethodMetadataBuilder getAccessorMethod(final JavaSymbolName fieldName, final JavaType fieldType) { return getAccessorMethod(fieldName, fieldType, InvocableMemberBodyBuilder.getInstance() .appendFormalLine("return " + fieldName + ";")); }
new JavaSymbolName("get" + StringUtils.capitalize(pathVariable)); new AnnotationMetadataBuilder(SpringJavaType.PATH_VARIABLE); pathVariableAnnotation.addStringAttribute("value", StringUtils.uncapitalize(pathVariable)); parameterTypes.add(new AnnotatedJavaType(idType, pathVariableAnnotation.build())); InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder(); bodyBuilder.appendFormalLine("%s %s = %s.%s(%s);", getNameOfJavaType(entityType), pathVariable, serviceField.getFieldName(), serviceMetadata.getCurrentFindOneMethod().getMethodName(), idName); bodyBuilder.appendFormalLine("if (%s == null) {", pathVariable); bodyBuilder.indent(); bodyBuilder.appendFormalLine( "throw new %s(String.format(\"%s with identifier '%%s' not found\",%s));", getNameOfJavaType(SpringletsJavaType.SPRINGLETS_NOT_FOUND_EXCEPTION), entityType.getSimpleTypeName(), idName); bodyBuilder.indentRemove(); bodyBuilder.appendFormalLine("}"); bodyBuilder.appendFormalLine("return %s;", pathVariable); methodBuilder.addAnnotation(ANN_MODEL_ATTRIBUTE); return methodBuilder.build();
InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder(); bodyBuilder.appendFormalLine("%s endpoint = new EndpointImpl(%s(), new %s(%s()));", getNameOfJavaType(new JavaType("org.apache.cxf.jaxws.EndpointImpl")), getAccessorMethod(getBusField()).getMethodName(), getNameOfJavaType(endpoint), getAccessorMethod(getServiceField(getServiceFromEndpoint(endpoint))).getMethodName()); bodyBuilder.appendFormalLine("endpoint.setFeatures(%s.asList(new %s(), new %s()));", getNameOfJavaType(JavaType.ARRAYS), getNameOfJavaType(new JavaType( "io.tracee.binding.cxf.TraceeCxfFeature")), getNameOfJavaType(new JavaType( "org.apache.cxf.feature.LoggingFeature"))); bodyBuilder.appendFormalLine("endpoint.publish(\"/%s\");", getSeiFromEndpoint(endpoint) .getSimpleTypeName()); bodyBuilder.appendFormalLine("return endpoint;"); new MethodMetadataBuilder(getId(), Modifier.PUBLIC, new JavaSymbolName( StringUtils.uncapitalize(endpoint.getSimpleTypeName())), JavaType.ENDPOINT, bodyBuilder); endpointMethod.addAnnotation(new AnnotationMetadataBuilder(SpringJavaType.BEAN)); endpointMethods.put(endpoint, endpointMethod.build()); return endpointMethod.build();
new JavaSymbolName("init" + entity.getSimpleTypeName() + "Binder"); parameterTypes.add(AnnotatedJavaType.convertFromJavaType(SpringJavaType.WEB_DATA_BINDER)); List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>(); parameterNames.add(new JavaSymbolName("binder")); InvocableMemberBodyBuilder body = new InvocableMemberBodyBuilder(); body.appendFormalLine("binder.setDisallowedFields(\"%s\");", entityIdentifier); body.appendFormalLine("binder.setDisallowedFields(\"%s.%s\");", item.getKey().fieldName, item .getValue().getCurrentIndentifierField().getFieldName()); body.appendFormalLine("// Register validators"); body.appendFormalLine("%s validator = new GenericValidator(%s.class, %s());", SpringletsJavaType.SPRINGLETS_GENERIC_VALIDATOR.getNameIncludingTypeParameters(false, builder.getImportRegistrationResolver()), entity.getBaseType() .getNameIncludingTypeParameters(false, builder.getImportRegistrationResolver()), getAccessorMethod(controllerMetadata.getServiceField()).getMethodName()); body.appendFormalLine("binder.addValidators(validator);"); new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, JavaType.VOID_PRIMITIVE, parameterTypes, parameterNames, body); methodBuilder.setAnnotations(annotations); return methodBuilder.build();
new JavaSymbolName(JpaEntityFactoryMetadata.SPECIFIC_METHOD_PREFIX + this.entity.getSimpleTypeName()); final JavaType parameterType = JavaType.INT_PRIMITIVE; final List<JavaSymbolName> parameterNames = Arrays.asList(INDEX_SYMBOL); final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder(); bodyBuilder.appendFormalLine("init();"); bodyBuilder.appendFormalLine("if (" + INDEX_VAR + " < 0) {"); bodyBuilder.indent(); bodyBuilder.appendFormalLine(INDEX_VAR + " = 0;"); bodyBuilder.indentRemove(); bodyBuilder.appendFormalLine("}"); bodyBuilder.appendFormalLine("if (" + INDEX_VAR + " > (" + getAccessorMethod(getDataField().build()).getMethodName() + "().size() - 1)) {"); bodyBuilder.indent(); bodyBuilder.appendFormalLine(INDEX_VAR + " = " + getAccessorMethod(getDataField().build()).getMethodName() + "().size() - 1;"); bodyBuilder.indentRemove(); bodyBuilder.appendFormalLine("}"); bodyBuilder.appendFormalLine("return %s().get(%s);", getAccessorMethod(getDataField().build()) new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, this.entity, AnnotatedJavaType.convertFromJavaTypes(parameterType), parameterNames, bodyBuilder); methodBuilder.setCommentStructure(comment); this.specificEntityMethod = methodBuilder.build(); return this.specificEntityMethod;
JavaSymbolName methodName = new JavaSymbolName("localeChangeInterceptor"); new JavaType("org.springframework.web.servlet.i18n.LocaleChangeInterceptor"); AnnotatedJavaType.convertFromAnnotatedJavaTypes(parameterTypes))) { return getGovernorMethod(methodName, AnnotatedJavaType.convertFromAnnotatedJavaTypes(parameterTypes)); InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder(); .appendFormalLine("LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();"); bodyBuilder.appendFormalLine("localeChangeInterceptor.setParamName(\"lang\");"); bodyBuilder.appendFormalLine("return localeChangeInterceptor;"); new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, returnType, parameterTypes, parameterNames, bodyBuilder); methodBuilder.addAnnotation(new AnnotationMetadataBuilder(SpringJavaType.BEAN)); return methodBuilder.build(); // Build and return a MethodMetadata
private InvocableMemberBodyBuilder getSingularEntityMutator(final FieldMetadata field, final JavaSymbolName hiddenIdFieldName) { final String entityName = field.getFieldName().getSymbolName(); final String entityIdName = hiddenIdFieldName.getSymbolName(); final String identifierMethodName = getIdentifierMethodName(field).getSymbolName(); final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder(); bodyBuilder.appendFormalLine("if (" + entityName + " != null) {"); bodyBuilder.indent(); bodyBuilder.appendFormalLine("if (" + entityName + "." + identifierMethodName + " () == null) {"); bodyBuilder.indent(); bodyBuilder.appendFormalLine(entityName + ".persist();"); bodyBuilder.indentRemove(); bodyBuilder.appendFormalLine("}"); bodyBuilder.appendFormalLine(THIS_DOT + entityIdName + " = " + entityName + "." + identifierMethodName + "();"); bodyBuilder.indentRemove(); bodyBuilder.appendFormalLine("} else {"); bodyBuilder.indent(); bodyBuilder.appendFormalLine(THIS_DOT + entityIdName + " = null;"); bodyBuilder.indentRemove(); bodyBuilder.appendFormalLine("}"); bodyBuilder.appendFormalLine(THIS_DOT + entityName + " = " + entityName + ";"); return bodyBuilder; }
InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder(); .appendFormalLine("if ( null == format || format.length() <= 0 ) {"); bodyBuilder .appendIndent() .appendFormalLine( "uiModel.addAttribute(\"error\", \"message_format_required\");"); bodyBuilder.appendIndent().appendFormalLine( "return \"".concat(annotationValues.getPath()).concat("/") .concat(reportName).concat("\";")); bodyBuilder.appendFormalLine("}"); bodyBuilder.appendFormalLine("final String REGEX = \"(".concat( reportFormats.replace(",", "|")).concat(")\";")); bodyBuilder .appendFormalLine("Pattern pattern = Pattern.compile(REGEX, Pattern.CASE_INSENSITIVE);"); bodyBuilder .appendFormalLine("Matcher matcher = pattern.matcher(format);"); bodyBuilder.appendFormalLine("if ( !matcher.matches() ) {"); bodyBuilder.appendIndent().appendFormalLine( "uiModel.addAttribute(\"error\", \"message_format_invalid\");"); bodyBuilder.appendIndent().appendFormalLine( "return \"".concat(annotationValues.getPath()).concat("/") .concat(reportName).concat("\";")); bodyBuilder.appendFormalLine("}"); bodyBuilder.appendFormalLine("Collection<" bodyBuilder.appendFormalLine("if (dataSource.isEmpty()) {");
private MethodMetadata getDeserializeMethod() { final JavaSymbolName methodName = new JavaSymbolName("deserializeObject"); InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder(); bodyBuilder.appendFormalLine("String idText = tree.asText();"); bodyBuilder.appendFormalLine("%s %s = %s.convert(idText, %s.class);", getNameOfJavaType(idType), entityMetadata.getCurrentIndentifierField().getFieldName(), conversionServiceField.getFieldName(), getNameOfJavaType(idType)); bodyBuilder.appendFormalLine("%s %s = %s.%s(%s);", getNameOfJavaType(entityMetadata.getDestination()), entityItemName, serviceField.getFieldName(), serviceMetadata.getCurrentFindOneMethod().getMethodName(), entityMetadata.getCurrentIndentifierField().getFieldName()); bodyBuilder.appendFormalLine("if (%s == null) {", entityItemName); bodyBuilder.indent(); bodyBuilder.appendFormalLine("throw new %s(\"%s not found\");", getNameOfJavaType(SpringletsJavaType.SPRINGLETS_NOT_FOUND_EXCEPTION), entityMetadata .getDestination().getSimpleTypeName()); bodyBuilder.indentRemove(); bodyBuilder.appendFormalLine("}"); bodyBuilder.appendFormalLine("return %s;", entityItemName);
final JavaSymbolName methodName = new JavaSymbolName(annotationValues.getToStringMethod()); final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder(); bodyBuilder.appendFormalLine(String.format("return \"%s {\" + ", target.getSimpleTypeName())); for (int i = 0; i < affectedFields.size(); i++) { bodyBuilder.appendIndent(); StringBuilder fieldString = new StringBuilder(); fieldString.append("\""); String fieldName = fieldMetadata.getFieldName().getSymbolName(); bodyBuilder.appendFormalLine(fieldString.toString()); bodyBuilder.appendFormalLine("\"}\" + super.toString();"); return new MethodMetadataBuilder(metadataId, Modifier.PUBLIC, methodName, STRING, bodyBuilder);
/** * This method returns the getXmlIdentityInfo() method. * * @return MethodMetadata that contains the getXmlIdentityInfoMethod */ public MethodMetadata getXmlIdentityInfoMethod() { // Check if already exists if (xmlIdentityInfoMethod != null) { return xmlIdentityInfoMethod; } // If not, generate a new one InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder(); // return getClass().getName() + ":" + getId(); bodyBuilder.appendFormalLine(String.format("return getClass().getName() + \":\" + %s();", identifierAccessor.getMethodName())); MethodMetadataBuilder method = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, new JavaSymbolName("getXmlIdentityInfo"), JavaType.STRING, bodyBuilder); method.addAnnotation(new AnnotationMetadataBuilder(JavaType.XML_ID)); AnnotationMetadataBuilder xmlAttributeAnnotation = new AnnotationMetadataBuilder(JavaType.XML_ATTRIBUTE); xmlAttributeAnnotation.addStringAttribute("name", "id"); method.addAnnotation(xmlAttributeAnnotation); CommentStructure comment = new CommentStructure(); comment.addComment(new JavadocComment("Must return an unique ID across all entities"), CommentLocation.BEGINNING); method.setCommentStructure(comment); xmlIdentityInfoMethod = method.build(); return xmlIdentityInfoMethod; }
private void createReceiverJmsService(JavaType service, String destinationProperty) { getPathResolver().getCanonicalPath(service.getModule(), Path.SRC_MAIN_JAVA, service); final String mid = PhysicalTypeIdentifier.createIdentifier(service, new AnnotationMetadataBuilder(SpringJavaType.SERVICE); cidBuilder.addAnnotation(serviceAnnotation); final JavaSymbolName methodName = new JavaSymbolName("receiveJmsMessage"); parameterTypes.add(new AnnotatedJavaType(JavaType.STRING)); parameterNames.add(new JavaSymbolName("msg")); new AnnotationMetadataBuilder(SpringJavaType.JMS_LISTENER); jmsListenerAnnotation.addStringAttribute("destination", "${".concat(destinationProperty) .concat("}")); annotations.add(jmsListenerAnnotation); InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder(); bodyBuilder.newLine(); bodyBuilder.appendFormalLine(" // To be implemented"); new MethodMetadataBuilder(mid, Modifier.PUBLIC, methodName, JavaType.VOID_PRIMITIVE, parameterTypes, parameterNames, bodyBuilder); methodBuilder.setAnnotations(annotations);
private MethodMetadata getAccessibilityMethod() { final JavaSymbolName methodName = new JavaSymbolName("accessibility"); parameterTypes.add(AnnotatedJavaType.convertFromJavaType(SpringJavaType.MODEL)); parameterNames.add(new JavaSymbolName("model")); AnnotatedJavaType.convertFromAnnotatedJavaTypes(parameterTypes)); if (existingMethod != null) { return existingMethod; new AnnotationMetadataBuilder(SpringJavaType.GET_MAPPING); getMapping.addStringAttribute("value", "/accessibility"); annotations.add(getMapping); InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder(); bodyBuilder.appendFormalLine( "model.addAttribute(\"application_locale\", %s.getLocale().getLanguage());", getNameOfJavaType(SpringJavaType.LOCALE_CONTEXT_HOLDER)); bodyBuilder.appendFormalLine("return \"accessibility\";"); new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, JavaType.STRING, parameterTypes, parameterNames, bodyBuilder); methodBuilder.setAnnotations(annotations); return methodBuilder.build();
private String getDigitsBody(final FieldMetadata field, final AnnotationMetadata digitsAnnotation, final String suffix) { final String fieldName = field.getFieldName().getSymbolName(); final JavaType fieldType = field.getFieldType(); final Integer integerValue = (Integer) digitsAnnotation.getAttribute(new JavaSymbolName("integer")).getValue(); final Integer fractionValue = (Integer) digitsAnnotation.getAttribute(new JavaSymbolName("fraction")).getValue(); final BigDecimal maxValue = new BigDecimal(StringUtils.rightPad("9", integerValue, '9') + "." + StringUtils.rightPad("9", fractionValue, '9')); final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder(); if (fieldType.equals(BIG_DECIMAL)) { bodyBuilder.appendFormalLine("if (" + fieldName + ".compareTo(new " + BIG_DECIMAL.getSimpleTypeName() + "(\"" + maxValue + "\")) == 1) {"); bodyBuilder.indent(); bodyBuilder.appendFormalLine(fieldName + " = new " + BIG_DECIMAL.getSimpleTypeName() + "(\"" + maxValue + "\");"); } else { bodyBuilder.appendFormalLine("if (" + fieldName + " > " + maxValue.doubleValue() + suffix + ") {"); bodyBuilder.indent(); bodyBuilder.appendFormalLine(fieldName + " = " + maxValue.doubleValue() + suffix + ";"); } bodyBuilder.indentRemove(); bodyBuilder.appendFormalLine("}"); return bodyBuilder.getOutput(); }
/** * This method generates new method instance using an existing * methodMetadata * * @param declaredByMetadataId * @param method * * @return */ private MethodMetadata getNewMethod(String declaredByMetadataId, MethodMetadata method) { // Create bodyBuilder InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder(); bodyBuilder.appendFormalLine(method.getBody()); // Use the MethodMetadataBuilder for easy creation of MethodMetadata // based on existing method MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(declaredByMetadataId, method.getModifier(), method.getMethodName(), method.getReturnType(), method.getParameterTypes(), method.getParameterNames(), bodyBuilder); methodBuilder.setAnnotations(method.getAnnotations()); // ROO-3834: Including default comment structure during push-in methodBuilder.setCommentStructure(method.getCommentStructure()); return methodBuilder.build(); }