raphw / byte-buddy

Runtime code generation for the Java virtual machine.
https://bytebuddy.net
Apache License 2.0
6.29k stars 807 forks source link

Compilation error after adding lombok to classpath #1594

Closed omidp closed 8 months ago

omidp commented 9 months ago

Hi,

I am not sure whether this is ByteBuddy or Lombok bug. I am trying to change javax.persistence to jakarta.persistence packages inside a legacy jar file with bytebuddy.

new ByteBuddy().redefine(clz, classFileLocator)
            .visit(new AsmVisitorWrapper() {
                @Override
                public int mergeWriter(int flags) {
                    return 0;
                }

                @Override
                public int mergeReader(int flags) {
                    return 0;
                }

                @Override
                public ClassVisitor wrap(TypeDescription instrumentedType, ClassVisitor classVisitor, Implementation.Context implementationContext, TypePool typePool, FieldList<FieldDescription.InDefinedShape> fields, MethodList<?> methods, int writerFlags, int readerFlags) {
                    return new ClassRemapper(classVisitor, new Remapper() {
                        @Override
                        public String map(String typeName) {
                            for (Map.Entry<String, String> javaxAnnotation : javaxAnnotations.entrySet()) {
                                String index = javaxAnnotation.getKey();
                                if (typeToName(typeName).startsWith(index)) {
                                    //change javax.persistence.* to jakarta.persistence.*
                                    return javaxAnnotation.getValue() + typeToName(typeName).substring(index.length());
                                }
                            }
                            return typeName;
                        }
                    });
                }
            }).make().getBytes();

It is successfully converting the packages, however, when I add lombok to my project classPath I am getting this compilation error.

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.11.0:compile (default-compile) on project domain-system: Compilation failure
[ERROR] cannot access jakarta$persistence$MappedSuperclass
[ERROR]   class file for jakarta$persistence$MappedSuperclass not found

Is ByteBuddy lombok-friendly and good choice for solving this problem ?

PS: I have all required dependencies in the classpath.

raphw commented 9 months ago

First, use AsmVisitorWrapper.AbstractBase or retain the modifiers. I do not think you want to reset them. The error happens during compilation in the annotation processor. This is before Byte Buddy is active, so I would not know how this is related to the library.