Skip to content

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

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

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
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,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 modelContext.as( JandexModelContext.class )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,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;

/**
* @see AnnotationHelper
Expand Down Expand Up @@ -206,7 +205,7 @@ 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 );
return matchValue.equals( usageName );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,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
Expand Up @@ -5,12 +5,14 @@
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;

import org.hibernate.models.ModelsException;
import org.hibernate.models.spi.AnnotationDescriptor;
import org.hibernate.models.spi.AttributeDescriptor;
import org.hibernate.models.spi.MutableAnnotationDescriptor;
Expand All @@ -22,6 +24,9 @@
* 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>
Expand All @@ -30,8 +35,8 @@ public class OrmAnnotationDescriptor<A extends Annotation, C extends 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 DeTypedCreator<A,C> deTypedCreator;

public OrmAnnotationDescriptor(
Expand All @@ -53,6 +58,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 @@ -69,17 +77,11 @@ public Class<C> getMutableAnnotationType() {

@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 @@ -102,93 +104,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 ModelsException( "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 ModelsException( "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 ModelsException( "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 ModelsException( "Unable to invoke JDK-variant constructor for `" + concreteClass.getName() + "`", e );
}
}
}

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

public DeTypedCreator(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( Map.class, SourceModelBuildingContext.class );
final MethodType methodType = MethodType.methodType( void.class, Map.class, SourceModelBuildingContext.class );
return MethodHandles.publicLookup().findConstructor( concreteClass, methodType );
}
catch (NoSuchMethodException e) {
throw new RuntimeException( e );
catch (Exception e) {
throw new ModelsException( "Unable to locate Jandex-variant constructor for `" + concreteClass.getName() + "`", e );
}
}

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

public C createUsage(Map<String,?> attributeValues, SourceModelBuildingContext context) {
try {
return constructor.newInstance( attributeValues, context );
//noinspection unchecked
return (C) constructor.invoke( attributeValues, context );
}
catch (InvocationTargetException | InstantiationException | IllegalAccessException e) {
throw new RuntimeException( e );
catch (Throwable e) {
throw new ModelsException( "Unable to invoke Jandex-variant constructor for `" + concreteClass.getName() + "`", e );
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,20 @@ public interface AttributeDescriptor<T> {
*/
ValueTypeDescriptor<T> getTypeDescriptor();

/**
* The attribute's type.
*/
default Class<T> getValueType() {
return getTypeDescriptor().getValueType();
}

/**
* The attribute's defined default value, if one.
*/
T getDefaultValue();

/**
* The attribute method.
*/
Method getAttributeMethod();

default boolean isMultiValued() {
return getAttributeMethod().getReturnType().isArray();
}
}
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.0.0-SNAPSHOT
1.0.0-METHOD-HANDLE