1414
1515package dev .cel .common .internal ;
1616
17- import com .google .common .base .CaseFormat ;
18- import com .google .common .base .Joiner ;
19- import com .google .common .base .Strings ;
20- import com .google .common .io .Files ;
21- import com .google .protobuf .DescriptorProtos .FileOptions ;
2217import com .google .protobuf .Descriptors .Descriptor ;
23- import com .google .protobuf .Descriptors .EnumDescriptor ;
2418import com .google .protobuf .Descriptors .FileDescriptor ;
25- import com .google .protobuf .Descriptors .GenericDescriptor ;
26- import com .google .protobuf .Descriptors .ServiceDescriptor ;
19+ import com .google .protobuf .GeneratorNames ;
2720import dev .cel .common .annotations .Internal ;
28- import java .util .ArrayDeque ;
2921
3022/**
3123 * Helper class for constructing a fully qualified Java class name from a protobuf descriptor.
3426 */
3527@ Internal
3628public final class ProtoJavaQualifiedNames {
37- // Controls how many times we should recursively inspect a nested message for building fully
38- // qualified java class name before aborting.
39- private static final int SAFE_RECURSE_LIMIT = 50 ;
40-
4129 /**
4230 * Retrieves the full Java class name from the given descriptor
4331 *
@@ -48,44 +36,7 @@ public final class ProtoJavaQualifiedNames {
4836 * (Nested class with java multiple files disabled)
4937 */
5038 public static String getFullyQualifiedJavaClassName (Descriptor descriptor ) {
51- return getFullyQualifiedJavaClassNameImpl (descriptor );
52- }
53-
54- private static String getFullyQualifiedJavaClassNameImpl (GenericDescriptor descriptor ) {
55- StringBuilder fullClassName = new StringBuilder ();
56-
57- fullClassName .append (getJavaPackageName (descriptor .getFile ())).append ("." );
58-
59- String javaOuterClass = getJavaOuterClassName (descriptor .getFile ());
60- if (!Strings .isNullOrEmpty (javaOuterClass )) {
61- fullClassName .append (javaOuterClass ).append ("$" );
62- }
63-
64- // Recursively build the target class name in case if the message is nested.
65- ArrayDeque <String > classNames = new ArrayDeque <>();
66- GenericDescriptor d = descriptor ;
67-
68- int recurseCount = 0 ;
69- while (d != null ) {
70- classNames .push (d .getName ());
71-
72- if (d instanceof EnumDescriptor ) {
73- d = ((EnumDescriptor ) d ).getContainingType ();
74- } else {
75- d = ((Descriptor ) d ).getContainingType ();
76- }
77- recurseCount ++;
78- if (recurseCount >= SAFE_RECURSE_LIMIT ) {
79- throw new IllegalStateException (
80- String .format (
81- "Recursion limit of %d hit while inspecting descriptor: %s" ,
82- SAFE_RECURSE_LIMIT , descriptor .getFullName ()));
83- }
84- }
85-
86- Joiner .on ("$" ).appendTo (fullClassName , classNames );
87-
88- return fullClassName .toString ();
39+ return GeneratorNames .getBytecodeClassName (descriptor );
8940 }
9041
9142 /**
@@ -94,68 +45,7 @@ private static String getFullyQualifiedJavaClassNameImpl(GenericDescriptor descr
9445 * on package name generation
9546 */
9647 public static String getJavaPackageName (FileDescriptor fileDescriptor ) {
97- FileOptions options = fileDescriptor .getFile ().getOptions ();
98- StringBuilder javaPackageName = new StringBuilder ();
99- if (options .hasJavaPackage ()) {
100- javaPackageName .append (options .getJavaPackage ());
101- } else {
102- javaPackageName
103- // CEL-Internal-1
104- .append (fileDescriptor .getPackage ());
105- }
106-
107- // CEL-Internal-2
108-
109- return javaPackageName .toString ();
110- }
111-
112- /**
113- * Gets a wrapping outer class name from the descriptor. The outer class name differs depending on
114- * the proto options set. See
115- * https://developers.google.com/protocol-buffers/docs/reference/java-generated#invocation
116- */
117- private static String getJavaOuterClassName (FileDescriptor descriptor ) {
118- FileOptions options = descriptor .getOptions ();
119-
120- if (options .getJavaMultipleFiles ()) {
121- // If java_multiple_files is enabled, protoc does not generate a wrapper outer class
122- return "" ;
123- }
124-
125- if (options .hasJavaOuterClassname ()) {
126- return options .getJavaOuterClassname ();
127- } else {
128- // If an outer class name is not explicitly set, the name is converted into
129- // Pascal case based on the snake cased file name
130- // Ex: messages_proto.proto becomes MessagesProto
131- String protoFileNameWithoutExtension =
132- Files .getNameWithoutExtension (descriptor .getFile ().getFullName ());
133- String outerClassName =
134- CaseFormat .LOWER_UNDERSCORE .to (CaseFormat .UPPER_CAMEL , protoFileNameWithoutExtension );
135- if (hasConflictingClassName (descriptor .getFile (), outerClassName )) {
136- outerClassName += "OuterClass" ;
137- }
138- return outerClassName ;
139- }
140- }
141-
142- private static boolean hasConflictingClassName (FileDescriptor file , String name ) {
143- for (EnumDescriptor enumDesc : file .getEnumTypes ()) {
144- if (name .equals (enumDesc .getName ())) {
145- return true ;
146- }
147- }
148- for (ServiceDescriptor serviceDesc : file .getServices ()) {
149- if (name .equals (serviceDesc .getName ())) {
150- return true ;
151- }
152- }
153- for (Descriptor messageDesc : file .getMessageTypes ()) {
154- if (name .equals (messageDesc .getName ())) {
155- return true ;
156- }
157- }
158- return false ;
48+ return GeneratorNames .getFileJavaPackage (fileDescriptor .toProto ());
15949 }
16050
16151 private ProtoJavaQualifiedNames () {}
0 commit comments