use of javax.lang.model.element.Modifier.DEFAULT in project servicetalk by apple.
the class Generator method newServiceInterfaceSpec.
/**
* Adds the service interface, either async or blocking, which extends all of the service RPC interfaces
*
* @param state The generator state
* @param blocking If true then add the interface for blocking service otherwise add async service interface
* @return The generated service interface
*/
private TypeSpec newServiceInterfaceSpec(final State state, final boolean blocking) {
final ClassName serviceClass = blocking ? state.blockingServiceClass : state.serviceClass;
final String name = serviceClass.simpleName();
final TypeSpec.Builder interfaceSpecBuilder = interfaceBuilder(name).addModifiers(PUBLIC).addSuperinterface(ParameterizedTypeName.get(GrpcBindableService, state.serviceClass));
state.serviceRpcInterfaces.stream().filter(e -> e.blocking == blocking).map(e -> e.className).forEach(interfaceSpecBuilder::addSuperinterface);
// Add the default bindService method.
MethodSpec.Builder b = methodBuilder(bind + Service);
if (printJavaDocs) {
b.addJavadoc("Makes a {@link $T} bound to this instance implementing {@link $T}", state.serviceFactoryClass, serviceClass);
}
interfaceSpecBuilder.addMethod(b.addAnnotation(Override.class).addModifiers(PUBLIC).addModifiers(DEFAULT).returns(state.serviceFactoryClass).addStatement("return new $T(this)", state.serviceFactoryClass).build());
// Add the default methodDescriptors method.
interfaceSpecBuilder.addMethod(methodBuilder(methodDescriptors).addAnnotation(Override.class).addModifiers(PUBLIC).addModifiers(DEFAULT).returns(GrpcMethodDescriptorCollection).addStatement("return $L", blocking ? BLOCKING_METHOD_DESCRIPTORS : ASYNC_METHOD_DESCRIPTORS).build());
return interfaceSpecBuilder.build();
}
use of javax.lang.model.element.Modifier.DEFAULT in project servicetalk by apple.
the class Generator method addClientInterfaces.
private TypeSpec.Builder addClientInterfaces(final State state, final TypeSpec.Builder serviceClassBuilder) {
final TypeSpec.Builder clientSpecBuilder = interfaceBuilder(state.clientClass).addModifiers(PUBLIC).addSuperinterface(ParameterizedTypeName.get(GrpcClient, state.blockingClientClass));
final TypeSpec.Builder blockingClientSpecBuilder = interfaceBuilder(state.blockingClientClass).addModifiers(PUBLIC).addSuperinterface(ParameterizedTypeName.get(BlockingGrpcClient, state.clientClass));
for (int i = 0; i < state.clientMetaDatas.size(); ++i) {
final int methodIndex = i;
ClientMetaData clientMetaData = state.clientMetaDatas.get(i);
clientSpecBuilder.addMethod(newRpcMethodSpec(clientMetaData.methodProto, EnumSet.of(INTERFACE, CLIENT), printJavaDocs, (__, b) -> {
b.addModifiers(ABSTRACT);
if (printJavaDocs) {
extractJavaDocComments(state, methodIndex, b);
}
return b;
})).addMethod(newRpcMethodSpec(clientMetaData.methodProto, EnumSet.of(INTERFACE, CLIENT), printJavaDocs, (methodName, b) -> {
ClassName inClass = messageTypesMap.get(clientMetaData.methodProto.getInputType());
b.addModifiers(ABSTRACT).addParameter(clientMetaData.className, metadata).addAnnotation(Deprecated.class);
if (printJavaDocs) {
extractJavaDocComments(state, methodIndex, b);
b.addJavadoc(JAVADOC_DEPRECATED + "Use {@link #$L($T,$T)}." + lineSeparator(), methodName, GrpcClientMetadata, clientMetaData.methodProto.getClientStreaming() ? Publisher : inClass).addJavadoc(JAVADOC_PARAM + metadata + " the metadata associated with this client call." + lineSeparator());
}
return b;
})).addMethod(newRpcMethodSpec(clientMetaData.methodProto, EnumSet.of(INTERFACE, CLIENT), printJavaDocs, (methodName, b) -> {
b.addModifiers(DEFAULT).addParameter(GrpcClientMetadata, metadata);
if (printJavaDocs) {
extractJavaDocComments(state, methodIndex, b);
b.addJavadoc(JAVADOC_PARAM + metadata + " the metadata associated with this client call." + lineSeparator());
}
return b.addStatement("return $L(new $T($L), $L)", methodName, clientMetaData.className, metadata, request);
}));
blockingClientSpecBuilder.addMethod(newRpcMethodSpec(clientMetaData.methodProto, EnumSet.of(BLOCKING, INTERFACE, CLIENT), printJavaDocs, (__, b) -> {
b.addModifiers(ABSTRACT);
if (printJavaDocs) {
extractJavaDocComments(state, methodIndex, b);
}
return b;
})).addMethod(newRpcMethodSpec(clientMetaData.methodProto, EnumSet.of(BLOCKING, INTERFACE, CLIENT), printJavaDocs, (methodName, b) -> {
ClassName inClass = messageTypesMap.get(clientMetaData.methodProto.getInputType());
b.addModifiers(ABSTRACT).addParameter(clientMetaData.className, metadata).addAnnotation(Deprecated.class);
if (printJavaDocs) {
extractJavaDocComments(state, methodIndex, b);
b.addJavadoc(JAVADOC_DEPRECATED + "Use {@link #$L($T,$T)}." + lineSeparator(), methodName, GrpcClientMetadata, clientMetaData.methodProto.getClientStreaming() ? Types.Iterable : inClass).addJavadoc(JAVADOC_PARAM + metadata + " the metadata associated with this client call." + lineSeparator());
}
return b;
})).addMethod(newRpcMethodSpec(clientMetaData.methodProto, EnumSet.of(BLOCKING, INTERFACE, CLIENT), printJavaDocs, (methodName, b) -> {
b.addModifiers(DEFAULT).addParameter(GrpcClientMetadata, metadata);
if (printJavaDocs) {
extractJavaDocComments(state, methodIndex, b);
b.addJavadoc(JAVADOC_PARAM + metadata + " the metadata associated with this client call." + lineSeparator());
}
return b.addStatement("return $L(new $T($L), $L)", methodName, clientMetaData.className, metadata, request);
}));
}
serviceClassBuilder.addType(clientSpecBuilder.build()).addType(blockingClientSpecBuilder.build());
return serviceClassBuilder;
}
use of javax.lang.model.element.Modifier.DEFAULT in project webofneeds by researchstudio-sat.
the class ShapeTypeInterfacePopulator method postprocess.
@Override
public Map<TypeSpec, TypeSpec> postprocess(Set<TypeSpec> allTypes) {
if (!config.isInterfacesForRdfTypes()) {
return Collections.emptyMap();
}
Map<TypeSpec, TypeSpec> modifiedInterfaces = new HashMap<>();
for (URI interfaceClassUri : shapeTypeInterfaceTypes.keySet()) {
TypeSpec interfaceType = shapeTypeInterfaceTypes.get(interfaceClassUri).get();
TypeSpec.Builder modifyingBuilder = interfaceType.toBuilder();
Map<String, Set<MethodSpec>> candidateInterfaceMethodsByType = new HashMap<>();
if (shapeTypeImplTypes.containsKey(interfaceClassUri)) {
Optional<Set<TypeSpec>> implementingTypes = shapeTypeImplTypes.get(interfaceClassUri);
if (implementingTypes.isPresent()) {
for (TypeSpec type : implementingTypes.get()) {
// generate public abstract method with same signature and remember with type
// name
Set<MethodSpec> candidateInterfaceMethods = type.methodSpecs.stream().filter(m -> m.hasModifier(PUBLIC)).filter(m -> !m.isConstructor()).filter(m -> !m.name.equals("getNode")).filter(m -> !m.name.equals("setNode")).filter(m -> !Arrays.stream(Object.class.getMethods()).anyMatch(om -> om.getName().equals(m.name))).map(methodSpec -> {
MethodSpec.Builder b = MethodSpec.methodBuilder(methodSpec.name);
b.returns(methodSpec.returnType);
b.addTypeVariables(methodSpec.typeVariables);
methodSpec.parameters.forEach(p -> b.addParameter(p));
if (!methodSpec.returnType.equals(ClassName.VOID)) {
if (methodSpec.returnType.isPrimitive()) {
b.addStatement("return ($T) null", methodSpec.returnType.box());
} else {
b.addStatement("return null");
}
}
b.addModifiers(PUBLIC, DEFAULT);
return b.build();
}).collect(Collectors.toSet());
candidateInterfaceMethodsByType.put(type.name, candidateInterfaceMethods);
}
}
Set<MethodSpec> commonMethodsForInterface = candidateInterfaceMethodsByType.values().stream().reduce(null, (left, right) -> {
if (left == null) {
return right;
} else if (right == null) {
return left;
}
HashSet<MethodSpec> intersection = new HashSet(left);
intersection.retainAll(right);
return intersection;
});
commonMethodsForInterface.forEach(m -> {
if (logger.isDebugEnabled()) {
logger.debug("Adding interface method {} to interface {}", m.name, shapeTypeInterfaceTypes.get(interfaceClassUri).get().name);
}
modifyingBuilder.addMethod(m);
});
}
modifiedInterfaces.put(interfaceType, modifyingBuilder.build());
}
return modifiedInterfaces;
}
use of javax.lang.model.element.Modifier.DEFAULT in project servicetalk by apple.
the class Generator method addClientMetadata.
private TypeSpec.Builder addClientMetadata(final State state, final TypeSpec.Builder serviceClassBuilder) {
state.serviceRpcInterfaces.stream().filter(rpcInterface -> !rpcInterface.blocking).forEach(rpcInterface -> {
MethodDescriptorProto methodProto = rpcInterface.methodProto;
final String name = context.deconflictJavaTypeName(sanitizeIdentifier(methodProto.getName(), false) + Metadata);
final ClassName metaDataClassName = ClassName.bestGuess(name);
final TypeSpec classSpec = classBuilder(name).addJavadoc(JAVADOC_DEPRECATED + "This class will be removed in the future in favor of direct " + "usage of {@link $T}. Deprecation of {@link $T#path()} renders this type unnecessary." + lineSeparator(), GrpcClientMetadata, GrpcClientMetadata).addAnnotation(Deprecated.class).addModifiers(PUBLIC, STATIC, FINAL).superclass(DefaultGrpcClientMetadata).addField(FieldSpec.builder(metaDataClassName, INSTANCE).addJavadoc(JAVADOC_DEPRECATED + "This class will be removed in the future in favor of direct usage of {@link $T}." + lineSeparator(), GrpcClientMetadata).addAnnotation(Deprecated.class).addModifiers(PUBLIC, STATIC, // redundant, default for interface field
FINAL).initializer("new $T()", metaDataClassName).build()).addMethod(constructorBuilder().addModifiers(PRIVATE).addParameter(GrpcClientMetadata, metadata, FINAL).addStatement("super($T.$L, $L)", rpcInterface.className, RPC_PATH, metadata).build()).addMethod(constructorBuilder().addModifiers(PRIVATE).addStatement("super($T.$L)", rpcInterface.className, RPC_PATH).build()).addMethod(constructorBuilder().addModifiers(PUBLIC).addParameter(ContentCodec, requestEncoding, FINAL).addStatement("super($T.$L, $L)", rpcInterface.className, RPC_PATH, requestEncoding).build()).addMethod(constructorBuilder().addModifiers(PUBLIC).addParameter(GrpcExecutionStrategy, strategy, FINAL).addStatement("super($T.$L, $L)", rpcInterface.className, RPC_PATH, strategy).build()).addMethod(constructorBuilder().addModifiers(PUBLIC).addParameter(Duration.class, timeout, FINAL).addStatement("super($T.$L, $L)", rpcInterface.className, RPC_PATH, timeout).build()).addMethod(constructorBuilder().addModifiers(PUBLIC).addParameter(GrpcExecutionStrategy, strategy, FINAL).addParameter(ContentCodec, requestEncoding, FINAL).addStatement("super($T.$L, $L, $L)", rpcInterface.className, RPC_PATH, strategy, requestEncoding).build()).addMethod(constructorBuilder().addModifiers(PUBLIC).addParameter(GrpcExecutionStrategy, strategy, FINAL).addParameter(ContentCodec, requestEncoding, FINAL).addParameter(Duration.class, timeout, FINAL).addStatement("super($T.$L, $L, $L, $L)", rpcInterface.className, RPC_PATH, strategy, requestEncoding, timeout).build()).build();
state.clientMetaDatas.add(new ClientMetaData(methodProto, metaDataClassName));
serviceClassBuilder.addType(classSpec);
});
return serviceClassBuilder;
}
Aggregations