Skip to content

Commit a479f4f

Browse files
committed
#107 - hibernate-models-bytebuddy
# Conflicts: # hibernate-models/src/test/java/org/hibernate/models/testing/tests/MultiDimensionalArrayTypeTests.java
1 parent ed7b56f commit a479f4f

File tree

66 files changed

+3680
-1
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

+3680
-1
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
plugins {
2+
id "published-java-module"
3+
}
4+
5+
description = "Support for hibernate-models based on ByteBuddy (isolated dependency)"
6+
7+
repositories {
8+
mavenCentral()
9+
}
10+
11+
dependencies {
12+
api project( ":hibernate-models" )
13+
14+
implementation libs.byteBuddy
15+
16+
testImplementation project( ":hibernate-models-testing" )
17+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright: Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.models.bytebuddy.internal;
6+
7+
import java.lang.annotation.Annotation;
8+
import java.util.Map;
9+
10+
import org.hibernate.models.internal.AnnotationTargetSupport;
11+
12+
import net.bytebuddy.description.annotation.AnnotationSource;
13+
14+
/**
15+
* @author Steve Ebersole
16+
*/
17+
public abstract class AbstractAnnotationTarget implements AnnotationTargetSupport {
18+
private final SourceModelBuildingContextImpl modelContext;
19+
20+
private Map<Class<? extends Annotation>, ? extends Annotation> usageMap;
21+
22+
public AbstractAnnotationTarget(SourceModelBuildingContextImpl modelContext) {
23+
this.modelContext = modelContext;
24+
}
25+
26+
public SourceModelBuildingContextImpl getModelContext() {
27+
return modelContext;
28+
}
29+
30+
protected abstract AnnotationSource getAnnotationSource();
31+
32+
@Override
33+
public Map<Class<? extends Annotation>, ? extends Annotation> getUsageMap() {
34+
if ( usageMap == null ) {
35+
usageMap = AnnotationUsageBuilder.collectUsages( getAnnotationSource(), modelContext );
36+
}
37+
return usageMap;
38+
}
39+
40+
@Override
41+
public void clearAnnotationUsages() {
42+
getUsageMap().clear();
43+
}
44+
45+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright: Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.models.bytebuddy.internal;
6+
7+
import java.lang.annotation.Annotation;
8+
9+
import org.hibernate.models.internal.StandardAnnotationDescriptor;
10+
import org.hibernate.models.spi.AnnotationDescriptor;
11+
import org.hibernate.models.spi.SourceModelBuildingContext;
12+
13+
import net.bytebuddy.description.annotation.AnnotationDescription;
14+
15+
/**
16+
* @author Steve Ebersole
17+
*/
18+
public class AnnotationDescriptorImpl<A extends Annotation> extends StandardAnnotationDescriptor<A> {
19+
public AnnotationDescriptorImpl(
20+
Class<A> annotationType,
21+
SourceModelBuildingContext buildingContext) {
22+
super( annotationType, buildingContext );
23+
}
24+
25+
public AnnotationDescriptorImpl(
26+
Class<A> annotationType,
27+
AnnotationDescriptor<?> repeatableContainer,
28+
SourceModelBuildingContext buildingContext) {
29+
super( annotationType, repeatableContainer, buildingContext );
30+
}
31+
32+
public A createUsage(AnnotationDescription annotationDescription, SourceModelBuildingContextImpl context) {
33+
return AnnotationUsageBuilder.makeUsage( annotationDescription, this, context );
34+
}
35+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright: Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.models.bytebuddy.internal;
6+
7+
import java.lang.annotation.Annotation;
8+
import java.util.Map;
9+
10+
import org.hibernate.models.internal.AnnotationDescriptorRegistryStandard;
11+
import org.hibernate.models.spi.AnnotationDescriptor;
12+
import org.hibernate.models.spi.SourceModelBuildingContext;
13+
14+
/**
15+
* @author Steve Ebersole
16+
*/
17+
public class AnnotationDescriptorRegistryImpl extends AnnotationDescriptorRegistryStandard {
18+
public AnnotationDescriptorRegistryImpl(SourceModelBuildingContext modelBuildingContext) {
19+
super( modelBuildingContext );
20+
}
21+
22+
@Override
23+
protected <A extends Annotation> AnnotationDescriptor<A> buildAnnotationDescriptor(
24+
Class<A> javaType,
25+
AnnotationDescriptor<? extends Annotation> containerDescriptor) {
26+
return new AnnotationDescriptorImpl<>( javaType, containerDescriptor, getModelBuildingContext() );
27+
}
28+
29+
public Map<Class<? extends Annotation>, AnnotationDescriptor<? extends Annotation>> getDescriptorMap() {
30+
return descriptorMap;
31+
}
32+
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright: Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.models.bytebuddy.internal;
6+
7+
import java.lang.annotation.Annotation;
8+
import java.lang.annotation.Documented;
9+
import java.lang.annotation.Repeatable;
10+
import java.lang.annotation.Retention;
11+
import java.lang.annotation.Target;
12+
import java.util.Collections;
13+
import java.util.HashMap;
14+
import java.util.Map;
15+
import java.util.concurrent.ConcurrentHashMap;
16+
import java.util.function.BiConsumer;
17+
18+
import org.hibernate.models.bytebuddy.spi.ValueExtractor;
19+
import org.hibernate.models.internal.util.CollectionHelper;
20+
import org.hibernate.models.spi.AnnotationDescriptor;
21+
import org.hibernate.models.spi.AnnotationDescriptorRegistry;
22+
import org.hibernate.models.spi.AttributeDescriptor;
23+
24+
import net.bytebuddy.description.annotation.AnnotationDescription;
25+
import net.bytebuddy.description.annotation.AnnotationList;
26+
import net.bytebuddy.description.annotation.AnnotationSource;
27+
28+
/**
29+
* Helper for building annotation usages/instances based on
30+
* Jandex {@linkplain AnnotationDescription} references
31+
*
32+
* @author Steve Ebersole
33+
*/
34+
public class AnnotationUsageBuilder {
35+
public static <A extends Annotation> A makeUsage(
36+
AnnotationDescription annotationDescription,
37+
AnnotationDescriptor<A> annotationDescriptor,
38+
SourceModelBuildingContextImpl modelContext) {
39+
final Map<String, Object> attributeValues = extractAttributeValues(
40+
annotationDescription,
41+
annotationDescriptor,
42+
modelContext
43+
);
44+
return annotationDescriptor.createUsage( attributeValues, modelContext );
45+
}
46+
47+
private static <A extends Annotation> Map<String, Object> extractAttributeValues(
48+
AnnotationDescription annotationDescription,
49+
AnnotationDescriptor<A> annotationDescriptor,
50+
SourceModelBuildingContextImpl modelContext) {
51+
52+
if ( CollectionHelper.isEmpty( annotationDescriptor.getAttributes() ) ) {
53+
return Collections.emptyMap();
54+
}
55+
56+
final ConcurrentHashMap<String, Object> valueMap = new ConcurrentHashMap<>();
57+
for ( int i = 0; i < annotationDescriptor.getAttributes().size(); i++ ) {
58+
final AttributeDescriptor<?> attributeDescriptor = annotationDescriptor.getAttributes().get( i );
59+
final ValueExtractor<?> extractor = modelContext
60+
.as( SourceModelBuildingContextImpl.class )
61+
.getValueExtractor( attributeDescriptor.getTypeDescriptor() );
62+
final Object attributeValue = extractor.extractValue(
63+
annotationDescription,
64+
attributeDescriptor.getName(),
65+
modelContext
66+
);
67+
valueMap.put( attributeDescriptor.getName(), attributeValue );
68+
}
69+
return valueMap;
70+
}
71+
72+
public static Map<Class<? extends Annotation>, ? extends Annotation> collectUsages(
73+
AnnotationSource annotationSource,
74+
SourceModelBuildingContextImpl modelContext) {
75+
if ( annotationSource == null ) {
76+
return Collections.emptyMap();
77+
}
78+
final Map<Class<? extends Annotation>, Annotation> result = new HashMap<>();
79+
processAnnotations(
80+
annotationSource.getDeclaredAnnotations(),
81+
result::put,
82+
modelContext
83+
);
84+
return result;
85+
}
86+
87+
/**
88+
* Process annotations creating usage instances passed back to the consumer
89+
*/
90+
public static void processAnnotations(
91+
AnnotationList annotations,
92+
BiConsumer<Class<? extends Annotation>, Annotation> consumer,
93+
SourceModelBuildingContextImpl buildingContext) {
94+
final AnnotationDescriptorRegistry annotationDescriptorRegistry = buildingContext.getAnnotationDescriptorRegistry();
95+
96+
for ( AnnotationDescription annotation : annotations ) {
97+
if ( annotation.getAnnotationType().represents( Documented.class )
98+
|| annotation.getAnnotationType().represents( Repeatable.class )
99+
|| annotation.getAnnotationType().represents( Retention.class )
100+
|| annotation.getAnnotationType().represents( Target.class ) ) {
101+
continue;
102+
}
103+
104+
final Class<? extends Annotation> annotationType = buildingContext
105+
.getClassLoading()
106+
.classForName( annotation.getAnnotationType().getTypeName() );
107+
final AnnotationDescriptor<?> annotationDescriptor = annotationDescriptorRegistry.getDescriptor( annotationType );
108+
final Annotation usage = makeUsage(
109+
annotation,
110+
annotationDescriptor,
111+
buildingContext
112+
);
113+
consumer.accept( annotationType, usage );
114+
}
115+
}
116+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright: Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.models.bytebuddy.internal;
6+
7+
import org.hibernate.models.spi.ClassDetails;
8+
import org.hibernate.models.spi.ClassDetailsBuilder;
9+
import org.hibernate.models.spi.SourceModelBuildingContext;
10+
11+
/**
12+
* @author Steve Ebersole
13+
*/
14+
public class ClassDetailsBuilderImpl implements ClassDetailsBuilder {
15+
private final SourceModelBuildingContextImpl modelContext;
16+
17+
public ClassDetailsBuilderImpl(SourceModelBuildingContextImpl modelContext) {
18+
this.modelContext = modelContext;
19+
}
20+
21+
@Override
22+
public ClassDetails buildClassDetails(String name, SourceModelBuildingContext buildingContext) {
23+
assert buildingContext == modelContext;
24+
return ModelBuilders.buildDetails( name, modelContext );
25+
}
26+
}

0 commit comments

Comments
 (0)