jaxws/src/share/classes/com/sun/codemodel/internal/JBlock.java
author asaha
Fri, 07 Aug 2009 11:27:00 -0700
changeset 3530 18fb7507984b
parent 2678 57cf2a1c1a05
permissions -rw-r--r--
6813167: 6u14 JAX-WS audit mutable static bugs 6803688: Integrate latest JAX-WS (2.1.6) in to JDK 6u14 Reviewed-by: darcy, ramap
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8
474761f14bca Initial load
duke
parents:
diff changeset
     1
/*
2678
57cf2a1c1a05 6831313: update jaxws in OpenJDK7 to 2.1 plus bug fixes from OpenJDK 6
tbell
parents: 8
diff changeset
     2
 * Copyright 2005-2006 Sun Microsystems, Inc.  All Rights Reserved.
8
474761f14bca Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
474761f14bca Initial load
duke
parents:
diff changeset
     4
 *
474761f14bca Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
474761f14bca Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
474761f14bca Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
474761f14bca Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
474761f14bca Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
474761f14bca Initial load
duke
parents:
diff changeset
    10
 *
474761f14bca Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
474761f14bca Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
474761f14bca Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
474761f14bca Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
474761f14bca Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
474761f14bca Initial load
duke
parents:
diff changeset
    16
 *
474761f14bca Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
474761f14bca Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
474761f14bca Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
474761f14bca Initial load
duke
parents:
diff changeset
    20
 *
474761f14bca Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
474761f14bca Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
474761f14bca Initial load
duke
parents:
diff changeset
    23
 * have any questions.
474761f14bca Initial load
duke
parents:
diff changeset
    24
 */
474761f14bca Initial load
duke
parents:
diff changeset
    25
474761f14bca Initial load
duke
parents:
diff changeset
    26
package com.sun.codemodel.internal;
474761f14bca Initial load
duke
parents:
diff changeset
    27
474761f14bca Initial load
duke
parents:
diff changeset
    28
import java.util.ArrayList;
474761f14bca Initial load
duke
parents:
diff changeset
    29
import java.util.List;
474761f14bca Initial load
duke
parents:
diff changeset
    30
import java.util.Collections;
474761f14bca Initial load
duke
parents:
diff changeset
    31
474761f14bca Initial load
duke
parents:
diff changeset
    32
/**
474761f14bca Initial load
duke
parents:
diff changeset
    33
 * A block of Java code, which may contain statements and local declarations.
474761f14bca Initial load
duke
parents:
diff changeset
    34
 *
474761f14bca Initial load
duke
parents:
diff changeset
    35
 * <p>
474761f14bca Initial load
duke
parents:
diff changeset
    36
 * {@link JBlock} contains a large number of factory methods that creates new
474761f14bca Initial load
duke
parents:
diff changeset
    37
 * statements/declarations. Those newly created statements/declarations are
474761f14bca Initial load
duke
parents:
diff changeset
    38
 * inserted into the {@link #pos() "current position"}. The position advances
474761f14bca Initial load
duke
parents:
diff changeset
    39
 * one every time you add a new instruction.
474761f14bca Initial load
duke
parents:
diff changeset
    40
 */
474761f14bca Initial load
duke
parents:
diff changeset
    41
public final class JBlock implements JGenerable, JStatement {
474761f14bca Initial load
duke
parents:
diff changeset
    42
474761f14bca Initial load
duke
parents:
diff changeset
    43
    /**
474761f14bca Initial load
duke
parents:
diff changeset
    44
     * Declarations and statements contained in this block.
474761f14bca Initial load
duke
parents:
diff changeset
    45
     * Either {@link JStatement} or {@link JDeclaration}.
474761f14bca Initial load
duke
parents:
diff changeset
    46
     */
474761f14bca Initial load
duke
parents:
diff changeset
    47
    private final List<Object> content = new ArrayList<Object>();
474761f14bca Initial load
duke
parents:
diff changeset
    48
474761f14bca Initial load
duke
parents:
diff changeset
    49
    /**
474761f14bca Initial load
duke
parents:
diff changeset
    50
     * Whether or not this block must be braced and indented
474761f14bca Initial load
duke
parents:
diff changeset
    51
     */
474761f14bca Initial load
duke
parents:
diff changeset
    52
    private boolean bracesRequired = true;
474761f14bca Initial load
duke
parents:
diff changeset
    53
    private boolean indentRequired = true;
474761f14bca Initial load
duke
parents:
diff changeset
    54
474761f14bca Initial load
duke
parents:
diff changeset
    55
    /**
474761f14bca Initial load
duke
parents:
diff changeset
    56
     * Current position.
474761f14bca Initial load
duke
parents:
diff changeset
    57
     */
474761f14bca Initial load
duke
parents:
diff changeset
    58
    private int pos;
474761f14bca Initial load
duke
parents:
diff changeset
    59
474761f14bca Initial load
duke
parents:
diff changeset
    60
    public JBlock() {
474761f14bca Initial load
duke
parents:
diff changeset
    61
        this(true,true);
474761f14bca Initial load
duke
parents:
diff changeset
    62
    }
474761f14bca Initial load
duke
parents:
diff changeset
    63
474761f14bca Initial load
duke
parents:
diff changeset
    64
    public JBlock(boolean bracesRequired, boolean indentRequired) {
474761f14bca Initial load
duke
parents:
diff changeset
    65
        this.bracesRequired = bracesRequired;
474761f14bca Initial load
duke
parents:
diff changeset
    66
        this.indentRequired = indentRequired;
474761f14bca Initial load
duke
parents:
diff changeset
    67
    }
474761f14bca Initial load
duke
parents:
diff changeset
    68
474761f14bca Initial load
duke
parents:
diff changeset
    69
    /**
474761f14bca Initial load
duke
parents:
diff changeset
    70
     * Returns a read-only view of {@link JStatement}s and {@link JDeclaration}
474761f14bca Initial load
duke
parents:
diff changeset
    71
     * in this block.
474761f14bca Initial load
duke
parents:
diff changeset
    72
     */
474761f14bca Initial load
duke
parents:
diff changeset
    73
    public List<Object> getContents() {
474761f14bca Initial load
duke
parents:
diff changeset
    74
        return Collections.unmodifiableList(content);
474761f14bca Initial load
duke
parents:
diff changeset
    75
    }
474761f14bca Initial load
duke
parents:
diff changeset
    76
474761f14bca Initial load
duke
parents:
diff changeset
    77
    private <T> T insert( T statementOrDeclaration ) {
474761f14bca Initial load
duke
parents:
diff changeset
    78
        content.add(pos,statementOrDeclaration);
474761f14bca Initial load
duke
parents:
diff changeset
    79
        pos++;
474761f14bca Initial load
duke
parents:
diff changeset
    80
        return statementOrDeclaration;
474761f14bca Initial load
duke
parents:
diff changeset
    81
    }
474761f14bca Initial load
duke
parents:
diff changeset
    82
474761f14bca Initial load
duke
parents:
diff changeset
    83
    /**
474761f14bca Initial load
duke
parents:
diff changeset
    84
     * Gets the current position to which new statements will be inserted.
474761f14bca Initial load
duke
parents:
diff changeset
    85
     *
474761f14bca Initial load
duke
parents:
diff changeset
    86
     * For example if the value is 0, newly created instructions will be
474761f14bca Initial load
duke
parents:
diff changeset
    87
     * inserted at the very beginning of the block.
474761f14bca Initial load
duke
parents:
diff changeset
    88
     *
474761f14bca Initial load
duke
parents:
diff changeset
    89
     * @see #pos(int)
474761f14bca Initial load
duke
parents:
diff changeset
    90
     */
474761f14bca Initial load
duke
parents:
diff changeset
    91
    public int pos() {
474761f14bca Initial load
duke
parents:
diff changeset
    92
        return pos;
474761f14bca Initial load
duke
parents:
diff changeset
    93
    }
474761f14bca Initial load
duke
parents:
diff changeset
    94
474761f14bca Initial load
duke
parents:
diff changeset
    95
    /**
474761f14bca Initial load
duke
parents:
diff changeset
    96
     * Sets the current position.
474761f14bca Initial load
duke
parents:
diff changeset
    97
     *
474761f14bca Initial load
duke
parents:
diff changeset
    98
     * @return
474761f14bca Initial load
duke
parents:
diff changeset
    99
     *      the old value of the current position.
474761f14bca Initial load
duke
parents:
diff changeset
   100
     * @throws IllegalArgumentException
474761f14bca Initial load
duke
parents:
diff changeset
   101
     *      if the new position value is illegal.
474761f14bca Initial load
duke
parents:
diff changeset
   102
     *
474761f14bca Initial load
duke
parents:
diff changeset
   103
     * @see #pos()
474761f14bca Initial load
duke
parents:
diff changeset
   104
     */
474761f14bca Initial load
duke
parents:
diff changeset
   105
    public int pos(int newPos) {
474761f14bca Initial load
duke
parents:
diff changeset
   106
        int r = pos;
474761f14bca Initial load
duke
parents:
diff changeset
   107
        if(newPos>content.size() || newPos<0)
474761f14bca Initial load
duke
parents:
diff changeset
   108
            throw new IllegalArgumentException();
474761f14bca Initial load
duke
parents:
diff changeset
   109
        pos = newPos;
474761f14bca Initial load
duke
parents:
diff changeset
   110
474761f14bca Initial load
duke
parents:
diff changeset
   111
        return r;
474761f14bca Initial load
duke
parents:
diff changeset
   112
    }
474761f14bca Initial load
duke
parents:
diff changeset
   113
3530
18fb7507984b 6813167: 6u14 JAX-WS audit mutable static bugs
asaha
parents: 2678
diff changeset
   114
    /**
18fb7507984b 6813167: 6u14 JAX-WS audit mutable static bugs
asaha
parents: 2678
diff changeset
   115
     * Returns true if this block is empty and does not contain
18fb7507984b 6813167: 6u14 JAX-WS audit mutable static bugs
asaha
parents: 2678
diff changeset
   116
     * any statement.
18fb7507984b 6813167: 6u14 JAX-WS audit mutable static bugs
asaha
parents: 2678
diff changeset
   117
     */
18fb7507984b 6813167: 6u14 JAX-WS audit mutable static bugs
asaha
parents: 2678
diff changeset
   118
    public boolean isEmpty() {
18fb7507984b 6813167: 6u14 JAX-WS audit mutable static bugs
asaha
parents: 2678
diff changeset
   119
        return content.isEmpty();
18fb7507984b 6813167: 6u14 JAX-WS audit mutable static bugs
asaha
parents: 2678
diff changeset
   120
    }
18fb7507984b 6813167: 6u14 JAX-WS audit mutable static bugs
asaha
parents: 2678
diff changeset
   121
8
474761f14bca Initial load
duke
parents:
diff changeset
   122
474761f14bca Initial load
duke
parents:
diff changeset
   123
    /**
474761f14bca Initial load
duke
parents:
diff changeset
   124
     * Adds a local variable declaration to this block
474761f14bca Initial load
duke
parents:
diff changeset
   125
     *
474761f14bca Initial load
duke
parents:
diff changeset
   126
     * @param type
474761f14bca Initial load
duke
parents:
diff changeset
   127
     *        JType of the variable
474761f14bca Initial load
duke
parents:
diff changeset
   128
     *
474761f14bca Initial load
duke
parents:
diff changeset
   129
     * @param name
474761f14bca Initial load
duke
parents:
diff changeset
   130
     *        Name of the variable
474761f14bca Initial load
duke
parents:
diff changeset
   131
     *
474761f14bca Initial load
duke
parents:
diff changeset
   132
     * @return Newly generated JVar
474761f14bca Initial load
duke
parents:
diff changeset
   133
     */
474761f14bca Initial load
duke
parents:
diff changeset
   134
    public JVar decl(JType type, String name) {
474761f14bca Initial load
duke
parents:
diff changeset
   135
        return decl(JMod.NONE, type, name, null);
474761f14bca Initial load
duke
parents:
diff changeset
   136
    }
474761f14bca Initial load
duke
parents:
diff changeset
   137
474761f14bca Initial load
duke
parents:
diff changeset
   138
    /**
474761f14bca Initial load
duke
parents:
diff changeset
   139
     * Adds a local variable declaration to this block
474761f14bca Initial load
duke
parents:
diff changeset
   140
     *
474761f14bca Initial load
duke
parents:
diff changeset
   141
     * @param type
474761f14bca Initial load
duke
parents:
diff changeset
   142
     *        JType of the variable
474761f14bca Initial load
duke
parents:
diff changeset
   143
     *
474761f14bca Initial load
duke
parents:
diff changeset
   144
     * @param name
474761f14bca Initial load
duke
parents:
diff changeset
   145
     *        Name of the variable
474761f14bca Initial load
duke
parents:
diff changeset
   146
     *
474761f14bca Initial load
duke
parents:
diff changeset
   147
     * @param init
474761f14bca Initial load
duke
parents:
diff changeset
   148
     *        Initialization expression for this variable.  May be null.
474761f14bca Initial load
duke
parents:
diff changeset
   149
     *
474761f14bca Initial load
duke
parents:
diff changeset
   150
     * @return Newly generated JVar
474761f14bca Initial load
duke
parents:
diff changeset
   151
     */
474761f14bca Initial load
duke
parents:
diff changeset
   152
    public JVar decl(JType type, String name, JExpression init) {
474761f14bca Initial load
duke
parents:
diff changeset
   153
        return decl(JMod.NONE, type, name, init);
474761f14bca Initial load
duke
parents:
diff changeset
   154
    }
474761f14bca Initial load
duke
parents:
diff changeset
   155
474761f14bca Initial load
duke
parents:
diff changeset
   156
    /**
474761f14bca Initial load
duke
parents:
diff changeset
   157
     * Adds a local variable declaration to this block
474761f14bca Initial load
duke
parents:
diff changeset
   158
     *
474761f14bca Initial load
duke
parents:
diff changeset
   159
     * @param mods
474761f14bca Initial load
duke
parents:
diff changeset
   160
     *        Modifiers for the variable
474761f14bca Initial load
duke
parents:
diff changeset
   161
     *
474761f14bca Initial load
duke
parents:
diff changeset
   162
     * @param type
474761f14bca Initial load
duke
parents:
diff changeset
   163
     *        JType of the variable
474761f14bca Initial load
duke
parents:
diff changeset
   164
     *
474761f14bca Initial load
duke
parents:
diff changeset
   165
     * @param name
474761f14bca Initial load
duke
parents:
diff changeset
   166
     *        Name of the variable
474761f14bca Initial load
duke
parents:
diff changeset
   167
     *
474761f14bca Initial load
duke
parents:
diff changeset
   168
     * @param init
474761f14bca Initial load
duke
parents:
diff changeset
   169
     *        Initialization expression for this variable.  May be null.
474761f14bca Initial load
duke
parents:
diff changeset
   170
     *
474761f14bca Initial load
duke
parents:
diff changeset
   171
     * @return Newly generated JVar
474761f14bca Initial load
duke
parents:
diff changeset
   172
     */
474761f14bca Initial load
duke
parents:
diff changeset
   173
    public JVar decl(int mods, JType type, String name, JExpression init) {
474761f14bca Initial load
duke
parents:
diff changeset
   174
        JVar v = new JVar(JMods.forVar(mods), type, name, init);
474761f14bca Initial load
duke
parents:
diff changeset
   175
        insert(v);
474761f14bca Initial load
duke
parents:
diff changeset
   176
        bracesRequired = true;
474761f14bca Initial load
duke
parents:
diff changeset
   177
        indentRequired = true;
474761f14bca Initial load
duke
parents:
diff changeset
   178
        return v;
474761f14bca Initial load
duke
parents:
diff changeset
   179
    }
474761f14bca Initial load
duke
parents:
diff changeset
   180
474761f14bca Initial load
duke
parents:
diff changeset
   181
    /**
474761f14bca Initial load
duke
parents:
diff changeset
   182
     * Creates an assignment statement and adds it to this block.
474761f14bca Initial load
duke
parents:
diff changeset
   183
     *
474761f14bca Initial load
duke
parents:
diff changeset
   184
     * @param lhs
474761f14bca Initial load
duke
parents:
diff changeset
   185
     *        Assignable variable or field for left hand side of expression
474761f14bca Initial load
duke
parents:
diff changeset
   186
     *
474761f14bca Initial load
duke
parents:
diff changeset
   187
     * @param exp
474761f14bca Initial load
duke
parents:
diff changeset
   188
     *        Right hand side expression
474761f14bca Initial load
duke
parents:
diff changeset
   189
     */
474761f14bca Initial load
duke
parents:
diff changeset
   190
    public JBlock assign(JAssignmentTarget lhs, JExpression exp) {
474761f14bca Initial load
duke
parents:
diff changeset
   191
        insert(new JAssignment(lhs, exp));
474761f14bca Initial load
duke
parents:
diff changeset
   192
        return this;
474761f14bca Initial load
duke
parents:
diff changeset
   193
    }
474761f14bca Initial load
duke
parents:
diff changeset
   194
474761f14bca Initial load
duke
parents:
diff changeset
   195
    public JBlock assignPlus(JAssignmentTarget lhs, JExpression exp) {
474761f14bca Initial load
duke
parents:
diff changeset
   196
        insert(new JAssignment(lhs, exp, "+"));
474761f14bca Initial load
duke
parents:
diff changeset
   197
        return this;
474761f14bca Initial load
duke
parents:
diff changeset
   198
    }
474761f14bca Initial load
duke
parents:
diff changeset
   199
474761f14bca Initial load
duke
parents:
diff changeset
   200
    /**
474761f14bca Initial load
duke
parents:
diff changeset
   201
     * Creates an invocation statement and adds it to this block.
474761f14bca Initial load
duke
parents:
diff changeset
   202
     *
474761f14bca Initial load
duke
parents:
diff changeset
   203
     * @param expr
474761f14bca Initial load
duke
parents:
diff changeset
   204
     *        JExpression evaluating to the class or object upon which
474761f14bca Initial load
duke
parents:
diff changeset
   205
     *        the named method will be invoked
474761f14bca Initial load
duke
parents:
diff changeset
   206
     *
474761f14bca Initial load
duke
parents:
diff changeset
   207
     * @param method
474761f14bca Initial load
duke
parents:
diff changeset
   208
     *        Name of method to invoke
474761f14bca Initial load
duke
parents:
diff changeset
   209
     *
474761f14bca Initial load
duke
parents:
diff changeset
   210
     * @return Newly generated JInvocation
474761f14bca Initial load
duke
parents:
diff changeset
   211
     */
474761f14bca Initial load
duke
parents:
diff changeset
   212
    public JInvocation invoke(JExpression expr, String method) {
474761f14bca Initial load
duke
parents:
diff changeset
   213
        JInvocation i = new JInvocation(expr, method);
474761f14bca Initial load
duke
parents:
diff changeset
   214
        insert(i);
474761f14bca Initial load
duke
parents:
diff changeset
   215
        return i;
474761f14bca Initial load
duke
parents:
diff changeset
   216
    }
474761f14bca Initial load
duke
parents:
diff changeset
   217
474761f14bca Initial load
duke
parents:
diff changeset
   218
    /**
474761f14bca Initial load
duke
parents:
diff changeset
   219
     * Creates an invocation statement and adds it to this block.
474761f14bca Initial load
duke
parents:
diff changeset
   220
     *
474761f14bca Initial load
duke
parents:
diff changeset
   221
     * @param expr
474761f14bca Initial load
duke
parents:
diff changeset
   222
     *        JExpression evaluating to the class or object upon which
474761f14bca Initial load
duke
parents:
diff changeset
   223
     *        the method will be invoked
474761f14bca Initial load
duke
parents:
diff changeset
   224
     *
474761f14bca Initial load
duke
parents:
diff changeset
   225
     * @param method
474761f14bca Initial load
duke
parents:
diff changeset
   226
     *        JMethod to invoke
474761f14bca Initial load
duke
parents:
diff changeset
   227
     *
474761f14bca Initial load
duke
parents:
diff changeset
   228
     * @return Newly generated JInvocation
474761f14bca Initial load
duke
parents:
diff changeset
   229
     */
474761f14bca Initial load
duke
parents:
diff changeset
   230
    public JInvocation invoke(JExpression expr, JMethod method) {
2678
57cf2a1c1a05 6831313: update jaxws in OpenJDK7 to 2.1 plus bug fixes from OpenJDK 6
tbell
parents: 8
diff changeset
   231
        return insert(new JInvocation(expr, method));
8
474761f14bca Initial load
duke
parents:
diff changeset
   232
    }
474761f14bca Initial load
duke
parents:
diff changeset
   233
474761f14bca Initial load
duke
parents:
diff changeset
   234
    /**
474761f14bca Initial load
duke
parents:
diff changeset
   235
     * Creates a static invocation statement.
474761f14bca Initial load
duke
parents:
diff changeset
   236
     */
474761f14bca Initial load
duke
parents:
diff changeset
   237
    public JInvocation staticInvoke(JClass type, String method) {
2678
57cf2a1c1a05 6831313: update jaxws in OpenJDK7 to 2.1 plus bug fixes from OpenJDK 6
tbell
parents: 8
diff changeset
   238
        return insert(new JInvocation(type, method));
8
474761f14bca Initial load
duke
parents:
diff changeset
   239
    }
474761f14bca Initial load
duke
parents:
diff changeset
   240
474761f14bca Initial load
duke
parents:
diff changeset
   241
    /**
474761f14bca Initial load
duke
parents:
diff changeset
   242
     * Creates an invocation statement and adds it to this block.
474761f14bca Initial load
duke
parents:
diff changeset
   243
     *
474761f14bca Initial load
duke
parents:
diff changeset
   244
     * @param method
474761f14bca Initial load
duke
parents:
diff changeset
   245
     *        Name of method to invoke
474761f14bca Initial load
duke
parents:
diff changeset
   246
     *
474761f14bca Initial load
duke
parents:
diff changeset
   247
     * @return Newly generated JInvocation
474761f14bca Initial load
duke
parents:
diff changeset
   248
     */
474761f14bca Initial load
duke
parents:
diff changeset
   249
    public JInvocation invoke(String method) {
2678
57cf2a1c1a05 6831313: update jaxws in OpenJDK7 to 2.1 plus bug fixes from OpenJDK 6
tbell
parents: 8
diff changeset
   250
        return insert(new JInvocation((JExpression)null, method));
8
474761f14bca Initial load
duke
parents:
diff changeset
   251
    }
474761f14bca Initial load
duke
parents:
diff changeset
   252
474761f14bca Initial load
duke
parents:
diff changeset
   253
    /**
474761f14bca Initial load
duke
parents:
diff changeset
   254
     * Creates an invocation statement and adds it to this block.
474761f14bca Initial load
duke
parents:
diff changeset
   255
     *
474761f14bca Initial load
duke
parents:
diff changeset
   256
     * @param method
474761f14bca Initial load
duke
parents:
diff changeset
   257
     *        JMethod to invoke
474761f14bca Initial load
duke
parents:
diff changeset
   258
     *
474761f14bca Initial load
duke
parents:
diff changeset
   259
     * @return Newly generated JInvocation
474761f14bca Initial load
duke
parents:
diff changeset
   260
     */
474761f14bca Initial load
duke
parents:
diff changeset
   261
    public JInvocation invoke(JMethod method) {
2678
57cf2a1c1a05 6831313: update jaxws in OpenJDK7 to 2.1 plus bug fixes from OpenJDK 6
tbell
parents: 8
diff changeset
   262
        return insert(new JInvocation((JExpression)null, method));
8
474761f14bca Initial load
duke
parents:
diff changeset
   263
    }
474761f14bca Initial load
duke
parents:
diff changeset
   264
474761f14bca Initial load
duke
parents:
diff changeset
   265
    /**
474761f14bca Initial load
duke
parents:
diff changeset
   266
     * Adds a statement to this block
474761f14bca Initial load
duke
parents:
diff changeset
   267
     *
474761f14bca Initial load
duke
parents:
diff changeset
   268
     * @param s
474761f14bca Initial load
duke
parents:
diff changeset
   269
     *        JStatement to be added
474761f14bca Initial load
duke
parents:
diff changeset
   270
     *
474761f14bca Initial load
duke
parents:
diff changeset
   271
     * @return This block
474761f14bca Initial load
duke
parents:
diff changeset
   272
     */
474761f14bca Initial load
duke
parents:
diff changeset
   273
    public JBlock add(JStatement s) { // ## Needed?
474761f14bca Initial load
duke
parents:
diff changeset
   274
        insert(s);
474761f14bca Initial load
duke
parents:
diff changeset
   275
        return this;
474761f14bca Initial load
duke
parents:
diff changeset
   276
    }
474761f14bca Initial load
duke
parents:
diff changeset
   277
474761f14bca Initial load
duke
parents:
diff changeset
   278
    /**
474761f14bca Initial load
duke
parents:
diff changeset
   279
     * Create an If statement and add it to this block
474761f14bca Initial load
duke
parents:
diff changeset
   280
     *
474761f14bca Initial load
duke
parents:
diff changeset
   281
     * @param expr
474761f14bca Initial load
duke
parents:
diff changeset
   282
     *        JExpression to be tested to determine branching
474761f14bca Initial load
duke
parents:
diff changeset
   283
     *
474761f14bca Initial load
duke
parents:
diff changeset
   284
     * @return Newly generated conditional statement
474761f14bca Initial load
duke
parents:
diff changeset
   285
     */
474761f14bca Initial load
duke
parents:
diff changeset
   286
    public JConditional _if(JExpression expr) {
474761f14bca Initial load
duke
parents:
diff changeset
   287
        return insert(new JConditional(expr));
474761f14bca Initial load
duke
parents:
diff changeset
   288
    }
474761f14bca Initial load
duke
parents:
diff changeset
   289
474761f14bca Initial load
duke
parents:
diff changeset
   290
    /**
474761f14bca Initial load
duke
parents:
diff changeset
   291
     * Create a For statement and add it to this block
474761f14bca Initial load
duke
parents:
diff changeset
   292
     *
474761f14bca Initial load
duke
parents:
diff changeset
   293
     * @return Newly generated For statement
474761f14bca Initial load
duke
parents:
diff changeset
   294
     */
474761f14bca Initial load
duke
parents:
diff changeset
   295
    public JForLoop _for() {
474761f14bca Initial load
duke
parents:
diff changeset
   296
        return insert(new JForLoop());
474761f14bca Initial load
duke
parents:
diff changeset
   297
    }
474761f14bca Initial load
duke
parents:
diff changeset
   298
474761f14bca Initial load
duke
parents:
diff changeset
   299
    /**
474761f14bca Initial load
duke
parents:
diff changeset
   300
     * Create a While statement and add it to this block
474761f14bca Initial load
duke
parents:
diff changeset
   301
     *
474761f14bca Initial load
duke
parents:
diff changeset
   302
     * @return Newly generated While statement
474761f14bca Initial load
duke
parents:
diff changeset
   303
     */
474761f14bca Initial load
duke
parents:
diff changeset
   304
    public JWhileLoop _while(JExpression test) {
474761f14bca Initial load
duke
parents:
diff changeset
   305
        return insert(new JWhileLoop(test));
474761f14bca Initial load
duke
parents:
diff changeset
   306
    }
474761f14bca Initial load
duke
parents:
diff changeset
   307
474761f14bca Initial load
duke
parents:
diff changeset
   308
    /**
474761f14bca Initial load
duke
parents:
diff changeset
   309
     * Create a switch/case statement and add it to this block
474761f14bca Initial load
duke
parents:
diff changeset
   310
     */
474761f14bca Initial load
duke
parents:
diff changeset
   311
    public JSwitch _switch(JExpression test) {
474761f14bca Initial load
duke
parents:
diff changeset
   312
        return insert(new JSwitch(test));
474761f14bca Initial load
duke
parents:
diff changeset
   313
    }
474761f14bca Initial load
duke
parents:
diff changeset
   314
474761f14bca Initial load
duke
parents:
diff changeset
   315
    /**
474761f14bca Initial load
duke
parents:
diff changeset
   316
     * Create a Do statement and add it to this block
474761f14bca Initial load
duke
parents:
diff changeset
   317
     *
474761f14bca Initial load
duke
parents:
diff changeset
   318
     * @return Newly generated Do statement
474761f14bca Initial load
duke
parents:
diff changeset
   319
     */
474761f14bca Initial load
duke
parents:
diff changeset
   320
    public JDoLoop _do(JExpression test) {
474761f14bca Initial load
duke
parents:
diff changeset
   321
        return insert(new JDoLoop(test));
474761f14bca Initial load
duke
parents:
diff changeset
   322
    }
474761f14bca Initial load
duke
parents:
diff changeset
   323
474761f14bca Initial load
duke
parents:
diff changeset
   324
    /**
474761f14bca Initial load
duke
parents:
diff changeset
   325
     * Create a Try statement and add it to this block
474761f14bca Initial load
duke
parents:
diff changeset
   326
     *
474761f14bca Initial load
duke
parents:
diff changeset
   327
     * @return Newly generated Try statement
474761f14bca Initial load
duke
parents:
diff changeset
   328
     */
474761f14bca Initial load
duke
parents:
diff changeset
   329
    public JTryBlock _try() {
474761f14bca Initial load
duke
parents:
diff changeset
   330
        return insert(new JTryBlock());
474761f14bca Initial load
duke
parents:
diff changeset
   331
    }
474761f14bca Initial load
duke
parents:
diff changeset
   332
474761f14bca Initial load
duke
parents:
diff changeset
   333
    /**
474761f14bca Initial load
duke
parents:
diff changeset
   334
     * Create a return statement and add it to this block
474761f14bca Initial load
duke
parents:
diff changeset
   335
     */
474761f14bca Initial load
duke
parents:
diff changeset
   336
    public void _return() {
474761f14bca Initial load
duke
parents:
diff changeset
   337
        insert(new JReturn(null));
474761f14bca Initial load
duke
parents:
diff changeset
   338
    }
474761f14bca Initial load
duke
parents:
diff changeset
   339
474761f14bca Initial load
duke
parents:
diff changeset
   340
    /**
474761f14bca Initial load
duke
parents:
diff changeset
   341
     * Create a return statement and add it to this block
474761f14bca Initial load
duke
parents:
diff changeset
   342
     */
474761f14bca Initial load
duke
parents:
diff changeset
   343
    public void _return(JExpression exp) {
474761f14bca Initial load
duke
parents:
diff changeset
   344
        insert(new JReturn(exp));
474761f14bca Initial load
duke
parents:
diff changeset
   345
    }
474761f14bca Initial load
duke
parents:
diff changeset
   346
474761f14bca Initial load
duke
parents:
diff changeset
   347
    /**
474761f14bca Initial load
duke
parents:
diff changeset
   348
     * Create a throw statement and add it to this block
474761f14bca Initial load
duke
parents:
diff changeset
   349
     */
474761f14bca Initial load
duke
parents:
diff changeset
   350
    public void _throw(JExpression exp) {
474761f14bca Initial load
duke
parents:
diff changeset
   351
        insert(new JThrow(exp));
474761f14bca Initial load
duke
parents:
diff changeset
   352
    }
474761f14bca Initial load
duke
parents:
diff changeset
   353
474761f14bca Initial load
duke
parents:
diff changeset
   354
    /**
474761f14bca Initial load
duke
parents:
diff changeset
   355
     * Create a break statement and add it to this block
474761f14bca Initial load
duke
parents:
diff changeset
   356
     */
474761f14bca Initial load
duke
parents:
diff changeset
   357
    public void _break() {
474761f14bca Initial load
duke
parents:
diff changeset
   358
        _break(null);
474761f14bca Initial load
duke
parents:
diff changeset
   359
    }
474761f14bca Initial load
duke
parents:
diff changeset
   360
474761f14bca Initial load
duke
parents:
diff changeset
   361
    public void _break(JLabel label) {
474761f14bca Initial load
duke
parents:
diff changeset
   362
        insert(new JBreak(label));
474761f14bca Initial load
duke
parents:
diff changeset
   363
    }
474761f14bca Initial load
duke
parents:
diff changeset
   364
474761f14bca Initial load
duke
parents:
diff changeset
   365
    /**
474761f14bca Initial load
duke
parents:
diff changeset
   366
     * Create a label, which can be referenced from
474761f14bca Initial load
duke
parents:
diff changeset
   367
     * <code>continue</code> and <code>break</code> statements.
474761f14bca Initial load
duke
parents:
diff changeset
   368
     */
474761f14bca Initial load
duke
parents:
diff changeset
   369
    public JLabel label(String name) {
474761f14bca Initial load
duke
parents:
diff changeset
   370
        JLabel l = new JLabel(name);
474761f14bca Initial load
duke
parents:
diff changeset
   371
        insert(l);
474761f14bca Initial load
duke
parents:
diff changeset
   372
        return l;
474761f14bca Initial load
duke
parents:
diff changeset
   373
    }
474761f14bca Initial load
duke
parents:
diff changeset
   374
474761f14bca Initial load
duke
parents:
diff changeset
   375
    /**
474761f14bca Initial load
duke
parents:
diff changeset
   376
     * Create a continue statement and add it to this block
474761f14bca Initial load
duke
parents:
diff changeset
   377
     */
474761f14bca Initial load
duke
parents:
diff changeset
   378
    public void _continue(JLabel label) {
474761f14bca Initial load
duke
parents:
diff changeset
   379
        insert(new JContinue(label));
474761f14bca Initial load
duke
parents:
diff changeset
   380
    }
474761f14bca Initial load
duke
parents:
diff changeset
   381
474761f14bca Initial load
duke
parents:
diff changeset
   382
    public void _continue() {
474761f14bca Initial load
duke
parents:
diff changeset
   383
        _continue(null);
474761f14bca Initial load
duke
parents:
diff changeset
   384
    }
474761f14bca Initial load
duke
parents:
diff changeset
   385
474761f14bca Initial load
duke
parents:
diff changeset
   386
    /**
474761f14bca Initial load
duke
parents:
diff changeset
   387
     * Create a sub-block and add it to this block
474761f14bca Initial load
duke
parents:
diff changeset
   388
     */
474761f14bca Initial load
duke
parents:
diff changeset
   389
    public JBlock block() {
474761f14bca Initial load
duke
parents:
diff changeset
   390
        JBlock b = new JBlock();
474761f14bca Initial load
duke
parents:
diff changeset
   391
        b.bracesRequired = false;
474761f14bca Initial load
duke
parents:
diff changeset
   392
        b.indentRequired = false;
474761f14bca Initial load
duke
parents:
diff changeset
   393
        return insert(b);
474761f14bca Initial load
duke
parents:
diff changeset
   394
    }
474761f14bca Initial load
duke
parents:
diff changeset
   395
474761f14bca Initial load
duke
parents:
diff changeset
   396
    /**
474761f14bca Initial load
duke
parents:
diff changeset
   397
     * Creates a "literal" statement directly.
474761f14bca Initial load
duke
parents:
diff changeset
   398
     *
474761f14bca Initial load
duke
parents:
diff changeset
   399
     * <p>
474761f14bca Initial load
duke
parents:
diff changeset
   400
     * Specified string is printed as-is.
474761f14bca Initial load
duke
parents:
diff changeset
   401
     * This is useful as a short-cut.
474761f14bca Initial load
duke
parents:
diff changeset
   402
     *
474761f14bca Initial load
duke
parents:
diff changeset
   403
     * <p>
474761f14bca Initial load
duke
parents:
diff changeset
   404
     * For example, you can invoke this method as:
474761f14bca Initial load
duke
parents:
diff changeset
   405
     * <code>directStatement("a=b+c;")</code>.
474761f14bca Initial load
duke
parents:
diff changeset
   406
     */
474761f14bca Initial load
duke
parents:
diff changeset
   407
    public JStatement directStatement(final String source) {
474761f14bca Initial load
duke
parents:
diff changeset
   408
        JStatement s = new JStatement() {
474761f14bca Initial load
duke
parents:
diff changeset
   409
            public void state(JFormatter f) {
474761f14bca Initial load
duke
parents:
diff changeset
   410
                f.p(source).nl();
474761f14bca Initial load
duke
parents:
diff changeset
   411
            }
474761f14bca Initial load
duke
parents:
diff changeset
   412
        };
474761f14bca Initial load
duke
parents:
diff changeset
   413
        add(s);
474761f14bca Initial load
duke
parents:
diff changeset
   414
        return s;
474761f14bca Initial load
duke
parents:
diff changeset
   415
    }
474761f14bca Initial load
duke
parents:
diff changeset
   416
474761f14bca Initial load
duke
parents:
diff changeset
   417
    public void generate(JFormatter f) {
474761f14bca Initial load
duke
parents:
diff changeset
   418
        if (bracesRequired)
474761f14bca Initial load
duke
parents:
diff changeset
   419
            f.p('{').nl();
474761f14bca Initial load
duke
parents:
diff changeset
   420
        if (indentRequired)
474761f14bca Initial load
duke
parents:
diff changeset
   421
            f.i();
474761f14bca Initial load
duke
parents:
diff changeset
   422
        generateBody(f);
474761f14bca Initial load
duke
parents:
diff changeset
   423
        if (indentRequired)
474761f14bca Initial load
duke
parents:
diff changeset
   424
            f.o();
474761f14bca Initial load
duke
parents:
diff changeset
   425
        if (bracesRequired)
474761f14bca Initial load
duke
parents:
diff changeset
   426
            f.p('}');
474761f14bca Initial load
duke
parents:
diff changeset
   427
    }
474761f14bca Initial load
duke
parents:
diff changeset
   428
474761f14bca Initial load
duke
parents:
diff changeset
   429
    void generateBody(JFormatter f) {
474761f14bca Initial load
duke
parents:
diff changeset
   430
        for (Object o : content) {
474761f14bca Initial load
duke
parents:
diff changeset
   431
            if (o instanceof JDeclaration)
474761f14bca Initial load
duke
parents:
diff changeset
   432
                f.d((JDeclaration) o);
474761f14bca Initial load
duke
parents:
diff changeset
   433
            else
474761f14bca Initial load
duke
parents:
diff changeset
   434
                f.s((JStatement) o);
474761f14bca Initial load
duke
parents:
diff changeset
   435
        }
474761f14bca Initial load
duke
parents:
diff changeset
   436
    }
474761f14bca Initial load
duke
parents:
diff changeset
   437
474761f14bca Initial load
duke
parents:
diff changeset
   438
    /**
474761f14bca Initial load
duke
parents:
diff changeset
   439
     * Creates an enhanced For statement based on j2se 1.5 JLS
474761f14bca Initial load
duke
parents:
diff changeset
   440
     * and add it to this block
474761f14bca Initial load
duke
parents:
diff changeset
   441
     *
474761f14bca Initial load
duke
parents:
diff changeset
   442
     * @return Newly generated enhanced For statement per j2se 1.5
474761f14bca Initial load
duke
parents:
diff changeset
   443
     * specification
474761f14bca Initial load
duke
parents:
diff changeset
   444
    */
474761f14bca Initial load
duke
parents:
diff changeset
   445
    public JForEach forEach(JType varType, String name, JExpression collection) {
474761f14bca Initial load
duke
parents:
diff changeset
   446
        return insert(new JForEach( varType, name, collection));
474761f14bca Initial load
duke
parents:
diff changeset
   447
474761f14bca Initial load
duke
parents:
diff changeset
   448
    }
474761f14bca Initial load
duke
parents:
diff changeset
   449
    public void state(JFormatter f) {
474761f14bca Initial load
duke
parents:
diff changeset
   450
        f.g(this);
474761f14bca Initial load
duke
parents:
diff changeset
   451
        if (bracesRequired)
474761f14bca Initial load
duke
parents:
diff changeset
   452
            f.nl();
474761f14bca Initial load
duke
parents:
diff changeset
   453
    }
474761f14bca Initial load
duke
parents:
diff changeset
   454
474761f14bca Initial load
duke
parents:
diff changeset
   455
}