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

Sat, 01 Dec 2007 00:00:00 +0000

author
duke
date
Sat, 01 Dec 2007 00:00:00 +0000
changeset 1
0961a4a21176
child 45
31822b475baa
permissions
-rw-r--r--

Initial load

     1 /*
     2  * Copyright 2006 Sun Microsystems, Inc.  All Rights Reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Sun designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Sun in the LICENSE file that accompanied this code.
    10  *
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    14  * version 2 for more details (a copy is included in the LICENSE file that
    15  * accompanied this code).
    16  *
    17  * You should have received a copy of the GNU General Public License version
    18  * 2 along with this work; if not, write to the Free Software Foundation,
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    20  *
    21  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
    23  * have any questions.
    24  */
    26 package com.sun.codemodel.internal;
    28 import java.lang.annotation.Annotation;
    29 import java.util.ArrayList;
    30 import java.util.List;
    31 import java.util.Set;
    32 import java.util.TreeSet;
    34 import com.sun.codemodel.internal.util.ClassNameComparator;
    36 /**
    37  * Java method.
    38  */
    39 public class JMethod extends JGenerifiableImpl implements JDeclaration, JAnnotatable {
    41         /**
    42          * Modifiers for this method
    43          */
    44         private JMods mods;
    46         /**
    47          * Return type for this method
    48          */
    49         private JType type = null;
    51         /**
    52          * Name of this method
    53          */
    54         private String name = null;
    56         /**
    57          * List of parameters for this method's declaration
    58          */
    59         private final List<JVar> params = new ArrayList<JVar>();
    61         /**
    62          * Set of exceptions that this method may throw.
    63      * A set instance lazily created.
    64          */
    65         private Set<JClass> _throws;
    67         /**
    68          * JBlock of statements that makes up the body this method
    69          */
    70         private JBlock body = null;
    72         private JDefinedClass outer;
    74         /**
    75          * javadoc comments for this JMethod
    76          */
    77         private JDocComment jdoc = null;
    79         /**
    80          * Variable parameter for this method's varargs declaration
    81          * introduced in J2SE 1.5
    82          */
    83         private JVar varParam = null;
    85     /**
    86      * Annotations on this variable. Lazily created.
    87      */
    88     private List<JAnnotationUse> annotations = null;
    91         private boolean isConstructor() {
    92                 return type == null;
    93         }
    95     /** To set the default value for the
    96      *  annotation member
    97      */
    98     private JExpression defaultValue = null;
   101         /**
   102          * JMethod constructor
   103          *
   104          * @param mods
   105          *        Modifiers for this method's declaration
   106          *
   107          * @param type
   108          *        Return type for the method
   109          *
   110          * @param name
   111          *        Name of this method
   112          */
   113         JMethod(JDefinedClass outer, int mods, JType type, String name) {
   114                 this.mods = JMods.forMethod(mods);
   115                 this.type = type;
   116                 this.name = name;
   117                 this.outer = outer;
   118         }
   120         /**
   121          * Constructor constructor
   122          *
   123          * @param mods
   124          *        Modifiers for this constructor's declaration
   125          *
   126          * @param _class
   127          *        JClass containing this constructor
   128          */
   129         JMethod(int mods, JDefinedClass _class) {
   130                 this.mods = JMods.forMethod(mods);
   131                 this.type = null;
   132                 this.name = _class.name();
   133                 this.outer = _class;
   134         }
   136     private Set<JClass> getThrows() {
   137         if(_throws==null)
   138             _throws = new TreeSet<JClass>(ClassNameComparator.theInstance);
   139         return _throws;
   140     }
   142         /**
   143          * Add an exception to the list of exceptions that this
   144          * method may throw.
   145          *
   146          * @param exception
   147          *        Name of an exception that this method may throw
   148          */
   149         public JMethod _throws(JClass exception) {
   150         getThrows().add(exception);
   151                 return this;
   152         }
   154         public JMethod _throws(Class exception) {
   155                 return _throws(outer.owner().ref(exception));
   156         }
   158         /**
   159          * Add the specified variable to the list of parameters
   160          * for this method signature.
   161          *
   162          * @param type
   163          *        JType of the parameter being added
   164          *
   165          * @param name
   166          *        Name of the parameter being added
   167          *
   168          * @return New parameter variable
   169          */
   170         public JVar param(int mods, JType type, String name) {
   171                 JVar v = new JVar(JMods.forVar(mods), type, name, null);
   172                 params.add(v);
   173                 return v;
   174         }
   176         public JVar param(JType type, String name) {
   177                 return param(JMod.NONE, type, name);
   178         }
   180         public JVar param(int mods, Class type, String name) {
   181                 return param(mods, outer.owner()._ref(type), name);
   182         }
   184         public JVar param(Class type, String name) {
   185                 return param(outer.owner()._ref(type), name);
   186         }
   188         /**
   189          * @see #varParam(JType, String)
   190          */
   191         public JVar varParam(Class type, String name) {
   192         return varParam(outer.owner()._ref(type),name);
   193     }
   195     /**
   196      * Add the specified variable argument to the list of parameters
   197      * for this method signature.
   198      *
   199      * @param type
   200      *      Type of the parameter being added.
   201      *
   202      * @param name
   203      *        Name of the parameter being added
   204      *
   205      * @return the variable parameter
   206      *
   207      * @throws IllegalStateException
   208      *      If this method is called twice.
   209      *      varargs in J2SE 1.5 can appear only once in the
   210      *      method signature.
   211      */
   212     public JVar varParam(JType type, String name) {
   213                 if (!hasVarArgs()) {
   215             varParam =
   216                                 new JVar(
   217                                         JMods.forVar(JMod.NONE),
   218                                         type.array(),
   219                                         name,
   220                                         null);
   221                         return varParam;
   222                 } else {
   223                         throw new IllegalStateException(
   224                                 "Cannot have two varargs in a method,\n"
   225                                         + "Check if varParam method of JMethod is"
   226                                         + " invoked more than once");
   228                 }
   230         }
   232     /**
   233      * Adds an annotation to this variable.
   234      * @param clazz
   235      *          The annotation class to annotate the field with
   236      */
   237     public JAnnotationUse annotate(JClass clazz){
   238         if(annotations==null)
   239            annotations = new ArrayList<JAnnotationUse>();
   240         JAnnotationUse a = new JAnnotationUse(clazz);
   241         annotations.add(a);
   242         return a;
   243     }
   245     /**
   246      * Adds an annotation to this variable.
   247      *
   248      * @param clazz
   249      *          The annotation class to annotate the field with
   250      */
   251     public JAnnotationUse annotate(Class <? extends Annotation> clazz){
   252         return annotate(owner().ref(clazz));
   253     }
   255     public <W extends JAnnotationWriter> W annotate2(Class<W> clazz) {
   256         return TypedAnnotationWriter.create(clazz,this);
   257     }
   259         /**
   260          * Check if there are any varargs declared
   261          * for this method signature.
   262          */
   263         public boolean hasVarArgs() {
   264                 return this.varParam!=null;
   265         }
   267         public String name() {
   268                 return name;
   269         }
   271         /**
   272          * Returns the return type.
   273          */
   274         public JType type() {
   275                 return type;
   276         }
   278         /**
   279          * Returns all the parameter types in an array.
   280          * @return
   281          *      If there's no parameter, an empty array will be returned.
   282          */
   283         public JType[] listParamTypes() {
   284                 JType[] r = new JType[params.size()];
   285                 for (int i = 0; i < r.length; i++)
   286                         r[i] = params.get(i).type();
   287                 return r;
   288         }
   290         /**
   291          * Returns  the varags parameter type.
   292          * @return
   293          * If there's no vararg parameter type, null will be returned.
   294          */
   295         public JType listVarParamType() {
   296                 if (varParam != null)
   297                         return varParam.type();
   298                 else
   299                         return null;
   300         }
   302         /**
   303          * Returns all the parameters in an array.
   304          * @return
   305          *      If there's no parameter, an empty array will be returned.
   306          */
   307         public JVar[] listParams() {
   308                 return params.toArray(new JVar[params.size()]);
   309         }
   311         /**
   312          * Returns the variable parameter
   313          * @return
   314          *      If there's no parameter, null will be returned.
   315          */
   316         public JVar listVarParam() {
   317                 return varParam;
   318         }
   320         /**
   321          * Returns true if the method has the specified signature.
   322          */
   323         public boolean hasSignature(JType[] argTypes) {
   324                 JVar[] p = listParams();
   325                 if (p.length != argTypes.length)
   326                         return false;
   328                 for (int i = 0; i < p.length; i++)
   329                         if (!p[i].type().equals(argTypes[i]))
   330                                 return false;
   332                 return true;
   333         }
   335         /**
   336          * Get the block that makes up body of this method
   337          *
   338          * @return Body of method
   339          */
   340         public JBlock body() {
   341                 if (body == null)
   342                         body = new JBlock();
   343                 return body;
   344         }
   346     /**
   347      * Specify the default value for this annotation member
   348      * @param value
   349      *           Default value for the annotation member
   350      *
   351      */
   352     public void declareDefaultValue(JExpression value){
   353         this.defaultValue = value;
   354     }
   356         /**
   357          * Creates, if necessary, and returns the class javadoc for this
   358          * JDefinedClass
   359          *
   360          * @return JDocComment containing javadocs for this class
   361          */
   362         public JDocComment javadoc() {
   363                 if (jdoc == null)
   364                         jdoc = new JDocComment(owner());
   365                 return jdoc;
   366         }
   368         public void declare(JFormatter f) {
   369                 if (jdoc != null)
   370                         f.g(jdoc);
   372         if (annotations != null){
   373             for (JAnnotationUse a : annotations)
   374                 f.g(a).nl();
   375         }
   377                 // declare the generics parameters
   378                 super.declare(f);
   380                 f.g(mods);
   381                 if (!isConstructor())
   382                         f.g(type);
   383                 f.id(name).p('(').i();
   384         // when parameters are printed in new lines, we want them to be indented.
   385         // there's a good chance no newlines happen, too, but just in case it does.
   386                 boolean first = true;
   387         for (JVar var : params) {
   388             if (!first)
   389                 f.p(',');
   390             if(var.isAnnotated())
   391                 f.nl();
   392             f.b(var);
   393             first = false;
   394         }
   395                 if (hasVarArgs()) {
   396                         if (!first)
   397                                 f.p(',');
   398                         f.g(varParam.type().elementType());
   399                         f.p("... ");
   400                         f.id(varParam.name());
   401                 }
   403                 f.o().p(')');
   404                 if (_throws!=null && !_throws.isEmpty()) {
   405                         f.nl().i().p("throws").g(_throws).nl().o();
   406                 }
   408         if (defaultValue != null) {
   409             f.p("default ");
   410             f.g(defaultValue);
   411         }
   412                 if (body != null) {
   413                         f.s(body);
   414                 } else if (
   415                         !outer.isInterface() && !outer.isAnnotationTypeDeclaration() && !mods.isAbstract() && !mods.isNative()) {
   416                         // Print an empty body for non-native, non-abstract methods
   417                         f.s(new JBlock());
   418                 } else {
   419                         f.p(';').nl();
   420                 }
   421         }
   423     /**
   424      * @return
   425      *      the current modifiers of this method.
   426      *      Always return non-null valid object.
   427      */
   428     public JMods mods() {
   429         return mods;
   430     }
   432     /**
   433      * @deprecated use {@link #mods()}
   434      */
   435     public JMods getMods() {
   436                 return mods;
   437         }
   439         protected JCodeModel owner() {
   440                 return outer.owner();
   441         }
   442 }

mercurial