Skip to content

Commit

Permalink
Fix javadoc handling and import handling for 1.5.9
Browse files Browse the repository at this point in the history
  • Loading branch information
scottslewis committed Jun 8, 2021
1 parent ec9f8c6 commit ccae596
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<launchConfiguration type="org.eclipse.jdt.launching.localJavaApplication">
<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_PATHS">
<listEntry value="/generator/src/main/java/org/eclipse/ecf/grpc/osgigenerator/OSGiGenerator.java"/>
<listEntry value="/grpc-osgi-generator/src/main/java/org/eclipse/ecf/grpc/osgigenerator/OSGiGenerator.java"/>
</listAttribute>
<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_TYPES">
<listEntry value="1"/>
</listAttribute>
<booleanAttribute key="org.eclipse.jdt.launching.ATTR_EXCLUDE_TEST_CODE" value="true"/>
<booleanAttribute key="org.eclipse.jdt.launching.ATTR_ATTR_USE_ARGFILE" value="false"/>
<booleanAttribute key="org.eclipse.jdt.launching.ATTR_SHOW_CODEDETAILS_IN_EXCEPTION_MESSAGES" value="true"/>
<booleanAttribute key="org.eclipse.jdt.launching.ATTR_USE_CLASSPATH_ONLY_JAR" value="false"/>
<booleanAttribute key="org.eclipse.jdt.launching.ATTR_USE_START_ON_FIRST_THREAD" value="true"/>
<stringAttribute key="org.eclipse.jdt.launching.CLASSPATH_PROVIDER" value="org.eclipse.m2e.launchconfig.classpathProvider"/>
<stringAttribute key="org.eclipse.jdt.launching.MAIN_TYPE" value="org.eclipse.ecf.grpc.osgigenerator.OSGiGenerator"/>
<stringAttribute key="org.eclipse.jdt.launching.MODULE_NAME" value="generator"/>
<stringAttribute key="org.eclipse.jdt.launching.PROGRAM_ARGUMENTS" value="examples\hello-world\target\generated-sources\protobuf\java\descriptor_dump"/>
<stringAttribute key="org.eclipse.jdt.launching.PROJECT_ATTR" value="generator"/>
<stringAttribute key="org.eclipse.jdt.launching.MODULE_NAME" value="grpc-osgi-generator"/>
<stringAttribute key="org.eclipse.jdt.launching.PROGRAM_ARGUMENTS" value="examples/hello-world/target/generated-sources/protobuf/java/descriptor_dump"/>
<stringAttribute key="org.eclipse.jdt.launching.PROJECT_ATTR" value="grpc-osgi-generator"/>
<stringAttribute key="org.eclipse.jdt.launching.SOURCE_PATH_PROVIDER" value="org.eclipse.m2e.launchconfig.sourcepathProvider"/>
</launchConfiguration>
11 changes: 11 additions & 0 deletions generator/examples/hello-world/src/main/proto/Hello.proto
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ service Greeter {
rpc Greet (HelloRequest) returns (HelloResponse) {}
}

/*
* Define the service's operations
*/
service Greeter1 {
option (generation_type) = GRPC_UNARY;
/*
* Greet1
*/
rpc Greet1 (HelloRequest) returns (HelloResponse) {}
}

/*
* Define the service's data structures
*/
Expand Down
2 changes: 1 addition & 1 deletion generator/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<version>1.5.6</version>
<version>1.5.9</version>

<artifactId>grpc-osgi-generator</artifactId>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class OSGiGenerator extends Generator {
private static final String FQ_SINGLE_CLASS = String.join(".", REACTIVE_PACKAGE, SINGLE_CLASS);

private static final String DEFAULT_BODY_NULL_RETURN = "return null";

public static void main(String[] args) throws Exception {
List<Generator> generators = new ArrayList<Generator>();
generators.add(new OSGiGenerator());
Expand Down Expand Up @@ -118,8 +118,8 @@ private ServiceContext buildServiceContext(ServiceDescriptorProto serviceProto,
serviceContext.javaDoc = getJavaDoc(getComments(serviceLocation), getServiceJavaDocPrefix());

for (int methodNumber = 0; methodNumber < serviceProto.getMethodCount(); methodNumber++) {
MethodContext methodContext = buildMethodContext(serviceContext, serviceProto.getMethod(methodNumber), typeMap, locations,
methodNumber);
MethodContext methodContext = buildMethodContext(serviceContext, serviceProto.getMethod(methodNumber),
typeMap, allLocationsForService, methodNumber);
if (methodContext != null) {
serviceContext.methods.add(methodContext);
}
Expand All @@ -128,11 +128,12 @@ private ServiceContext buildServiceContext(ServiceDescriptorProto serviceProto,
}

private void setImport(ServiceContext serviceContext, String importClass) {
serviceContext.imports.add(new Import(importClass));
if (!serviceContext.imports.stream().filter(i -> i.importClass.equals(importClass)).findAny().isPresent())
serviceContext.imports.add(new Import(importClass));
}
private MethodContext buildMethodContext(ServiceContext serviceContext, MethodDescriptorProto methodProto, ProtoTypeMap typeMap,
List<Location> locations, int methodNumber) {

private MethodContext buildMethodContext(ServiceContext serviceContext, MethodDescriptorProto methodProto,
ProtoTypeMap typeMap, List<Location> locations, int methodNumber) {

MethodContext methodContext = new MethodContext();
methodContext.isManyInput = methodProto.getClientStreaming();
Expand All @@ -142,14 +143,25 @@ private MethodContext buildMethodContext(ServiceContext serviceContext, MethodDe
String baseInputType = typeMap.toJavaTypeName(methodProto.getInputType());
String baseOutputType = typeMap.toJavaTypeName(methodProto.getOutputType());
methodContext.defaultBody = DEFAULT_BODY_NULL_RETURN;

setImport(serviceContext, FQ_FLOWABLE_CLASS);
setImport(serviceContext, FQ_SINGLE_CLASS);

methodContext.inputType = methodContext.isManyInput?FLOWABLE_CLASS:SINGLE_CLASS;
// Argument types
if (methodContext.isManyInput) {
setImport(serviceContext, FQ_FLOWABLE_CLASS);
methodContext.inputType = FLOWABLE_CLASS;
} else {
setImport(serviceContext, FQ_SINGLE_CLASS);
methodContext.inputType = SINGLE_CLASS;
}
methodContext.inputGenericType = baseInputType;
methodContext.outputType = methodContext.isManyOutput?FLOWABLE_CLASS:SINGLE_CLASS;
// return types
if (methodContext.isManyOutput) {
setImport(serviceContext, FQ_FLOWABLE_CLASS);
methodContext.outputType = FLOWABLE_CLASS;
} else {
setImport(serviceContext, FQ_SINGLE_CLASS);
methodContext.outputType = SINGLE_CLASS;
}
methodContext.outputGenericType = baseOutputType;

methodContext.deprecated = methodProto.getOptions() != null && methodProto.getOptions().getDeprecated();
Location methodLocation = locations.stream()
.filter(location -> location.getPathCount() == METHOD_NUMBER_OF_PATHS
Expand All @@ -159,7 +171,7 @@ private MethodContext buildMethodContext(ServiceContext serviceContext, MethodDe

return methodContext;
}

private String lowerCaseFirst(String s) {
return Character.toLowerCase(s.charAt(0)) + s.substring(1);
}
Expand All @@ -178,7 +190,9 @@ private String getMethodJavaDocPrefix() {

private PluginProtos.CodeGeneratorResponse.File buildFile(ServiceContext context) {
return PluginProtos.CodeGeneratorResponse.File.newBuilder().setName(absoluteFileName(context))
.setContent(applyTemplate(context.methodTypes == GenerationType.GRPC_UNARY?"GrpcService.mustache":"ReactiveXService.mustache", context)).build();
.setContent(applyTemplate(context.methodTypes == GenerationType.GRPC_UNARY ? "GrpcService.mustache"
: "ReactiveXService.mustache", context))
.build();
}

private String absoluteFileName(ServiceContext ctx) {
Expand Down Expand Up @@ -206,20 +220,21 @@ private String getJavaDoc(String comments, String prefix) {
return null;
}

public static <T> Predicate<T> distinctByKey(Function<? super T, Object> keyExtractor)
{
Map<Object, Boolean> map = new ConcurrentHashMap<>();
return t -> map.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
}

public static <T> Predicate<T> distinctByKey(Function<? super T, Object> keyExtractor) {
Map<Object, Boolean> map = new ConcurrentHashMap<>();
return t -> map.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
}

private class Import {
@SuppressWarnings("unused")
public String importClass;

@SuppressWarnings("unused")
Import(String clazzName) {
this.importClass = clazzName;
}
}

/**
* Template class for proto Service objects.
*/
Expand Down Expand Up @@ -259,7 +274,7 @@ public List<MethodContext> clientStreamingRequestMethods() {
public List<MethodContext> bidiStreamingRequestMethods() {
return methods.stream().filter(m -> (m.isManyInput && m.isManyOutput)).collect(Collectors.toList());
}

@SuppressWarnings("unused")
public List<Import> importClassNames() {
return imports.stream().filter(distinctByKey(i -> i.importClass)).collect(Collectors.toList());
Expand Down Expand Up @@ -308,7 +323,7 @@ public String methodNameCamelCase() {
String mn = methodName.replace("_", "");
return String.valueOf(Character.toLowerCase(mn.charAt(0))) + mn.substring(1);
}

}

}

0 comments on commit ccae596

Please sign in to comment.