Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport HHH-14828 to branch 5.4 - Bytecode enhancement generates invalid bytecode for final fields #4237

Merged
merged 3 commits into from
Oct 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ public void visitFieldInsn(int opcode, String owner, String name, String desc) {
);
return;
case Opcodes.PUTFIELD:
if ( field.getFieldDescription().isFinal() ) {
// Final fields will only be written to from the constructor,
// so there's no point trying to replace final field writes with a method call.
break;
}
methodVisitor.visitMethodInsn(
Opcodes.INVOKEVIRTUAL,
owner,
Expand All @@ -103,9 +108,7 @@ public void visitFieldInsn(int opcode, String owner, String name, String desc) {
throw new EnhancementException( "Unexpected opcode: " + opcode );
}
}
else {
super.visitFieldInsn( opcode, owner, name, desc );
}
super.visitFieldInsn( opcode, owner, name, desc );
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/
package org.hibernate.bytecode.enhance.internal.bytebuddy;

import static net.bytebuddy.matcher.ElementMatchers.anyOf;
import static net.bytebuddy.matcher.ElementMatchers.nameStartsWith;
import static net.bytebuddy.matcher.ElementMatchers.not;

Expand All @@ -15,10 +16,8 @@
import java.util.Collections;
import java.util.List;
import java.util.Objects;

import javax.persistence.Embedded;

import net.bytebuddy.utility.OpenedClassReader;
import org.hibernate.bytecode.enhance.internal.bytebuddy.EnhancerImpl.AnnotatedFieldDescription;
import org.hibernate.bytecode.enhance.spi.EnhancerConstants;
import org.hibernate.engine.spi.CompositeOwner;
Expand All @@ -27,8 +26,10 @@

import net.bytebuddy.asm.Advice;
import net.bytebuddy.asm.AsmVisitorWrapper;
import net.bytebuddy.asm.ModifierAdjustment;
import net.bytebuddy.description.field.FieldDescription;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.description.modifier.ModifierContributor;
import net.bytebuddy.description.modifier.Visibility;
import net.bytebuddy.description.type.TypeDefinition;
import net.bytebuddy.description.type.TypeDescription;
Expand All @@ -42,12 +43,29 @@
import net.bytebuddy.jar.asm.Type;
import net.bytebuddy.matcher.ElementMatcher.Junction;
import net.bytebuddy.pool.TypePool;
import net.bytebuddy.utility.OpenedClassReader;

final class PersistentAttributeTransformer implements AsmVisitorWrapper.ForDeclaredMethods.MethodVisitorWrapper {

private static final CoreMessageLogger log = CoreLogging.messageLogger( PersistentAttributeTransformer.class );

private static final Junction<MethodDescription> NOT_HIBERNATE_GENERATED = not( nameStartsWith( "$$_hibernate_" ) );
private static final ModifierContributor.ForField REMOVE_FINAL_MODIFIER = new ModifierContributor.ForField() {
@Override
public int getMask() {
return EMPTY_MASK; // Do not add any modifier
}

@Override
public int getRange() {
return Opcodes.ACC_FINAL; // Remove the "final" modifier
}

@Override
public boolean isDefault() {
return false;
}
};

private final TypeDescription managedCtClass;

Expand Down Expand Up @@ -138,7 +156,8 @@ public MethodVisitor wrap(
return new MethodVisitor( OpenedClassReader.ASM_API, methodVisitor ) {
@Override
public void visitFieldInsn(int opcode, String owner, String name, String desc) {
if ( isEnhanced( owner, name, desc ) ) {
AnnotatedFieldDescription enhancedField = getEnhancedField( owner, name, desc );
if ( enhancedField != null ) {
switch ( opcode ) {
case Opcodes.GETFIELD:
methodVisitor.visitMethodInsn(
Expand All @@ -150,6 +169,11 @@ public void visitFieldInsn(int opcode, String owner, String name, String desc) {
);
return;
case Opcodes.PUTFIELD:
if ( enhancedField.getFieldDescription().isFinal() ) {
// Final fields will only be written to from the constructor,
// so there's no point trying to replace final field writes with a method call.
break;
}
methodVisitor.visitMethodInsn(
Opcodes.INVOKEVIRTUAL,
owner,
Expand All @@ -165,37 +189,51 @@ public void visitFieldInsn(int opcode, String owner, String name, String desc) {
};
}

private boolean isEnhanced(String owner, String name, String desc) {
private AnnotatedFieldDescription getEnhancedField(String owner, String name, String desc) {
for ( AnnotatedFieldDescription enhancedField : enhancedFields ) {
if ( enhancedField.getName().equals( name )
&& enhancedField.getDescriptor().equals( desc )
&& enhancedField.getDeclaringType().asErasure().getInternalName().equals( owner ) ) {
return true;
return enhancedField;
}
}
return false;
return null;
}

DynamicType.Builder<?> applyTo(DynamicType.Builder<?> builder) {
boolean compositeOwner = false;

builder = builder.visit( new AsmVisitorWrapper.ForDeclaredMethods().invokable( NOT_HIBERNATE_GENERATED, this ) );
// Remove the final modifier from all enhanced fields, because:
// 1. We sometimes need to write to final fields when they are lazy.
// 2. Those fields are already written to by Hibernate ORM through reflection anyway.
// 3. The compiler already makes sure that final fields are not written to from the user's source code.
List<FieldDescription.InDefinedShape> enhancedFieldsAsDefined = new ArrayList<>();
for ( AnnotatedFieldDescription f : enhancedFields ) {
enhancedFieldsAsDefined.add( f.asDefined() );
}
builder = builder.visit( new ModifierAdjustment().withFieldModifiers( anyOf( enhancedFieldsAsDefined ),
REMOVE_FINAL_MODIFIER ) );
for ( AnnotatedFieldDescription enhancedField : enhancedFields ) {
builder = builder
.defineMethod(
EnhancerConstants.PERSISTENT_FIELD_READER_PREFIX + enhancedField.getName(),
enhancedField.getType().asErasure(),
Visibility.PUBLIC
)
.intercept( fieldReader( enhancedField )
)
.defineMethod(
EnhancerConstants.PERSISTENT_FIELD_WRITER_PREFIX + enhancedField.getName(),
TypeDescription.VOID,
Visibility.PUBLIC
)
.withParameters( enhancedField.getType().asErasure() )
.intercept( fieldWriter( enhancedField ) );
.intercept( fieldReader( enhancedField ) );
// Final fields will only be written to from the constructor,
// so there's no point trying to replace final field writes with a method call.
if ( !enhancedField.getFieldDescription().isFinal() ) {
builder = builder
.defineMethod(
EnhancerConstants.PERSISTENT_FIELD_WRITER_PREFIX + enhancedField.getName(),
TypeDescription.VOID,
Visibility.PUBLIC
)
.withParameters( enhancedField.getType().asErasure() )
.intercept( fieldWriter( enhancedField ) );
}

if ( !compositeOwner
&& !enhancementContext.isMappedSuperclassClass( managedCtClass )
Expand Down
Loading