aboutsummaryrefslogtreecommitdiffstats
path: root/apigenerator/src/com/android/apigenerator/ParserState.java
blob: 7ffb57a0d3736041f66fefa47164b12e6ce4f96a (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
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed 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 com.android.apigenerator;

import java.util.Map;

/**
 * Parser state used during parsing of the platform API files.
 *
 */
class ParserState {

    private final int mApiLevel;

    private final Map<String, ApiClass> mClasses;

    private String mCurrentPackage;
    private ApiClass mCurrentClass;

    private String mMethodName;
    private StringBuilder mMethodParams = new StringBuilder();
    private String mMethodReturnType;

    ParserState(Map<String, ApiClass> classes, int apiLevel) {
        mClasses = classes;
        mApiLevel = apiLevel;
    }

    Map<String, ApiClass> getClasses() {
        return mClasses;
    }

    void addPackage(String packageName) {
        mCurrentPackage = packageName;
    }

    void addClass(String className) {
        String fqcn = makeJavaClass(mCurrentPackage + "." + className);
        mCurrentClass = addClass(fqcn, mApiLevel);
    }

    void addSuperClass(String superClass) {
        mCurrentClass.addSuperClass(makeJavaClass(superClass), mApiLevel);
    }

    void addInterface(String interfaceClass) {
        mCurrentClass.addInterface(makeJavaClass(interfaceClass), mApiLevel);
    }

    void startNewConstructor() {
        mMethodParams.setLength(0);
        mMethodName = "<init>";
        mMethodReturnType = "V";
    }

    void startNewMethod(String name, String returnType) {
        mMethodParams.setLength(0);
        mMethodName = name;
        mMethodReturnType = parseType(returnType);
    }

    void addMethodParameter(String parameter) {
        mMethodParams.append(parseType(parameter));
    }

    void finishMethod() {
        addMethod(mMethodName + "(" + mMethodParams.toString() + ")" +
                (mMethodReturnType != null ? mMethodReturnType : ""));
    }

    void addMethod(String methodSignature) {
        mCurrentClass.addMethod(methodSignature, mApiLevel);
    }

    void addField(String fieldName) {
        mCurrentClass.addField(fieldName, mApiLevel);
    }

    void finishClass() {
        mCurrentClass = null;
    }

    void finishPackage() {
        finishClass();
        mCurrentPackage = null;
    }

    void done() {
        finishPackage();
    }

    private ApiClass addClass(String name, int apiLevel) {
        ApiClass theClass = mClasses.get(name);
        if (theClass == null) {
            theClass = new ApiClass(name, apiLevel);
            mClasses.put(name, theClass);
        }

        return theClass;
    }


    private String makeJavaClass(String fqcn) {
        final int length = fqcn.length();

        StringBuilder sb = new StringBuilder(length);

        boolean isClass = Character.isUpperCase(fqcn.charAt(0));
        for (int i = 0 ; i < length ; i++) {
            if (fqcn.charAt(i) == '.') {
                if (isClass) {
                    sb.append('$');
                } else {
                    sb.append('/');
                }

                if (i < length -1 ) {
                    isClass = Character.isUpperCase(fqcn.charAt(i+1));
                }
            } else {
                if (fqcn.charAt(i) == '<') {
                    break;
                }

                sb.append(fqcn.charAt(i));
            }
        }

        return sb.toString();
    }

    private String parseType(String type) {
        StringBuilder result = new StringBuilder();

        if (type.endsWith("...")) {
            result.append('[');
            type = type.substring(0, type.length() - 3);
        }

        while (type.endsWith("[]")) {
            result.append('[');
            type = type.substring(0, type.length() - 2);
        }

        if ("byte".equals(type))         result.append('B');
        else if ("char".equals(type))    result.append('C');
        else if ("double".equals(type))  result.append('D');
        else if ("float".equals(type))   result.append('F');
        else if ("int".equals(type))     result.append('I');
        else if ("long".equals(type))    result.append('J');
        else if ("short".equals(type))   result.append('S');
        else if ("void".equals(type))    result.append('V');
        else if ("boolean".equals(type)) result.append('Z');
        else {
            result.append('L').append(makeJavaClass(type)).append(';');
        }

        return result.toString();
    }
}