getClass().getEnclosingClass() returns null for non-obfuscated enum instances?

Hi All,

We have some enums in our codebase that are excluded from obfuscation using -keep. They don’t appear to be obfuscated, but if you call EnumClass.VALUE.getClass().getEnclosingClass() it returns null, where in their non-obfuscated form it would return EnumClass.class.

I have put together what I think is a minimal reproduction, could someone please tell me if I’m doing something wrong or if this is an issue with ProGuard.

Regards,
Nick

Hi Nick,

First off, welcome to our community! I’ve cloned your repository and have tried the shell scripts. The clean install of both obfuscated and unobfuscated versions builds fine, but I get the following error when running the test module (mvn test -f test-module/pom.xml):

[ERROR] Failed to execute goal on project test-module: Could not resolve dependencies for project org.example:test-module:jar:1.0-SNAPSHOT: Failed to collect dependencies at org.example:obfuscated-module:jar:1.0-SNAPSHOT: Failed to read artifact descriptor for org.example:obfuscated-module:jar:1.0-SNAPSHOT: Could not find artifact org.example:GetEnclosingClassBugRepro:pom:1.0-SNAPSHOT -> [Help 1]

I’ve also tried the option -U but nothing additional seems to be built i.e. git doesn’t see any new files generated. Perhaps there are missing files in the repo? Or perhaps there should be some modifications to the pom.xml file in the test module so that it points to the snapshot jar file located within the obfuscated-module/target folder? Could you clarify how exactly you build the sample?

Kind regards,
Amy

Hi Amy,

Thanks for taking a look. Apologies for that, I moved some stuff around and didn’t re-run it with an empty maven repo. I’ve fixed it now so the ./build_test_(un)obfuscated.sh scripts should work now.

Regards,
Nick

Hi Nick,

It looks like you should keep the EnclosingMethod attribute (Chapter 4. The class File Format):

diff --git a/obfuscated-module/pom.xml b/obfuscated-module/pom.xml
index 7598f19..4fc3a33 100644
--- a/obfuscated-module/pom.xml
+++ b/obfuscated-module/pom.xml
@@ -38,7 +38,7 @@
                             <obfuscate>true</obfuscate>
                             <options>
                                 <option>-dontshrink</option>
-                                <option>-keepattributes Exceptions,Signature,InnerClasses,SourceFile,*Annotation*
+                                <option>-keepattributes Exceptions,Signature,InnerClasses,SourceFile,*Annotation*,EnclosingMethod
                                 </option>
                                 <option>-keepclassmembers,allowoptimization enum * {
                                     public static **[] values();

Kind regards,
Amy

1 Like

Thanks Amy, that’s done the trick.

I wonder why this is not included in the minimum recommendations for a library,

For example, you should at least keep the Exceptions , InnerClasses , and Signature attributes when processing a library

(although I see now it is in the library example)

Thanks again!

1 Like

Yeah, the details are a bit hidden sometimes.
Sure thing, glad we could help!

ltp|16705957144282371