Skip to content

Commit 1c32978

Browse files
committed
Rebase
1 parent 03e7b0b commit 1c32978

File tree

8 files changed

+167
-45
lines changed

8 files changed

+167
-45
lines changed

hibernate-core/src/main/java/org/hibernate/boot/model/internal/AnnotatedColumn.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343

4444
import org.jboss.logging.Logger;
4545

46-
import static org.hibernate.boot.model.internal.BinderHelper.getOverridableAnnotation;
46+
import static org.hibernate.boot.model.internal.DialectOverridesAnnotationHelper.getOverridableAnnotation;
4747
import static org.hibernate.boot.model.internal.BinderHelper.getPath;
4848
import static org.hibernate.boot.model.internal.BinderHelper.getRelativePath;
4949
import static org.hibernate.internal.util.StringHelper.isEmpty;

hibernate-core/src/main/java/org/hibernate/boot/model/internal/AnyBinder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
import jakarta.persistence.JoinTable;
2929

3030
import static org.hibernate.boot.model.internal.BinderHelper.getCascadeStrategy;
31-
import static org.hibernate.boot.model.internal.BinderHelper.getOverridableAnnotation;
31+
import static org.hibernate.boot.model.internal.DialectOverridesAnnotationHelper.getOverridableAnnotation;
3232
import static org.hibernate.boot.model.internal.BinderHelper.getPath;
3333

3434
public class AnyBinder {

hibernate-core/src/main/java/org/hibernate/boot/model/internal/BinderHelper.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import org.hibernate.AnnotationException;
2222
import org.hibernate.AssertionFailure;
2323
import org.hibernate.FetchMode;
24-
import org.hibernate.HibernateException;
2524
import org.hibernate.MappingException;
2625
import org.hibernate.annotations.AnyDiscriminatorValue;
2726
import org.hibernate.annotations.AnyDiscriminatorValues;

hibernate-core/src/main/java/org/hibernate/boot/model/internal/CollectionBinder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@
166166
import static org.hibernate.boot.model.internal.BinderHelper.extractFromPackage;
167167
import static org.hibernate.boot.model.internal.BinderHelper.getCascadeStrategy;
168168
import static org.hibernate.boot.model.internal.BinderHelper.getFetchMode;
169-
import static org.hibernate.boot.model.internal.BinderHelper.getOverridableAnnotation;
169+
import static org.hibernate.boot.model.internal.DialectOverridesAnnotationHelper.getOverridableAnnotation;
170170
import static org.hibernate.boot.model.internal.BinderHelper.getPath;
171171
import static org.hibernate.boot.model.internal.BinderHelper.isDefault;
172172
import static org.hibernate.boot.model.internal.BinderHelper.isPrimitive;

hibernate-core/src/main/java/org/hibernate/boot/model/internal/ColumnsBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
import static org.hibernate.boot.model.internal.AnnotatedColumn.buildColumnFromNoAnnotation;
4545
import static org.hibernate.boot.model.internal.AnnotatedColumn.buildColumnsFromAnnotations;
4646
import static org.hibernate.boot.model.internal.AnnotatedColumn.buildFormulaFromAnnotation;
47-
import static org.hibernate.boot.model.internal.BinderHelper.getOverridableAnnotation;
47+
import static org.hibernate.boot.model.internal.DialectOverridesAnnotationHelper.getOverridableAnnotation;
4848
import static org.hibernate.boot.model.internal.BinderHelper.getPath;
4949
import static org.hibernate.boot.model.internal.BinderHelper.getPropertyOverriddenByMapperOrMapsId;
5050
import static org.hibernate.boot.models.internal.AnnotationUsageHelper.applyAttributeIfSpecified;
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
5+
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html.
6+
*/
7+
package org.hibernate.boot.model.internal;
8+
9+
import java.lang.annotation.Annotation;
10+
import java.util.HashMap;
11+
import java.util.List;
12+
import java.util.Locale;
13+
import java.util.Map;
14+
15+
import org.hibernate.HibernateException;
16+
import org.hibernate.annotations.DialectOverride;
17+
import org.hibernate.boot.spi.MetadataBuildingContext;
18+
import org.hibernate.dialect.DatabaseVersion;
19+
import org.hibernate.dialect.Dialect;
20+
import org.hibernate.models.spi.AnnotationTarget;
21+
import org.hibernate.models.spi.AnnotationUsage;
22+
import org.hibernate.models.spi.ClassDetails;
23+
24+
/**
25+
* @author Steve Ebersole
26+
*/
27+
public class DialectOverridesAnnotationHelper {
28+
private static Map<Class<? extends Annotation>, Class<? extends Annotation>> OVERRIDE_MAP = buildOverrideMap();
29+
30+
private static Map<Class<? extends Annotation>, Class<? extends Annotation>> buildOverrideMap() {
31+
// not accessed concurrently
32+
final Map<Class<? extends Annotation>, Class<? extends Annotation>> results = new HashMap<>();
33+
34+
final Class<?>[] dialectOverrideMembers = DialectOverride.class.getNestMembers();
35+
for ( Class<?> dialectOverrideMember : dialectOverrideMembers ) {
36+
if ( !dialectOverrideMember.isAnnotation() ) {
37+
continue;
38+
}
39+
40+
final DialectOverride.OverridesAnnotation overrideAnnotation = dialectOverrideMember.getAnnotation( DialectOverride.OverridesAnnotation.class );
41+
if ( overrideAnnotation == null ) {
42+
continue;
43+
}
44+
45+
// The "real" annotation. e.g. `org.hibernate.annotations.Formula`
46+
final Class<? extends Annotation> baseAnnotation = overrideAnnotation.value();
47+
48+
// the "override" annotation. e.g. `org.hibernate.annotations.DialectOverride.Formula`
49+
//noinspection unchecked
50+
final Class<? extends Annotation> dialectOverrideAnnotation = (Class<? extends Annotation>) dialectOverrideMember;
51+
52+
results.put( baseAnnotation, dialectOverrideAnnotation );
53+
}
54+
55+
return results;
56+
}
57+
58+
public static <A extends Annotation, O extends Annotation> Class<O> getOverrideAnnotation(Class<A> annotationType) {
59+
final Class<O> overrideAnnotation = findOverrideAnnotation( annotationType );
60+
if ( overrideAnnotation != null ) {
61+
return overrideAnnotation;
62+
}
63+
throw new HibernateException(
64+
String.format(
65+
Locale.ROOT,
66+
"Specified Annotation type (%s) does not have an override form",
67+
annotationType.getName()
68+
)
69+
);
70+
}
71+
72+
public static <A extends Annotation, O extends Annotation> Class<O> findOverrideAnnotation(Class<A> annotationType) {
73+
//noinspection unchecked
74+
return (Class<O>) OVERRIDE_MAP.get( annotationType );
75+
}
76+
77+
public static <T extends Annotation> AnnotationUsage<T> getOverridableAnnotation(
78+
AnnotationTarget element,
79+
Class<T> annotationType,
80+
MetadataBuildingContext context) {
81+
final Class<? extends Annotation> overrideAnnotation = OVERRIDE_MAP.get( annotationType );
82+
if ( overrideAnnotation != null ) {
83+
// the requested annotation does have a DialectOverride variant - look for matching one of those...
84+
final Dialect dialect = context.getMetadataCollector().getDatabase().getDialect();
85+
final DatabaseVersion version = dialect.getVersion();
86+
87+
final List<? extends AnnotationUsage<? extends Annotation>> overrides = element.getRepeatedAnnotationUsages( overrideAnnotation );
88+
for ( AnnotationUsage<? extends Annotation> override : overrides ) {
89+
if ( overrideMatchesDialect( override, dialect ) ) {
90+
// we found an override match...
91+
// the override's `override` attribute is the thing to return
92+
return override.getNestedUsage( "override" );
93+
}
94+
}
95+
}
96+
97+
// no override was found. return the base annotation (if one)
98+
return element.getSingleAnnotationUsage( annotationType );
99+
}
100+
101+
public static boolean overrideMatchesDialect(AnnotationUsage<? extends Annotation> override, Dialect dialect) {
102+
final ClassDetails overrideDialect = override.getClassDetails( "dialect" );
103+
final Class<? extends Dialect> overrideDialectJavaType = overrideDialect.toJavaClass();
104+
if ( !overrideDialectJavaType.isAssignableFrom( dialect.getClass() ) ) {
105+
return false;
106+
}
107+
108+
final AnnotationUsage<DialectOverride.Version> beforeAnn = override.getNestedUsage( "before" );
109+
final AnnotationUsage<DialectOverride.Version> sameOrAfterAnn = override.getNestedUsage( "sameOrAfter" );
110+
final DatabaseVersion version = dialect.getVersion();
111+
112+
if ( version.isBefore( beforeAnn.getInteger( "major" ), beforeAnn.getInteger( "minor" ) )
113+
&& version.isSameOrAfter( sameOrAfterAnn.getInteger( "major" ), sameOrAfterAnn.getInteger( "minor" ) ) ) {
114+
return true;
115+
}
116+
117+
return false;
118+
}
119+
}

hibernate-core/src/main/java/org/hibernate/boot/model/internal/EntityBinder.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,13 @@
144144
import static org.hibernate.boot.model.internal.AnnotatedJoinColumn.buildInheritanceJoinColumn;
145145
import static org.hibernate.boot.model.internal.BinderHelper.extractFromPackage;
146146
import static org.hibernate.boot.model.internal.BinderHelper.getMappedSuperclassOrNull;
147-
import static org.hibernate.boot.model.internal.DialectOverridesAnnotationHelper.getOverridableAnnotation;
148-
import static org.hibernate.boot.model.internal.BinderHelper.getOverrideAnnotation;
149147
import static org.hibernate.boot.model.internal.BinderHelper.hasToOneAnnotation;
150148
import static org.hibernate.boot.model.internal.BinderHelper.noConstraint;
151-
import static org.hibernate.boot.model.internal.BinderHelper.overrideMatchesDialect;
152149
import static org.hibernate.boot.model.internal.BinderHelper.toAliasEntityMap;
153150
import static org.hibernate.boot.model.internal.BinderHelper.toAliasTableMap;
151+
import static org.hibernate.boot.model.internal.DialectOverridesAnnotationHelper.getOverridableAnnotation;
152+
import static org.hibernate.boot.model.internal.DialectOverridesAnnotationHelper.getOverrideAnnotation;
153+
import static org.hibernate.boot.model.internal.DialectOverridesAnnotationHelper.overrideMatchesDialect;
154154
import static org.hibernate.boot.model.internal.EmbeddableBinder.fillEmbeddable;
155155
import static org.hibernate.boot.model.internal.GeneratorBinder.makeIdGenerator;
156156
import static org.hibernate.boot.model.internal.InheritanceState.getInheritanceStateOfSuperEntity;
@@ -1363,7 +1363,7 @@ public void bindEntity() {
13631363
persistentClass.setCached( isCached );
13641364
persistentClass.setLazy( lazy );
13651365
persistentClass.setQueryCacheLayout( queryCacheLayout );
1366-
if ( proxyClass != null ) {
1366+
if ( proxyClass != null && proxyClass != ClassDetails.VOID_CLASS_DETAILS ) {
13671367
persistentClass.setProxyInterfaceName( proxyClass.getName() );
13681368
}
13691369

@@ -1636,7 +1636,7 @@ public void bindProxy() {
16361636
final AnnotationUsage<Proxy> proxy = annotatedClass.getAnnotationUsage( Proxy.class );
16371637
if ( proxy != null ) {
16381638
lazy = proxy.getBoolean( "lazy" );
1639-
proxyClass = lazy ? proxy.getClassDetails( "proxyClass" ) : null;
1639+
proxyClass = lazy ? resolveProxyClass( proxy, annotatedClass ) : null;
16401640
}
16411641
else {
16421642
//needed to allow association lazy loading.
@@ -1645,6 +1645,14 @@ public void bindProxy() {
16451645
}
16461646
}
16471647

1648+
private static ClassDetails resolveProxyClass(AnnotationUsage<Proxy> proxy, ClassDetails annotatedClass) {
1649+
final ClassDetails proxyClass = proxy.getClassDetails( "proxyClass" );
1650+
if ( proxyClass == ClassDetails.VOID_CLASS_DETAILS ) {
1651+
return annotatedClass;
1652+
}
1653+
return proxyClass;
1654+
}
1655+
16481656
public void bindConcreteProxy() {
16491657
final AnnotationUsage<ConcreteProxy> annotationUsage = annotatedClass.getAnnotationUsage( ConcreteProxy.class );
16501658
if ( annotationUsage != null ) {

hibernate-core/src/main/java/org/hibernate/bytecode/internal/bytebuddy/ByteBuddyState.java

Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,6 @@
66
*/
77
package org.hibernate.bytecode.internal.bytebuddy;
88

9-
import static net.bytebuddy.matcher.ElementMatchers.isDeclaredBy;
10-
import static net.bytebuddy.matcher.ElementMatchers.isFinalizer;
11-
import static net.bytebuddy.matcher.ElementMatchers.isSynthetic;
12-
import static net.bytebuddy.matcher.ElementMatchers.isVirtual;
13-
import static net.bytebuddy.matcher.ElementMatchers.nameStartsWith;
14-
import static net.bytebuddy.matcher.ElementMatchers.named;
15-
import static net.bytebuddy.matcher.ElementMatchers.not;
16-
import static net.bytebuddy.matcher.ElementMatchers.returns;
17-
import static net.bytebuddy.matcher.ElementMatchers.takesNoArguments;
18-
import static org.hibernate.internal.CoreLogging.messageLogger;
19-
209
import java.io.File;
2110
import java.io.IOException;
2211
import java.lang.invoke.MethodHandles;
@@ -48,6 +37,17 @@
4837
import net.bytebuddy.matcher.ElementMatcher;
4938
import net.bytebuddy.pool.TypePool;
5039

40+
import static net.bytebuddy.matcher.ElementMatchers.isDeclaredBy;
41+
import static net.bytebuddy.matcher.ElementMatchers.isFinalizer;
42+
import static net.bytebuddy.matcher.ElementMatchers.isSynthetic;
43+
import static net.bytebuddy.matcher.ElementMatchers.isVirtual;
44+
import static net.bytebuddy.matcher.ElementMatchers.nameStartsWith;
45+
import static net.bytebuddy.matcher.ElementMatchers.named;
46+
import static net.bytebuddy.matcher.ElementMatchers.not;
47+
import static net.bytebuddy.matcher.ElementMatchers.returns;
48+
import static net.bytebuddy.matcher.ElementMatchers.takesNoArguments;
49+
import static org.hibernate.internal.CoreLogging.messageLogger;
50+
5151
/**
5252
* A utility to hold all ByteBuddy related state, as in the current version of
5353
* Hibernate the Bytecode Provider state is held in a static field, yet ByteBuddy
@@ -86,6 +86,7 @@ public ByteBuddyState() {
8686
this.byteBuddy = new ByteBuddy( classFileVersion ).with( TypeValidation.DISABLED );
8787
this.proxyCache = new TypeCache( TypeCache.Sort.WEAK );
8888
this.basicProxyCache = new TypeCache( TypeCache.Sort.WEAK );
89+
this.classRewriter = new StandardClassRewriter();
8990
}
9091

9192
/**
@@ -232,30 +233,6 @@ public EnhancerImplConstants getEnhancerConstants() {
232233
return this.enhancerConstants;
233234
}
234235

235-
private static class GetDeclaredMethodAction implements PrivilegedAction<Method> {
236-
private final Class<?> clazz;
237-
private final String methodName;
238-
private final Class<?>[] parameterTypes;
239-
240-
private GetDeclaredMethodAction(Class<?> clazz, String methodName, Class<?>... parameterTypes) {
241-
this.clazz = clazz;
242-
this.methodName = methodName;
243-
this.parameterTypes = parameterTypes;
244-
}
245-
246-
@Override
247-
public Method run() {
248-
try {
249-
Method method = clazz.getDeclaredMethod( methodName, parameterTypes );
250-
251-
return method;
252-
}
253-
catch (NoSuchMethodException e) {
254-
throw new HibernateException( "Unable to prepare getDeclaredMethod()/getMethod() substitution", e );
255-
}
256-
}
257-
}
258-
259236
/**
260237
* Shared proxy definition helpers. They are immutable so we can safely share them.
261238
*/
@@ -320,6 +297,25 @@ public DynamicType.Builder<?> appendIgnoreAlsoAtEnd(DynamicType.Builder<?> build
320297
}
321298
}
322299

300+
private interface ClassRewriter {
301+
DynamicType.Builder<?> installReflectionMethodVisitors(DynamicType.Builder<?> builder);
302+
303+
void registerAuthorizedClass(Unloaded<?> unloadedClass);
304+
}
305+
306+
private static class StandardClassRewriter implements ClassRewriter {
307+
@Override
308+
public DynamicType.Builder<?> installReflectionMethodVisitors(DynamicType.Builder<?> builder) {
309+
// do nothing
310+
return builder;
311+
}
312+
313+
@Override
314+
public void registerAuthorizedClass(Unloaded<?> unloadedClass) {
315+
// do nothing
316+
}
317+
}
318+
323319
private static ClassLoadingStrategy<ClassLoader> resolveClassLoadingStrategy(Class<?> originalClass) {
324320
try {
325321
return ClassLoadingStrategy.UsingLookup.of( MethodHandles.privateLookupIn( originalClass, LOOKUP ) );

0 commit comments

Comments
 (0)