private void addCatchAllFormParametersArgument(final MimeType bodyMimeType, final JMethod method, final JDocComment javadoc, final JType argumentType) { method.param(argumentType, GENERIC_PAYLOAD_ARGUMENT_NAME); // build a javadoc text out of all the params List<INamedParam> formParameters = bodyMimeType.getFormParameters(); if(formParameters!=null){ for (INamedParam formParameter : formParameters) { final StringBuilder sb = new StringBuilder(); sb.append(formParameter.getKey()).append(": "); appendParameterJavadocDescription(formParameter, sb); javadoc.addParam(GENERIC_PAYLOAD_ARGUMENT_NAME).add(sb.toString()); } } }
/** * <p>addFormParameters.</p> * * @param bodyMimeType a {@link org.aml.apimodel.MimeType} object. * @param method a {@link com.sun.codemodel.JMethod} object. * @param javadoc a {@link com.sun.codemodel.JDocComment} object. * @throws java.lang.Exception if any. */ protected void addFormParameters(final MimeType bodyMimeType, final JMethod method, final JDocComment javadoc) throws Exception { if (hasAMultiTypeFormParameter(bodyMimeType)) { // use a "catch all" MultivaluedMap<String, String> parameter final JClass type = types.getGeneratorClass(MultivaluedMap.class) .narrow(String.class, String.class); addCatchAllFormParametersArgument(bodyMimeType, method, javadoc, type); } else { for (final INamedParam namedFormParameters : bodyMimeType.getFormParameters()) { addParameter(namedFormParameters.getKey(), namedFormParameters, FormParam.class, method, javadoc); } } }
/** * <p>addBodyParameters.</p> * * @param bodyMimeType a {@link org.aml.apimodel.MimeType} object. * @param method a {@link com.sun.codemodel.JMethod} object. * @param javadoc a {@link com.sun.codemodel.JDocComment} object. * @throws java.lang.Exception if any. */ protected void addBodyParameters(final MimeType bodyMimeType, final JMethod method, final JDocComment javadoc) throws Exception { if (bodyMimeType == null) { return; } else if (MediaType.APPLICATION_FORM_URLENCODED.equals(bodyMimeType .getType())) { addFormParameters(bodyMimeType, method, javadoc); } else if (MediaType.MULTIPART_FORM_DATA.equals(bodyMimeType.getType())) { // use a "catch all" javax.mail.internet.MimeMultipart parameter addCatchAllFormParametersArgument(bodyMimeType, method, javadoc, types.getGeneratorType(MimeMultipart.class)); } else { addPlainBodyArgument(bodyMimeType, method, javadoc); } }
private void renderConstantsClass(JDefinedClass classModel) throws Exception { // define constants class JDefinedClass constantsClass = classModel._class(JMod.STATIC, Util.CONSTANTS_CLASS_NAME); // generate the javadoc on the top of the Constants class JDocComment javadoc = constantsClass.javadoc(); javadoc.append(Util.CONSTANTS_CLASS_JAVADOC); // render root element name JFieldVar rootElementField = constantsClass.field(JMod.FINAL | JMod.STATIC, String.class, Util.ROOT_ELEMENT_NAME_FIELD); rootElementField.init(JExpr.lit(Util.toLowerCaseFirstLetter(classModel.name()))); // render type name JFieldVar typeNameField = constantsClass.field(JMod.FINAL | JMod.STATIC, String.class, Util.TYPE_NAME_FIELD); typeNameField.init(JExpr.lit(classModel.name() + Util.TYPE_NAME_SUFFIX)); }
private void renderElementsClass(JDefinedClass classModel, List<FieldModel> fields) throws Exception { // define constants class JDefinedClass elementsClass = classModel._class(JMod.STATIC, Util.ELEMENTS_CLASS_NAME); // generate the javadoc on the top of the Elements class JDocComment javadoc = elementsClass.javadoc(); javadoc.append(Util.ELEMENTS_CLASS_JAVADOC); // go through each field and create a corresponding constant for (FieldModel fieldModel : fields) { if (Util.isCommonElement(fieldModel.fieldName)) { continue; } JFieldVar elementFieldVar = elementsClass.field(JMod.FINAL | JMod.STATIC, String.class, Util.toConstantsVariable(fieldModel.fieldName)); elementFieldVar.init(JExpr.lit(fieldModel.fieldName)); } }
private void replaceGetter(ClassOutline co, JFieldVar f, JType inner) { //Create the method name String get = "get"; String name = f.name().substring(0, 1).toUpperCase() + f.name().substring(1); String methodName = get+name; //Create HashSet JType JType hashSetType = co.parent().getCodeModel().ref(HashSet.class).narrow(inner); //Find and remove Old Getter! JMethod oldGetter = co.implClass.getMethod(methodName, new JType[0]); JDocComment comment = oldGetter.javadoc(); co.implClass.methods().remove(oldGetter); //Create New Getter JMethod getter = co.implClass.method(JMod.PUBLIC, f.type(), methodName); getter.javadoc().addAll(comment); //Create Getter Body -> {if (f = null) f = new HashSet(); return f;} getter.body()._if(JExpr.ref(f.name()).eq(JExpr._null()))._then() .assign(f, JExpr._new(hashSetType)); getter.body()._return(JExpr.ref(f.name())); }
@Override public JDocComment apply(String nodeName, JsonNode node, JDocCommentable generatableType, Schema currentSchema) { JDocComment javaDoc = generatableType.javadoc(); javaDoc.append(String.format("%nCorresponds to the \"%s\" property.", nodeName)); return javaDoc; }
/** {@inheritDoc} */ protected void addResourceMethod(final JDefinedClass resourceInterface, final Resource resource, final String resourceInterfacePath, final Action action, final MimeType bodyMimeType, final boolean addBodyMimeTypeInMethodName, final Collection<MimeType> uniqueResponseMimeTypes) throws Exception { MimeType actualBodyMimeType = addBodyMimeTypeInMethodName ? bodyMimeType : null; String methodName = null; if(this.extensions!=null){ for(GeneratorExtension ext : this.extensions){ if(ext instanceof MethodNameBuilderExtension){ methodName = ((MethodNameBuilderExtension)ext) .buildResourceMethodName(action, actualBodyMimeType,resource); break; } } } if(methodName==null){ methodName = Names.buildResourceMethodName(action,actualBodyMimeType); } final JType resourceMethodReturnType = getResourceMethodReturnType( methodName, action, uniqueResponseMimeTypes.isEmpty(), false, resourceInterface); final JMethod method = context.createResourceMethod(resourceInterface, methodName, resourceMethodReturnType); context.addHttpMethodAnnotation(action.method(), method); addParamAnnotation(resourceInterfacePath, action, method); addConsumesAnnotation(bodyMimeType, method); addProducesAnnotation(uniqueResponseMimeTypes, method); final JDocComment javadoc = addBaseJavaDoc(action, method); addParameters(action, bodyMimeType, method, javadoc); /* call registered extensions */ for (GeneratorExtension e : extensions) { e.onAddResourceMethod(method, action, bodyMimeType, uniqueResponseMimeTypes); } }
private void addParameters(final Action action, final MimeType bodyMimeType, final JMethod method, final JDocComment javadoc) throws Exception { addPathParameters(action, method, javadoc); addHeaderParameters(action, method, javadoc); addQueryParameters(action, method, javadoc); addBodyParameters(bodyMimeType, method, javadoc); }
private void addPlainBodyArgument(final MimeType bodyMimeType, final JMethod method, final JDocComment javadoc) throws IOException { if(context.getConfiguration().isUseJsr303Annotations()) { method.param(types.getRequestEntityClass(bodyMimeType), GENERIC_PAYLOAD_ARGUMENT_NAME).annotate(Valid.class); } else { method.param(types.getRequestEntityClass(bodyMimeType), GENERIC_PAYLOAD_ARGUMENT_NAME); } javadoc.addParam(GENERIC_PAYLOAD_ARGUMENT_NAME).add( getPrefixedExampleOrBlank(bodyMimeType.getExample())); }
private void addAllResourcePathParameters(Resource resource, final JMethod method, final JDocComment javadoc) throws Exception { for (final INamedParam namedUriParameter : resource.uriParameters()) { addParameter(namedUriParameter.getKey(), namedUriParameter, PathParam.class, method, javadoc); } Resource parentResource = resource.parentResource(); if (parentResource != null) { addAllResourcePathParameters(parentResource, method, javadoc); } }
/** * <p>addHeaderParameters.</p> * * @param action a {@link org.aml.apimodel.Action} object. * @param method a {@link com.sun.codemodel.JMethod} object. * @param javadoc a {@link com.sun.codemodel.JDocComment} object. * @throws java.lang.Exception if any. */ protected void addHeaderParameters(final Action action, final JMethod method, final JDocComment javadoc) throws Exception { for (final INamedParam namedHeaderParameter : action.headers()) { addParameter(namedHeaderParameter.getKey(), namedHeaderParameter, HeaderParam.class, method, javadoc); } }
/** * <p>addBaseJavaDoc.</p> * * @param action a {@link org.aml.apimodel.Action} object. * @param method a {@link com.sun.codemodel.JMethod} object. * @return a {@link com.sun.codemodel.JDocComment} object. */ protected JDocComment addBaseJavaDoc(final Action action, final JMethod method) { final JDocComment javadoc = method.javadoc(); if (isNotBlank(action.description())) { javadoc.add(action.description()); } return javadoc; }
/** * <p>addQueryParameters.</p> * * @param action a {@link org.aml.apimodel.Action} object. * @param method a {@link com.sun.codemodel.JMethod} object. * @param javadoc a {@link com.sun.codemodel.JDocComment} object. * @throws java.lang.Exception if any. */ protected void addQueryParameters(final Action action, final JMethod method, final JDocComment javadoc) throws Exception { for (final INamedParam namedQueryParameter : action.queryParameters()) { addParameter(namedQueryParameter.getKey(), namedQueryParameter, QueryParam.class, method, javadoc); } }
private void addParameter(final String name, final INamedParam parameter, final Class<? extends Annotation> annotationClass, final JMethod method, final JDocComment javadoc) throws Exception { if (this.context.getConfiguration().getIgnoredParameterNames().contains(name)){ return; } for (GeneratorExtension e : extensions) { if (!e.AddParameterFilter(name, parameter, annotationClass, method)) { return; } } final String argumentName = Names.buildVariableName(name); final JVar argumentVariable = method .param(types.buildParameterType(parameter, argumentName), argumentName); argumentVariable.annotate(annotationClass).param( DEFAULT_ANNOTATION_PARAMETER, name); if (parameter.getDefaultValue() != null) { argumentVariable.annotate(DefaultValue.class).param( DEFAULT_ANNOTATION_PARAMETER, parameter.getDefaultValue()); } if (context.getConfiguration().isUseJsr303Annotations()) { addJsr303Annotations(parameter, argumentVariable); } addParameterJavaDoc(parameter, argumentVariable.name(), javadoc); }
private void addParameter(final String name, final INamedParam parameter, final Class<? extends Annotation> annotationClass, final JMethod method, final JDocComment javadoc) throws Exception { if (this.context.getConfiguration().getIgnoredParameterNames().contains(name)){ return; } for (GeneratorExtension e : extensions) { if (!e.AddParameterFilter(name, parameter, annotationClass, method)) { return; } } final String argumentName = Names.buildVariableName(name); final JVar argumentVariable = method .param(types.buildParameterType(parameter, argumentName), argumentName); argumentVariable.annotate(annotationClass).param( DEFAULT_ANNOTATION_PARAMETER, name); if (parameter.getDefaultValue() != null) { argumentVariable.annotate(DefaultValue.class).param( DEFAULT_ANNOTATION_PARAMETER, parameter.getDefaultValue()); } if (context.getConfiguration().isUseJsr303Annotations()) { new ParameterValidationGenerator().addValidation(parameter, argumentVariable); } addParameterJavaDoc(parameter, argumentVariable.name(), javadoc); }
private void renderBuilderClass(JDefinedClass classModel, List<FieldModel> fields, Class<?> contractInterface) throws Exception { // define constants class JDefinedClass builderClass = classModel._class(JMod.PUBLIC | JMod.STATIC | JMod.FINAL, Util.BUILDER_CLASS_NAME); // create a literal version of the Builder class so that the code generator won't pre-pend Builder class references with outermost class JClass literalBuilderClass = codeModel.ref("Builder"); // generate the javadoc on the top of the Elements class JDocComment javadoc = builderClass.javadoc(); javadoc.append(Util.generateBuilderJavadoc(classModel.name(), contractInterface.getSimpleName())); builderClass._implements(contractInterface); builderClass._implements(ModelBuilder.class); builderClass._implements(Serializable.class); // render the builder fields for (FieldModel fieldModel : fields) { builderClass.field(JMod.PRIVATE, fieldModel.fieldType, fieldModel.fieldName); } // render default empty constructor for builder JMethod constructor = builderClass.constructor(JMod.PRIVATE); constructor.body().directStatement("// TODO modify this constructor as needed to pass any required values and invoke the appropriate 'setter' methods"); renderBuilderDefaultCreate(builderClass, literalBuilderClass); renderBuilderCreateContract(builderClass, literalBuilderClass, fields, contractInterface); renderBuild(builderClass); renderGetters(builderClass, fields); renderSetters(builderClass, fields); }
public AbstractBuilder withClassComment(String classComment) { pojoCreationCheck(); JDocComment javadoc = this.pojo.javadoc(); // javadoc.add javadoc.add(toJavaComment(classComment)); javadoc.add("\n\nGenerated using springmvc-raml-plugin on " + new SimpleDateFormat("MM/dd/yyyy HH:mm:ss").format(new Date())); return this; }
@Override public JDocComment apply(ApiResourceMetadata controllerMetadata, JDefinedClass generatableType) { String comments = "No description"; if(controllerMetadata.getDescription() != null) { comments = controllerMetadata.getDescription(); } generatableType.javadoc().append(comments); generatableType.javadoc().append("\n(Generated with springmvc-raml-parser v."+ CodeModelHelper.getVersion()+")"); return generatableType.javadoc(); }
@Override public JDocComment apply(ApiActionMetadata endpointMetadata, JMethod generatableType) { String comments = "No description"; if(endpointMetadata.getDescription() != null) { comments = endpointMetadata.getDescription(); } return generatableType.javadoc().append(comments); }
@Test public void applyRule_shouldCreate_validMethodComment() throws JClassAlreadyExistsException { JDefinedClass jClass = jCodeModel.rootPackage()._class("TestController"); JMethod jMethod = jClass.method(JMod.PUBLIC, ResponseEntity.class, "getBaseById"); JDocComment jDocComment = rule.apply(getEndpointMetadata(2), jMethod); assertNotNull(jDocComment); assertThat(serializeModel(), containsString("* Get base entity by ID")); }
@Test public void applyRule_shouldCreate_validClassComment() throws JClassAlreadyExistsException { JDefinedClass jClass = jCodeModel.rootPackage()._class("BaseController"); JDocComment jDocComment = rule.apply(getControllerMetadata(), jClass); assertNotNull(jDocComment); assertThat(serializeModel(), containsString("* The BaseController class")); }
@Override public void generatePartialArgs(final JDocComment javadoc) { javadoc .addParam(this.propertyTreeParam) .append(PartialCopyGenerator.RESOURCE_BUNDLE.getString("javadoc.method.param.propertyTree")); javadoc .addParam(this.propertyTreeUseParam) .append(MessageFormat.format(PartialCopyGenerator.RESOURCE_BUNDLE.getString("javadoc.method.param.propertyTreeUse"), this.propertyTreeParam.name())); }
private static void addSetterDeclaration(Names names, Class<?> paramType, JDefinedClass setterReturnType) { JMethod method = setterReturnType.method(JMod.NONE, setterReturnType, names.getSetterName()); method.param(paramType, names.getParamName()); JDocComment javadoc = method.javadoc(); javadoc.append(names.getJavadoc()); }
private static void addSetterDeclaration(Names names, JDefinedClass paramType, JDefinedClass setterReturnType) { JMethod method = setterReturnType.method(JMod.NONE, setterReturnType, names.getSetterName()); method.param(paramType, names.getParamName()); JDocComment javadoc = method.javadoc(); javadoc.append(names.getJavadoc()); }
public static void addGetterSetterDeclaration(Names names, JClass type, JDefinedClass jDefinedClass) { addGetterDeclaration(names, type, jDefinedClass); JMethod method = jDefinedClass.method(JMod.NONE, jDefinedClass, names.getSetterName()); method.param(type, names.getParamName()); JDocComment javadoc = method.javadoc(); javadoc.append(names.getJavadoc()); }