Repository: aurora Updated Branches: refs/heads/master aac7aad37 -> 4a9f2ff5f
Simplify thrift service metadata. This leverages java.lang.reflect.Parameter.getName as enabled by turning on emission of parameter names in the debug info for aurora classfiles. Reviewed at https://reviews.apache.org/r/41834/ Project: http://git-wip-us.apache.org/repos/asf/aurora/repo Commit: http://git-wip-us.apache.org/repos/asf/aurora/commit/4a9f2ff5 Tree: http://git-wip-us.apache.org/repos/asf/aurora/tree/4a9f2ff5 Diff: http://git-wip-us.apache.org/repos/asf/aurora/diff/4a9f2ff5 Branch: refs/heads/master Commit: 4a9f2ff5f2150c8f6bba4a51f06c707b471a8ad1 Parents: aac7aad Author: John Sirois <[email protected]> Authored: Fri Jan 1 15:12:35 2016 -0800 Committer: Bill Farner <[email protected]> Committed: Fri Jan 1 15:12:35 2016 -0800 ---------------------------------------------------------------------- build.gradle | 9 +++++ .../org/apache/aurora/build/ThriftPlugin.groovy | 2 + .../aurora/scheduler/http/api/ApiBeta.java | 26 ++++++------- .../aurora/tools/java/thrift_wrapper_codegen.py | 40 +++++--------------- 4 files changed, 33 insertions(+), 44 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/aurora/blob/4a9f2ff5/build.gradle ---------------------------------------------------------------------- diff --git a/build.gradle b/build.gradle index 5ff10d9..6f89958 100644 --- a/build.gradle +++ b/build.gradle @@ -272,6 +272,8 @@ compileJava { options.compilerArgs << '-Xlint:-processing' // Don't fail for serialVersionUID warnings. options.compilerArgs << '-Xlint:-serial' + // Capture method parameter names in classfiles. + options.compilerArgs << '-parameters' } task enforceVersion { @@ -479,6 +481,8 @@ idea { ipr { withXml { def projectNode = it.asNode() + + // Configure an annotation processor profile. def compilerConfiguration = projectNode.find { it.name() == 'component' && it.@name == 'CompilerConfiguration' } @@ -501,6 +505,11 @@ idea { it.name() == 'component' && it.@name == 'ProjectRootManager' } projectRoot.remove(projectRoot.output) + + // Add parameter names to generated class file debug info. + projectNode + .appendNode('component', [name: 'JavacSettings']) + .appendNode('option', [name: 'ADDITIONAL_OPTIONS_STRING', value: '-parameters']) } } } http://git-wip-us.apache.org/repos/asf/aurora/blob/4a9f2ff5/buildSrc/src/main/groovy/org/apache/aurora/build/ThriftPlugin.groovy ---------------------------------------------------------------------- diff --git a/buildSrc/src/main/groovy/org/apache/aurora/build/ThriftPlugin.groovy b/buildSrc/src/main/groovy/org/apache/aurora/build/ThriftPlugin.groovy index 44cb723..fc2bc90 100644 --- a/buildSrc/src/main/groovy/org/apache/aurora/build/ThriftPlugin.groovy +++ b/buildSrc/src/main/groovy/org/apache/aurora/build/ThriftPlugin.groovy @@ -72,6 +72,8 @@ class ThriftPlugin implements Plugin<Project> { classpath = configurations.thriftCompile destinationDir = file(thrift.genClassesDir) options.warnings = false + // Capture method parameter names in classfiles. + options.compilerArgs << '-parameters' } configurations.create('thriftRuntime') http://git-wip-us.apache.org/repos/asf/aurora/blob/4a9f2ff5/src/main/java/org/apache/aurora/scheduler/http/api/ApiBeta.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/aurora/scheduler/http/api/ApiBeta.java b/src/main/java/org/apache/aurora/scheduler/http/api/ApiBeta.java index b889f93..9a18c69 100644 --- a/src/main/java/org/apache/aurora/scheduler/http/api/ApiBeta.java +++ b/src/main/java/org/apache/aurora/scheduler/http/api/ApiBeta.java @@ -16,11 +16,10 @@ package org.apache.aurora.scheduler.http.api; import java.io.OutputStreamWriter; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; -import java.lang.reflect.Type; +import java.lang.reflect.Parameter; import java.net.URI; import java.nio.charset.StandardCharsets; import java.util.List; -import java.util.Map; import java.util.Objects; import java.util.logging.Level; import java.util.logging.Logger; @@ -89,30 +88,29 @@ public class ApiBeta { * will be substituted. * * @param json Incoming request data, to translate into method parameters. - * @param fields Field metadata map. Map <strong>iteration order must match</strong> the order - * defined in the thrift method. + * @param method The thrift method to bind parameters for. * @return Parsed method parameters. * @throws WebApplicationException If a parameter could not be parsed. */ - private Object[] readParams(JsonObject json, Map<String, Type> fields) + private Object[] readParams(JsonObject json, Method method) throws WebApplicationException { List<Object> params = Lists.newArrayList(); - for (Map.Entry<String, Type> entry : fields.entrySet()) { + for (Parameter param : method.getParameters()) { try { - params.add(GSON.fromJson(getJsonMember(json, entry.getKey()), entry.getValue())); + params.add(GSON.fromJson(getJsonMember(json, param.getName()), param.getType())); } catch (JsonParseException e) { throw new WebApplicationException( e, - badRequest("Failed to parse parameter " + entry.getKey() + ": " + e.getMessage())); + badRequest("Failed to parse parameter " + param + ": " + e.getMessage())); } } return params.toArray(); } - private Method getApiMethod(String name, Map<String, Type> metadata) { + private Method getApiMethod(String name, Class<?>[] parameterTypes) { try { - return Iface.class.getMethod(name, metadata.values().toArray(new Class<?>[0])); + return Iface.class.getMethod(name, parameterTypes); } catch (NoSuchMethodException e) { throw Throwables.propagate(e); } @@ -127,8 +125,8 @@ public class ApiBeta { } // First, verify that this is a valid method on the interface. - Map<String, Type> methodMetadata = AuroraAdminMetadata.METHODS.get(methodName); - if (methodMetadata == null) { + Class<?>[] methodParameterTypes = AuroraAdminMetadata.METHODS.get(methodName); + if (methodParameterTypes == null) { return errorResponse(Status.NOT_FOUND, "Method " + methodName + " does not exist."); } @@ -146,8 +144,8 @@ public class ApiBeta { throw new WebApplicationException(e, badRequest("Request must be valid JSON")); } - final Method method = getApiMethod(methodName, methodMetadata); - final Object[] params = readParams(parameters, methodMetadata); + final Method method = getApiMethod(methodName, methodParameterTypes); + final Object[] params = readParams(parameters, method); return Response.ok((StreamingOutput) output -> { try { Object response = method.invoke(api, params); http://git-wip-us.apache.org/repos/asf/aurora/blob/4a9f2ff5/src/main/python/apache/aurora/tools/java/thrift_wrapper_codegen.py ---------------------------------------------------------------------- diff --git a/src/main/python/apache/aurora/tools/java/thrift_wrapper_codegen.py b/src/main/python/apache/aurora/tools/java/thrift_wrapper_codegen.py index 49cfa9f..e9e9368 100644 --- a/src/main/python/apache/aurora/tools/java/thrift_wrapper_codegen.py +++ b/src/main/python/apache/aurora/tools/java/thrift_wrapper_codegen.py @@ -336,7 +336,7 @@ class Method(object): self.return_type = return_type def __str__(self): - return '%s(%s)' % (self.name, ', '.join(map(str, self.parameters))) + return '%s(%s)' % (self.name, ', '.join(self.parameters)) class Parameter(object): def __init__(self, name, type_name): @@ -353,31 +353,26 @@ class GenericParameter(Parameter): GET_SUPER_METHODS = '.putAll(%(super)sMetadata.METHODS)' -PARAM_METADATA_TEMPLATE = '.put("%(name)s", %(type)s.class)' - -GENERIC_PARAM_METADATA_TEMPLATE = ( - '.put("%(name)s", new TypeToken<%(type)s<%(params)s>>() {}.getType())') +PARAM_METADATA_TEMPLATE = '%(type)s.class,' METHOD_METADATA_TEMPLATE = '''.put( "%(name)s", - ImmutableMap.<String, Type>builder()%(params)s - .build())''' + new Class<?>[] {%(params)s + })''' SERVICE_METADATA_TEMPLATE = '''package %(package)s; -import java.lang.reflect.Type; import java.util.List; import java.util.Map; import java.util.Set; import com.google.common.collect.ImmutableMap; -import com.google.gson.reflect.TypeToken; import org.apache.aurora.gen.*; public final class %(name)sMetadata { - public static final Map<String, Map<String, Type>> METHODS = - ImmutableMap.<String, Map<String, Type>>builder() + public static final ImmutableMap<String, Class<?>[]> METHODS = + ImmutableMap.<String, Class<?>[]>builder() %(methods)s .build(); @@ -391,7 +386,7 @@ SERVICE_RE = 'service (?P<name>\w+)\s+(extends\s+(?P<super>\w+)\s+)?{(?P<body>[^ METHOD_RE = '\s*(?P<return>\w+)\s+(?P<name>\w+)\((?P<params>[^\)]*)\)' -PARAM_RE = '\d+\:\s+%s\s+(?P<name>\w+)' % TYPE_PATTERN +PARAM_RE = '\d+\:\s+%s\s+(?:\w+)' % TYPE_PATTERN THRIFT_TYPES = { 'bool': PrimitiveType('boolean', 'Boolean'), @@ -464,13 +459,7 @@ def parse_services(service_defs): for method in re.finditer(METHOD_RE, s.group('body'), flags=re.MULTILINE): params = [] for param in re.finditer(PARAM_RE, method.group('params'), flags=re.MULTILINE): - if param.group('params'): - params.append(GenericParameter( - param.group('name'), - param.group('type'), - param.group('params').replace(' ', '').split(','))) - else: - params.append(Parameter(param.group('name'), param.group('type'))) + params.append(param.group('type')) methods.append(Method(method.group('name'), params, method.group('return'))) @@ -695,21 +684,12 @@ if __name__ == '__main__': if isinstance(thrift_type, PrimitiveType): return thrift_type.boxed_name else: - return name + return thrift_type.name return name def add_param(param): - if param.type_name in THRIFT_TYPES: - thrift_type = THRIFT_TYPES[param.type_name] - if not isinstance(thrift_type, PrimitiveType): - return GENERIC_PARAM_METADATA_TEMPLATE % { - 'name': param.name, - 'type': thrift_type.name, - 'params': ', '.join(map(get_type_name, param.parameters)) - } return PARAM_METADATA_TEMPLATE % { - 'name': param.name, - 'type': get_type_name(param.type_name) + 'type': get_type_name(param) } def add_method(method):
