Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#80 - Investigate use of MethodHandles instead of Constructor and Method references #81

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 9 additions & 21 deletions src/main/java/org/hibernate/models/internal/AnnotationHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,13 @@
import java.lang.annotation.Annotation;
import java.lang.annotation.Inherited;
import java.lang.annotation.Target;
import java.lang.reflect.InvocationTargetException;
import java.util.EnumSet;
import java.util.List;
import java.util.Locale;

import org.hibernate.models.AnnotationAccessException;
import org.hibernate.models.spi.AnnotationDescriptor;
import org.hibernate.models.spi.AnnotationTarget;
import org.hibernate.models.spi.AttributeDescriptor;
import org.hibernate.models.spi.JdkValueExtractor;
import org.hibernate.models.spi.RenderingCollector;
import org.hibernate.models.spi.SourceModelBuildingContext;

Expand Down Expand Up @@ -67,7 +65,7 @@ public static <A extends Annotation> void render(
attributes.forEach( (attribute) -> attribute.getTypeDescriptor().render(
collector,
attribute.getName(),
extractValue( annotation, attribute ),
extractValue( annotation, attribute, context ),
context
) );

Expand Down Expand Up @@ -107,7 +105,7 @@ public static <A extends Annotation> void render(
attributes.forEach( (attribute) -> attribute.getTypeDescriptor().render(
collector,
attribute.getName(),
extractValue( annotation, attribute ),
extractValue( annotation, attribute, context ),
context
) );

Expand All @@ -116,21 +114,11 @@ public static <A extends Annotation> void render(
}
}

public static <A extends Annotation, R> R extractValue(A annotationUsage, AttributeDescriptor<R> attributeDescriptor) {
try {
//noinspection unchecked
return (R) attributeDescriptor.getAttributeMethod().invoke( annotationUsage );
}
catch (IllegalAccessException | InvocationTargetException e) {
throw new AnnotationAccessException(
String.format(
Locale.ROOT,
"Unable to access annotation attribute value : %s.%s",
annotationUsage.annotationType().getName(),
attributeDescriptor.getName()
),
e
);
}
public static <A extends Annotation, R> R extractValue(
A annotationUsage,
AttributeDescriptor<R> attributeDescriptor,
SourceModelBuildingContext modelContext) {
final JdkValueExtractor<R> valueExtractor = attributeDescriptor.getTypeDescriptor().createJdkValueExtractor( modelContext );
return valueExtractor.extractValue( annotationUsage, attributeDescriptor, modelContext );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import org.hibernate.models.spi.AnnotationDescriptor;
import org.hibernate.models.spi.AttributeDescriptor;
import org.hibernate.models.spi.SourceModelBuildingContext;
import org.hibernate.models.spi.SourceModelContext;

import org.jboss.jandex.AnnotationInstance;

Expand Down Expand Up @@ -123,7 +122,7 @@ public static <A extends Annotation, C extends Annotation> void forEachRepeatedA
if ( container != null ) {
final AnnotationDescriptor<C> containerDescriptor = modelContext.getAnnotationDescriptorRegistry().getDescriptor( containerType );
final AttributeDescriptor<A[]> attribute = containerDescriptor.getAttribute( "value" );
final A[] repetitions = AnnotationHelper.extractValue( container, attribute );
final A[] repetitions = AnnotationHelper.extractValue( container, attribute, modelContext );
CollectionHelper.forEach( repetitions, consumer );
}
}
Expand All @@ -144,7 +143,7 @@ public static <A extends Annotation> void forEachRepeatedAnnotationUsages(
if ( container != null ) {
final AnnotationDescriptor<?> containerDescriptor = modelContext.getAnnotationDescriptorRegistry().getDescriptor( containerType );
final AttributeDescriptor<A[]> attribute = containerDescriptor.getAttribute( "value" );
final A[] repetitions = AnnotationHelper.extractValue( container, attribute );
final A[] repetitions = AnnotationHelper.extractValue( container, attribute, modelContext );
CollectionHelper.forEach( repetitions, consumer );
}
}
Expand All @@ -162,7 +161,7 @@ public static <A extends Annotation> A[] getRepeatedUsages(

if ( containerUsage != null ) {
final AttributeDescriptor<A[]> attribute = type.getRepeatableContainer().getAttribute( "value" );
final A[] repeatableValues = AnnotationHelper.extractValue( containerUsage, attribute );
final A[] repeatableValues = AnnotationHelper.extractValue( containerUsage, attribute, modelContext );

if ( CollectionHelper.isNotEmpty( repeatableValues ) ) {
if ( usage != null ) {
Expand Down Expand Up @@ -227,9 +226,9 @@ private static <A extends Annotation> boolean nameMatches(
AnnotationDescriptor<A> descriptor,
String matchValue,
String attributeToMatch,
SourceModelContext modelContext) {
SourceModelBuildingContext modelContext) {
final AttributeDescriptor<String> attributeDescriptor = descriptor.getAttribute( attributeToMatch );
final String usageName = AnnotationHelper.extractValue( annotationUsage, attributeDescriptor );
final String usageName = AnnotationHelper.extractValue( annotationUsage, attributeDescriptor, modelContext );
return matchValue.equals( usageName );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ public ValueTypeDescriptor<T> getTypeDescriptor() {
return typeDescriptor;
}

@Override
public T getDefaultValue() {
//noinspection unchecked
return (T) method.getDefaultValue();
}

@Override
public Method getAttributeMethod() {
return method;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* SPDX-License-Identifier: Apache-2.0
* Copyright: Red Hat Inc. and Hibernate Authors
*/

package org.hibernate.models.internal;

import org.hibernate.models.ModelsException;

/**
* @author Steve Ebersole
*/
public class MethodInvocationException extends ModelsException {
public MethodInvocationException(String message) {
super( message );
}

public MethodInvocationException(String message, Throwable cause) {
super( message, cause );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* SPDX-License-Identifier: Apache-2.0
* Copyright: Red Hat Inc. and Hibernate Authors
*/

package org.hibernate.models.internal;

import org.hibernate.models.ModelsException;

/**
* @author Steve Ebersole
*/
public class MethodResolutionException extends ModelsException {
public MethodResolutionException(String message) {
super( message );
}

public MethodResolutionException(String message, Throwable cause) {
super( message, cause );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
package org.hibernate.models.internal;

import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.util.Collections;
import java.util.List;
import java.util.Map;
Expand All @@ -26,14 +27,17 @@
* does not collect annotations from the annotation class as we never care about
* meta-annotations in these cases.
*
* @implNote There are a few cases in Hibernate ORM e.g. where we do care about meta-annotations,
* but those are handled specially there.
*
* @author Steve Ebersole
*/
public class OrmAnnotationDescriptor<A extends Annotation, C extends A> extends AbstractAnnotationDescriptor<A> {
private final Class<C> concreteClass;
private final List<AttributeDescriptor<?>> attributeDescriptors;

private DynamicCreator<A,C> dynamicCreator;
private JdkCreator<A,C> jdkCreator;
private final DynamicCreator<A,C> dynamicCreator;
private final JdkCreator<A,C> jdkCreator;
private JandexCreator<A,C> jandexCreator;

public OrmAnnotationDescriptor(
Expand All @@ -55,6 +59,9 @@ public OrmAnnotationDescriptor(

this.concreteClass = concreteClass;
this.attributeDescriptors = AnnotationDescriptorBuilding.extractAttributeDescriptors( annotationType );

this.dynamicCreator = new DynamicCreator<>( annotationType, concreteClass );
this.jdkCreator = new JdkCreator<>( annotationType, concreteClass );
}

@Override
Expand All @@ -66,17 +73,11 @@ public OrmAnnotationDescriptor(

@Override
public C createUsage(SourceModelBuildingContext context) {
if ( dynamicCreator == null ) {
dynamicCreator = new DynamicCreator<>( getAnnotationType(), concreteClass );
}
return dynamicCreator.createUsage( context );
}

@Override
public C createUsage(A jdkAnnotation, SourceModelBuildingContext context) {
if ( jdkCreator == null ) {
jdkCreator = new JdkCreator<>( getAnnotationType(), concreteClass );
}
return jdkCreator.createUsage( jdkAnnotation, context );
}

Expand All @@ -99,93 +100,105 @@ public String toString() {
}

public static class DynamicCreator<A extends Annotation, C extends A> {
private final Constructor<C> constructor;
private final MethodHandle constructor;
private final Class<C> concreteClass;

public DynamicCreator(Class<A> annotationType, Class<C> concreteClass) {
this( resolveConstructor( concreteClass ) );
this( resolveConstructor( concreteClass ), concreteClass );
}

private static <A extends Annotation, C extends A> Constructor<C> resolveConstructor(Class<C> concreteClass) {
private static <A extends Annotation, C extends A> MethodHandle resolveConstructor(Class<C> concreteClass) {
try {
return concreteClass.getDeclaredConstructor( SourceModelBuildingContext.class );
final MethodType methodType = MethodType.methodType( void.class, SourceModelBuildingContext.class );
return MethodHandles.publicLookup().findConstructor( concreteClass, methodType );
}
catch (NoSuchMethodException e) {
throw new RuntimeException( e );
catch (Exception e) {
throw new MethodResolutionException( "Unable to locate default-variant constructor for `" + concreteClass.getName() + "`", e );
}
}

public DynamicCreator(Constructor<C> constructor) {
public DynamicCreator(MethodHandle constructor, Class<C> concreteClass) {
this.constructor = constructor;
this.concreteClass = concreteClass;
}

public C createUsage(SourceModelBuildingContext context) {
try {
return constructor.newInstance( context );
//noinspection unchecked
return (C) constructor.invoke( context );
}
catch (InvocationTargetException | InstantiationException | IllegalAccessException e) {
throw new RuntimeException( e );
catch (Throwable e) {
throw new MethodInvocationException( "Unable to invoke default-variant constructor for `" + concreteClass.getName() + "`", e );
}
}
}

public static class JdkCreator<A extends Annotation, C extends A> {
private final Constructor<C> constructor;
private final MethodHandle constructor;
private final Class<C> concreteClass;

public JdkCreator(Class<A> annotationType, Class<C> concreteClass) {
this( resolveConstructor( annotationType, concreteClass ) );
this( resolveConstructor( annotationType, concreteClass ), concreteClass );
}

private static <A extends Annotation, C extends A> Constructor<C> resolveConstructor(
private static <A extends Annotation, C extends A> MethodHandle resolveConstructor(
Class<A> annotationType,
Class<C> concreteClass) {
try {
return concreteClass.getDeclaredConstructor( annotationType, SourceModelBuildingContext.class );
final MethodType methodType = MethodType.methodType( void.class, annotationType, SourceModelBuildingContext.class );
return MethodHandles.publicLookup().findConstructor( concreteClass, methodType );
}
catch (NoSuchMethodException e) {
throw new RuntimeException( e );
catch (Exception e) {
throw new MethodResolutionException( "Unable to locate JDK-variant constructor for `" + concreteClass.getName() + "`", e );
}
}

public JdkCreator(Constructor<C> constructor) {
public JdkCreator(MethodHandle constructor, Class<C> concreteClass) {
this.constructor = constructor;
this.concreteClass = concreteClass;
}

public C createUsage(A jdkAnnotation, SourceModelBuildingContext context) {
try {
return constructor.newInstance( jdkAnnotation, context );
//noinspection unchecked
return (C) constructor.invoke( jdkAnnotation, context );
}
catch (InvocationTargetException | InstantiationException | IllegalAccessException e) {
throw new RuntimeException( e );
catch (Throwable e) {
throw new MethodInvocationException( "Unable to invoke JDK-variant constructor for `" + concreteClass.getName() + "`", e );
}
}
}

public static class JandexCreator<A extends Annotation, C extends A> {
private final Constructor<C> constructor;
private final MethodHandle constructor;
private final Class<C> concreteClass;

public JandexCreator(Class<C> concreteClass) {
this( resolveConstructor( concreteClass ) );
this( resolveConstructor( concreteClass ), concreteClass );
}

private static <A extends Annotation, C extends A> Constructor<C> resolveConstructor(Class<C> concreteClass) {
private static <A extends Annotation, C extends A> MethodHandle resolveConstructor(Class<C> concreteClass) {
try {
return concreteClass.getDeclaredConstructor( AnnotationInstance.class, SourceModelBuildingContext.class );
final MethodType methodType = MethodType.methodType( void.class, AnnotationInstance.class, SourceModelBuildingContext.class );
return MethodHandles.publicLookup().findConstructor( concreteClass, methodType );
}
catch (NoSuchMethodException e) {
throw new RuntimeException( e );
catch (Exception e) {
throw new MethodResolutionException( "Unable to locate Jandex-variant constructor for `" + concreteClass.getName() + "`", e );
}
}

public JandexCreator(Constructor<C> constructor) {
public JandexCreator(MethodHandle constructor, Class<C> concreteClass) {
this.constructor = constructor;
this.concreteClass = concreteClass;
}

public C createUsage(AnnotationInstance jandexAnnotation, SourceModelBuildingContext context) {
try {
return constructor.newInstance( jandexAnnotation, context );
//noinspection unchecked
return (C) constructor.invoke( jandexAnnotation, context );
}
catch (InvocationTargetException | InstantiationException | IllegalAccessException e) {
throw new RuntimeException( e );
catch (Throwable e) {
throw new MethodInvocationException( "Unable to invoke Jandex-variant constructor for `" + concreteClass.getName() + "`", e );
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ public static <V> V extractOptionalValue(
SourceModelBuildingContext modelContext) {
final AnnotationValue value = usage.value( attributeDescriptor.getName() );
if ( value == null ) {
//noinspection unchecked
return (V) attributeDescriptor.getAttributeMethod().getDefaultValue();
return attributeDescriptor.getDefaultValue();
}
return attributeDescriptor.getTypeDescriptor().createJandexValueConverter( modelContext ).convert( value, modelContext );
}
Expand Down
Loading
Loading