Skip to content

Commit

Permalink
Switch from java.lang.Class to java.lang.reflect.Type in the metamode…
Browse files Browse the repository at this point in the history
…l to support parameterized types
  • Loading branch information
beikov committed Mar 9, 2021
1 parent 4a1a084 commit 350fd81
Show file tree
Hide file tree
Showing 27 changed files with 174 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ else if ( reflectedJtd != null ) {
if ( jdbcMapping == null ) {
throw new MappingException(
"Could not determine JavaTypeDescriptor nor SqlTypeDescriptor to use" + "" +
" for " + ( (BasicValue) stdIndicators ).getResolvedJavaClass() +
" for " + ( (BasicValue) stdIndicators ).getResolvedJavaType() +
"; table = " + table.getName() +
"; column = " + selectable.getText()
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ public class VersionResolution<E> implements BasicValue.Resolution<E> {

@SuppressWarnings({"rawtypes", "unchecked"})
public static <E> VersionResolution<E> from(
Function<TypeConfiguration, Class> implicitJavaTypeAccess,
Function<TypeConfiguration, java.lang.reflect.Type> implicitJavaTypeAccess,
Function<TypeConfiguration, BasicJavaDescriptor> explicitJtdAccess,
Function<TypeConfiguration, SqlTypeDescriptor> explicitStdAccess,
TypeConfiguration typeConfiguration,
@SuppressWarnings("unused") MetadataBuildingContext context) {

// todo (6.0) : add support for Dialect-specific interpretation?

final Class implicitJavaType = implicitJavaTypeAccess.apply( typeConfiguration );
final java.lang.reflect.Type implicitJavaType = implicitJavaTypeAccess.apply( typeConfiguration );
final JavaTypeDescriptor registered = typeConfiguration.getJavaTypeDescriptorRegistry().resolveDescriptor( implicitJavaType );

if ( registered instanceof PrimitiveByteArrayTypeDescriptor ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.jdbc.spi.JdbcServices;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.internal.util.ReflectHelper;
import org.hibernate.mapping.BasicValue;
import org.hibernate.mapping.Table;
import org.hibernate.type.descriptor.java.BasicJavaDescriptor;
Expand Down Expand Up @@ -98,7 +99,7 @@ public enum Kind {
private Function<TypeConfiguration,SqlTypeDescriptor> explicitSqlTypeAccess;
private Function<TypeConfiguration, BasicJavaDescriptor> explicitJtdAccess;
private Function<TypeConfiguration, MutabilityPlan> explicitMutabilityAccess;
private Function<TypeConfiguration, Class> implicitJavaTypeAccess;
private Function<TypeConfiguration, java.lang.reflect.Type> implicitJavaTypeAccess;

private AccessType accessType;

Expand Down Expand Up @@ -595,7 +596,7 @@ private void normalSupplementalDetails(
}

// see if the value's type Class is annotated `@Immutable`
final Class attributeType = implicitJavaTypeAccess.apply( typeConfiguration );
final Class attributeType = ReflectHelper.getClass( implicitJavaTypeAccess.apply( typeConfiguration ) );
if ( attributeType.isAnnotationPresent( Immutable.class ) ) {
return ImmutableMutabilityPlan.instance();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.util.Locale;
import java.util.regex.Pattern;
import javax.persistence.Transient;
Expand Down Expand Up @@ -214,6 +215,19 @@ public static Class reflectedPropertyClass(
String className,
String name,
ClassLoaderService classLoaderService) throws MappingException {
try {
Class clazz = classLoaderService.classForName( className );
return getter( clazz, name ).getReturnTypeClass();
}
catch ( ClassLoadingException e ) {
throw new MappingException( "class " + className + " not found while looking for property: " + name, e );
}
}

public static java.lang.reflect.Type reflectedPropertyType(
String className,
String name,
ClassLoaderService classLoaderService) throws MappingException {
try {
Class clazz = classLoaderService.classForName( className );
return getter( clazz, name ).getReturnType();
Expand All @@ -232,7 +246,7 @@ public static Class reflectedPropertyClass(
* @throws MappingException Indicates we were unable to locate the property.
*/
public static Class reflectedPropertyClass(Class clazz, String name) throws MappingException {
return getter( clazz, name ).getReturnType();
return getter( clazz, name ).getReturnTypeClass();
}

private static Getter getter(Class clazz, String name) throws MappingException {
Expand Down Expand Up @@ -723,4 +737,17 @@ public static Method findGetterMethodForFieldAccess(Field field, String property

return null;
}

public static <T> Class<T> getClass(java.lang.reflect.Type type) {
if ( type == null ) {
return null;
}
else if ( type instanceof Class<?> ) {
return (Class<T>) type;
}
else if ( type instanceof ParameterizedType ) {
return (Class<T>) ( (ParameterizedType) type ).getRawType();
}
throw new UnsupportedOperationException( "Can't get java type class from type: " + type );
}
}
30 changes: 15 additions & 15 deletions hibernate-core/src/main/java/org/hibernate/mapping/BasicValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,15 @@ public class BasicValue extends SimpleValue implements SqlTypeDescriptorIndicato
private String explicitTypeName;
private Map explicitLocalTypeParams;

private Function<TypeConfiguration,BasicJavaDescriptor> explicitJavaTypeAccess;
private Function<TypeConfiguration,SqlTypeDescriptor> explicitSqlTypeAccess;
private Function<TypeConfiguration,MutabilityPlan> explicitMutabilityPlanAccess;
private Function<TypeConfiguration,Class> implicitJavaTypeAccess;
private Function<TypeConfiguration, BasicJavaDescriptor> explicitJavaTypeAccess;
private Function<TypeConfiguration, SqlTypeDescriptor> explicitSqlTypeAccess;
private Function<TypeConfiguration, MutabilityPlan> explicitMutabilityPlanAccess;
private Function<TypeConfiguration, java.lang.reflect.Type> implicitJavaTypeAccess;

private EnumType enumerationStyle;
private TemporalType temporalPrecision;

private Class resolvedJavaClass;
private java.lang.reflect.Type resolvedJavaType;

private String ownerName;
private String propertyName;
Expand Down Expand Up @@ -138,7 +138,7 @@ public void setExplicitMutabilityPlanAccess(Function<TypeConfiguration, Mutabili
this.explicitMutabilityPlanAccess = explicitMutabilityPlanAccess;
}

public void setImplicitJavaTypeAccess(Function<TypeConfiguration, Class> implicitJavaTypeAccess) {
public void setImplicitJavaTypeAccess(Function<TypeConfiguration, java.lang.reflect.Type> implicitJavaTypeAccess) {
this.implicitJavaTypeAccess = implicitJavaTypeAccess;
}

Expand All @@ -149,8 +149,8 @@ public Selectable getColumn() {
return getColumn( 0 );
}

public Class getResolvedJavaClass() {
return resolvedJavaClass;
public java.lang.reflect.Type getResolvedJavaType() {
return resolvedJavaType;
}

@Override
Expand Down Expand Up @@ -344,7 +344,7 @@ public TypeConfiguration getTypeConfiguration() {

if ( jtd == null ) {
if ( implicitJavaTypeAccess != null ) {
final Class implicitJtd = implicitJavaTypeAccess.apply( typeConfiguration );
final java.lang.reflect.Type implicitJtd = implicitJavaTypeAccess.apply( typeConfiguration );
if ( implicitJtd != null ) {
jtd = typeConfiguration.getJavaTypeDescriptorRegistry().getDescriptor( implicitJtd );
}
Expand Down Expand Up @@ -403,10 +403,10 @@ public TypeConfiguration getTypeConfiguration() {
}

private JavaTypeDescriptor determineReflectedJavaTypeDescriptor() {
final Class impliedJavaType;
final java.lang.reflect.Type impliedJavaType;

if ( resolvedJavaClass != null ) {
impliedJavaType = resolvedJavaClass;
if ( resolvedJavaType != null ) {
impliedJavaType = resolvedJavaType;
}
else if ( implicitJavaTypeAccess != null ) {
impliedJavaType = implicitJavaTypeAccess.apply( typeConfiguration );
Expand All @@ -415,7 +415,7 @@ else if ( ownerName != null && propertyName != null ) {
final ServiceRegistry serviceRegistry = typeConfiguration.getServiceRegistry();
final ClassLoaderService classLoaderService = serviceRegistry.getService( ClassLoaderService.class );

impliedJavaType = ReflectHelper.reflectedPropertyClass(
impliedJavaType = ReflectHelper.reflectedPropertyType(
ownerName,
propertyName,
classLoaderService
Expand All @@ -425,7 +425,7 @@ else if ( ownerName != null && propertyName != null ) {
return null;
}

resolvedJavaClass = impliedJavaType;
resolvedJavaType = impliedJavaType;
return typeConfiguration.getJavaTypeDescriptorRegistry().resolveDescriptor( impliedJavaType );
}

Expand All @@ -434,7 +434,7 @@ else if ( ownerName != null && propertyName != null ) {
private static Resolution interpretExplicitlyNamedType(
String name,
EnumType enumerationStyle,
Function<TypeConfiguration, Class> implicitJavaTypeAccess,
Function<TypeConfiguration, java.lang.reflect.Type> implicitJavaTypeAccess,
Function<TypeConfiguration, BasicJavaDescriptor> explicitJtdAccess,
Function<TypeConfiguration, SqlTypeDescriptor> explicitStdAccess,
Function<TypeConfiguration, MutabilityPlan> explicitMutabilityPlanAccess,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public Value getValue() {
}

public boolean isPrimitive(Class clazz) {
return getGetter(clazz).getReturnType().isPrimitive();
return getGetter(clazz).getReturnTypeClass().isPrimitive();
}

public CascadeStyle getCascadeStyle() throws MappingException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ private ReflectionOptimizer buildReflectionOptimizer(

getterNames[i] = propertyAccess.getGetter().getMethodName();
setterNames[i] = propertyAccess.getSetter().getMethodName();
propTypes[i] = propertyAccess.getGetter().getReturnType();
propTypes[i] = propertyAccess.getGetter().getReturnTypeClass();
}

return Environment.getBytecodeProvider().getReflectionOptimizer(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ private ReflectionOptimizer resolveReflectionOptimizer(
}

getterNames.add( propertyAccess.getGetter().getMethodName() );
getterTypes.add( propertyAccess.getGetter().getReturnType() );
getterTypes.add( propertyAccess.getGetter().getReturnTypeClass() );

setterNames.add( propertyAccess.getSetter().getMethodName() );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.Map;

import org.hibernate.engine.spi.SessionFactoryImplementor;
Expand All @@ -21,7 +22,7 @@
* PropertyAccess for handling non-aggregated composites.
* <p/>
* IMPL NOTE : We actually use a singleton for the Setter; we cannot for the getter mainly
* because we need to differentiate {@link Getter#getReturnType()}. Ultimately I'd prefer to
* because we need to differentiate {@link Getter#getReturnTypeClass()}. Ultimately I'd prefer to
* model that "common information" on PropertyAccess itself.
*
* @author Gavin King
Expand Down Expand Up @@ -73,7 +74,12 @@ public Object getForInsert(Object owner, Map mergeMap, SharedSessionContractImpl
}

@Override
public Class getReturnType() {
public Class<?> getReturnTypeClass() {
return containerType;
}

@Override
public Type getReturnType() {
return containerType;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.Map;

import org.hibernate.engine.spi.SessionFactoryImplementor;
Expand Down Expand Up @@ -68,11 +69,16 @@ public Object getForInsert(Object owner, Map mergeMap, SharedSessionContractImpl
}

@Override
public Class getReturnType() {
public Class<?> getReturnTypeClass() {
// we just don't know...
return Object.class;
}

@Override
public Type getReturnType() {
return Object.class;
}

@Override
public Member getMember() {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.io.Serializable;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.Map;

import org.hibernate.engine.spi.SessionFactoryImplementor;
Expand Down Expand Up @@ -102,7 +103,12 @@ public Object getForInsert(Object owner, Map mergeMap, SharedSessionContractImpl
}

@Override
public Class getReturnType() {
public Class<?> getReturnTypeClass() {
return Object.class;
}

@Override
public Type getReturnType() {
return Object.class;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.Map;

import org.hibernate.engine.spi.SessionFactoryImplementor;
Expand Down Expand Up @@ -85,7 +86,12 @@ public Object getForInsert(Object owner, Map mergeMap, SharedSessionContractImpl
}

@Override
public Class getReturnType() {
public Class<?> getReturnTypeClass() {
return Object.class;
}

@Override
public Type getReturnType() {
return Object.class;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.Map;

import org.hibernate.engine.spi.SessionFactoryImplementor;
Expand Down Expand Up @@ -74,7 +75,12 @@ public Object getForInsert(Object owner, Map mergeMap, SharedSessionContractImpl
}

@Override
public Class getReturnType() {
public Class<?> getReturnTypeClass() {
return Object.class;
}

@Override
public Type getReturnType() {
return Object.class;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.lang.reflect.Field;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.Map;

import org.hibernate.PropertyAccessException;
Expand Down Expand Up @@ -102,10 +103,15 @@ public Object getForInsert(Object owner, Map mergeMap, SharedSessionContractImpl
}

@Override
public Class getReturnType() {
public Class<?> getReturnTypeClass() {
return getterMethod.getReturnType();
}

@Override
public Type getReturnType() {
return getterMethod.getGenericReturnType();
}

@Override
public Member getMember() {
return getterMethod;
Expand Down

0 comments on commit 350fd81

Please sign in to comment.