use of com.squareup.javapoet.CodeBlock in project Rocket by mozilla-tw.
the class RequestBuilderGenerator method generateGeneratedRequestOptionEquivalent.
/**
* Generates a particular method with an equivalent name and arguments to the given method
* from the generated {@link com.bumptech.glide.request.BaseRequestBuilder} subclass.
*/
private MethodSpec generateGeneratedRequestOptionEquivalent(MethodSpec requestOptionMethod) {
CodeBlock callRequestOptionsMethod = CodeBlock.builder().add(".$N(", requestOptionMethod.name).add(FluentIterable.from(requestOptionMethod.parameters).transform(new Function<ParameterSpec, String>() {
@Override
public String apply(ParameterSpec input) {
return input.name;
}
}).join(Joiner.on(", "))).add(");\n").build();
MethodSpec.Builder result = MethodSpec.methodBuilder(requestOptionMethod.name).addJavadoc(processorUtil.generateSeeMethodJavadoc(requestOptionsClassName, requestOptionMethod)).addModifiers(Modifier.PUBLIC).varargs(requestOptionMethod.varargs).addAnnotations(FluentIterable.from(requestOptionMethod.annotations).filter(new Predicate<AnnotationSpec>() {
@Override
public boolean apply(AnnotationSpec input) {
return !input.type.equals(TypeName.get(Override.class)) && // non-final to allow for mocking.
!input.type.equals(TypeName.get(SafeVarargs.class)) && // autoClone() in RequestBuilder.
!input.type.equals(CHECK_RESULT_CLASS_NAME);
}
}).toList()).addTypeVariables(requestOptionMethod.typeVariables).addParameters(requestOptionMethod.parameters).returns(generatedRequestBuilderOfTranscodeType).beginControlFlow("if (getMutableOptions() instanceof $T)", requestOptionsClassName).addCode("this.requestOptions = (($T) getMutableOptions())", requestOptionsClassName).addCode(callRequestOptionsMethod).nextControlFlow("else").addCode(CodeBlock.of("this.requestOptions = new $T().apply(this.requestOptions)", requestOptionsClassName)).addCode(callRequestOptionsMethod).endControlFlow().addStatement("return this");
if (requestOptionMethod.annotations.contains(AnnotationSpec.builder(SafeVarargs.class).build())) {
result.addAnnotation(AnnotationSpec.builder(SuppressWarnings.class).addMember("value", "$S", "unchecked").addMember("value", "$S", "varargs").build());
}
return result.build();
}
use of com.squareup.javapoet.CodeBlock in project microservice_framework by CJSCommonPlatform.
the class JaxRsImplementationGenerator method putAllQueryParamsInCollectionBuilder.
/**
* Generate code to add all query parameters to the params map.
*
* @param parameters the params to add to the map
* @return the {@link CodeBlock} that represents the generated code
*/
private CodeBlock putAllQueryParamsInCollectionBuilder(final Map<String, QueryParameter> parameters) {
final CodeBlock.Builder builder = CodeBlock.builder();
parameters.entrySet().forEach(paramEntry -> {
final String name = paramEntry.getKey();
final ParameterType parameterType = ParameterType.valueOfQueryType(paramEntry.getValue().getType().name());
if (paramEntry.getValue().isRequired()) {
builder.addStatement(PARAMS_PUT_REQUIRED_STATEMENT_FORMAT, VALID_PARAMETER_COLLECTION_BUILDER_VARIABLE, name, name, ParameterType.class, parameterType.name());
} else {
builder.addStatement(PARAMS_PUT_OPTIONAL_STATEMENT_FORMAT, VALID_PARAMETER_COLLECTION_BUILDER_VARIABLE, name, name, ParameterType.class, parameterType.name());
}
});
return builder.build();
}
use of com.squareup.javapoet.CodeBlock in project microservice_framework by CJSCommonPlatform.
the class ActionMappingGenerator method mapperConstructorCodeFor.
private CodeBlock mapperConstructorCodeFor(final Resource resource) {
final CodeBlock.Builder constructorCode = CodeBlock.builder().addStatement("this.$L = $L", ACTION_MAPPER_HELPER_FIELD, ACTION_MAPPER_HELPER_FIELD);
// NOTE: there's a bit of ambiguity here: ramlActions (http methods) are not framework actions
resource.getActions().values().forEach(ramlAction -> {
final List<ActionMapping> actionMappings = new ActionMappingParser().listOf(ramlAction.getDescription());
final ActionType actionType = ramlAction.getType();
if (isSupportedActionType(actionType)) {
actionMappings.forEach(actionMapping -> {
final String mediaType = actionMapping.mimeTypeFor(ramlAction.getType());
constructorCode.addStatement("$L.add($S, $S, $S)", ACTION_MAPPER_HELPER_FIELD, methodNameForAction(ramlAction, actionType, mediaType), mediaType, actionMapping.getName());
});
} else {
throw new IllegalStateException(format("Http Method of type %s is not supported by the Action Mapper", actionType.toString()));
}
});
return constructorCode.build();
}
use of com.squareup.javapoet.CodeBlock in project web3sdk by FISCO-BCOS.
the class SolidityFunctionWrapper method buildTypedResponse.
CodeBlock buildTypedResponse(String objectName, List<SolidityFunctionWrapper.NamedTypeName> indexedParameters, List<SolidityFunctionWrapper.NamedTypeName> nonIndexedParameters, boolean flowable) {
String nativeConversion;
if (useNativeJavaTypes) {
nativeConversion = ".getValue()";
} else {
nativeConversion = "";
}
CodeBlock.Builder builder = CodeBlock.builder();
if (flowable) {
builder.addStatement("$L.log = log", objectName);
} else {
builder.addStatement("$L.log = eventValues.getLog()", objectName);
}
for (int i = 0; i < indexedParameters.size(); i++) {
builder.addStatement("$L.$L = ($T) eventValues.getIndexedValues().get($L)" + nativeConversion, objectName, indexedParameters.get(i).getName(), getIndexedEventWrapperType(indexedParameters.get(i).getTypeName()), i);
}
for (int i = 0; i < nonIndexedParameters.size(); i++) {
builder.addStatement("$L.$L = ($T) eventValues.getNonIndexedValues().get($L)" + nativeConversion, objectName, nonIndexedParameters.get(i).getName(), getWrapperType(nonIndexedParameters.get(i).getTypeName()), i);
}
return builder.build();
}
use of com.squareup.javapoet.CodeBlock in project web3sdk by FISCO-BCOS.
the class SolidityFunctionWrapper method buildVariableLengthEventInitializer.
private static CodeBlock buildVariableLengthEventInitializer(String eventName, List<NamedTypeName> parameterTypes) {
List<Object> objects = new ArrayList<>();
objects.add(Event.class);
objects.add(eventName);
objects.add(Arrays.class);
objects.add(TypeReference.class);
for (NamedTypeName parameterType : parameterTypes) {
objects.add(TypeReference.class);
objects.add(parameterType.getTypeName());
}
String asListParams = parameterTypes.stream().map(type -> {
if (type.isIndexed()) {
return "new $T<$T>(true) {}";
} else {
return "new $T<$T>() {}";
}
}).collect(Collectors.joining(", "));
return CodeBlock.builder().addStatement("new $T($S, \n" + "$T.<$T<?>>asList(" + asListParams + "))", objects.toArray()).build();
}
Aggregations