From 69bfe7c6d7f06cb90d913a27a5f12c17ba1fdb82 Mon Sep 17 00:00:00 2001 From: "Nadav Sr. Samet" Date: Sat, 22 Aug 2015 12:38:29 -0700 Subject: [PATCH] Detect java name conflicts and append OuterClass suffix when detected. This follows the behavior of the official Java generator. Fixes #26 --- .../scalapb/compiler/DescriptorPimps.scala | 14 +++++++++++++- e2e/src/main/protobuf/issue26.proto | 9 +++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 e2e/src/main/protobuf/issue26.proto diff --git a/compiler-plugin/src/main/scala/com/trueaccord/scalapb/compiler/DescriptorPimps.scala b/compiler-plugin/src/main/scala/com/trueaccord/scalapb/compiler/DescriptorPimps.scala index 293954f9c..c93e76669 100644 --- a/compiler-plugin/src/main/scala/com/trueaccord/scalapb/compiler/DescriptorPimps.scala +++ b/compiler-plugin/src/main/scala/com/trueaccord/scalapb/compiler/DescriptorPimps.scala @@ -150,6 +150,11 @@ trait DescriptorPimps { case None => message.getFile.scalaPackageName + "." + nameSymbol } + private[compiler] def hasConflictingJavaClassName(className: String): Boolean = ( + (message.getName == className) || + (message.getEnumTypes.exists(_.getName == className)) || + (message.nestedTypes.exists(_.hasConflictingJavaClassName(className)))) + def javaTypeName = message.getFile.fullJavaName(message.getFullName) def messageOptions: MessageOptions = message.getOptions.getExtension[MessageOptions](Scalapb.message) @@ -227,11 +232,18 @@ trait DescriptorPimps { def javaPackageAsSymbol: String = javaPackage.split('.').map(_.asSymbol).mkString(".") + private def hasConflictingJavaClassName(className: String) = ( + file.getEnumTypes.exists(_.getName == className) || + file.getServices.exists(_.getName == className) || + file.getMessageTypes.exists(_.hasConflictingJavaClassName(className))) + def javaOuterClassName = if (file.getOptions.hasJavaOuterClassname) file.getOptions.getJavaOuterClassname else { - snakeCaseToCamelCase(baseName(file.getName), true) + val r = snakeCaseToCamelCase(baseName(file.getName), true) + if (!hasConflictingJavaClassName(r)) r + else r + "OuterClass" } def scalaPackageName = { diff --git a/e2e/src/main/protobuf/issue26.proto b/e2e/src/main/protobuf/issue26.proto new file mode 100644 index 000000000..5c5db0069 --- /dev/null +++ b/e2e/src/main/protobuf/issue26.proto @@ -0,0 +1,9 @@ +// Regression test for https://github.com/trueaccord/ScalaPB/issues/26 +syntax = "proto3"; + +package name_test; + +message Issue26 { + int32 a = 1; +} +