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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package Torello.JavaDoc;


// *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
// Standard-Java Imports
// *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***

import java.io.IOException;
import java.sql.CallableStatement;
import java.util.Optional;


// *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
// Java-HTML Imports
// *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***

import Torello.Java.*;

import static Torello.JavaDoc.PF.*;

import Torello.Java.Additional.Ret4;

import Torello.JDUInternal.DataClasses.MainLoopData.CallableSignature;


// *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
// The new Source-Code Parser: com.sun.source.*
// *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***

import com.sun.source.tree.MethodTree;


/**
 * <B STYLE='color:darkred;'>Reflection Class:</B>
 * 
 * Holds all information extracted from <CODE>'&#46;java'</CODE> Source-Files about
 * Constructor's identified in that file.
 * 
 * <EMBED CLASS='external-html' DATA-FILE-ID=JPB_GET_INST>
 * <EMBED CLASS='external-html' DATA-FILE-ID=JPB_CONSTRUCTOR>
 * <EMBED CLASS='external-html' DATA-FILE-ID=JPB_DIAGRAM>
 */
@JDHeaderBackgroundImg(EmbedTagFileID={"REFLECTION_EXTENSION"})
public class Constructor extends Callable
    implements java.io.Serializable, Comparable<Constructor>, Cloneable
{
    /** <EMBED CLASS='external-html' DATA-FILE-ID=SVUID> */
    public static final long serialVersionUID = 1;


    // ********************************************************************************************
    // ********************************************************************************************
    // Reference-Hook: com.sun.source.tree
    // ********************************************************************************************
    // ********************************************************************************************


    /**
     * <EMBED CLASS='external-html' DATA-FILE-ID=SSTB_HOOK_FIELD>
     * 
     * If a user decides to make use of the native Sun/Oracle {@code MethodTree} instance that was
     * used to build this {@code Constructor} instance, it may be retrieved from this
     * {@code transient} field.
     * 
     * <BR /><BR /><B>NOTE:</B> The package {@code com.sun.source.tree} "reuses" or "overloads" the
     * {@code MethodTree} object such that it may represent either a Method, or a Constructor.
     * When a {@code MethodTree} instance is actually representing a constructor, the name that is
     * used for the instance will be {@code '<init>'}, rather than a method that abides by the 
     * standard Java naming conventions.
     */
    public final transient MethodTree methodTree;


    // ********************************************************************************************
    // ********************************************************************************************
    // Constructor - com.sun.source.tree 
    // ********************************************************************************************
    // ********************************************************************************************


    /**
     * <EMBED CLASS="defs" DATA-KIND=Constructor DATA-ENTITY=MethodTree>
     * <EMBED CLASS='external-html' DATA-FILE-ID=RC_DESCRIPTION>
     * @param mt <EMBED CLASS='external-html' DATA-FILE-ID=RC_PARAM_TREE>
     * @param util <EMBED CLASS='external-html' DATA-FILE-ID=RC_PARAM_UTIL>
     * @param constructorName The name of the type (which is also the name of the constructor) has
     * to be passed here, in order for this constructor to work.
     */
    public Constructor(MethodTree mt, TreeUtils util, String constructorName)
    {
        super(mt, constructorName, Entity.CONSTRUCTOR, util);

        // Only the 2 reference hooks are needed here
        this.methodTree = mt;
    }


    // ********************************************************************************************
    // ********************************************************************************************
    // Constructor: Used Internally by SignatureParse
    // ********************************************************************************************
    // ********************************************************************************************


    // Ensures that the Version with longer type-information strings is used.
    // Java Doc often uses longer type strings than is available from the source-code parse
    // Remember, JavaParser Symbol-Solver doesn't work well, and the Sun/Oracle Parser doesn't have
    // a linker at all.

    public Constructor(CallableSignature cSig, Constructor cFromSourceParser)
    {
        // Does the same thing as the loop statement below, but for the "parameterTypes"
        super(cSig, cFromSourceParser);

        this.methodTree = cFromSourceParser.methodTree;
    }

    // This is only used to construct a constructor for SYNTHETIC-CONSTRUCTORS.  At the present
    // moment, that means this is only used when there is a Zero-Argument Constructor that the user
    // did not actually type into his code, but rather is being auto-added by javac.

    public Constructor(CallableSignature cSig)
    {
        super(cSig, Entity.CONSTRUCTOR);

        this.methodTree = null;
    }


    // ********************************************************************************************
    // ********************************************************************************************
    // toString()
    // ********************************************************************************************
    // ********************************************************************************************


    /**
     * Generates a string of this constructor, with most information included.
     * 
     * <BR /><BR /><B>NOTE:</B> This will not return every piece of information contained by this
     * class. For example, both the constructor body, and any possible JavaDoc Comments are not
     * included.  For a more enhanced {@code toString()} version, call the one that accepts flags.
     * 
     * @return A printable string of this field.
     * 
     * @see PF
     * @see #toString(int)
     * @see StrCSV#toCSV(String[], boolean, boolean, Integer)
     */
    public String toString()
    {
        return
            "Name:            [" + name + "]\n" +
            "Signature:       [" + StrPrint.abbrevEndRDSF(signature, MAX_STR_LEN, true) + "]\n" +
            "Modifiers:       [" + StrCSV.toCSV(modifiers, true, true, null) + "]\n" +
            printedParameterNamesTS() +
            printedParameterTypesTS() +
            printedExceptionsTS() +

            // This will **NEVER** be null - unless 'this' instance was built from an HTML File,
            // rather than a source-code file.  Instances like that are only used temporarily, and
            // are garbage collected instantly.  Do this check anyway (just in case).

            "Location:        " + ((this.location == null)
                ? "null" 
                : ('[' + this.location.quickSummary() + ']'));
    }

    /**
     * This class expects a flags that has been masked using the constant ({@code public, static,
     * final int}) fields of class {@link PF}.  Please view this class for more information about
     * the flags available for modifying the return-value of this {@code toString()} method.
     * 
     * @param flags These are the {@code toString} flags from class PF ("Print Flags").  View
     * available flags listed in class {@link PF}.
     * 
     * @return A printable {@code String} of this {@code Constructor}, with comment information
     * included as well.
     * 
     * @see #toString()
     * @see StrCSV#toCSV(String[], boolean, boolean, Integer)
     * @see PF
     */
    public String toString(int flags)
    {
        boolean color = (flags & UNIX_COLORS) > 0;

        return
            printedName("Constructor", 20, color) + 
            printedSignature(20, color) +
            printedModifiers(20) +
            printedParamNames() + 
            printedParamTypes(jowFlags(flags)) +
            printedExceptions() +
            printedLocation(20, color, (flags & BRIEF_LOCATION) > 0) +

            // The previous method does not add a '\n' end to the end of the returned string
            // These are both optional, they add a '\n' AT THE BEGINNING if one of them is included

            printedComments(20, color, (flags & JAVADOC_COMMENTS) > 0) +
            printedCallableBody(flags);
    }


    // ********************************************************************************************
    // ********************************************************************************************
    //  CompareTo and Equals
    // ********************************************************************************************
    // ********************************************************************************************


    /**
     * Java's {@code interface Comparable<T>} requirements.  This looks at the number of
     * parameters, and parameter types in making a sort-decision.
     * 
     * @param c Any other {@code Constructor} to be compared to {@code 'this' Constructor}
     * 
     * @return An integer that fulfils Java's {@code interface Comparable<Constructor> public
     * boolean compareTo(Constructor c)} method requirements.
     */
    public int compareTo(Constructor c)
    {
        if (this == c) return 0;

        int ret = this.numParameters() - c.numParameters();
        if (ret != 0) return ret;

        if (this.parameterTypesJOW != null)

            for (int i=0; i < this.parameterTypesJOW.size(); i++)
            {
                ret = this.parameterTypesJOW.get(i).compareTo(c.parameterTypesJOW.get(i));
                if (ret != 0) return ret;
            }

        return 0;
    }

    /**
     * This <I>should be called an "atypical version" of</I> the usual
     * {@code equals(Object other)} constructor.  This version of equals merely compares the name
     * and parameters-list of the constructor.  The presumption here is that the definition of a
     * 'constructor' only has meaning - <I>at all</I> - inside the context of a {@code class} or
     * {@code enumerated-type} where that constructor is defined. Since inside any {@code '.java'}
     * source-code file, there may only be one constructor with a given parameter-list, this shall
     * return {@code TRUE} whenever the constructor being compared has the same parameter types as
     * {@code 'this'} does.
     * 
     * @param other This may be any other constructor.  It is <I><B>strongly suggested</B></I> that
     * this be a constructor defined in the same {@code '.java'} source-code file as 'this'
     * constructor.
     * 
     * @return This method returns {@code TRUE} when 'this' instance of Constructor has the same
     * parameter-list as {@code 'other'}.
     */
    public boolean equals(Constructor other)
    {
        // If the number of parameters in the 'other' instance of Constructor differ from the
        // number of parameters in 'this' Constructor, then return FALSE immediately.  It cannot be
        // a match

        if (this.numParameters() != other.numParameters()) return false;

        // If there are no parameters (for either of them), then return true immediately
        if (this.numParameters() == 0) return true;

        // If any of the parameter-names are different, break immediately and return false;
        for (int i=0; i < this.parameterNames.size(); i++)
            if (! this.parameterNames.get(i).equals(other.parameterNames.get(i)))
                return false;

        // If the parameter-types listed by the javadoc '.html' file differ from parameter-types
        // listed in the original '.java' source-code file, then break immediately.
        //
        // NOTE: The "package-information" for the FULL CLASS OR INTERFACE NAME is not always
        // available.

        for (int i=0; i < this.parameterTypes.size(); i++)
            if (! this.parameterTypesJOW.get(i).equals(other.parameterTypesJOW.get(i)))
                return false;

        // ALL TESTS PASSED
        return true;
    }
}