2012-08-03 20 views
9

Bu tipik bir kopyalama yapıştırma hatası geçerli:Yinelenen arabirim GUID'lerini algılayan bir araç var mı?

GUID'lerin arayüz beyanlarını içeren bazı Delphi kod kopyalanıp yapıştırılan ise, Delphi şikayet ve farklı yerlerde aynı GUID-re kullanan kod derleme olmayacaktır.

"Destekler" işlevi, GUID'lerini temel alan arabirimlerle çalışır, dolayısıyla hatalar mümkündür.

Onları tespit edebilecek bir “kalite güvence” aracı var mı (Peganza veya Delphi Sonar eklentisi)?

+3

Daha kesin olması için, çeşitli arabirim tanımları için GUID kullanıldığını algılayan bir şey ister misiniz? GUID, aynı arabirim için kaynak kodda birden çok kez görünebilir. Ancak IMHO, Ctrl + Shift + G yerine yeni bir tanım için kopyalayıp yapıştırmak için bir kodlama hatasıdır. –

+4

PAL'in böyle bir raporun olduğunun farkında değilim, ancak bunun iyi bir uzantısı olacaktır. Bir özellik isteğim var. –

cevap

1

bir unix/mac iseniz bu deneyin - ya da PC'nizde cygwin varsa

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

Ardından bireysel bir mac üzerinde test

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

kopyalar bulmak için

+0

ha - bu sorunun 2 yaşında olduğunu anlamadı: - / – daven11

1

Yalnızca Delphi'nin son sürümüyle çalışır. Sen Çalışma zamanında bu algılamak için aşağıdaki kodu kullanabilirsiniz:

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. 

Tabii ki sizin ihtiyaçlarınıza uygun. Bunu üretim koduna dahil etmek en iyi fikir değil. Bununla birlikte, test koduna dahil edilebilir.

İlgili konular