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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package Torello.JDUInternal.Annotations.EntityAnnotations.Mirror;

import Torello.JDUInternal.Messager.Messager;
import Torello.JDUInternal.Messager.MsgControl;
import Torello.JDUInternal.Messager.MsgPrintTools;
import Torello.JDUInternal.Messager.Where.JDUAnnotations;

import Torello.JDUInternal.Annotations.EntityAnnotations.EntityAnnotationData;

import Torello.Java.ReadOnly.ReadOnlyList;
import Torello.Java.ReadOnly.ROArrayListBuilder;
import Torello.Java.ReadOnly.ReadOnlyArrayList;

import static Torello.Java.C.BRED;
import static Torello.Java.C.RESET;

import java.util.List;

import com.sun.source.tree.Tree;
import com.sun.source.tree.AnnotationTree;
import com.sun.source.tree.IdentifierTree;
import com.sun.source.tree.MemberSelectTree;


// The following field is declared inside class Torello.JavaDoc.Declaration:
// final EntityAnnotationMirrors jduAnnnotationMirrors;

public class EntityAnnotationMirrors  
{
    // ********************************************************************************************
    // ********************************************************************************************
    // An Empty-Mirror Reference
    // ********************************************************************************************
    // ********************************************************************************************


    // If there are ZERO annotations that have been placed on a particular "Entity" (Method, Field,
    // Constructor, etc...), then there is no chance whatsoever that there are any JDU Annotations!
    // In such cases, the entire constructor is skipeed, and this "EMPTY_MIRROR" is assigned,
    // instead.

    public static final EntityAnnotationMirrors EMPTY_MIRROR = new EntityAnnotationMirrors();

    @SuppressWarnings("unchecked")
    private EntityAnnotationMirrors()
    {
        this.ljsMirrors = ReadOnlyArrayList.emptyROAL();
        this.ihtMirror  = null;
    }


    // ********************************************************************************************
    // ********************************************************************************************
    // The Actual Mirrors - as Fields of this class
    // ********************************************************************************************
    // ********************************************************************************************


    // These are the "Link Java Source" Mirrors.  This list will never be empty, instead if there
    // weren't any @LinkJavaSource Annotations placed on this particular Entity, then this field 
    // will contain an empty list.
    // 
    // This is a "Repeatable" Annotation, which is why it is a (Read-Only) list

    public final ReadOnlyList<LJSMirror> ljsMirrors;

    // Non Repeatable Annotation
    public final IHTMirror ihtMirror;


    // ********************************************************************************************
    // ********************************************************************************************
    // Constructor: This is used (ONLY !) in class Torello.JavaDoc.Declaration
    // ********************************************************************************************
    // ********************************************************************************************


    @SuppressWarnings("unchecked")
    public EntityAnnotationMirrors(
            final List<AnnotationTree>  annotList,
            final String                signature,
            final EntityAnnotationData  ead
        )
    {
        // @LinkJavaSource is a @Repeatable Annotation ==> Keep Track of all of them
        ROArrayListBuilder<LJSMirror> ljsROALB = new ROArrayListBuilder<>();


        // @IntoHTMLTable is not *NOT* @Repeatable ==> it's an error to have more than one
        // This is declared as non-final, because it is also "Optional", and because it needs to 
        // be assigned to the actual final Instance-Field "ihtMirror".
        // 
        // NOTE: If you try to change this a third time, you are just goig to revert it back to the
        //       way it is right now...

        IHTMirror ihtMirror = null;

        TOP:
        for (AnnotationTree at : annotList)
        {
            Tree annotationType = at.getAnnotationType();

            // Say: "Thank-You ChatGPT"
            // That thing saved me a lot of time AND stress levels today.

            final String annotationClassName;

            if (annotationType instanceof IdentifierTree)
            {
                // This handles simple annotations, for instance: @SuppressWarnings and @Override
                // NOTE: Chat-GPT explained this whole thing - where there are 2 kinds of
                // "AnnotatinTypes" that are used by the Sun-Oracle Parser...
                // 
                // Unfortunately I forgot to write down what it said.  It was very right though !

                annotationClassName = ((IdentifierTree) annotationType).getName().toString();

                // System.out.println("\nIdentifierTree => annotationClassName: " + annotationClassName);
            }

            else if (annotationType instanceof MemberSelectTree)
            {
                // This handles fully qualified annotations like @java.lang.SuppressWarnings
                annotationClassName = ((MemberSelectTree) annotationType).getIdentifier().toString();

                // System.out.println("\nMemberSelectTree => annotationClassName: " + annotationClassName);
            }

            else
            {
                annotationClassName = null;

                throw (Error) Messager.assertFailOracleParser(
                    BRED + "My Annotation Parser just bit the dust" + RESET + '\n' +
                    "Extracted instance of 'com.sun.source.tree.AnnotationTree' was neither an " +
                    "instance of 'IdentifierTree' nor 'MemberSelectTree'.\n" +
                    "This is not supposed to be possible / This is unreachable code.\n" +
                    "The returned Type / Class-Name was:\n" +
                    BRED + annotationType.getClass().getCanonicalName() + RESET,
                    signature,
                    JDUAnnotations.EntityAnnotationMirrors
                );
            }

            switch (annotationClassName)
            {
                case "LinkJavaSource" :

                    final LJSMirror ljsm = new LJSMirror(
                        at.toString(),
                        at.getArguments(),
                        signature,
                        ead.unParsedLJSFilesList,
                        ead.allPkgLJSHandlesList
                    );

                    // Skip the Annotation completely if there were errors
                    if (MsgControl.hadErrors()) break TOP;

                    // System.out.println(at.toString() + '\n' + m.toString());
                    ljsROALB.add(ljsm);

                    break;


                case "IntoHTMLTable" :

                    if (ihtMirror != null) throw (Error) Messager.assertFailOracleParser(
                        "There appears to be a second instance of the @IntoHTMLTable " +
                            "Annotation.  However, this is not a @Repeatable Annotation." +
                        MsgPrintTools.annotationProcessingError(),
                        signature,
                        JDUAnnotations.EntityAnnotationMirrors
                    );


                    // The parsed arguments to to the Annotation
                    // final List<? extends ExpressionTree> arguments,
                    // 
                    // This is the Annotation, as a simple Java-String.  This is very useful for
                    // Error-Printing with the Messager.  That's the only purpose of it
                    // final String annotationAsStr,
                    //
                    // The Signature of the Detail-Entity onto which this "@LinkJavaSource"
                    // Annotation was placed.  Very important for error messages
                    // final String signature

                    ihtMirror = new IHTMirror(at.getArguments(), at.toString(), signature);

                    // Skip the Annotation completely if there were errors
                    if (MsgControl.hadErrors()) break TOP;

                    break;

                default: continue TOP;
            }
        }

        this.ljsMirrors = (MsgControl.hadErrors() || (ljsROALB.size() == 0))
            ? ReadOnlyArrayList.emptyROAL()
            : ljsROALB.build();

        this.ihtMirror = ihtMirror;
    }
};