1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package Torello.Browser.JsonAST;

/**
 * Converts a single type reference as a {@code String} into a genuine {@link TypeNode} instance.
 * <EMBED CLASS='external-html' DATA-FILE-ID=L_FIND_TYPENODE>
 */
@Torello.JavaDoc.Annotations.StaticFunctional
@Torello.JavaDoc.Annotations.JDHeaderBackgroundImg(EmbedTagFileID="LINKER_JDHBI")
public class Helper$FindTypeNode
{
    private Helper$FindTypeNode() { }

    static TypeNode find(final PPR ppr, final String ref)
    {
        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
        // Identify the Domain in which the reference is located (as instance of Domain, not String
        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
        // 
        // Since e.ref is some String, first figure out whether or not the Domain to which
        // the relevant TCE belongs is the domain in which this PPR resides, or if it is another
        // domain.
        // 
        // All that the following two lines are doing is checking if:
        // 
        // this.ref = "SomeTypeTypeNode.name"
        // this.ref = "OtherDomain.SomeOtherTypeNode.name"
        // 
        // In the former case, use the Domain that owns this Entity
        // In the later case, convert the Domain-Name (currently as a String), into an actual
        // pointer to the JsonAST.Domain reference.

        final int pos = ref.indexOf('.');

        final Domain linkedDomain = (pos == -1)
            ? ppr.ownerDomain
            : Helper$FindDomain.find(ref.substring(0, pos));


        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
        // Search the Domain, to find the TypeNode of the reference
        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***

        final String expectedName = (pos == -1)
            ? ref
            : ref.substring(pos + 1);

        for (final TypeNode tn : linkedDomain.types)
            if (tn.name.equals(expectedName))
                return tn;

        throw new LinkingStateError(
            Helper$FindTypeNode.class.getSimpleName() + ": \n" +
            "Could not link:     [" + ref + "]\n" +
            "Expected in domain: [" + linkedDomain.name  + "]\n" +
            ppr.exceptionLocationSummary()
        );
    }
}