1/*
2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. 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.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package com.sun.tools.internal.ws.processor.model.java;
27
28import com.sun.tools.internal.ws.resources.ModelMessages;
29import com.sun.tools.internal.ws.wscompile.ErrorReceiver;
30import com.sun.tools.internal.ws.wscompile.WsimportOptions;
31import com.sun.tools.internal.ws.processor.model.Parameter;
32
33import java.util.ArrayList;
34import java.util.List;
35import java.util.Iterator;
36
37/**
38 * @author WS Development Team
39 */
40public class JavaMethod {
41
42    private final ErrorReceiver errorReceiver;
43    private final String name;
44    private final List<JavaParameter> parameters = new ArrayList<JavaParameter>();
45    private final List<String> exceptions = new ArrayList<String>();
46    private final WsimportOptions options;
47    private JavaType returnType;
48
49    public JavaMethod(String name, WsimportOptions options, ErrorReceiver receiver) {
50        this.name = name;
51        this.returnType = null;
52        this.errorReceiver = receiver;
53        this.options = options;
54    }
55
56    public String getName() {
57        return name;
58    }
59
60    public JavaType getReturnType() {
61        return returnType;
62    }
63
64    public void setReturnType(JavaType returnType) {
65        this.returnType = returnType;
66    }
67
68    private boolean hasParameter(String paramName) {
69        for (JavaParameter parameter : parameters) {
70            if (paramName.equals(parameter.getName())) {
71                return true;
72            }
73        }
74        return false;
75    }
76
77    private Parameter getParameter(String paramName){
78        for (JavaParameter parameter : parameters) {
79            if (paramName.equals(parameter.getName())) {
80                return parameter.getParameter();
81            }
82        }
83        return null;
84    }
85
86    public void addParameter(JavaParameter param) {
87        // verify that this member does not already exist
88        if (hasParameter(param.getName())) {
89            if (options.isExtensionMode()) {
90                param.setName(getUniqueName(param.getName()));
91            } else {
92                Parameter duplicParam = getParameter(param.getName());
93                if(param.getParameter().isEmbedded()){
94                    errorReceiver.error(param.getParameter().getLocator(), ModelMessages.MODEL_PARAMETER_NOTUNIQUE_WRAPPER(param.getName(), param.getParameter().getEntityName()));
95                    errorReceiver.error(duplicParam.getLocator(), ModelMessages.MODEL_PARAMETER_NOTUNIQUE_WRAPPER(param.getName(), duplicParam.getEntityName()));
96                } else {
97                    errorReceiver.error(param.getParameter().getLocator(), ModelMessages.MODEL_PARAMETER_NOTUNIQUE(param.getName(), param.getParameter().getEntityName()));
98                    errorReceiver.error(duplicParam.getLocator(), ModelMessages.MODEL_PARAMETER_NOTUNIQUE(param.getName(), duplicParam.getEntityName()));
99                }
100                return;
101            }
102        }
103        parameters.add(param);
104    }
105
106    public List<JavaParameter> getParametersList() {
107        return parameters;
108    }
109
110    public void addException(String exception) {
111        // verify that this exception does not already exist
112        if (!exceptions.contains(exception)) {
113            exceptions.add(exception);
114        }
115    }
116
117    /** TODO: NB uses it, remove it once we expose it thru some API **/
118    public Iterator<String> getExceptions() {
119        return exceptions.iterator();
120    }
121
122    private String getUniqueName(String param){
123        int parmNum = 0;
124        while (hasParameter(param)) {
125            param = param + Integer.toString(parmNum++);
126        }
127        return param;
128    }
129}
130