是否有检测重复的接口的GUID的工具吗?(Is there a tool which detects

2019-07-30 18:44发布

这是一个典型的复制粘贴错误:

如果包含接口声明与GUID的一些Delphi代码是复制粘贴,德尔福不会抱怨和编译代码,从而重新使用相同的GUID在不同的地方。

该“支架”功能基于其GUID接口的作品,所以可能会出错。

是否有“质量保证”的工具可用(Peganza或德尔福声纳插件可能),它可以检测到它们?

Answer 1:

如果你使用的是UNIX / Mac上试试这个-或者,如果你有Cygwin的你的电脑上

find . -name '*.pas' -exec awk "/\['{.*}'\]/ {print $1}" {} \; | sed 's/ //g' | sort | uniq -d

然后找个人副本

find . -name '*.pas' -exec grep -i -l 'XXXX-XXX-XXX' {} \;

经测试在Mac



Answer 2:

只能与最新版本的德尔福。 您可以使用下面的代码在运行时检测到这一点:

unit uInterfaces.Duplicates;

interface

uses
  System.Rtti,
  Spring,
  Spring.Collections;

type
  /// <summary>
  ///   Class allows to list the interfaces which are not implemented by any class in your module.
  /// </summary>
  InterfacesWithDuplicateGUID = class
  private class var
    /// <summary>
    ///   Reference to the RTTI context.
    /// </summary>
    FCtx: TRttiContext;
  public
    /// <summary>
    ///   Function returns the list of interfaces with duplicate GUID.
    /// </summary>
    /// <param name="AFilter">
    ///   A filter predicate for types to process.
    /// </param>
    class function Map(AFilter: TPredicate<TRttiInterfaceType> = nil): IMultiMap<TGUID, TRttiInterfaceType>;

    class constructor Create;
    class destructor Destroy;
  end;

implementation

uses
  System.TypInfo;

{ InterfacesNotImplemented }

class constructor InterfacesWithDuplicateGUID.Create;
begin
  FCtx := TRttiContext.Create;
end;

class destructor InterfacesWithDuplicateGUID.Destroy;
begin
  FCtx.Free;
end;

class function InterfacesWithDuplicateGUID.Map(AFilter: TPredicate<TRttiInterfaceType> = nil): IMultiMap<TGUID, TRttiInterfaceType>;
var
  LType: TRttiType;
  LIntf: TRttiInterfaceType;
  LTypes: IList<TRttiInterfaceType>;
begin
  { Create the result instance }
  Result := TCollections.CreateMultiMap<TGUID, TRttiInterfaceType>;

  { Get all the types }
  LTypes := TCollections.CreateList<TRttiInterfaceType>;

  { Build the multimap }
  for LType in FCtx.GetTypes do
    { Add only classes and interfaces }
    if LType.TypeKind = tkInterface then
      { Skip interfaces which does not have GUID }
      if TRttiInterfaceType(LType).GUID <> TGUID.Empty then
        begin
          { Handle user filter }
          if Assigned(AFilter) then
            if not AFilter(TRttiInterfaceType(LType)) then
              Continue;

          LTypes.Add(TRttiInterfaceType(LType));
        end;

  { For all interaces }
  for LIntf in LTypes do
    if LTypes.Any(
      function (const AType: TRttiInterfaceType): Boolean
      begin
        Result := (AType.GUID = LIntf.GUID) and (LIntf.QualifiedName <> AType.QualifiedName);
      end) then
      Result.Add(LIntf.GUID, LIntf);
end;

end.

当然,如果它适合你的需求。 因为它不是最好的主意,包括投入生产的代码这一点。 然而,可以包括在测试代码。



文章来源: Is there a tool which detects duplicate interface GUIDs?
标签: delphi guid