shadow icon indicating copy to clipboard operation
shadow copied to clipboard

Shadow with AspectJ aspects error

Open sergeykad opened this issue 9 years ago • 5 comments

Shadow Version 1.2.3

Gradle Version 2.14

Expected Behavior Aspects inside shadow jar work correctly

Actual Behavior Aspects fail to load

Tried several Java versions from 6 to 8. AspectJ 1.8.9

Shadow modifies aspect class files regardless if it is actually required or not. If change was not required it is possible to work around the problem by copying overwriting aspects class files in the fat (shadow) with ones from original (slim) jar.

As I understand it, BCEL cannot read class atributes.

The following exceptions are thrown during startup:

SEVERE: register definition failed
java.lang.RuntimeException: Problem processing attributes in com.my.GreatAspect
        at org.aspectj.weaver.bcel.BcelObjectType.ensureAspectJAttributesUnpacked(BcelObjectType.java:387)
        at org.aspectj.weaver.bcel.BcelObjectType.(BcelObjectType.java:162)
        at org.aspectj.weaver.bcel.BcelWorld.buildBcelDelegate(BcelWorld.java:410)
        at org.aspectj.weaver.bcel.BcelWorld.resolveDelegate(BcelWorld.java:405)
        at org.aspectj.weaver.ltw.LTWWorld.resolveDelegate(LTWWorld.java:107)
        at org.aspectj.weaver.World.resolveToReferenceType(World.java:478)
        at org.aspectj.weaver.World.resolve(World.java:319)
        at org.aspectj.weaver.bcel.BcelWeaver.addLibraryAspect(BcelWeaver.java:159)
        at org.aspectj.weaver.loadtime.ClassLoaderWeavingAdaptor.registerAspects(ClassLoaderWeavingAdaptor.java:485)
        at org.aspectj.weaver.loadtime.ClassLoaderWeavingAdaptor.registerDefinitions(ClassLoaderWeavingAdaptor.java:304)
        at org.aspectj.weaver.loadtime.ClassLoaderWeavingAdaptor.initialize(ClassLoaderWeavingAdaptor.java:171)
        at org.aspectj.weaver.loadtime.Aj$ExplicitlyInitializedClassLoaderWeavingAdaptor.initialize(Aj.java:340)
        at org.aspectj.weaver.loadtime.Aj$ExplicitlyInitializedClassLoaderWeavingAdaptor.getWeavingAdaptor(Aj.java:345)
        at org.aspectj.weaver.loadtime.Aj$WeaverContainer.getWeaver(Aj.java:319)
        at org.aspectj.weaver.loadtime.Aj.preProcess(Aj.java:113)
        at org.aspectj.weaver.loadtime.ClassPreProcessorAgentAdapter.transform(ClassPreProcessorAgentAdapter.java:54)

sergeykad avatar Aug 09 '16 14:08 sergeykad

can you post an example project. I'm not very educated in AspectJ, so I'll need help in this area.

johnrengelman avatar Aug 10 '16 12:08 johnrengelman

I'll try to prepare/find something suitable for the problem demonstration.

The main problem IMHO is that Shadow performs many manipulations to classes that seems to be excessive. I noticed many changes in the class that remained in its original package and do not use any classes that were moved to another package. Such class could be copied to the fat jar as is, but multiple modifications were performed on it.

Additional problem (related to #232) is that names of class attributes are not changed according to the change in package names (although constants used to read these attributes are changed). This includes the following attributes:

  • org.aspectj.weaver.WeaverVersion
  • org.aspectj.weaver.Aspect
  • org.aspectj.weaver.SourceContext
  • org.aspectj.weaver.WeaverState

There is also additional AspectJ specific issue: it checks that first character of attribute name is 'o' (for performance optimization I guess). Therefore changing AspectJ package to something that not starts with 'o' will result in errors unless you either manipulate code in org.aspectj.weaver.bcel.Utility or don't change the attributes listed above and corresponding constants.

sergeykad avatar Aug 10 '16 13:08 sergeykad

A project that demonstrates the problem is attached. I got error uploading ZIP file, so I changed the extension to PDF. aspect-demo.zip.pdf

sergeykad avatar Aug 11 '16 09:08 sergeykad

the easiest thing to do is post this code in a public GitHub repo

johnrengelman avatar Aug 11 '16 13:08 johnrengelman

Here you go https://github.com/sergeykad/AspectJ-Shadow

sergeykad avatar Aug 11 '16 13:08 sergeykad

@sergeykad Can you try with the latest version?

Goooler avatar Jan 24 '25 12:01 Goooler

Wow, I didn't expect it. It's great that the problem is being fixed, but the project I was working on does not exist anymore and I don't remember the details very well. If your fix works on the reproducer I linked above it should be OK.

sergeykad avatar Jan 24 '25 13:01 sergeykad

I'll close this until someone complains again.

Goooler avatar Jan 24 '25 13:01 Goooler