Skip to content

Commit 0c09cf1

Browse files
committed
#53 - Simplified creation and addition of attributes for dynamic models
#54 - Simplified creation and addition of AnnotationUsage references #55 - Clean up unused methods
1 parent db8506f commit 0c09cf1

File tree

9 files changed

+209
-107
lines changed

9 files changed

+209
-107
lines changed

src/main/java/org/hibernate/models/internal/ModifierUtils.java

+17-35
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,22 @@
1010

1111
import org.hibernate.models.spi.MemberDetails;
1212

13-
import static java.lang.reflect.Modifier.ABSTRACT;
14-
1513
/**
1614
* Fills-in non-public aspects of the {@link Modifier} class
1715
*
1816
* @author Steve Ebersole
1917
*/
2018
public class ModifierUtils {
2119

22-
private static final int BRIDGE = 0x00000040;
23-
private static final int SYNTHETIC = 0x00001000;
20+
public static final int BRIDGE = 0x00000040;
21+
public static final int SYNTHETIC = 0x00001000;
22+
23+
public static final int DYNAMIC_ATTRIBUTE_MODIFIERS = ~Modifier.ABSTRACT
24+
& ~BRIDGE
25+
& ~Modifier.FINAL
26+
& ~Modifier.STATIC
27+
& ~SYNTHETIC
28+
& ~Modifier.TRANSIENT;
2429

2530
/**
2631
* Disallow instantiation. This is a utility class, use statically.
@@ -78,7 +83,7 @@ public static boolean isBridge(int modifierFlags) {
7883
}
7984

8085
public static boolean isAbstract(int modifierFlags) {
81-
return (modifierFlags & ABSTRACT) != 0;
86+
return (modifierFlags & Modifier.ABSTRACT) != 0;
8287
}
8388

8489
/**
@@ -91,19 +96,9 @@ public static boolean isAbstract(int modifierFlags) {
9196
* @see MemberDetails#isPersistable()
9297
*/
9398
public static boolean hasPersistableFieldModifiers(int modifierFlags) {
94-
if ( isTransient( modifierFlags ) ) {
95-
return false;
96-
}
97-
98-
if ( ModifierUtils.isSynthetic( modifierFlags ) ) {
99-
return false;
100-
}
101-
102-
if ( ModifierUtils.isStatic( modifierFlags ) ) {
103-
return false;
104-
}
105-
106-
return true;
99+
return !isTransient( modifierFlags )
100+
&& !isSynthetic( modifierFlags )
101+
&& !isStatic( modifierFlags );
107102
}
108103

109104
/**
@@ -116,22 +111,9 @@ public static boolean hasPersistableFieldModifiers(int modifierFlags) {
116111
* @see MemberDetails#isPersistable()
117112
*/
118113
public static boolean hasPersistableMethodModifiers(int modifierFlags) {
119-
if ( ModifierUtils.isStatic( modifierFlags ) ) {
120-
return false;
121-
}
122-
123-
if ( ModifierUtils.isBridge( modifierFlags ) ) {
124-
return false;
125-
}
126-
127-
if ( isTransient( modifierFlags ) ) {
128-
return false;
129-
}
130-
131-
if ( ModifierUtils.isSynthetic( modifierFlags ) ) {
132-
return false;
133-
}
134-
135-
return true;
114+
return !isStatic( modifierFlags )
115+
&& !isBridge( modifierFlags )
116+
&& !isTransient( modifierFlags )
117+
&& !isSynthetic( modifierFlags );
136118
}
137119
}

src/main/java/org/hibernate/models/internal/dynamic/AbstractAnnotationTarget.java

-5
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,6 @@ public void clearAnnotationUsages() {
4040
usageMap.clear();
4141
}
4242

43-
@Override
44-
public <X extends Annotation> void removeAnnotationUsage(Class<X> annotationType) {
45-
usageMap.remove( annotationType );
46-
}
47-
4843
/**
4944
* Applies the given {@code annotationUsage} to this target.
5045
*

src/main/java/org/hibernate/models/internal/dynamic/DynamicClassDetails.java

+49
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
import java.util.ArrayList;
1010
import java.util.Collections;
1111
import java.util.List;
12+
import java.util.function.Consumer;
1213

1314
import org.hibernate.models.internal.ClassDetailsSupport;
15+
import org.hibernate.models.internal.ClassTypeDetailsImpl;
1416
import org.hibernate.models.spi.ClassDetails;
1517
import org.hibernate.models.spi.FieldDetails;
1618
import org.hibernate.models.spi.MethodDetails;
@@ -19,6 +21,7 @@
1921
import org.hibernate.models.spi.TypeDetails;
2022
import org.hibernate.models.spi.TypeVariableDetails;
2123

24+
import static org.hibernate.models.internal.ModifierUtils.DYNAMIC_ATTRIBUTE_MODIFIERS;
2225
import static org.hibernate.models.internal.util.StringHelper.isEmpty;
2326

2427
/**
@@ -166,6 +169,52 @@ public void addMethod(MethodDetails methodDetails) {
166169
this.methods.add( methodDetails );
167170
}
168171

172+
/**
173+
* Creates a field representing an attribute and adds it to this class.
174+
*/
175+
public DynamicFieldDetails applyAttribute(
176+
String name,
177+
ClassDetails type,
178+
boolean isArray,
179+
boolean isPlural,
180+
Consumer<DynamicFieldDetails> configuration,
181+
SourceModelBuildingContext context) {
182+
return applyAttribute(
183+
name,
184+
new ClassTypeDetailsImpl( type, TypeDetails.Kind.CLASS ),
185+
isArray,
186+
isPlural,
187+
configuration,
188+
context
189+
);
190+
}
191+
192+
/**
193+
* Creates a field representing an attribute and adds it to this class
194+
*/
195+
public DynamicFieldDetails applyAttribute(
196+
String name,
197+
TypeDetails type,
198+
boolean isArray,
199+
boolean isPlural,
200+
Consumer<DynamicFieldDetails> configuration,
201+
SourceModelBuildingContext context) {
202+
final DynamicFieldDetails attribute = new DynamicFieldDetails(
203+
name,
204+
type,
205+
this,
206+
DYNAMIC_ATTRIBUTE_MODIFIERS,
207+
isArray,
208+
isPlural,
209+
context
210+
);
211+
if ( configuration != null ) {
212+
configuration.accept( attribute );
213+
}
214+
addField( attribute );
215+
return attribute;
216+
}
217+
169218
@Override
170219
public <X> Class<X> toJavaClass() {
171220
if ( javaType == null ) {

src/main/java/org/hibernate/models/internal/dynamic/DynamicFieldDetails.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
import java.util.Collection;
1111
import java.util.Map;
1212

13-
import org.hibernate.models.spi.MutableMemberDetails;
1413
import org.hibernate.models.spi.ClassDetails;
1514
import org.hibernate.models.spi.FieldDetails;
15+
import org.hibernate.models.spi.MutableMemberDetails;
1616
import org.hibernate.models.spi.SourceModelBuildingContext;
1717
import org.hibernate.models.spi.TypeDetails;
1818
import org.hibernate.models.spi.TypeVariableScope;

src/main/java/org/hibernate/models/internal/jandex/AbstractAnnotationTarget.java

-5
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,6 @@ public void clearAnnotationUsages() {
4444
getUsageMap().clear();
4545
}
4646

47-
@Override
48-
public <X extends Annotation> void removeAnnotationUsage(Class<X> annotationType) {
49-
usageMap.remove( annotationType );
50-
}
51-
5247
@Override
5348
public <X extends Annotation> void addAnnotationUsage(AnnotationUsage<X> annotationUsage) {
5449
getUsageMap().put( annotationUsage.getAnnotationType(), annotationUsage );

src/main/java/org/hibernate/models/internal/jdk/AbstractAnnotationTarget.java

-7
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,6 @@ public void clearAnnotationUsages() {
6363
getUsageMap().clear();
6464
}
6565

66-
@Override
67-
public <X extends Annotation> void removeAnnotationUsage(Class<X> annotationType) {
68-
if ( usagesMap != null ) {
69-
usagesMap.remove( annotationType );
70-
}
71-
}
72-
7366
@Override
7467
public <X extends Annotation> void addAnnotationUsage(AnnotationUsage<X> annotationUsage) {
7568
getUsageMap().put( annotationUsage.getAnnotationType(), annotationUsage );

src/main/java/org/hibernate/models/spi/AnnotationDescriptor.java

+21-11
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,7 @@ default <V> AttributeDescriptor<V> getAttribute(String name) {
9696
* @param context Access to needed services
9797
*/
9898
default MutableAnnotationUsage<A> createUsage(AnnotationTarget target, SourceModelBuildingContext context) {
99-
final DynamicAnnotationUsage<A> usage = new DynamicAnnotationUsage<>( this, target, context );
100-
getAttributes().forEach( (attr) -> {
101-
final Object value = attr.getTypeDescriptor().createValue( attr, target, context );
102-
if ( value != null ) {
103-
usage.setAttributeValue( attr.getName(), value );
104-
}
105-
} );
106-
return usage;
99+
return createUsage( target, null, context );
107100
}
108101

109102
/**
@@ -113,9 +106,26 @@ default MutableAnnotationUsage<A> createUsage(AnnotationTarget target, SourceMod
113106
* @param adjuster Callback to allow adjusting the created usage prior to return.
114107
* @param context Access to needed services
115108
*/
116-
default MutableAnnotationUsage<A> createUsage(AnnotationTarget target, Consumer<MutableAnnotationUsage<A>> adjuster, SourceModelBuildingContext context) {
117-
final MutableAnnotationUsage<A> usage = createUsage( target, context );
118-
adjuster.accept( usage );
109+
default MutableAnnotationUsage<A> createUsage(
110+
AnnotationTarget target,
111+
Consumer<MutableAnnotationUsage<A>> adjuster,
112+
SourceModelBuildingContext context) {
113+
// create the "empty" usage
114+
final DynamicAnnotationUsage<A> usage = new DynamicAnnotationUsage<>( this, target, context );
115+
116+
// apply attribute defaults
117+
getAttributes().forEach( (attr) -> {
118+
final Object value = attr.getTypeDescriptor().createValue( attr, target, context );
119+
if ( value != null ) {
120+
usage.setAttributeValue( attr.getName(), value );
121+
}
122+
} );
123+
124+
// allow configuration
125+
if ( adjuster != null ) {
126+
adjuster.accept( usage );
127+
}
128+
119129
return usage;
120130
}
121131
}

src/main/java/org/hibernate/models/spi/MutableAnnotationTarget.java

+27-5
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,43 @@
77
package org.hibernate.models.spi;
88

99
import java.lang.annotation.Annotation;
10-
import java.util.List;
10+
import java.util.function.Consumer;
1111

1212
/**
1313
* Extension of AnnotationTarget which allows manipulation of the annotations
1414
*
1515
* @author Steve Ebersole
1616
*/
1717
public interface MutableAnnotationTarget extends AnnotationTarget {
18+
/**
19+
* Removes all annotation usages currently associated with this target.
20+
* Useful for complete XML mappings.
21+
*/
1822
void clearAnnotationUsages();
1923

20-
<X extends Annotation> void removeAnnotationUsage(Class<X> annotationType);
21-
24+
/**
25+
* Add an annotation usage to this target
26+
*/
2227
<X extends Annotation> void addAnnotationUsage(AnnotationUsage<X> annotationUsage);
2328

24-
default <X extends Annotation> void addAnnotationUsages(List<AnnotationUsage<X>> annotationUsages) {
25-
annotationUsages.forEach( this::addAnnotationUsage );
29+
/**
30+
* Creates a usage and adds it to this target.
31+
*/
32+
default <A extends Annotation> MutableAnnotationUsage<A> applyAnnotationUsage(
33+
AnnotationDescriptor<A> annotationType,
34+
SourceModelBuildingContext buildingContext) {
35+
return applyAnnotationUsage( annotationType, null, buildingContext );
36+
}
37+
38+
/**
39+
* Creates a usage and adds it to this target, allowing for configuration of the created usage
40+
*/
41+
default <A extends Annotation> MutableAnnotationUsage<A> applyAnnotationUsage(
42+
AnnotationDescriptor<A> annotationType,
43+
Consumer<MutableAnnotationUsage<A>> configuration,
44+
SourceModelBuildingContext buildingContext) {
45+
final MutableAnnotationUsage<A> usage = annotationType.createUsage( this, configuration, buildingContext );
46+
addAnnotationUsage( usage );
47+
return usage;
2648
}
2749
}

0 commit comments

Comments
 (0)