有没有检测重复接口GUID的工具? [英] Is there a tool which detects duplicate interface GUIDs?
问题描述
这是一个典型的复制粘贴错误:
This is a typical copy-paste error:
如果包含GUID的接口声明的Delphi代码被复制粘贴,Delphi将不会抱怨并编译代码 - 在不同的地方使用相同的GUID。
if some Delphi code containing interface declarations with GUIDs is copy-pasted, Delphi will not complain and compile code which re-uses the same GUID in different places.
支持功能适用于基于GUID的界面,因此可能会出错。
The "Supports" function works with interfaces based on their GUID, so errors are possible.
是否有可以检测到质量保证工具(Peganza或Delphi Sonar插件)?
Is there a 'quality assurance' tool available (Peganza or the Delphi Sonar plugin maybe) which can detect them?
推荐答案
只适用于最新版本的Delphi。您可以使用以下代码在运行时检测到这一点:
Only works with the recent version of the Delphi. You can use the following code to detect this at run-time:
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.
当然如果它符合您的需求。因为将其包含在生产代码中并不是最好的方法。但是可以包含在测试代码中。
Of course if it fits your needs. As it's not the best idea to include this into production code. However can be included in the test code.
这篇关于有没有检测重复接口GUID的工具?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!