Skip to content

Commit

Permalink
Sonar fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
josephmoresena committed Feb 17, 2024
1 parent 90fea60 commit 2fe9b25
Show file tree
Hide file tree
Showing 14 changed files with 48 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,15 @@ internal static class CommonConstants
public const String CheckIdS2436 = "S2436:Types and methods should not have too many generic parameters";
public const String CheckIdS2234 = "S2234:Arguments should be passed in the same order as the method parameters";
public const String CheckIdS4035 = "S4035:Classes implementing \"IEquatable<T>\" should be sealed";
public const String CheckIdS4136 = "S4136:Method overloads should be grouped together";
public const String BinaryStructJustification = "This struct is created only by binary operations.";
public const String AbstractProxyJustification = "This object is an abstract proxy.";
public const String JavaInheritanceJustification =
"Any JReferenceObject type tree of classes is inherently longer than a normal C# class.";
public const String ReferenceableFieldJustification = "Field value can be set using a managed reference.";
public const String BackwardOperationJustification = "Backward operation is needed.";
public const String InternalInheritanceJustification = "Only internal inheritance is supported.";
public const String PublicInitPrivateSetJustification = "The property must be publicly 'init' but privately 'set'.";
public const String NoMethodOverloadingJustification =
"Homonymous functions are different and should not be considered overloading.";
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,22 @@ internal JLocalObject CreateInitialObject(JObjectLocalRef localRef)
return this.CreateInitialClass(classRef);
}
/// <summary>
/// Retrieves initial <typaramref name="TObject"/> instance for <paramref name="localRef"/>.
/// </summary>
/// <typeparam name="TObject">A <see cref="IReferenceType"/> type.</typeparam>
/// <param name="localRef">A <see cref="JObjectLocalRef"/> reference.</param>
/// <returns>Initial <typaramref name="TObject"/> instance for <paramref name="localRef"/>.</returns>
internal TObject CreateInitialObject<TObject>(JObjectLocalRef localRef)
where TObject : JLocalObject, IReferenceType<TObject>
{
JReferenceTypeMetadata metadata = (JReferenceTypeMetadata)MetadataHelper.GetMetadata<TObject>();
if (metadata.Modifier == JTypeModifier.Final) return this.CreateFinalObject<TObject>(localRef);
if (JLocalObject.IsObjectType<TObject>())
return (TObject)this.CreateInitialObject(localRef);
JClassObject jClass = this.GetObjectClass(localRef, true);
return (TObject)metadata.CreateInstance(jClass, localRef, true);
}
/// <summary>
/// Retrieves initial <see cref="JLocalObject"/> instance for <paramref name="classRef"/>.
/// </summary>
/// <param name="classRef">A <see cref="JClassLocalRef"/> reference.</param>
Expand All @@ -53,21 +69,5 @@ internal JClassObject CreateInitialClass(JClassLocalRef classRef, Boolean valida
}
return result;
}
/// <summary>
/// Retrieves initial <typaramref name="TObject"/> instance for <paramref name="localRef"/>.
/// </summary>
/// <typeparam name="TObject">A <see cref="IReferenceType"/> type.</typeparam>
/// <param name="localRef">A <see cref="JObjectLocalRef"/> reference.</param>
/// <returns>Initial <typaramref name="TObject"/> instance for <paramref name="localRef"/>.</returns>
internal TObject CreateInitialObject<TObject>(JObjectLocalRef localRef)
where TObject : JLocalObject, IReferenceType<TObject>
{
JReferenceTypeMetadata metadata = (JReferenceTypeMetadata)MetadataHelper.GetMetadata<TObject>();
if (metadata.Modifier == JTypeModifier.Final) return this.CreateFinalObject<TObject>(localRef);
if (JLocalObject.IsObjectType<TObject>())
return (TObject)this.CreateInitialObject(localRef);
JClassObject jClass = this.GetObjectClass(localRef, true);
return (TObject)metadata.CreateInstance(jClass, localRef, true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,4 @@ namespace Rxmxnx.JNetInterface;
/// This interface exposes an <c>[]</c> instance.
/// </summary>
/// <typeparam name="TElement">Element type of array.</typeparam>
public interface IArrayObject<out TElement> : IObject where TElement : IObject, IDataType<TElement>
{
// /// <summary>
// /// Retrieves array metadata for an array of current array.
// /// </summary>
// public static JArrayTypeMetadata ArrayMetadata => IArrayType.GetArrayArrayMetadata<TElement>();
}
public interface IArrayObject<out TElement> : IObject where TElement : IObject, IDataType<TElement>;
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ namespace Rxmxnx.JNetInterface.Internal;
/// <summary>
/// This class stores a cache of heavily used functions.
/// </summary>
[SuppressMessage(CommonConstants.CheckIdS4136, CommonConstants.CheckIdS2292,
Justification = CommonConstants.NoMethodOverloadingJustification)]
internal abstract class FunctionCache
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ public static void ThrowIfSameType<TBase, TReference>(ReadOnlySpan<Byte> typeNam
public static void ValidateBaseTypes<TBase, TReference>(ReadOnlySpan<Byte> typeName)
where TBase : JReferenceObject, IReferenceType<TBase> where TReference : TBase, IReferenceType<TReference>
{
ISet<Type> baseBaseTypes = IReferenceType<TBase>.GetBaseTypes().ToHashSet();
ISet<Type> baseTypes = IReferenceType<TReference>.GetBaseTypes().ToHashSet();
HashSet<Type> baseBaseTypes = IReferenceType<TBase>.GetBaseTypes().ToHashSet();
HashSet<Type> baseTypes = IReferenceType<TReference>.GetBaseTypes().ToHashSet();
if (!baseTypes.IsProperSupersetOf(baseBaseTypes))
throw new InvalidOperationException(
$"{typeName.ToCString()} type can't be based on a type which is derived from it.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ namespace Rxmxnx.JNetInterface.Internal;
/// <summary>
/// This object stores the lifetime for a java object instance.
/// </summary>
[SuppressMessage(CommonConstants.CSharpSquid, CommonConstants.CheckIdS2292,
Justification = CommonConstants.PublicInitPrivateSetJustification)]
internal sealed partial class ObjectLifetime : IDisposable
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@ public static ThreadCreationArgs Create(ThreadPurpose purpose)
private static CString GetThreadName(ThreadPurpose purpose)
{
CString prefix = ThreadCreationArgs.threadNames.GetValueOrDefault(purpose) ?? CString.Zero;
return CString.Concat(prefix, (CString)Environment.CurrentManagedThreadId.ToString());
return CString.Concat(prefix.AsSpan(), (CString)Environment.CurrentManagedThreadId.ToString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public abstract partial class JThrowableException : Exception
/// </summary>
/// <param name="jGlobal">A <see cref="JGlobalBase"/> throwable instance.</param>
/// <param name="message">Exception message.</param>
internal JThrowableException(JGlobalBase jGlobal, String? message) : base(message)
private protected JThrowableException(JGlobalBase jGlobal, String? message) : base(message)
{
this._jGlobal = jGlobal;
this._thread = Thread.CurrentThread;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public void AddField(ReadOnlySpan<Byte> enumTypeName, Int32 ordinal, CString nam
/// </summary>
/// <param name="enumTypeName">Enum type name.</param>
/// <returns>The current instance.</returns>
public IEnumFieldList Validate(ReadOnlySpan<Byte> enumTypeName)
public FieldList Validate(ReadOnlySpan<Byte> enumTypeName)
{
NativeValidationUtilities.ThrowIfInvalidList(enumTypeName, this);
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ public String GetChars(Int32 startIndex, Int32 count)
return this.Value;
if (this._value is not null)
return this._value[startIndex..count];
Int32 lenght = count - startIndex;
return String.Create(lenght, (this, startIndex), JStringObject.GetChars);
Int32 length = count - startIndex;
return String.Create(length, (this, startIndex), JStringObject.GetChars);
}
/// <summary>
/// Creates an <see cref="CString"/> containing a copy of the UTF-8 chars on the current
Expand All @@ -93,8 +93,8 @@ public String GetChars(Int32 startIndex, Int32 count)
/// </returns>
public CString GetUtf8Chars(Int32 startIndex, Int32 count)
{
Int32 lenght = count - startIndex;
Byte[] utf8Data = new Byte[lenght + 1];
Int32 length = count - startIndex;
Byte[] utf8Data = new Byte[length + 1];
IEnvironment env = this.Environment;
env.StringFeature.GetCopyUtf8(this, utf8Data.AsMemory()[..^1], startIndex);
return utf8Data;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ internal JStringObject(JClassObject jStringClass, JStringLocalRef stringRef, Int
/// <param name="jGlobal"><see cref="JGlobalBase"/> instance.</param>
internal JStringObject(IEnvironment env, JGlobalBase jGlobal) : base(env, jGlobal)
{
if (this._length is not null) return;
this._length ??= this.Environment.StringFeature.GetLength(jGlobal);
this._utf8Length ??= this.Environment.StringFeature.GetUtf8Length(jGlobal);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public partial class JExecutableObject
/// This record stores the metadata of a <see cref="JExecutableObject"/> in order to create a
/// <see cref="JGlobalBase"/> instance.
/// </summary>
protected record ExecutableObjectMetadata : ObjectMetadata
protected sealed record ExecutableObjectMetadata : ObjectMetadata
{
/// <summary>
/// Execution definition.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public partial class JFieldObject
/// This record stores the metadata of a <see cref="JFieldObject"/> in order to create a
/// <see cref="JGlobalBase"/> instance.
/// </summary>
private record FieldObjectMetadata : ObjectMetadata
private sealed record FieldObjectMetadata : ObjectMetadata
{
/// <summary>
/// Field definition.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,18 @@
namespace Rxmxnx.JNetInterface.SourceGenerator;

[ExcludeFromCodeCoverage]
[SuppressMessage("csharpsquid", "S3963:\"static\" fields should be initialized inline",
Justification = "Static constructor is needed.")]
internal static class NativeGenerator
{
public const String AssemblyName = "Rxmxnx.JNetInterface.Native.Intermediate";

private const String sourceNamespace = "namespace Rxmxnx.JNetInterface.Native;";
private const String genericDocFormat =
private const String SourceNamespace = "namespace Rxmxnx.JNetInterface.Native;";
private const String GenericDocFormat =
"/// <typeparam name=\"TArg{0}\"><see cref=\"IDataType\"/> type of {0}{1} method argument.</typeparam>";
private const String argTypeFormat = "TArg{0}";
private const String constraintFormat = "\twhere TArg{0} : IDataType, IObject ";
private const String argumentFormat = "JArgumentMetadata.Create<TArg{0}>()";
private const String ArgTypeFormat = "TArg{0}";
private const String ConstraintFormat = "\twhere TArg{0} : IDataType, IObject ";
private const String ArgumentFormat = "JArgumentMetadata.Create<TArg{0}>()";

private static readonly NativeMethodGenerator constructor;
private static readonly NativeMethodGenerator function;
Expand Down Expand Up @@ -115,14 +117,14 @@ private static void Generate(GeneratorExecutionContext context, NativeMethodGene
for (UInt32 i = 1; i <= Byte.MaxValue; i++)
{
NativeGenerator.PrepareFile(strBuildDoc, strBuildArgType, strBuildArg, i);
strBuildDoc.Append(String.Format(NativeGenerator.genericDocFormat, i, i.GetOrdinalSuffix()));
strBuildArgType.Append(String.Format(NativeGenerator.argTypeFormat, i));
strBuildCons.AppendLine(String.Format(NativeGenerator.constraintFormat, i));
strBuildArg.Append(String.Format(NativeGenerator.argumentFormat, i));
strBuildDoc.Append(String.Format(NativeGenerator.GenericDocFormat, i, i.GetOrdinalSuffix()));
strBuildArgType.Append(String.Format(NativeGenerator.ArgTypeFormat, i));
strBuildCons.AppendLine(String.Format(NativeGenerator.ConstraintFormat, i));
strBuildArg.Append(String.Format(NativeGenerator.ArgumentFormat, i));

StringBuilder strBuildSource = new();

strBuildSource.AppendLine(NativeGenerator.sourceNamespace);
strBuildSource.AppendLine(NativeGenerator.SourceNamespace);
strBuildSource.AppendLine(method.Documentation);
strBuildSource.AppendLine(strBuildDoc.ToString());
strBuildSource.Append(method.DefinitionPrefix);
Expand Down

0 comments on commit 2fe9b25

Please sign in to comment.