View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.tobago.apt.generate;
21  
22  import java.util.HashMap;
23  import java.util.Map;
24  
25  public class ClassUtils {
26  
27    private static final Map<String, Class> PRIMITIVE_MAP;
28  
29    static {
30      PRIMITIVE_MAP = new HashMap<>(8);
31      PRIMITIVE_MAP.put("boolean", Boolean.class);
32      PRIMITIVE_MAP.put("byte", Byte.class);
33      PRIMITIVE_MAP.put("char", Character.class);
34      PRIMITIVE_MAP.put("short", Short.class);
35      PRIMITIVE_MAP.put("int", Integer.class);
36      PRIMITIVE_MAP.put("long", Long.class);
37      PRIMITIVE_MAP.put("float", Float.class);
38      PRIMITIVE_MAP.put("double", Double.class);
39    }
40  
41    private ClassUtils() {
42    }
43  
44    public static String getPackageName(final String qualifiedName) {
45      final int pos = qualifiedName.lastIndexOf('.');
46      if (pos != -1) {
47        return qualifiedName.substring(0, pos);
48      }
49      return null;
50    }
51  
52    public static String getSimpleName(final String qualifiedName) {
53      final int pos = qualifiedName.lastIndexOf('.');
54      if (pos != -1) {
55        return qualifiedName.substring(pos + 1);
56      }
57      return null;
58    }
59  
60    public static String getSmallSimpleName(final String qualifiedName) {
61      final String simpleName = getSimpleName(qualifiedName);
62      if (simpleName != null && simpleName.length() > 2) {
63        return simpleName.substring(2, 3).toLowerCase() + simpleName.substring(3);
64      } else {
65        return simpleName;
66      }
67    }
68  
69    public static boolean isSystemClass(final String qualifiedClassName) {
70      return qualifiedClassName.startsWith("java.lang.");
71    }
72  
73    public static boolean isPrimitive(final String name) {
74      return PRIMITIVE_MAP.containsKey(name);
75    }
76  
77    public static Class getWrapper(final String primitive) {
78      return PRIMITIVE_MAP.get(primitive);
79    }
80  }