Skip to content

Change access modifiers on AliasToBeanResultTransformer #2832

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

Merged
merged 2 commits into from
Jun 21, 2021
Merged
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
35 changes: 18 additions & 17 deletions src/NHibernate/Transform/AliasToBeanResultTransformer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace NHibernate.Transform
/// )
/// .SetResultTransformer( new AliasToBeanResultTransformer(typeof(StudentDTO)))
/// .List();
///
///
/// StudentDTO dto = (StudentDTO)resultWithAliasedBean[0];
/// </code>
/// </example>
Expand All @@ -35,23 +35,21 @@ namespace NHibernate.Transform
[Serializable]
public class AliasToBeanResultTransformer : AliasedTupleSubsetResultTransformer, IEquatable<AliasToBeanResultTransformer>
{
private readonly System.Type _resultClass;
private readonly ConstructorInfo _beanConstructor;
private readonly Dictionary<string, NamedMember<FieldInfo>> _fieldsByNameCaseSensitive;
private readonly Dictionary<string, NamedMember<FieldInfo>> _fieldsByNameCaseInsensitive;
private readonly Dictionary<string, NamedMember<PropertyInfo>> _propertiesByNameCaseSensitive;
private readonly Dictionary<string, NamedMember<PropertyInfo>> _propertiesByNameCaseInsensitive;

public AliasToBeanResultTransformer(System.Type resultClass)
{
_resultClass = resultClass ?? throw new ArgumentNullException("resultClass");
ResultClass = resultClass ?? throw new ArgumentNullException("resultClass");

const BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
_beanConstructor = resultClass.GetConstructor(bindingFlags, null, System.Type.EmptyTypes, null);
BeanConstructor = resultClass.GetConstructor(bindingFlags, null, System.Type.EmptyTypes, null);

// if resultClass is a ValueType (struct), GetConstructor will return null...
// if resultClass is a ValueType (struct), GetConstructor will return null...
// in that case, we'll use Activator.CreateInstance instead of the ConstructorInfo to create instances
if (_beanConstructor == null && resultClass.IsClass)
if (BeanConstructor == null && resultClass.IsClass)
{
throw new ArgumentException(
"The target class of a AliasToBeanResultTransformer need a parameter-less constructor",
Expand All @@ -68,6 +66,9 @@ public AliasToBeanResultTransformer(System.Type resultClass)
_propertiesByNameCaseInsensitive = GetMapByName(properties, StringComparer.OrdinalIgnoreCase);
}

protected System.Type ResultClass { get; }
protected ConstructorInfo BeanConstructor { get; }

public override bool IsTransformedValueATupleElement(String[] aliases, int tupleLength)
{
return false;
Expand All @@ -83,9 +84,9 @@ public override object TransformTuple(object[] tuple, String[] aliases)

try
{
result = _resultClass.IsClass
? _beanConstructor.Invoke(null)
: Activator.CreateInstance(_resultClass, true);
result = ResultClass.IsClass
? BeanConstructor.Invoke(null)
: Activator.CreateInstance(ResultClass, true);

for (int i = 0; i < aliases.Length; i++)
{
Expand All @@ -94,11 +95,11 @@ public override object TransformTuple(object[] tuple, String[] aliases)
}
catch (InstantiationException e)
{
throw new HibernateException("Could not instantiate result class: " + _resultClass.FullName, e);
throw new HibernateException("Could not instantiate result class: " + ResultClass.FullName, e);
}
catch (MethodAccessException e)
{
throw new HibernateException("Could not instantiate result class: " + _resultClass.FullName, e);
throw new HibernateException("Could not instantiate result class: " + ResultClass.FullName, e);
}

return result;
Expand All @@ -111,7 +112,7 @@ public override IList TransformList(IList collection)

protected virtual void OnPropertyNotFound(string propertyName)
{
throw new PropertyNotFoundException(_resultClass.GetType(), propertyName, "setter");
throw new PropertyNotFoundException(ResultClass.GetType(), propertyName, "setter");
}

#region Setter resolution
Expand All @@ -126,7 +127,7 @@ protected virtual void OnPropertyNotFound(string propertyName)
/// <exception cref="PropertyNotFoundException">Thrown if no matching property or field can be found.</exception>
/// <exception cref="AmbiguousMatchException">Thrown if many matching properties or fields are found, having the
/// same visibility and inheritance depth.</exception>
private void SetProperty(string alias, object value, object resultObj)
protected void SetProperty(string alias, object value, object resultObj)
{
if (alias == null)
// Grouping properties in criteria are selected without alias, just ignore them.
Expand Down Expand Up @@ -187,7 +188,7 @@ private void CheckMember<T>(NamedMember<T> member, string alias) where T : Membe
private void FetchFieldsAndProperties(List<RankedMember<FieldInfo>> fields, List<RankedMember<PropertyInfo>> properties)
{
const BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly;
var currentType = _resultClass;
var currentType = ResultClass;
var rank = 1;
// For grasping private members, we need to manually walk the hierarchy.
while (currentType != null && currentType != typeof(object))
Expand Down Expand Up @@ -315,12 +316,12 @@ public bool Equals(AliasToBeanResultTransformer other)
{
return true;
}
return Equals(other._resultClass, _resultClass);
return Equals(other.ResultClass, ResultClass);
}

public override int GetHashCode()
{
return _resultClass.GetHashCode();
return ResultClass.GetHashCode();
}

#endregion
Expand Down