src/share/classes/com/sun/codemodel/internal/JMethod.java

changeset 50
42dfec6871f6
parent 45
31822b475baa
child 78
860b95cc8d1d
     1.1 --- a/src/share/classes/com/sun/codemodel/internal/JMethod.java	Mon Apr 20 15:25:02 2009 -0700
     1.2 +++ b/src/share/classes/com/sun/codemodel/internal/JMethod.java	Mon May 04 21:10:41 2009 -0700
     1.3 @@ -38,49 +38,49 @@
     1.4   */
     1.5  public class JMethod extends JGenerifiableImpl implements JDeclaration, JAnnotatable {
     1.6  
     1.7 -    /**
     1.8 -     * Modifiers for this method
     1.9 -     */
    1.10 -    private JMods mods;
    1.11 +        /**
    1.12 +         * Modifiers for this method
    1.13 +         */
    1.14 +        private JMods mods;
    1.15  
    1.16 -    /**
    1.17 -     * Return type for this method
    1.18 -     */
    1.19 -    private JType type = null;
    1.20 +        /**
    1.21 +         * Return type for this method
    1.22 +         */
    1.23 +        private JType type = null;
    1.24  
    1.25 -    /**
    1.26 -     * Name of this method
    1.27 -     */
    1.28 -    private String name = null;
    1.29 +        /**
    1.30 +         * Name of this method
    1.31 +         */
    1.32 +        private String name = null;
    1.33  
    1.34 -    /**
    1.35 -     * List of parameters for this method's declaration
    1.36 -     */
    1.37 -    private final List<JVar> params = new ArrayList<JVar>();
    1.38 +        /**
    1.39 +         * List of parameters for this method's declaration
    1.40 +         */
    1.41 +        private final List<JVar> params = new ArrayList<JVar>();
    1.42  
    1.43 -    /**
    1.44 -     * Set of exceptions that this method may throw.
    1.45 +        /**
    1.46 +         * Set of exceptions that this method may throw.
    1.47       * A set instance lazily created.
    1.48 -     */
    1.49 -    private Set<JClass> _throws;
    1.50 +         */
    1.51 +        private Set<JClass> _throws;
    1.52  
    1.53 -    /**
    1.54 -     * JBlock of statements that makes up the body this method
    1.55 -     */
    1.56 -    private JBlock body = null;
    1.57 +        /**
    1.58 +         * JBlock of statements that makes up the body this method
    1.59 +         */
    1.60 +        private JBlock body = null;
    1.61  
    1.62 -    private JDefinedClass outer;
    1.63 +        private JDefinedClass outer;
    1.64  
    1.65 -    /**
    1.66 -     * javadoc comments for this JMethod
    1.67 -     */
    1.68 -    private JDocComment jdoc = null;
    1.69 +        /**
    1.70 +         * javadoc comments for this JMethod
    1.71 +         */
    1.72 +        private JDocComment jdoc = null;
    1.73  
    1.74 -    /**
    1.75 -     * Variable parameter for this method's varargs declaration
    1.76 -     * introduced in J2SE 1.5
    1.77 -     */
    1.78 -    private JVar varParam = null;
    1.79 +        /**
    1.80 +         * Variable parameter for this method's varargs declaration
    1.81 +         * introduced in J2SE 1.5
    1.82 +         */
    1.83 +        private JVar varParam = null;
    1.84  
    1.85      /**
    1.86       * Annotations on this variable. Lazily created.
    1.87 @@ -88,9 +88,9 @@
    1.88      private List<JAnnotationUse> annotations = null;
    1.89  
    1.90  
    1.91 -    private boolean isConstructor() {
    1.92 -        return type == null;
    1.93 -    }
    1.94 +        private boolean isConstructor() {
    1.95 +                return type == null;
    1.96 +        }
    1.97  
    1.98      /** To set the default value for the
    1.99       *  annotation member
   1.100 @@ -98,40 +98,40 @@
   1.101      private JExpression defaultValue = null;
   1.102  
   1.103  
   1.104 -    /**
   1.105 -     * JMethod constructor
   1.106 -     *
   1.107 -     * @param mods
   1.108 -     *        Modifiers for this method's declaration
   1.109 -     *
   1.110 -     * @param type
   1.111 -     *        Return type for the method
   1.112 -     *
   1.113 -     * @param name
   1.114 -     *        Name of this method
   1.115 -     */
   1.116 -    JMethod(JDefinedClass outer, int mods, JType type, String name) {
   1.117 -        this.mods = JMods.forMethod(mods);
   1.118 -        this.type = type;
   1.119 -        this.name = name;
   1.120 -        this.outer = outer;
   1.121 -    }
   1.122 +        /**
   1.123 +         * JMethod constructor
   1.124 +         *
   1.125 +         * @param mods
   1.126 +         *        Modifiers for this method's declaration
   1.127 +         *
   1.128 +         * @param type
   1.129 +         *        Return type for the method
   1.130 +         *
   1.131 +         * @param name
   1.132 +         *        Name of this method
   1.133 +         */
   1.134 +        JMethod(JDefinedClass outer, int mods, JType type, String name) {
   1.135 +                this.mods = JMods.forMethod(mods);
   1.136 +                this.type = type;
   1.137 +                this.name = name;
   1.138 +                this.outer = outer;
   1.139 +        }
   1.140  
   1.141 -    /**
   1.142 -     * Constructor constructor
   1.143 -     *
   1.144 -     * @param mods
   1.145 -     *        Modifiers for this constructor's declaration
   1.146 -     *
   1.147 -     * @param _class
   1.148 -     *        JClass containing this constructor
   1.149 -     */
   1.150 -    JMethod(int mods, JDefinedClass _class) {
   1.151 -        this.mods = JMods.forMethod(mods);
   1.152 -        this.type = null;
   1.153 -        this.name = _class.name();
   1.154 -        this.outer = _class;
   1.155 -    }
   1.156 +        /**
   1.157 +         * Constructor constructor
   1.158 +         *
   1.159 +         * @param mods
   1.160 +         *        Modifiers for this constructor's declaration
   1.161 +         *
   1.162 +         * @param _class
   1.163 +         *        JClass containing this constructor
   1.164 +         */
   1.165 +        JMethod(int mods, JDefinedClass _class) {
   1.166 +                this.mods = JMods.forMethod(mods);
   1.167 +                this.type = null;
   1.168 +                this.name = _class.name();
   1.169 +                this.outer = _class;
   1.170 +        }
   1.171  
   1.172      private Set<JClass> getThrows() {
   1.173          if(_throws==null)
   1.174 @@ -139,56 +139,56 @@
   1.175          return _throws;
   1.176      }
   1.177  
   1.178 -    /**
   1.179 -     * Add an exception to the list of exceptions that this
   1.180 -     * method may throw.
   1.181 -     *
   1.182 -     * @param exception
   1.183 -     *        Name of an exception that this method may throw
   1.184 -     */
   1.185 -    public JMethod _throws(JClass exception) {
   1.186 +        /**
   1.187 +         * Add an exception to the list of exceptions that this
   1.188 +         * method may throw.
   1.189 +         *
   1.190 +         * @param exception
   1.191 +         *        Name of an exception that this method may throw
   1.192 +         */
   1.193 +        public JMethod _throws(JClass exception) {
   1.194          getThrows().add(exception);
   1.195 -        return this;
   1.196 -    }
   1.197 +                return this;
   1.198 +        }
   1.199  
   1.200 -    public JMethod _throws(Class exception) {
   1.201 -        return _throws(outer.owner().ref(exception));
   1.202 -    }
   1.203 +        public JMethod _throws(Class exception) {
   1.204 +                return _throws(outer.owner().ref(exception));
   1.205 +        }
   1.206  
   1.207 -    /**
   1.208 -     * Add the specified variable to the list of parameters
   1.209 -     * for this method signature.
   1.210 -     *
   1.211 -     * @param type
   1.212 -     *        JType of the parameter being added
   1.213 -     *
   1.214 -     * @param name
   1.215 -     *        Name of the parameter being added
   1.216 -     *
   1.217 -     * @return New parameter variable
   1.218 -     */
   1.219 -    public JVar param(int mods, JType type, String name) {
   1.220 -        JVar v = new JVar(JMods.forVar(mods), type, name, null);
   1.221 -        params.add(v);
   1.222 -        return v;
   1.223 -    }
   1.224 +        /**
   1.225 +         * Add the specified variable to the list of parameters
   1.226 +         * for this method signature.
   1.227 +         *
   1.228 +         * @param type
   1.229 +         *        JType of the parameter being added
   1.230 +         *
   1.231 +         * @param name
   1.232 +         *        Name of the parameter being added
   1.233 +         *
   1.234 +         * @return New parameter variable
   1.235 +         */
   1.236 +        public JVar param(int mods, JType type, String name) {
   1.237 +                JVar v = new JVar(JMods.forVar(mods), type, name, null);
   1.238 +                params.add(v);
   1.239 +                return v;
   1.240 +        }
   1.241  
   1.242 -    public JVar param(JType type, String name) {
   1.243 -        return param(JMod.NONE, type, name);
   1.244 -    }
   1.245 +        public JVar param(JType type, String name) {
   1.246 +                return param(JMod.NONE, type, name);
   1.247 +        }
   1.248  
   1.249 -    public JVar param(int mods, Class type, String name) {
   1.250 -        return param(mods, outer.owner()._ref(type), name);
   1.251 -    }
   1.252 +        public JVar param(int mods, Class type, String name) {
   1.253 +                return param(mods, outer.owner()._ref(type), name);
   1.254 +        }
   1.255  
   1.256 -    public JVar param(Class type, String name) {
   1.257 -        return param(outer.owner()._ref(type), name);
   1.258 -    }
   1.259 +        public JVar param(Class type, String name) {
   1.260 +                return param(outer.owner()._ref(type), name);
   1.261 +        }
   1.262  
   1.263 -    /**
   1.264 -     * @see #varParam(JType, String)
   1.265 -     */
   1.266 -    public JVar varParam(Class type, String name) {
   1.267 +        /**
   1.268 +         * @see #varParam(JType, String)
   1.269 +         */
   1.270 +        public JVar varParam(Class type, String name) {
   1.271          return varParam(outer.owner()._ref(type),name);
   1.272      }
   1.273  
   1.274 @@ -210,25 +210,25 @@
   1.275       *      method signature.
   1.276       */
   1.277      public JVar varParam(JType type, String name) {
   1.278 -        if (!hasVarArgs()) {
   1.279 +                if (!hasVarArgs()) {
   1.280  
   1.281              varParam =
   1.282 -                new JVar(
   1.283 -                    JMods.forVar(JMod.NONE),
   1.284 -                    type.array(),
   1.285 -                    name,
   1.286 -                    null);
   1.287 -            return varParam;
   1.288 -        } else {
   1.289 -            throw new IllegalStateException(
   1.290 -                "Cannot have two varargs in a method,\n"
   1.291 -                    + "Check if varParam method of JMethod is"
   1.292 -                    + " invoked more than once");
   1.293 +                                new JVar(
   1.294 +                                        JMods.forVar(JMod.NONE),
   1.295 +                                        type.array(),
   1.296 +                                        name,
   1.297 +                                        null);
   1.298 +                        return varParam;
   1.299 +                } else {
   1.300 +                        throw new IllegalStateException(
   1.301 +                                "Cannot have two varargs in a method,\n"
   1.302 +                                        + "Check if varParam method of JMethod is"
   1.303 +                                        + " invoked more than once");
   1.304 +
   1.305 +                }
   1.306  
   1.307          }
   1.308  
   1.309 -    }
   1.310 -
   1.311      /**
   1.312       * Adds an annotation to this variable.
   1.313       * @param clazz
   1.314 @@ -256,17 +256,17 @@
   1.315          return TypedAnnotationWriter.create(clazz,this);
   1.316      }
   1.317  
   1.318 -    /**
   1.319 -     * Check if there are any varargs declared
   1.320 -     * for this method signature.
   1.321 -     */
   1.322 -    public boolean hasVarArgs() {
   1.323 -        return this.varParam!=null;
   1.324 -    }
   1.325 +        /**
   1.326 +         * Check if there are any varargs declared
   1.327 +         * for this method signature.
   1.328 +         */
   1.329 +        public boolean hasVarArgs() {
   1.330 +                return this.varParam!=null;
   1.331 +        }
   1.332  
   1.333 -    public String name() {
   1.334 -        return name;
   1.335 -    }
   1.336 +        public String name() {
   1.337 +                return name;
   1.338 +        }
   1.339  
   1.340      /**
   1.341       * Changes the name of the method.
   1.342 @@ -276,11 +276,11 @@
   1.343      }
   1.344  
   1.345      /**
   1.346 -     * Returns the return type.
   1.347 -     */
   1.348 -    public JType type() {
   1.349 -        return type;
   1.350 -    }
   1.351 +         * Returns the return type.
   1.352 +         */
   1.353 +        public JType type() {
   1.354 +                return type;
   1.355 +        }
   1.356  
   1.357      /**
   1.358       * Overrides the return type.
   1.359 @@ -290,72 +290,72 @@
   1.360      }
   1.361  
   1.362      /**
   1.363 -     * Returns all the parameter types in an array.
   1.364 -     * @return
   1.365 -     *      If there's no parameter, an empty array will be returned.
   1.366 -     */
   1.367 -    public JType[] listParamTypes() {
   1.368 -        JType[] r = new JType[params.size()];
   1.369 -        for (int i = 0; i < r.length; i++)
   1.370 -            r[i] = params.get(i).type();
   1.371 -        return r;
   1.372 -    }
   1.373 +         * Returns all the parameter types in an array.
   1.374 +         * @return
   1.375 +         *      If there's no parameter, an empty array will be returned.
   1.376 +         */
   1.377 +        public JType[] listParamTypes() {
   1.378 +                JType[] r = new JType[params.size()];
   1.379 +                for (int i = 0; i < r.length; i++)
   1.380 +                        r[i] = params.get(i).type();
   1.381 +                return r;
   1.382 +        }
   1.383  
   1.384 -    /**
   1.385 -     * Returns  the varags parameter type.
   1.386 -     * @return
   1.387 -     * If there's no vararg parameter type, null will be returned.
   1.388 -     */
   1.389 -    public JType listVarParamType() {
   1.390 -        if (varParam != null)
   1.391 -            return varParam.type();
   1.392 -        else
   1.393 -            return null;
   1.394 -    }
   1.395 +        /**
   1.396 +         * Returns  the varags parameter type.
   1.397 +         * @return
   1.398 +         * If there's no vararg parameter type, null will be returned.
   1.399 +         */
   1.400 +        public JType listVarParamType() {
   1.401 +                if (varParam != null)
   1.402 +                        return varParam.type();
   1.403 +                else
   1.404 +                        return null;
   1.405 +        }
   1.406  
   1.407 -    /**
   1.408 -     * Returns all the parameters in an array.
   1.409 -     * @return
   1.410 -     *      If there's no parameter, an empty array will be returned.
   1.411 -     */
   1.412 -    public JVar[] listParams() {
   1.413 -        return params.toArray(new JVar[params.size()]);
   1.414 -    }
   1.415 +        /**
   1.416 +         * Returns all the parameters in an array.
   1.417 +         * @return
   1.418 +         *      If there's no parameter, an empty array will be returned.
   1.419 +         */
   1.420 +        public JVar[] listParams() {
   1.421 +                return params.toArray(new JVar[params.size()]);
   1.422 +        }
   1.423  
   1.424 -    /**
   1.425 -     * Returns the variable parameter
   1.426 -     * @return
   1.427 -     *      If there's no parameter, null will be returned.
   1.428 -     */
   1.429 -    public JVar listVarParam() {
   1.430 -        return varParam;
   1.431 -    }
   1.432 +        /**
   1.433 +         * Returns the variable parameter
   1.434 +         * @return
   1.435 +         *      If there's no parameter, null will be returned.
   1.436 +         */
   1.437 +        public JVar listVarParam() {
   1.438 +                return varParam;
   1.439 +        }
   1.440  
   1.441 -    /**
   1.442 -     * Returns true if the method has the specified signature.
   1.443 -     */
   1.444 -    public boolean hasSignature(JType[] argTypes) {
   1.445 -        JVar[] p = listParams();
   1.446 -        if (p.length != argTypes.length)
   1.447 -            return false;
   1.448 +        /**
   1.449 +         * Returns true if the method has the specified signature.
   1.450 +         */
   1.451 +        public boolean hasSignature(JType[] argTypes) {
   1.452 +                JVar[] p = listParams();
   1.453 +                if (p.length != argTypes.length)
   1.454 +                        return false;
   1.455  
   1.456 -        for (int i = 0; i < p.length; i++)
   1.457 -            if (!p[i].type().equals(argTypes[i]))
   1.458 -                return false;
   1.459 +                for (int i = 0; i < p.length; i++)
   1.460 +                        if (!p[i].type().equals(argTypes[i]))
   1.461 +                                return false;
   1.462  
   1.463 -        return true;
   1.464 -    }
   1.465 +                return true;
   1.466 +        }
   1.467  
   1.468 -    /**
   1.469 -     * Get the block that makes up body of this method
   1.470 -     *
   1.471 -     * @return Body of method
   1.472 -     */
   1.473 -    public JBlock body() {
   1.474 -        if (body == null)
   1.475 -            body = new JBlock();
   1.476 -        return body;
   1.477 -    }
   1.478 +        /**
   1.479 +         * Get the block that makes up body of this method
   1.480 +         *
   1.481 +         * @return Body of method
   1.482 +         */
   1.483 +        public JBlock body() {
   1.484 +                if (body == null)
   1.485 +                        body = new JBlock();
   1.486 +                return body;
   1.487 +        }
   1.488  
   1.489      /**
   1.490       * Specify the default value for this annotation member
   1.491 @@ -367,37 +367,37 @@
   1.492          this.defaultValue = value;
   1.493      }
   1.494  
   1.495 -    /**
   1.496 -     * Creates, if necessary, and returns the class javadoc for this
   1.497 -     * JDefinedClass
   1.498 -     *
   1.499 -     * @return JDocComment containing javadocs for this class
   1.500 -     */
   1.501 -    public JDocComment javadoc() {
   1.502 -        if (jdoc == null)
   1.503 -            jdoc = new JDocComment(owner());
   1.504 -        return jdoc;
   1.505 -    }
   1.506 +        /**
   1.507 +         * Creates, if necessary, and returns the class javadoc for this
   1.508 +         * JDefinedClass
   1.509 +         *
   1.510 +         * @return JDocComment containing javadocs for this class
   1.511 +         */
   1.512 +        public JDocComment javadoc() {
   1.513 +                if (jdoc == null)
   1.514 +                        jdoc = new JDocComment(owner());
   1.515 +                return jdoc;
   1.516 +        }
   1.517  
   1.518 -    public void declare(JFormatter f) {
   1.519 -        if (jdoc != null)
   1.520 -            f.g(jdoc);
   1.521 +        public void declare(JFormatter f) {
   1.522 +                if (jdoc != null)
   1.523 +                        f.g(jdoc);
   1.524  
   1.525          if (annotations != null){
   1.526              for (JAnnotationUse a : annotations)
   1.527                  f.g(a).nl();
   1.528          }
   1.529  
   1.530 -        // declare the generics parameters
   1.531 -        super.declare(f);
   1.532 +                // declare the generics parameters
   1.533 +                super.declare(f);
   1.534  
   1.535 -        f.g(mods);
   1.536 -        if (!isConstructor())
   1.537 -            f.g(type);
   1.538 -        f.id(name).p('(').i();
   1.539 +                f.g(mods);
   1.540 +                if (!isConstructor())
   1.541 +                        f.g(type);
   1.542 +                f.id(name).p('(').i();
   1.543          // when parameters are printed in new lines, we want them to be indented.
   1.544          // there's a good chance no newlines happen, too, but just in case it does.
   1.545 -        boolean first = true;
   1.546 +                boolean first = true;
   1.547          for (JVar var : params) {
   1.548              if (!first)
   1.549                  f.p(',');
   1.550 @@ -406,33 +406,33 @@
   1.551              f.b(var);
   1.552              first = false;
   1.553          }
   1.554 -        if (hasVarArgs()) {
   1.555 -            if (!first)
   1.556 -                f.p(',');
   1.557 -            f.g(varParam.type().elementType());
   1.558 -            f.p("... ");
   1.559 -            f.id(varParam.name());
   1.560 -        }
   1.561 +                if (hasVarArgs()) {
   1.562 +                        if (!first)
   1.563 +                                f.p(',');
   1.564 +                        f.g(varParam.type().elementType());
   1.565 +                        f.p("... ");
   1.566 +                        f.id(varParam.name());
   1.567 +                }
   1.568  
   1.569 -        f.o().p(')');
   1.570 -        if (_throws!=null && !_throws.isEmpty()) {
   1.571 -            f.nl().i().p("throws").g(_throws).nl().o();
   1.572 -        }
   1.573 +                f.o().p(')');
   1.574 +                if (_throws!=null && !_throws.isEmpty()) {
   1.575 +                        f.nl().i().p("throws").g(_throws).nl().o();
   1.576 +                }
   1.577  
   1.578          if (defaultValue != null) {
   1.579              f.p("default ");
   1.580              f.g(defaultValue);
   1.581          }
   1.582 -        if (body != null) {
   1.583 -            f.s(body);
   1.584 -        } else if (
   1.585 -            !outer.isInterface() && !outer.isAnnotationTypeDeclaration() && !mods.isAbstract() && !mods.isNative()) {
   1.586 -            // Print an empty body for non-native, non-abstract methods
   1.587 -            f.s(new JBlock());
   1.588 -        } else {
   1.589 -            f.p(';').nl();
   1.590 +                if (body != null) {
   1.591 +                        f.s(body);
   1.592 +                } else if (
   1.593 +                        !outer.isInterface() && !outer.isAnnotationTypeDeclaration() && !mods.isAbstract() && !mods.isNative()) {
   1.594 +                        // Print an empty body for non-native, non-abstract methods
   1.595 +                        f.s(new JBlock());
   1.596 +                } else {
   1.597 +                        f.p(';').nl();
   1.598 +                }
   1.599          }
   1.600 -    }
   1.601  
   1.602      /**
   1.603       * @return
   1.604 @@ -447,10 +447,10 @@
   1.605       * @deprecated use {@link #mods()}
   1.606       */
   1.607      public JMods getMods() {
   1.608 -        return mods;
   1.609 -    }
   1.610 +                return mods;
   1.611 +        }
   1.612  
   1.613 -    protected JCodeModel owner() {
   1.614 -        return outer.owner();
   1.615 -    }
   1.616 +        protected JCodeModel owner() {
   1.617 +                return outer.owner();
   1.618 +        }
   1.619  }

mercurial