10

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

如果复制粘贴了一些包含带有 GUID 的接口声明的 Delphi 代码,Delphi 将不会抱怨和编译在不同地方重用相同 GUID 的代码。

“支持”功能基于其 GUID 与接口一起使用,因此可能会出错。

是否有可用的“质量保证”工具(可能是 Peganza 或 Delphi Sonar 插件)可以检测到它们?

4

2 回答 2

2

仅适用于最新版本的 Delphi。您可以使用以下代码在运行时检测到这一点:

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.

当然,如果它符合您的需求。因为将其包含到生产代码中并不是最好的主意。但是可以包含在测试代码中。

于 2016-11-04T16:18:58.737 回答
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上测试

于 2014-12-20T09:01:22.417 回答