Skip to content

Commit 2255d3a

Browse files
committed
#9 - Add details about "index" and "element"/"component" types to MemberDetails
#21 - Work on generics / parameterized-types
1 parent 29a6dbe commit 2255d3a

File tree

66 files changed

+2200
-497
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+2200
-497
lines changed

README.adoc

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
:fn-managed: footnote:[The application's domain and related classes]
22
== Hibernate models
33

4-
A de-typed abstraction over "reflection" and annotations (and in the case of Hibernate ORM,
5-
mapping XML also).
6-
74
Uses a mix of https://smallrye.io/jandex/[Jandex] and Java reflection to build the de-typed abstraction model of
85
classes and annotations referenced by an application's managed resources{fn-managed}.
96

107
Consumers can then access details from that abstraction model in a unified way, regardless of the underlying
11-
source (Jandex or reflection). For classes which we are able to access from a Jandex index, this has the benefit
12-
that the classes are not loaded into the ClassLoader; which is important because once a classes is loaded into
13-
a ClassLoader, its bytecode cannot be changed and run-time bytecode enhancement is not possible.
8+
source. For classes which we are able to access from a Jandex index, this has the benefit that the classes are
9+
not loaded into the ClassLoader which is important because once a classes is loaded into a ClassLoader, its
10+
bytecode cannot be changed and run-time bytecode enhancement is not possible.
1411

1512
This work is intended to replace the https://github.com/hibernate/hibernate-commons-annotations[`hibernate-commons-annotation`] (HCANN)
1613
library, which suffered from a number of shortcomings.
14+
15+
16+
=== Annotations
17+
18+
Hibernate Models defines an actual descriptor for annotations called `AnnotationDescriptor` which provides access
19+
to information (whether it is repeatable, etc.) about the annotation class. These descriptors are available from the
20+
`AnnotationDescriptorRegistry`.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
import java.util.List;
1313
import java.util.Map;
1414

15-
import org.hibernate.models.internal.util.CollectionHelper;
1615
import org.hibernate.models.AnnotationAccessException;
16+
import org.hibernate.models.internal.util.CollectionHelper;
1717
import org.hibernate.models.spi.AnnotationDescriptor;
1818
import org.hibernate.models.spi.AnnotationUsage;
1919

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
* Copyright: Red Hat Inc. and Hibernate Authors
6+
*/
7+
8+
package org.hibernate.models.internal;
9+
10+
import java.util.Objects;
11+
12+
import org.hibernate.models.internal.util.StringHelper;
13+
import org.hibernate.models.spi.ArrayTypeDetails;
14+
import org.hibernate.models.spi.ClassDetails;
15+
import org.hibernate.models.spi.TypeDetails;
16+
17+
/**
18+
* @author Steve Ebersole
19+
*/
20+
public class ArrayTypeDetailsImpl implements ArrayTypeDetails {
21+
private final ClassDetails arrayClassDetails;
22+
private final TypeDetails constituentType;
23+
private final int dimensions;
24+
25+
public ArrayTypeDetailsImpl(ClassDetails arrayClassDetails, TypeDetails constituentType) {
26+
this.arrayClassDetails = arrayClassDetails;
27+
this.constituentType = constituentType;
28+
this.dimensions = StringHelper.countArrayDimensions( arrayClassDetails.getName() );
29+
}
30+
31+
@Override
32+
public ClassDetails getArrayClassDetails() {
33+
return arrayClassDetails;
34+
}
35+
36+
@Override
37+
public TypeDetails getConstituentType() {
38+
return constituentType;
39+
}
40+
41+
@Override
42+
public int getDimensions() {
43+
return dimensions;
44+
}
45+
46+
@Override
47+
public String getName() {
48+
StringBuilder builder = new StringBuilder();
49+
50+
TypeDetails type = this;
51+
while ( type.getTypeKind() == Kind.ARRAY ) {
52+
int dimensions = type.asArrayType().getDimensions();
53+
while ( dimensions-- > 0 ) {
54+
builder.append( '[' );
55+
}
56+
type = type.asArrayType().getConstituentType();
57+
}
58+
59+
// here, `type` is an element type of the array, i.e., never array
60+
if ( type.getTypeKind() == Kind.PRIMITIVE ) {
61+
builder.append( type.asPrimitiveType().toCode() );
62+
}
63+
else {
64+
// This relies on name() representing the erased type name
65+
// For historical 1.x reasons, we follow the Java reflection format
66+
// instead of the Java descriptor format.
67+
builder.append( 'L' ).append( type.getName() ).append( ';' );
68+
}
69+
70+
return builder.toString();
71+
}
72+
73+
@Override
74+
public boolean isImplementor(Class<?> checkType) {
75+
if ( ClassDetails.OBJECT_CLASS_DETAILS.isImplementor( checkType ) ) {
76+
return true;
77+
}
78+
79+
if ( !checkType.isArray() ) {
80+
return false;
81+
}
82+
83+
return constituentType.isImplementor( checkType.getComponentType() );
84+
}
85+
86+
@Override
87+
public boolean equals(Object o) {
88+
if ( this == o ) {
89+
return true;
90+
}
91+
if ( o == null || getClass() != o.getClass() ) {
92+
return false;
93+
}
94+
ArrayTypeDetailsImpl that = (ArrayTypeDetailsImpl) o;
95+
return Objects.equals( arrayClassDetails, that.arrayClassDetails );
96+
}
97+
98+
@Override
99+
public int hashCode() {
100+
return Objects.hash( arrayClassDetails );
101+
}
102+
}

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

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

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
import org.hibernate.models.UnknownClassException;
1313
import org.hibernate.models.internal.jandex.JandexBuilders;
1414
import org.hibernate.models.internal.jdk.JdkBuilders;
15-
import org.hibernate.models.spi.ObjectClassDetails;
16-
import org.hibernate.models.spi.VoidClassDetails;
1715
import org.hibernate.models.spi.ClassDetails;
1816
import org.hibernate.models.spi.ClassDetailsBuilder;
1917
import org.hibernate.models.spi.ClassDetailsRegistry;
@@ -36,9 +34,9 @@ public ClassDetailsRegistryStandard(SourceModelBuildingContext context) {
3634
this.context = context;
3735
this.standardClassDetailsBuilder = new StandardClassDetailsBuilder( JdkBuilders.DEFAULT_BUILDER, context.getJandexIndex() );
3836

39-
classDetailsMap.put( VoidClassDetails.VOID_CLASS_DETAILS.getClassName(), VoidClassDetails.VOID_CLASS_DETAILS );
40-
classDetailsMap.put( VoidClassDetails.VOID_OBJECT_CLASS_DETAILS.getClassName(), VoidClassDetails.VOID_OBJECT_CLASS_DETAILS );
41-
classDetailsMap.put( ObjectClassDetails.OBJECT_CLASS_DETAILS.getClassName(), ObjectClassDetails.OBJECT_CLASS_DETAILS );
37+
classDetailsMap.put( ClassDetails.VOID_CLASS_DETAILS.getClassName(), ClassDetails.VOID_CLASS_DETAILS );
38+
classDetailsMap.put( ClassDetails.VOID_OBJECT_CLASS_DETAILS.getClassName(), ClassDetails.VOID_OBJECT_CLASS_DETAILS );
39+
classDetailsMap.put( ClassDetails.OBJECT_CLASS_DETAILS.getClassName(), ClassDetails.OBJECT_CLASS_DETAILS );
4240
}
4341

4442
@Override
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
* Copyright: Red Hat Inc. and Hibernate Authors
6+
*/
7+
8+
package org.hibernate.models.internal;
9+
10+
import java.util.Objects;
11+
12+
import org.hibernate.models.spi.ClassDetails;
13+
import org.hibernate.models.spi.ClassTypeDetails;
14+
15+
/**
16+
* @author Steve Ebersole
17+
*/
18+
public class ClassTypeDetailsImpl implements ClassTypeDetails {
19+
private final ClassDetails classDetails;
20+
private final Kind kind;
21+
22+
public ClassTypeDetailsImpl(ClassDetails classDetails, Kind kind) {
23+
assert classDetails != null;
24+
assert kind == Kind.CLASS || kind == Kind.PRIMITIVE || kind == Kind.VOID;
25+
this.classDetails = classDetails;
26+
this.kind = kind;
27+
}
28+
29+
public ClassDetails getClassDetails() {
30+
return classDetails;
31+
}
32+
33+
@Override
34+
public String getName() {
35+
return classDetails.getName();
36+
}
37+
38+
@Override
39+
public Kind getTypeKind() {
40+
return kind;
41+
}
42+
43+
@Override
44+
public String toString() {
45+
return "ClassTypeDetails(" + classDetails.getName() + ")";
46+
}
47+
48+
@Override
49+
public boolean equals(Object o) {
50+
if ( this == o ) {
51+
return true;
52+
}
53+
if ( o == null || getClass() != o.getClass() ) {
54+
return false;
55+
}
56+
ClassTypeDetailsImpl that = (ClassTypeDetailsImpl) o;
57+
return Objects.equals( classDetails, that.classDetails ) && kind == that.kind;
58+
}
59+
60+
@Override
61+
public int hashCode() {
62+
return Objects.hash( classDetails, kind );
63+
}
64+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
import org.hibernate.models.internal.jandex.EnumValueExtractor;
1212
import org.hibernate.models.internal.jandex.EnumValueWrapper;
1313
import org.hibernate.models.internal.jdk.PassThruExtractor;
14-
import org.hibernate.models.spi.ValueExtractor;
1514
import org.hibernate.models.spi.SourceModelBuildingContext;
15+
import org.hibernate.models.spi.ValueExtractor;
1616
import org.hibernate.models.spi.ValueWrapper;
1717

1818
import org.jboss.jandex.AnnotationInstance;

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

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

13+
import static java.lang.reflect.Modifier.ABSTRACT;
14+
1315
/**
1416
* Fills-in non-public aspects of the {@link Modifier} class
1517
*
@@ -75,6 +77,10 @@ public static boolean isBridge(int modifierFlags) {
7577
return (modifierFlags & BRIDGE) != 0;
7678
}
7779

80+
public static boolean isAbstract(int modifierFlags) {
81+
return (modifierFlags & ABSTRACT) != 0;
82+
}
83+
7884
/**
7985
* Determine if the modifier flags from a field indicate persist-ability.
8086
*
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
* Copyright: Red Hat Inc. and Hibernate Authors
6+
*/
7+
8+
package org.hibernate.models.internal;
9+
10+
import java.util.List;
11+
12+
import org.hibernate.models.spi.ClassDetails;
13+
import org.hibernate.models.spi.ParameterizedTypeDetails;
14+
import org.hibernate.models.spi.TypeDetails;
15+
16+
/**
17+
* @author Steve Ebersole
18+
*/
19+
public class ParameterizedTypeDetailsImpl implements ParameterizedTypeDetails {
20+
private final ClassDetails genericClassDetails;
21+
private final List<TypeDetails> arguments;
22+
private final TypeDetails owner;
23+
24+
public ParameterizedTypeDetailsImpl(ClassDetails genericClassDetails, List<TypeDetails> arguments, TypeDetails owner) {
25+
this.genericClassDetails = genericClassDetails;
26+
this.arguments = arguments;
27+
this.owner = owner;
28+
}
29+
30+
@Override
31+
public ClassDetails getGenericClassDetails() {
32+
return genericClassDetails;
33+
}
34+
35+
@Override
36+
public List<TypeDetails> getArguments() {
37+
return arguments;
38+
}
39+
40+
@Override
41+
public TypeDetails getOwner() {
42+
return owner;
43+
}
44+
}

0 commit comments

Comments
 (0)