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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
/*
 * Copyright (c) 1997, 2008, Oracle and/or its affiliates. All rights reserved.
 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. 
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 */

package com.sun.codemodel.internal;

import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import com.sun.codemodel.internal.util.ClassNameComparator;

/**
 * Java method.
 */
public class JMethod extends JGenerifiableImpl implements JDeclaration, JAnnotatable {

    /**
     * Modifiers for this method
     */
    private JMods mods;

    /**
     * Return type for this method
     */
    private JType type = null;

    /**
     * Name of this method
     */
    private String name = null;

    /**
     * List of parameters for this method's declaration
     */
    private final List<JVar> params = new ArrayList<JVar>();

    /**
     * Set of exceptions that this method may throw.
     * A set instance lazily created.
     */
    private Set<JClass> _throws;

    /**
     * JBlock of statements that makes up the body this method
     */
    private JBlock body = null;

    private JDefinedClass outer;

    /**
     * javadoc comments for this JMethod
     */
    private JDocComment jdoc = null;

    /**
     * Variable parameter for this method's varargs declaration
     * introduced in J2SE 1.5
     */
    private JVar varParam = null;

    /**
     * Annotations on this variable. Lazily created.
     */
    private List<JAnnotationUse> annotations = null;


    private boolean isConstructor() {
        return type == null;
    }
    
    /** To set the default value for the
     *  annotation member
     */
    private JExpression defaultValue = null;
    

    /**
     * JMethod constructor
     *
     * @param mods
     *        Modifiers for this method's declaration
     *
     * @param type
     *        Return type for the method
     *
     * @param name
     *        Name of this method
     */
    JMethod(JDefinedClass outer, int mods, JType type, String name) {
        this.mods = JMods.forMethod(mods);
        this.type = type;
        this.name = name;
        this.outer = outer;
    }

    /**
     * Constructor constructor
     *
     * @param mods
     *        Modifiers for this constructor's declaration
     *
     * @param _class
     *        JClass containing this constructor
     */
    JMethod(int mods, JDefinedClass _class) {
        this.mods = JMods.forMethod(mods);
        this.type = null;
        this.name = _class.name();
        this.outer = _class;
    }
    
    private Set<JClass> getThrows() {
        if(_throws==null)
            _throws = new TreeSet<JClass>(ClassNameComparator.theInstance);
        return _throws;
    }

    /**
     * Add an exception to the list of exceptions that this
     * method may throw.
     *
     * @param exception
     *        Name of an exception that this method may throw
     */
    public JMethod _throws(JClass exception) {
        getThrows().add(exception);
        return this;
    }

    public JMethod _throws(Class exception) {
        return _throws(outer.owner().ref(exception));
    }

    /**
     * Add the specified variable to the list of parameters
     * for this method signature.
     *
     * @param type
     *        JType of the parameter being added
     *
     * @param name
     *        Name of the parameter being added
     *
     * @return New parameter variable
     */
    public JVar param(int mods, JType type, String name) {
        JVar v = new JVar(JMods.forVar(mods), type, name, null);
        params.add(v);
        return v;
    }

    public JVar param(JType type, String name) {
        return param(JMod.NONE, type, name);
    }

    public JVar param(int mods, Class type, String name) {
        return param(mods, outer.owner()._ref(type), name);
    }

    public JVar param(Class type, String name) {
        return param(outer.owner()._ref(type), name);
    }

    /**
     * @see #varParam(JType, String)
     */
    public JVar varParam(Class type, String name) {
        return varParam(outer.owner()._ref(type),name);
    }

    /**
     * Add the specified variable argument to the list of parameters
     * for this method signature.
     *
     * @param type
     *      Type of the parameter being added.
     *
     * @param name
     *        Name of the parameter being added
     *
     * @return the variable parameter
     * 
     * @throws IllegalStateException
     *      If this method is called twice.
     *      varargs in J2SE 1.5 can appear only once in the 
     *      method signature.
     */
    public JVar varParam(JType type, String name) {
        if (!hasVarArgs()) {

            varParam =
                new JVar(
                    JMods.forVar(JMod.NONE),
                    type.array(),
                    name,
                    null);
            return varParam;
        } else {
            throw new IllegalStateException(
                "Cannot have two varargs in a method,\n"
                    + "Check if varParam method of JMethod is"
                    + " invoked more than once");

        }

    }

    /**
     * Adds an annotation to this variable.
     * @param clazz
     *          The annotation class to annotate the field with
     */
    public JAnnotationUse annotate(JClass clazz){
        if(annotations==null)
           annotations = new ArrayList<JAnnotationUse>();
        JAnnotationUse a = new JAnnotationUse(clazz);
        annotations.add(a);
        return a;
    }

    /**
     * Adds an annotation to this variable.
     *
     * @param clazz
     *          The annotation class to annotate the field with
     */
    public JAnnotationUse annotate(Class <? extends Annotation> clazz){
        return annotate(owner().ref(clazz));
    }

    public <W extends JAnnotationWriter> W annotate2(Class<W> clazz) {
        return TypedAnnotationWriter.create(clazz,this);
    }

    /**
     * Check if there are any varargs declared
     * for this method signature.
     */
    public boolean hasVarArgs() {
        return this.varParam!=null;
    }

    public String name() {
        return name;
    }

    /**
     * Changes the name of the method.
     */
    public void name(String n) {
        this.name = n;
    }

    /**
     * Returns the return type.
     */
    public JType type() {
        return type;
    }

    /**
     * Overrides the return type.
     */
    public void type(JType t) {
        this.type = t;
    }

    /**
     * Returns all the parameter types in an array.
     * @return
     *      If there's no parameter, an empty array will be returned.
     */
    public JType[] listParamTypes() {
        JType[] r = new JType[params.size()];
        for (int i = 0; i < r.length; i++)
            r[i] = params.get(i).type();
        return r;
    }

    /**
     * Returns  the varags parameter type.
     * @return
     * If there's no vararg parameter type, null will be returned.
     */
    public JType listVarParamType() {
        if (varParam != null)
            return varParam.type();
        else
            return null;
    }

    /**
     * Returns all the parameters in an array.
     * @return
     *      If there's no parameter, an empty array will be returned.
     */
    public JVar[] listParams() {
        return params.toArray(new JVar[params.size()]);
    }

    /**
     * Returns the variable parameter
     * @return
     *      If there's no parameter, null will be returned.
     */
    public JVar listVarParam() {
        return varParam;
    }

    /**
     * Returns true if the method has the specified signature.
     */
    public boolean hasSignature(JType[] argTypes) {
        JVar[] p = listParams();
        if (p.length != argTypes.length)
            return false;

        for (int i = 0; i < p.length; i++)
            if (!p[i].type().equals(argTypes[i]))
                return false;

        return true;
    }

    /**
     * Get the block that makes up body of this method
     *
     * @return Body of method
     */
    public JBlock body() {
        if (body == null)
            body = new JBlock();
        return body;
    }
    
    /**
     * Specify the default value for this annotation member
     * @param value 
     *           Default value for the annotation member
     * 
     */
    public void declareDefaultValue(JExpression value){
        this.defaultValue = value;
    }

    /**
     * Creates, if necessary, and returns the class javadoc for this
     * JDefinedClass
     *
     * @return JDocComment containing javadocs for this class
     */
    public JDocComment javadoc() {
        if (jdoc == null)
            jdoc = new JDocComment(owner());
        return jdoc;
    }

    public void declare(JFormatter f) {
        if (jdoc != null)
            f.g(jdoc);

        if (annotations != null){
            for (JAnnotationUse a : annotations)
                f.g(a).nl();
        }

        f.g(mods);
        
        // declare the generics parameters
        super.declare(f);

        if (!isConstructor())
            f.g(type);
        f.id(name).p('(').i();
        // when parameters are printed in new lines, we want them to be indented.
        // there's a good chance no newlines happen, too, but just in case it does.
        boolean first = true;
        for (JVar var : params) {
            if (!first)
                f.p(',');
            if(var.isAnnotated())
                f.nl();
            f.b(var);
            first = false;
        }
        if (hasVarArgs()) {
            if (!first)
                f.p(',');
            f.g(varParam.type().elementType());
            f.p("... ");
            f.id(varParam.name());
        }

        f.o().p(')');
        if (_throws!=null && !_throws.isEmpty()) {
            f.nl().i().p("throws").g(_throws).nl().o();
        }
        
        if (defaultValue != null) {
            f.p("default ");
            f.g(defaultValue);
        }
        if (body != null) {
            f.s(body);
        } else if (
            !outer.isInterface() && !outer.isAnnotationTypeDeclaration() && !mods.isAbstract() && !mods.isNative()) {
            // Print an empty body for non-native, non-abstract methods
            f.s(new JBlock());
        } else {
            f.p(';').nl();
        }
    }

    /**
     * @return
     *      the current modifiers of this method.
     *      Always return non-null valid object.
     */
    public JMods mods() {
        return mods;
    }

    /**
     * @deprecated use {@link #mods()} 
     */
    public JMods getMods() {
        return mods;
    }

    protected JCodeModel owner() {
        return outer.owner();
    }
}
			
			

Browsed Source: [clear]