Java UNRECOGNIZED Enum exception

837 views
Skip to first unread message

Karthi Ayappan

unread,
May 8, 2023, 11:17:19 PM5/8/23
to Protocol Buffers
Hi,

Context: I am trying to migrate very old protoc generated models library (protobuf-lite which is no longer available) to protobuf-javalite. Once I switched to latest protoc version, the generated enum throws IllegalStateException on getNumber via setEnum methods. We have huge number of such usage in our very big project. 

Question: 
1. How to gracefully avoid it without making sweeping changes in the code. I am talking about hundreds of such new enum classes and even more number of usages of those enums in multiple modules in our code. Specially I want to avoid this exception with minimal change.
2. Could someone point me to doc or give me context on why we have this unChecked exception (escpecially while referring from kotlin code). 

Any idea to fix this is very welcome. Thanks in advance. 



Matthew Fowles Kulukundis

unread,
May 8, 2023, 11:23:02 PM5/8/23
to Karthi Ayappan, Protocol Buffers
Karthi~

I am not entirely sure I understand your question, but I suspect the note on Java's enum behavior at https://protobuf.dev/programming-guides/enum/#java might help.  If you use the `get<FieldName>Value` and `set<FieldName>Value` methods they will not throw exceptions.

Cheers,
Matt

--
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to protobuf+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/protobuf/54542179-cb17-43c7-b2cb-67268819c05en%40googlegroups.com.

Karthi Ayappan

unread,
May 8, 2023, 11:50:15 PM5/8/23
to Protocol Buffers
Thank you Matthew for the quick response. Sorry that my question was not very clear.  The document you shared is helpful.
 Let me rephrase my question

The enum classes generated out of protoc version (very old), that we are using currently,  do not throw any exception. So we have lot of usages of set<FieldNameXX> throughout the project for multiple classes. Now after changing to latest protoc generator, all these usages can throw exception runtime and crash the application.  Finding direct usages of these classes are very difficult and given it is runtime exception, we can't be 100% sure we removed all those usages. 

1) So I am looking for idea to tackle this situation. 
2) How to make sure, in future, no one accidentally uses this set<FieldNameXX> or Enum.getNumber()methods [I am talking about huge team with lot of engineers]

Matthew Fowles Kulukundis

unread,
May 9, 2023, 8:25:36 AM5/9/23
to Karthi Ayappan, Protocol Buffers
Karthi~

I would probably use a java refactoring/rewriting tool to switch calls to the `get<FieldName>Value`/`set<FieldName>Value` APIs and use presubmit linting to ensure developers don't add new ones.  Unfortunately, anything more specific likely depends heavily on the exact setup of your build and version control systems.

Matt

Reply all
Reply to author
Forward
0 new messages