Java Java String Format toJavaSourceType(String type)

Here you can find the source of toJavaSourceType(String type)

Description

Class.getName() return arrays in the form "[[[", where et, the element type can be one of ZBCDFIJS or L; It is converted into forms that can be understood by javac.

License

Apache License

Declaration

public static String toJavaSourceType(String type) 

Method Source Code

//package com.java2s;
/*//  ww w .j a v a  2 s.  c om
 * 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.
 */

public class Main {
    /**
     * Class.getName() return arrays in the form "[[[<et>", where et, the
     * element type can be one of ZBCDFIJS or L<classname>; It is converted
     * into forms that can be understood by javac.
     */
    public static String toJavaSourceType(String type) {

        if (type.charAt(0) != '[') {
            return type;
        }

        int dims = 1;
        String t = null;
        for (int i = 1; i < type.length(); i++) {
            if (type.charAt(i) == '[') {
                dims++;
            } else {
                switch (type.charAt(i)) {
                case 'Z':
                    t = "boolean";
                    break;
                case 'B':
                    t = "byte";
                    break;
                case 'C':
                    t = "char";
                    break;
                case 'D':
                    t = "double";
                    break;
                case 'F':
                    t = "float";
                    break;
                case 'I':
                    t = "int";
                    break;
                case 'J':
                    t = "long";
                    break;
                case 'S':
                    t = "short";
                    break;
                case 'L':
                    t = type.substring(i + 1, type.indexOf(';'));
                    break;
                }
                break;
            }
        }
        StringBuilder resultType = new StringBuilder(t);
        for (; dims > 0; dims--) {
            resultType.append("[]");
        }
        return resultType.toString();
    }
}

Related

  1. toJavaPattern(String pattern)
  2. toJavascriptArray(String[][] Vals)
  3. toJavaScriptEncode(String string)
  4. toJavaScriptSafeString(String content)
  5. toJavaSecs(long secs)
  6. toJavaStaticIdentifier(String string)
  7. toJavaString(final String s)
  8. toJavaString(String buf)
  9. toJavaString(String text)