Menu

getReferences doesnt return caller

Developers
2011-08-01
2012-10-08
  • Frederik Schmidt

    Hi, I wrote a little function to find the dependencies between
    typedeclarations. Unfortuntately, I cannot access the caller
    typeDeclaration.Instead, only a reference to the target type declaration is
    returned. Below you find a code snipped and an example output. The number of
    references is correct. However, im not able to access the caller type
    declaration. Maybe you got an idea.

    thanks fred

    public void getDependencies()
    {
    CrossReferenceServiceConfiguration crsc =
    CrossReferenceServiceConfigurationFactory.getInstance();
    SourceFileRepository sfr = crsc.getSourceFileRepository();

    List<CompilationUnit> allCompilationUnitsFromPath;
    allCompilationUnitsFromPath = sfr.getCompilationUnits();

    for (CompilationUnit compilationUnit : allCompilationUnitsFromPath)
    {
    for (int i = 0; i < compilationUnit.getTypeDeclarationCount(); i++)
    {
    TypeDeclaration typeDeclarationTarget = compilationUnit
    .getTypeDeclarationAt(i);

    System.out.println("search Reference for type : " +
    typeDeclarationTarget.getName());
    CrossReferenceSourceInfo crossReferenceSourceInfo =
    crsc.getCrossReferenceSourceInfo();
    List<TypeReference> typeReferences = crossReferenceSourceInfo
    .getReferences(typeDeclarationTarget);

    for (TypeReference typeDeclarationReferenceSource : typeReferences)
    {
    TypeDeclaration classtypeSource = (TypeDeclaration) crsc
    .getCrossReferenceSourceInfo().getType(typeDeclarationReferenceSource);

    System.out.println(" --> found Reference from type : " +
    classtypeSource.getName());
    }
    }
    }

    Output example:
    search Reference for type : Address
    --> found Reference from type : Address
    --> found Reference from type : Address
    --> found Reference from type : Address
    search Reference for type : CustomerView
    .......

     
  • Tobias Gutzmann

    Tobias Gutzmann - 2011-08-07

    Hi,

    So you want to get the enclosing type declarations of type references to a
    given type declaration. I think it should work if you replace the innermost
    loop with

    TypeDeclaration classtypeSource =
    recoder.kit.MiscKit.getParentTypeDeclaration(typeReference);
    System.out.println...

    Hope this helps!

    /Tobias

     
  • Frederik Schmidt

    Hi,

    your hint works perfectly fine. Thanks for your help...

    cheers
    frederik

     

Log in to post a comment.