Java Code Examples for com.sun.codemodel.JVar#annotate()

The following examples show how to use com.sun.codemodel.JVar#annotate() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MethodParamsRule.java    From springmvc-raml-plugin with Apache License 2.0 5 votes vote down vote up
protected JVar paramObjects(ApiActionMetadata endpointMetadata, CodeModelHelper.JExtMethod generatableType) {

		String requestBodyName = endpointMetadata.getRequestBody().getName();
		String requestBodyFullName = requestBodyName;
		if (BigDecimal.class.getSimpleName().equals(requestBodyName)) {
			requestBodyFullName = BigDecimal.class.getName();
		} else if (BigInteger.class.getSimpleName().equals(requestBodyName)) {
			requestBodyFullName = BigInteger.class.getName();
		}

		boolean array = endpointMetadata.getRequestBody().isArray();

		List<JCodeModel> codeModels = new ArrayList<>();
		if (endpointMetadata.getRequestBody().getCodeModel() != null) {
			codeModels.add(endpointMetadata.getRequestBody().getCodeModel());
		}

		if (generatableType.owner() != null) {
			codeModels.add(generatableType.owner());
		}

		JClass requestBodyType = findFirstClassBySimpleName(codeModels.toArray(new JCodeModel[codeModels.size()]), requestBodyFullName);
		if (allowArrayParameters && array) {
			JClass arrayType = generatableType.owner().ref(List.class);
			requestBodyType = arrayType.narrow(requestBodyType);
		}
		if (addParameterJavadoc) {
			generatableType.get().javadoc().addParam(NamingHelper.getParameterName(requestBodyName) + " The Request Body Payload");
		}
		JVar param = generatableType.get().param(requestBodyType, NamingHelper.getParameterName(requestBodyName));
		if (Config.getPojoConfig().isIncludeJsr303Annotations() && !RamlActionType.PATCH.equals(endpointMetadata.getActionType())) {
			// skip Valid annotation for PATCH actions since it's a partial
			// update so some required fields might be omitted
			param.annotate(Valid.class);
		}
		return param;
	}
 
Example 2
Source File: SpringMethodParamsRule.java    From springmvc-raml-plugin with Apache License 2.0 4 votes vote down vote up
@Override
protected JVar paramQueryForm(ApiParameterMetadata paramMetaData, CodeModelHelper.JExtMethod generatableType,
		ApiActionMetadata endpointMetadata) {
	JVar jVar = super.paramQueryForm(paramMetaData, generatableType, endpointMetadata);
	JAnnotationUse jAnnotationUse;
	if (paramMetaData.getRamlParam() instanceof RamlUriParameter) {
		jAnnotationUse = jVar.annotate(PathVariable.class);
		if (!paramMetaData.getName().equals(jVar.name())) {
			jAnnotationUse.param("name", paramMetaData.getName());
		}
		if (!paramMetaData.getRamlParam().isRequired()) {
			jAnnotationUse.param("required", false);
		}
		return jVar;
	} else if (paramMetaData.getRamlParam() instanceof RamlHeader) {
		jAnnotationUse = jVar.annotate(RequestHeader.class);
		if (!paramMetaData.getName().equals(jVar.name())) {
			jAnnotationUse.param("name", paramMetaData.getName());
		}
		if (!paramMetaData.getRamlParam().isRequired()) {
			jAnnotationUse.param("required", false);
		}

		if (StringUtils.hasText(paramMetaData.getRamlParam().getDefaultValue())) {
			jAnnotationUse.param("defaultValue", paramMetaData.getRamlParam().getDefaultValue());
			// Supplying a default value implicitly sets required to false.
			jAnnotationUse.param("required", false);
		}

		return jVar;
	} else {
		if (paramMetaData.getRamlParam() == null) {
			return jVar;
		}

		jAnnotationUse = jVar.annotate(RequestParam.class);
		if (!paramMetaData.getName().equals(jVar.name())) {
			jAnnotationUse.param("name", paramMetaData.getName());
		}
		// In RAML parameters are optional unless the required attribute is
		// included and its value set to 'true'.
		// In Spring a parameter is required by default unlesse the required
		// attribute is included and its value is set to 'false'
		// So we just need to set required=false if the RAML "required"
		// parameter is not set or explicitly set to false.
		if (!paramMetaData.getRamlParam().isRequired()) {
			jAnnotationUse.param("required", false);
		}

		if (StringUtils.hasText(paramMetaData.getRamlParam().getDefaultValue())) {
			jAnnotationUse.param("defaultValue", paramMetaData.getRamlParam().getDefaultValue());
			// Supplying a default value implicitly sets required to false.
			jAnnotationUse.param("required", false);
		}

		if (paramMetaData.getRamlParam().getType() == RamlParamType.DATE
				&& paramMetaData.getRamlParam() instanceof RJP10V2RamlQueryParameter) {

			RJP10V2RamlQueryParameter queryParameter = (RJP10V2RamlQueryParameter) paramMetaData.getRamlParam();
			if (StringUtils.hasText(queryParameter.getRawType())) {
				jAnnotationUse = jVar.annotate(DateTimeFormat.class);
				RamlTypeHelper.annotateDateWithPattern(jAnnotationUse, queryParameter.getRawType(), queryParameter.getFormat());
			}
		}

		return jVar;
	}
}
 
Example 3
Source File: SpringMethodParamsRule.java    From springmvc-raml-plugin with Apache License 2.0 4 votes vote down vote up
@Override
protected JVar paramObjects(ApiActionMetadata endpointMetadata, CodeModelHelper.JExtMethod generatableType) {
	JVar param = super.paramObjects(endpointMetadata, generatableType);
	param.annotate(RequestBody.class);
	return param;
}
 
Example 4
Source File: MethodParamsRule.java    From springmvc-raml-plugin with Apache License 2.0 4 votes vote down vote up
protected JVar paramQueryForm(ApiParameterMetadata paramMetaData, CodeModelHelper.JExtMethod generatableType,
		ApiActionMetadata endpointMetadata) {

	String javaName = paramMetaData.getJavaName();

	if (addParameterJavadoc) {
		String paramComment = "";
		if (paramMetaData.getRamlParam() != null && StringUtils.hasText(paramMetaData.getRamlParam().getDescription())) {
			paramComment = NamingHelper.cleanForJavadoc(paramMetaData.getRamlParam().getDescription());
		}
		generatableType.get().javadoc().addParam(javaName + " " + paramComment);
	}

	JClass type = null;
	if (paramMetaData.getRamlParam() instanceof RJP10V2RamlUriParameter && paramMetaData.isNullable()) {
		// for optional uri parameters use java.util.Optional since Spring
		// doesn't support optional uri parameters
		type = generatableType.owner().ref(Optional.class).narrow(paramMetaData.getType());
	}
	if (type == null) {
		type = generatableType.owner().ref(paramMetaData.getType());
	}

	if (!allowArrayParameters && paramMetaData.isArray()) {
		type = generatableType.owner().ref(paramMetaData.getType().getComponentType());
	} else {
		// TODO should this be blank?
	}

	JVar jVar = null;

	// data types as query parameters
	RamlAbstractParam ramlParam = paramMetaData.getRamlParam();
	if (ramlParam.getType() == RamlParamType.DATA_TYPE && ramlParam instanceof RJP10V2RamlQueryParameter) {
		JClass jc = findFirstClassBySimpleName(paramMetaData.getCodeModel(), ramlParam.getRawType());
		jVar = generatableType.get().param(jc, paramMetaData.getName());

		if (Config.getPojoConfig().isIncludeJsr303Annotations() && !RamlActionType.PATCH.equals(endpointMetadata.getActionType())
				&& jVar.type() instanceof JDefinedClass) {
			// skip Valid annotation for PATCH actions since it's a partial
			// update so some required fields might be omitted
			boolean isPOJO = ((JDefinedClass) jVar.type())._package().name().startsWith(Config.getBasePackage());
			if (isPOJO) {
				jVar.annotate(Valid.class);
			}
		}
		return jVar;
	}

	jVar = generatableType.get().param(type, javaName);
	if (paramMetaData.getRamlParam().getPattern() != null) {
		jVar.annotate(Pattern.class).param("regexp", paramMetaData.getRamlParam().getPattern());
	}

	if (paramMetaData.getRamlParam().getMinLength() != null || paramMetaData.getRamlParam().getMaxLength() != null) {
		JAnnotationUse jAnnotationUse = jVar.annotate(Size.class);
		if (paramMetaData.getRamlParam().getMinLength() != null) {
			jAnnotationUse.param("min", paramMetaData.getRamlParam().getMinLength());
		}
		if (paramMetaData.getRamlParam().getMaxLength() != null) {
			jAnnotationUse.param("max", paramMetaData.getRamlParam().getMaxLength());
		}
	}

	if (paramMetaData.getRamlParam().getMinimum() != null) {
		jVar.annotate(Min.class).param("value", paramMetaData.getRamlParam().getMinimum().longValue());
	}
	if (paramMetaData.getRamlParam().getMaximum() != null) {
		jVar.annotate(Max.class).param("value", paramMetaData.getRamlParam().getMaximum().longValue());
	}

	return jVar;
}