1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *      http://www.apache.org/licenses/LICENSE-2.0
7 * Unless required by applicable law or agreed to in writing, software
8 * distributed under the License is distributed on an "AS IS" BASIS,
9 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 * See the License for the specific language governing permissions and
11 * limitations under the License.
12 */
13
14package android.databinding.tool.reflection.java;
15
16import android.databinding.tool.reflection.ModelClass;
17import android.databinding.tool.reflection.ModelMethod;
18import android.databinding.tool.reflection.TypeUtil;
19import android.databinding.tool.util.L;
20
21import java.lang.reflect.Array;
22import java.lang.reflect.Method;
23
24public class JavaTypeUtil extends TypeUtil {
25
26    @Override
27    public String getDescription(ModelClass modelClass) {
28        return modelClass.getCanonicalName().replace('.', '/');
29    }
30
31    @Override
32    public String getDescription(ModelMethod modelMethod) {
33        Method method = ((JavaMethod) modelMethod).mMethod;
34        StringBuilder sb  = new StringBuilder();
35        sb.append(method.getName());
36        sb.append("(");
37        for (Class param : method.getParameterTypes()) {
38            sb.append(getDescription(param));
39        }
40        sb.append(")");
41        sb.append(getDescription(method.getReturnType()));
42        return sb.toString();
43    }
44
45    private String getDescription(Class klass) {
46        if (klass == null) {
47            throw new UnsupportedOperationException();
48        }
49        if (boolean.class.equals(klass)) {
50            return BOOLEAN;
51        }
52        if (byte.class.equals(klass)) {
53            return BYTE;
54        }
55        if (short.class.equals(klass)) {
56            return SHORT;
57        }
58        if (int.class.equals(klass)) {
59            return INT;
60        }
61        if (long.class.equals(klass)) {
62            return LONG;
63        }
64        if (char.class.equals(klass)) {
65            return CHAR;
66        }
67        if (float.class.equals(klass)) {
68            return FLOAT;
69        }
70        if (double.class.equals(klass)) {
71            return DOUBLE;
72        }
73        if (void.class.equals(klass)) {
74            return VOID;
75        }
76        if (Object.class.isAssignableFrom(klass)) {
77            return CLASS_PREFIX + klass.getCanonicalName().replace('.', '/') + CLASS_SUFFIX;
78        }
79        if (Array.class.isAssignableFrom(klass)) {
80            return ARRAY + getDescription(klass.getComponentType());
81        }
82
83        UnsupportedOperationException ex
84                = new UnsupportedOperationException("cannot understand type "
85                + klass.toString() + ", kind:");
86        L.e(ex, "cannot create JNI type for %s", klass.getCanonicalName());
87        throw ex;
88    }
89}
90