Skip to content

Commit

Permalink
HSEARCH-4988 Simplify testing of isolated components involving PojoTy…
Browse files Browse the repository at this point in the history
…peModel
  • Loading branch information
yrodiere committed Oct 17, 2023
1 parent 6e030f7 commit 4809368
Show file tree
Hide file tree
Showing 9 changed files with 213 additions and 255 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,30 +113,23 @@ protected <T> ValueCreateHandle<T> createValueCreateHandle(Constructor<T> constr
return valueHandleFactory.createForConstructor( constructor );
}

@Override
protected ValueReadHandle<?> createValueReadHandle(Member member) throws IllegalAccessException {
setAccessible( member );
return super.createValueReadHandle( member );
}

ValueReadHandle<?> createValueReadHandle(Class<?> holderClass, Member member,
HibernateOrmBasicClassPropertyMetadata ormPropertyMetadata)
throws IllegalAccessException {
if ( member instanceof Method ) {
Method method = (Method) member;
setAccessible( method );
return valueHandleFactory.createForMethod( method );
}
else if ( member instanceof Field ) {
Field field = (Field) member;
if ( ormPropertyMetadata != null && !ormPropertyMetadata.isId() ) {
Method bytecodeEnhancerReaderMethod = getBytecodeEnhancerReaderMethod( holderClass, field );
if ( bytecodeEnhancerReaderMethod != null ) {
setAccessible( bytecodeEnhancerReaderMethod );
return valueHandleFactory.createForMethod( bytecodeEnhancerReaderMethod );
}
if ( member instanceof Field && ormPropertyMetadata != null && !ormPropertyMetadata.isId() ) {
Method bytecodeEnhancerReaderMethod = getBytecodeEnhancerReaderMethod( holderClass, (Field) member );
if ( bytecodeEnhancerReaderMethod != null ) {
return createValueReadHandle( bytecodeEnhancerReaderMethod );
}

setAccessible( field );
return valueHandleFactory.createForField( field );
}
else {
throw new AssertionFailure( "Unexpected type for a " + Member.class.getName() + ": " + member );
}

return createValueReadHandle( member );
}

@SuppressWarnings("rawtypes")
Expand All @@ -160,14 +153,15 @@ private <T> HibernateOrmClassRawTypeModel<T> createClassTypeModel(Class<T> type)
);
}

private static void setAccessible(AccessibleObject member) {
private static void setAccessible(Member member) {
try {
// always set accessible to true as it bypass the security model checks
// at execution time and is faster.
member.setAccessible( true );
// always try to set accessible to true regardless of visibility
// as it's faster even for public fields:
// it bypasses the security model checks at execution time.
( (AccessibleObject) member ).setAccessible( true );
}
catch (SecurityException se) {
if ( !Modifier.isPublic( ( (Member) member ).getModifiers() ) ) {
if ( !Modifier.isPublic( member.getModifiers() ) ) {
throw se;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
Expand All @@ -26,6 +29,7 @@
import org.hibernate.search.util.common.impl.StreamHelper;
import org.hibernate.search.util.common.reflect.spi.ValueCreateHandle;
import org.hibernate.search.util.common.reflect.spi.ValueHandleFactory;
import org.hibernate.search.util.common.reflect.spi.ValueReadHandle;

public abstract class AbstractPojoHCAnnBootstrapIntrospector implements PojoBootstrapIntrospector {

Expand Down Expand Up @@ -77,6 +81,20 @@ protected <T> ValueCreateHandle<T> createValueCreateHandle(Constructor<T> constr
+ " '" + getClass().getName() + " should be updated to implement createValueCreateHandle(Constructor)." );
}

protected ValueReadHandle<?> createValueReadHandle(Member member) throws IllegalAccessException {
if ( member instanceof Method ) {
Method method = (Method) member;
return valueHandleFactory.createForMethod( method );
}
else if ( member instanceof Field ) {
Field field = (Field) member;
return valueHandleFactory.createForField( field );
}
else {
throw new AssertionFailure( "Unexpected type for a " + Member.class.getName() + ": " + member );
}
}

public Class<?> toClass(XClass xClass) {
return reflectionManager.toClass( xClass );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,18 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.search.mapper.pojo.standalone.model.impl;
package org.hibernate.search.mapper.pojo.model.hcann.spi;

import java.lang.reflect.Member;
import java.util.List;

import org.hibernate.annotations.common.reflection.XProperty;
import org.hibernate.search.mapper.pojo.model.hcann.spi.AbstractPojoHCAnnPropertyModel;
import org.hibernate.search.util.common.reflect.spi.ValueReadHandle;

class StandalonePojoPropertyModel<T> extends AbstractPojoHCAnnPropertyModel<T, StandalonePojoBootstrapIntrospector> {
final class PojoSimpleHCAnnPropertyModel<T> extends AbstractPojoHCAnnPropertyModel<T, AbstractPojoHCAnnBootstrapIntrospector> {

StandalonePojoPropertyModel(StandalonePojoBootstrapIntrospector introspector,
StandalonePojoRawTypeModel<?> holderTypeModel,
PojoSimpleHCAnnPropertyModel(AbstractPojoHCAnnBootstrapIntrospector introspector,
PojoSimpleHCAnnRawTypeModel<?> holderTypeModel,
String name, List<XProperty> declaredXProperties,
List<Member> members) {
super( introspector, holderTypeModel, name, declaredXProperties, members );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.search.mapper.pojo.standalone.model.impl;
package org.hibernate.search.mapper.pojo.model.hcann.spi;

import java.lang.reflect.Member;
import java.util.ArrayList;
Expand All @@ -15,33 +15,35 @@
import java.util.stream.Stream;

import org.hibernate.annotations.common.reflection.XProperty;
import org.hibernate.search.mapper.pojo.model.hcann.spi.AbstractPojoHCAnnRawTypeModel;
import org.hibernate.search.mapper.pojo.model.spi.GenericContextAwarePojoGenericTypeModel.RawTypeDeclaringContext;
import org.hibernate.search.mapper.pojo.model.spi.PojoPropertyModel;
import org.hibernate.search.mapper.pojo.model.spi.PojoRawTypeIdentifier;

class StandalonePojoRawTypeModel<T> extends AbstractPojoHCAnnRawTypeModel<T, StandalonePojoBootstrapIntrospector> {
public final class PojoSimpleHCAnnRawTypeModel<T>
extends AbstractPojoHCAnnRawTypeModel<T, AbstractPojoHCAnnBootstrapIntrospector> {

StandalonePojoRawTypeModel(StandalonePojoBootstrapIntrospector introspector, PojoRawTypeIdentifier<T> typeIdentifier,
public PojoSimpleHCAnnRawTypeModel(AbstractPojoHCAnnBootstrapIntrospector introspector,
PojoRawTypeIdentifier<T> typeIdentifier,
RawTypeDeclaringContext<T> rawTypeDeclaringContext) {
super( introspector, typeIdentifier, rawTypeDeclaringContext );
}

@Override
@SuppressWarnings("unchecked") // xClass represents T, so its supertypes represent ? super T
public Stream<StandalonePojoRawTypeModel<? super T>> ascendingSuperTypes() {
public Stream<PojoSimpleHCAnnRawTypeModel<? super T>> ascendingSuperTypes() {
return introspector.ascendingSuperClasses( xClass )
.map( xc -> (StandalonePojoRawTypeModel<? super T>) introspector.typeModel( xc ) );
.map( xc -> (PojoSimpleHCAnnRawTypeModel<? super T>) introspector.typeModel( xc ) );
}

@Override
@SuppressWarnings("unchecked") // xClass represents T, so its supertypes represent ? super T
public Stream<StandalonePojoRawTypeModel<? super T>> descendingSuperTypes() {
public Stream<PojoSimpleHCAnnRawTypeModel<? super T>> descendingSuperTypes() {
return introspector.descendingSuperClasses( xClass )
.map( xc -> (StandalonePojoRawTypeModel<? super T>) introspector.typeModel( xc ) );
.map( xc -> (PojoSimpleHCAnnRawTypeModel<? super T>) introspector.typeModel( xc ) );
}

@Override
protected StandalonePojoPropertyModel<?> createPropertyModel(String propertyName) {
protected PojoPropertyModel<?> createPropertyModel(String propertyName) {
List<XProperty> declaredXProperties = new ArrayList<>( 2 );
List<XProperty> methodAccessXProperties = declaredMethodAccessXPropertiesByName().get( propertyName );
if ( methodAccessXProperties != null ) {
Expand All @@ -57,7 +59,7 @@ protected StandalonePojoPropertyModel<?> createPropertyModel(String propertyName
return null;
}

return new StandalonePojoPropertyModel<>( introspector, this, propertyName,
return new PojoSimpleHCAnnPropertyModel<>( introspector, this, propertyName,
declaredXProperties, members );
}

Expand All @@ -72,7 +74,7 @@ private List<Member> findPropertyMember(String propertyName) {
return field == null ? null : Collections.singletonList( field );
}

private <T2> T2 findInSelfOrParents(Function<StandalonePojoRawTypeModel<?>, T2> getter) {
private <T2> T2 findInSelfOrParents(Function<PojoSimpleHCAnnRawTypeModel<?>, T2> getter) {
return ascendingSuperTypes()
.map( getter )
.filter( Objects::nonNull )
Expand Down
Loading

0 comments on commit 4809368

Please sign in to comment.