325N/A/*
325N/A * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
325N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
325N/A *
325N/A * This code is free software; you can redistribute it and/or modify it
325N/A * under the terms of the GNU General Public License version 2 only, as
325N/A * published by the Free Software Foundation. Oracle designates this
325N/A * particular file as subject to the "Classpath" exception as provided
325N/A * by Oracle in the LICENSE file that accompanied this code.
325N/A *
325N/A * This code is distributed in the hope that it will be useful, but WITHOUT
325N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
325N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
325N/A * version 2 for more details (a copy is included in the LICENSE file that
325N/A * accompanied this code).
325N/A *
325N/A * You should have received a copy of the GNU General Public License version
325N/A * 2 along with this work; if not, write to the Free Software Foundation,
325N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
325N/A *
325N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
325N/A * or visit www.oracle.com if you need additional information or have any
325N/A * questions.
325N/A */
325N/A
325N/Apackage com.sun.codemodel.internal;
325N/A
325N/Aimport java.lang.annotation.Annotation;
325N/Aimport java.util.ArrayList;
325N/Aimport java.util.List;
325N/Aimport java.util.Set;
325N/Aimport java.util.TreeSet;
325N/Aimport java.util.Collections;
325N/Aimport java.util.Collection;
325N/A
325N/Aimport com.sun.codemodel.internal.util.ClassNameComparator;
325N/A
325N/A/**
325N/A * Java method.
325N/A */
325N/Apublic class JMethod extends JGenerifiableImpl implements JDeclaration, JAnnotatable, JDocCommentable {
325N/A
325N/A /**
325N/A * Modifiers for this method
325N/A */
325N/A private JMods mods;
325N/A
325N/A /**
325N/A * Return type for this method
325N/A */
325N/A private JType type = null;
325N/A
325N/A /**
325N/A * Name of this method
325N/A */
325N/A private String name = null;
325N/A
325N/A /**
325N/A * List of parameters for this method's declaration
325N/A */
325N/A private final List<JVar> params = new ArrayList<JVar>();
325N/A
325N/A /**
325N/A * Set of exceptions that this method may throw.
325N/A * A set instance lazily created.
325N/A */
325N/A private Set<JClass> _throws;
325N/A
325N/A /**
325N/A * JBlock of statements that makes up the body this method
325N/A */
325N/A private JBlock body = null;
325N/A
325N/A private JDefinedClass outer;
325N/A
325N/A /**
325N/A * javadoc comments for this JMethod
325N/A */
325N/A private JDocComment jdoc = null;
325N/A
325N/A /**
325N/A * Variable parameter for this method's varargs declaration
325N/A * introduced in J2SE 1.5
325N/A */
325N/A private JVar varParam = null;
325N/A
325N/A /**
325N/A * Annotations on this variable. Lazily created.
325N/A */
325N/A private List<JAnnotationUse> annotations = null;
325N/A
325N/A
325N/A private boolean isConstructor() {
325N/A return type == null;
325N/A }
325N/A
325N/A /** To set the default value for the
325N/A * annotation member
325N/A */
325N/A private JExpression defaultValue = null;
325N/A
325N/A
325N/A /**
325N/A * JMethod constructor
325N/A *
325N/A * @param mods
325N/A * Modifiers for this method's declaration
325N/A *
325N/A * @param type
325N/A * Return type for the method
325N/A *
325N/A * @param name
325N/A * Name of this method
325N/A */
325N/A JMethod(JDefinedClass outer, int mods, JType type, String name) {
325N/A this.mods = JMods.forMethod(mods);
325N/A this.type = type;
325N/A this.name = name;
325N/A this.outer = outer;
325N/A }
325N/A
325N/A /**
325N/A * Constructor constructor
325N/A *
325N/A * @param mods
325N/A * Modifiers for this constructor's declaration
325N/A *
325N/A * @param _class
325N/A * JClass containing this constructor
325N/A */
325N/A JMethod(int mods, JDefinedClass _class) {
325N/A this.mods = JMods.forMethod(mods);
325N/A this.type = null;
325N/A this.name = _class.name();
325N/A this.outer = _class;
325N/A }
325N/A
325N/A private Set<JClass> getThrows() {
325N/A if(_throws==null)
325N/A _throws = new TreeSet<JClass>(ClassNameComparator.theInstance);
325N/A return _throws;
325N/A }
325N/A
325N/A /**
325N/A * Add an exception to the list of exceptions that this
325N/A * method may throw.
325N/A *
325N/A * @param exception
325N/A * Name of an exception that this method may throw
325N/A */
325N/A public JMethod _throws(JClass exception) {
325N/A getThrows().add(exception);
325N/A return this;
325N/A }
325N/A
325N/A public JMethod _throws(Class<? extends Throwable> exception) {
325N/A return _throws(outer.owner().ref(exception));
325N/A }
325N/A
325N/A /**
325N/A * Returns the list of variable of this method.
325N/A *
325N/A * @return List of parameters of this method. This list is not modifiable.
325N/A */
325N/A public List<JVar> params() {
325N/A return Collections.<JVar>unmodifiableList(params);
325N/A }
325N/A
325N/A /**
325N/A * Add the specified variable to the list of parameters
325N/A * for this method signature.
325N/A *
325N/A * @param type
325N/A * JType of the parameter being added
325N/A *
325N/A * @param name
325N/A * Name of the parameter being added
325N/A *
325N/A * @return New parameter variable
325N/A */
325N/A public JVar param(int mods, JType type, String name) {
325N/A JVar v = new JVar(JMods.forVar(mods), type, name, null);
325N/A params.add(v);
325N/A return v;
325N/A }
325N/A
325N/A public JVar param(JType type, String name) {
325N/A return param(JMod.NONE, type, name);
325N/A }
325N/A
325N/A public JVar param(int mods, Class<?> type, String name) {
325N/A return param(mods, outer.owner()._ref(type), name);
325N/A }
325N/A
325N/A public JVar param(Class<?> type, String name) {
325N/A return param(outer.owner()._ref(type), name);
325N/A }
325N/A
325N/A /**
325N/A * @see #varParam(JType, String)
325N/A */
325N/A public JVar varParam(Class<?> type, String name) {
325N/A return varParam(outer.owner()._ref(type),name);
325N/A }
325N/A
325N/A /**
325N/A * Add the specified variable argument to the list of parameters
325N/A * for this method signature.
325N/A *
325N/A * @param type
325N/A * Type of the parameter being added.
325N/A *
325N/A * @param name
325N/A * Name of the parameter being added
325N/A *
325N/A * @return the variable parameter
325N/A *
325N/A * @throws IllegalStateException
325N/A * If this method is called twice.
325N/A * varargs in J2SE 1.5 can appear only once in the
325N/A * method signature.
325N/A */
325N/A public JVar varParam(JType type, String name) {
325N/A if (!hasVarArgs()) {
325N/A
325N/A varParam =
325N/A new JVar(
325N/A JMods.forVar(JMod.NONE),
325N/A type.array(),
325N/A name,
325N/A null);
325N/A return varParam;
325N/A } else {
325N/A throw new IllegalStateException(
325N/A "Cannot have two varargs in a method,\n"
325N/A + "Check if varParam method of JMethod is"
325N/A + " invoked more than once");
325N/A
325N/A }
325N/A
325N/A }
325N/A
325N/A /**
325N/A * Adds an annotation to this variable.
325N/A * @param clazz
325N/A * The annotation class to annotate the field with
325N/A */
325N/A public JAnnotationUse annotate(JClass clazz){
325N/A if(annotations==null)
325N/A annotations = new ArrayList<JAnnotationUse>();
325N/A JAnnotationUse a = new JAnnotationUse(clazz);
325N/A annotations.add(a);
325N/A return a;
325N/A }
325N/A
325N/A /**
325N/A * Adds an annotation to this variable.
325N/A *
325N/A * @param clazz
325N/A * The annotation class to annotate the field with
325N/A */
325N/A public JAnnotationUse annotate(Class <? extends Annotation> clazz){
325N/A return annotate(owner().ref(clazz));
325N/A }
325N/A
325N/A public <W extends JAnnotationWriter> W annotate2(Class<W> clazz) {
325N/A return TypedAnnotationWriter.create(clazz,this);
325N/A }
325N/A
325N/A public Collection<JAnnotationUse> annotations() {
325N/A if (annotations == null)
325N/A annotations = new ArrayList<JAnnotationUse>();
325N/A return Collections.unmodifiableList(annotations);
325N/A }
325N/A
325N/A /**
325N/A * Check if there are any varargs declared
325N/A * for this method signature.
325N/A */
325N/A public boolean hasVarArgs() {
325N/A return this.varParam!=null;
325N/A }
325N/A
325N/A public String name() {
325N/A return name;
325N/A }
325N/A
325N/A /**
325N/A * Changes the name of the method.
325N/A */
325N/A public void name(String n) {
325N/A this.name = n;
325N/A }
325N/A
325N/A /**
325N/A * Returns the return type.
325N/A */
325N/A public JType type() {
325N/A return type;
325N/A }
325N/A
325N/A /**
325N/A * Overrides the return type.
325N/A */
325N/A public void type(JType t) {
325N/A this.type = t;
325N/A }
325N/A
325N/A /**
325N/A * Returns all the parameter types in an array.
325N/A * @return
325N/A * If there's no parameter, an empty array will be returned.
325N/A */
325N/A public JType[] listParamTypes() {
325N/A JType[] r = new JType[params.size()];
325N/A for (int i = 0; i < r.length; i++)
325N/A r[i] = params.get(i).type();
325N/A return r;
325N/A }
325N/A
325N/A /**
325N/A * Returns the varags parameter type.
325N/A * @return
325N/A * If there's no vararg parameter type, null will be returned.
325N/A */
325N/A public JType listVarParamType() {
325N/A if (varParam != null)
325N/A return varParam.type();
325N/A else
325N/A return null;
325N/A }
325N/A
325N/A /**
325N/A * Returns all the parameters in an array.
325N/A * @return
325N/A * If there's no parameter, an empty array will be returned.
325N/A */
325N/A public JVar[] listParams() {
325N/A return params.toArray(new JVar[params.size()]);
325N/A }
325N/A
325N/A /**
325N/A * Returns the variable parameter
325N/A * @return
325N/A * If there's no parameter, null will be returned.
325N/A */
325N/A public JVar listVarParam() {
325N/A return varParam;
325N/A }
325N/A
325N/A /**
325N/A * Returns true if the method has the specified signature.
325N/A */
325N/A public boolean hasSignature(JType[] argTypes) {
325N/A JVar[] p = listParams();
325N/A if (p.length != argTypes.length)
325N/A return false;
325N/A
325N/A for (int i = 0; i < p.length; i++)
325N/A if (!p[i].type().equals(argTypes[i]))
325N/A return false;
325N/A
325N/A return true;
325N/A }
325N/A
325N/A /**
325N/A * Get the block that makes up body of this method
325N/A *
325N/A * @return Body of method
325N/A */
325N/A public JBlock body() {
325N/A if (body == null)
325N/A body = new JBlock();
325N/A return body;
325N/A }
325N/A
325N/A /**
325N/A * Specify the default value for this annotation member
325N/A * @param value
325N/A * Default value for the annotation member
325N/A *
325N/A */
325N/A public void declareDefaultValue(JExpression value){
325N/A this.defaultValue = value;
325N/A }
325N/A
325N/A /**
325N/A * Creates, if necessary, and returns the class javadoc for this
325N/A * JDefinedClass
325N/A *
325N/A * @return JDocComment containing javadocs for this class
325N/A */
325N/A public JDocComment javadoc() {
325N/A if (jdoc == null)
325N/A jdoc = new JDocComment(owner());
325N/A return jdoc;
325N/A }
325N/A
325N/A public void declare(JFormatter f) {
325N/A if (jdoc != null)
325N/A f.g(jdoc);
325N/A
325N/A if (annotations != null){
325N/A for (JAnnotationUse a : annotations)
325N/A f.g(a).nl();
325N/A }
325N/A
325N/A f.g(mods);
325N/A
325N/A // declare the generics parameters
325N/A super.declare(f);
325N/A
325N/A if (!isConstructor())
325N/A f.g(type);
325N/A f.id(name).p('(').i();
325N/A // when parameters are printed in new lines, we want them to be indented.
325N/A // there's a good chance no newlines happen, too, but just in case it does.
325N/A boolean first = true;
325N/A for (JVar var : params) {
325N/A if (!first)
325N/A f.p(',');
325N/A if(var.isAnnotated())
325N/A f.nl();
325N/A f.b(var);
325N/A first = false;
325N/A }
325N/A if (hasVarArgs()) {
325N/A if (!first)
325N/A f.p(',');
325N/A f.g(varParam.type().elementType());
325N/A f.p("... ");
325N/A f.id(varParam.name());
325N/A }
325N/A
325N/A f.o().p(')');
325N/A if (_throws!=null && !_throws.isEmpty()) {
325N/A f.nl().i().p("throws").g(_throws).nl().o();
325N/A }
325N/A
325N/A if (defaultValue != null) {
325N/A f.p("default ");
325N/A f.g(defaultValue);
325N/A }
325N/A if (body != null) {
325N/A f.s(body);
325N/A } else if (
325N/A !outer.isInterface() && !outer.isAnnotationTypeDeclaration() && !mods.isAbstract() && !mods.isNative()) {
325N/A // Print an empty body for non-native, non-abstract methods
325N/A f.s(new JBlock());
325N/A } else {
325N/A f.p(';').nl();
325N/A }
325N/A }
325N/A
325N/A /**
325N/A * @return
325N/A * the current modifiers of this method.
325N/A * Always return non-null valid object.
325N/A */
325N/A public JMods mods() {
325N/A return mods;
325N/A }
325N/A
325N/A /**
325N/A * @deprecated use {@link #mods()}
325N/A */
325N/A public JMods getMods() {
325N/A return mods;
325N/A }
325N/A
325N/A protected JCodeModel owner() {
325N/A return outer.owner();
325N/A }
325N/A}