summaryrefslogtreecommitdiffstats
path: root/sca-java-2.x/tags/2.0.1-RC1/modules/binding-ejb-runtime/src/main/java/org/apache/tuscany/sca/binding/ejb/util/MethodInfo.java
blob: e7ea7c9077b74750c4d25784e7680fa958762719 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.    
 */
package org.apache.tuscany.sca.binding.ejb.util;

import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * MetaData for a java method
 *
 * @version $Rev$ $Date$
 */
class MethodInfo implements Serializable {

    /** Automatically generated javadoc for: serialVersionUID */
    private static final long serialVersionUID = -5557260979514687514L;
    private String name;
    private String returnType;
    private String[] parameterTypes;
    private String[] exceptionTypes;

    private String IDLName;
    
    private transient Method method;

    /**
     * Type Signature Java Type -------------- --------- Z boolean B byte C char
     * S short I int J long F float D double L fully-qualified-class ;
     * fully-qualified-class [ type type[] ( arg-types ) ret-type method type
     */
    private static final Map signatures = new HashMap();
    static {
        signatures.put("Z", boolean.class);
        signatures.put("B", byte.class);
        signatures.put("C", char.class);
        signatures.put("S", short.class);
        signatures.put("I", int.class);
        signatures.put("J", long.class);
        signatures.put("F", float.class);
        signatures.put("D", double.class);
        signatures.put("V", void.class);
    }

    MethodInfo(Method method) {
        this.method = method;
        this.name = method.getName();
        // this.declaringClass = method.getDeclaringClass().getName();
        this.returnType = method.getReturnType().getName();
        Class[] types = method.getParameterTypes();
        this.parameterTypes = new String[types.length];
        for (int i = 0; i < types.length; i++) {
            this.parameterTypes[i] = types[i].getName();
        }
        types = method.getExceptionTypes();
        this.exceptionTypes = new String[types.length];
        for (int i = 0; i < types.length; i++) {
            this.exceptionTypes[i] = types[i].getName();
        }
        IDLName = this.name;
    }

    MethodInfo(String name, String returnType, String[] parameterTypes, String[] exceptionTypes) {
        this.name = name;
        this.returnType = returnType;
        this.parameterTypes = parameterTypes;
        this.exceptionTypes = exceptionTypes;
        this.IDLName = name;
    }

    /**
     * Parse the class name from the internal signature Sample signatures: int
     * ---> I; int[] ---> [I Object ---> java/lang/Object Object[] --->
     * [Ljava/lang/Object;
     * 
     * @param value
     * @return
     */
    private static String getName(String signature) {
        String name = signature;
        // Remove leading ARRAY ([) signatures
        int index = name.lastIndexOf('[');
        if (index != -1)
            name = name.substring(index + 1);

        // Remove L<...>;
        if (name.charAt(0) == 'L' && name.charAt(name.length() - 1) == ';')
            name = name.substring(1, name.length() - 1);

        // Primitive types
        Class primitiveClass = (Class)signatures.get(name);
        if (primitiveClass != null) {
            name = primitiveClass.getName();
        }

        for (int i = 0; i < index + 1; i++) {
            name = name + "[]";
        }
        return name;
    }

    /**
     * @return
     */
    String getName() {
        return name;
    }

    /**
     * @return
     */
    String[] getParameterTypes() {
        return parameterTypes;
    }

    /**
     * @return
     */
    String getReturnType() {
        return returnType;
    }

    /**
     * @return
     */
    String[] getExceptionTypes() {
        return exceptionTypes;
    }

    @Override
    public String toString() {
        StringBuffer sb = new StringBuffer();
        sb.append(getName(returnType)).append(" ").append(name).append("(");
        for (int j = 0; j < parameterTypes.length; j++) {
            sb.append(getName(parameterTypes[j])).append(" ").append("arg" + j);
            if (j < (parameterTypes.length - 1))
                sb.append(", ");
        }
        sb.append(")");
        if (exceptionTypes.length > 0) {
            sb.append(" throws ");
            for (int k = 0; k < exceptionTypes.length; k++) {
                sb.append(exceptionTypes[k]);
                if (k < (exceptionTypes.length - 1))
                    sb.append(", ");
            }
        }
        sb.append(";");
        return sb.toString();
    }

    /**
     * @return Returns the iDLName.
     */
    String getIDLName() {
        return IDLName;
    }

    /**
     * @param name The iDLName to set.
     */
    void setIDLName(String name) {
        IDLName = name;
    }

    /**
     * @return the method
     */
    Method getMethod() {
        return method;
    }
}