Skip to content

Commit 26eed58

Browse files
committed
Various code cleanup
1 parent 3b00822 commit 26eed58

File tree

6 files changed

+125
-72
lines changed

6 files changed

+125
-72
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* SPDX-License-Identifier: LGPL-2.1-or-later
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.bytecode.internal.bytebuddy;
6+
7+
import jakarta.persistence.Id;
8+
import jakarta.persistence.MappedSuperclass;
9+
10+
@MappedSuperclass
11+
public abstract class BaseMappedSuperclass {
12+
@Id
13+
protected Long id;
14+
protected String value;
15+
16+
public Long getId() {
17+
return id;
18+
}
19+
20+
public void setId(Long id) {
21+
this.id = id;
22+
}
23+
24+
public String getValue() {
25+
return value;
26+
}
27+
28+
public void setValue(String value) {
29+
this.value = value;
30+
}
31+
}

hibernate-core/src/test/java/org/hibernate/bytecode/internal/bytebuddy/GenerateProxiesTest.java

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,11 @@
44
*/
55
package org.hibernate.bytecode.internal.bytebuddy;
66

7-
import static org.junit.Assert.assertEquals;
87
import static org.junit.Assert.assertNotNull;
98

10-
import java.io.IOException;
119
import java.lang.reflect.InvocationTargetException;
1210

13-
import java.util.LinkedHashMap;
14-
import java.util.Map;
15-
import org.hibernate.bytecode.enhance.internal.bytebuddy.EnhancerImpl;
16-
import org.hibernate.bytecode.enhance.spi.DefaultEnhancementContext;
17-
import org.hibernate.bytecode.enhance.spi.EnhancementException;
18-
import org.hibernate.bytecode.enhance.spi.Enhancer;
19-
import org.hibernate.bytecode.spi.ByteCodeHelper;
20-
import org.hibernate.bytecode.spi.ReflectionOptimizer;
21-
import org.hibernate.property.access.internal.PropertyAccessStrategyFieldImpl;
22-
import org.hibernate.property.access.spi.PropertyAccess;
2311
import org.hibernate.proxy.pojo.bytebuddy.ByteBuddyProxyHelper;
24-
import org.hibernate.testing.orm.junit.JiraKey;
2512
import org.junit.Test;
2613

2714
public class GenerateProxiesTest {
@@ -41,45 +28,4 @@ public void generateProxy() throws InstantiationException, IllegalAccessExceptio
4128
assertNotNull( proxyClass );
4229
assertNotNull( proxyClass.getConstructor().newInstance() );
4330
}
44-
45-
@Test
46-
public void generateFastClassAndReflectionOptimizer() {
47-
BytecodeProviderImpl bytecodeProvider = new BytecodeProviderImpl();
48-
ReflectionOptimizer reflectionOptimizer = bytecodeProvider.getReflectionOptimizer( SimpleEntity.class,
49-
new String[]{ "getId", "getName" }, new String[]{ "setId", "setName" },
50-
new Class<?>[]{ Long.class, String.class } );
51-
assertEquals( 2, reflectionOptimizer.getAccessOptimizer().getPropertyNames().length );
52-
assertNotNull( reflectionOptimizer.getInstantiationOptimizer().newInstance() );
53-
}
54-
55-
@Test
56-
@JiraKey("HHH-16772")
57-
public void generateFastMappedSuperclassAndReflectionOptimizer() {
58-
BytecodeProviderImpl bytecodeProvider = new BytecodeProviderImpl();
59-
final Map<String, PropertyAccess> propertyAccessMap = new LinkedHashMap<>();
60-
61-
final PropertyAccessStrategyFieldImpl propertyAccessStrategy = new PropertyAccessStrategyFieldImpl();
62-
63-
propertyAccessMap.put(
64-
"timestamp",
65-
propertyAccessStrategy.buildPropertyAccess(MappedSuperclassEntity.class, "value", true )
66-
);
67-
68-
ReflectionOptimizer reflectionOptimizer = bytecodeProvider.getReflectionOptimizer(
69-
MappedSuperclassEntity.class,
70-
propertyAccessMap
71-
);
72-
73-
assertNotNull(reflectionOptimizer);
74-
assertEquals( 1, reflectionOptimizer.getAccessOptimizer().getPropertyNames().length );
75-
assertNotNull( reflectionOptimizer.getInstantiationOptimizer().newInstance() );
76-
}
77-
78-
@Test
79-
public void generateEnhancedClass() throws EnhancementException, IOException {
80-
Enhancer enhancer = new EnhancerImpl( new DefaultEnhancementContext(), new ByteBuddyState() );
81-
enhancer.enhance( SimpleEntity.class.getName(),
82-
ByteCodeHelper.readByteCode( SimpleEntity.class.getClassLoader()
83-
.getResourceAsStream( SimpleEntity.class.getName().replace( '.', '/' ) + ".class" ) ) );
84-
}
8531
}

hibernate-core/src/test/java/org/hibernate/bytecode/internal/bytebuddy/MappedSuperclassEntity.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
package org.hibernate.bytecode.internal.bytebuddy;
66

77
import jakarta.persistence.Entity;
8-
import org.hibernate.bytecode.internal.bytebuddy.mappedsuperclass.BaseMappedSuperclass;
98

109
@Entity
1110
public class MappedSuperclassEntity extends BaseMappedSuperclass {
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* SPDX-License-Identifier: LGPL-2.1-or-later
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.bytecode.internal.bytebuddy;
6+
7+
import org.hibernate.bytecode.spi.ReflectionOptimizer;
8+
import org.hibernate.testing.orm.junit.JiraKey;
9+
import org.junit.Test;
10+
11+
import static org.assertj.core.api.Assertions.assertThat;
12+
import static org.junit.Assert.assertNotNull;
13+
14+
/**
15+
* @author Steve Ebersole
16+
*/
17+
public class ReflectionOptimizerTests {
18+
19+
@Test
20+
public void generateFastClassAndReflectionOptimizer() {
21+
BytecodeProviderImpl bytecodeProvider = new BytecodeProviderImpl();
22+
ReflectionOptimizer reflectionOptimizer = bytecodeProvider.getReflectionOptimizer( SimpleEntity.class,
23+
new String[]{ "getId", "getName" }, new String[]{ "setId", "setName" },
24+
new Class<?>[]{ Long.class, String.class } );
25+
assertThat( reflectionOptimizer ).isNotNull();
26+
27+
final ReflectionOptimizer.AccessOptimizer accessOptimizer = reflectionOptimizer.getAccessOptimizer();
28+
assertThat( accessOptimizer.getPropertyNames() ).hasSize( 2 );
29+
30+
final Object instance = reflectionOptimizer.getInstantiationOptimizer().newInstance();
31+
assertNotNull( instance );
32+
33+
final Object[] initialValues = accessOptimizer.getPropertyValues( instance );
34+
assertThat( initialValues ).containsExactly( null, null );
35+
36+
accessOptimizer.setPropertyValues( instance, new Object[] { 1L, "a name" } );
37+
38+
final Object[] injectedValues = accessOptimizer.getPropertyValues( instance );
39+
assertThat( injectedValues ).containsExactly( 1L, "a name" );
40+
}
41+
42+
@Test
43+
@JiraKey("HHH-16772")
44+
public void generateFastMappedSuperclassAndReflectionOptimizer() {
45+
BytecodeProviderImpl bytecodeProvider = new BytecodeProviderImpl();
46+
ReflectionOptimizer reflectionOptimizer = bytecodeProvider.getReflectionOptimizer(
47+
MappedSuperclassEntity.class,
48+
new String[]{ "getId", "getValue" }, new String[]{ "setId", "setValue" },
49+
new Class<?>[]{ Long.class, String.class }
50+
);
51+
assertThat( reflectionOptimizer ).isNotNull();
52+
53+
final ReflectionOptimizer.AccessOptimizer accessOptimizer = reflectionOptimizer.getAccessOptimizer();
54+
assertThat( accessOptimizer.getPropertyNames() ).hasSize( 2 );
55+
56+
final Object instance = reflectionOptimizer.getInstantiationOptimizer().newInstance();
57+
assertNotNull( instance );
58+
59+
final Object[] initialValues = accessOptimizer.getPropertyValues( instance );
60+
assertThat( initialValues ).containsExactly( null, null );
61+
62+
accessOptimizer.setPropertyValues( instance, new Object[] { 1L, "a value" } );
63+
64+
final Object[] injectedValues = accessOptimizer.getPropertyValues( instance );
65+
assertThat( injectedValues ).containsExactly( 1L, "a value" );
66+
}
67+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* SPDX-License-Identifier: LGPL-2.1-or-later
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.bytecode.internal.bytebuddy;
6+
7+
import org.hibernate.bytecode.enhance.internal.bytebuddy.EnhancerImpl;
8+
import org.hibernate.bytecode.enhance.spi.DefaultEnhancementContext;
9+
import org.hibernate.bytecode.enhance.spi.EnhancementException;
10+
import org.hibernate.bytecode.enhance.spi.Enhancer;
11+
import org.hibernate.bytecode.spi.ByteCodeHelper;
12+
import org.junit.Test;
13+
14+
import java.io.IOException;
15+
16+
/**
17+
* @author Steve Ebersole
18+
*/
19+
public class SimpleEnhancerTests {
20+
@Test
21+
public void generateEnhancedClass() throws EnhancementException, IOException {
22+
Enhancer enhancer = new EnhancerImpl( new DefaultEnhancementContext(), new ByteBuddyState() );
23+
enhancer.enhance( SimpleEntity.class.getName(),
24+
ByteCodeHelper.readByteCode( SimpleEntity.class.getClassLoader()
25+
.getResourceAsStream( SimpleEntity.class.getName().replace( '.', '/' ) + ".class" ) ) );
26+
}
27+
}

hibernate-core/src/test/java/org/hibernate/bytecode/internal/bytebuddy/mappedsuperclass/BaseMappedSuperclass.java

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)