001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.xbean.finder.util;
018
019 import java.lang.reflect.Array;
020 import java.util.ArrayList;
021 import java.util.HashMap;
022 import java.util.List;
023 import java.util.Map;
024
025 public class Classes {
026
027 private static final Map<Class<?>, Class<?>> primitiveWrappers = new HashMap<Class<?>, Class<?>>();
028 private static final HashMap<String, Class> primitives = new HashMap<String, Class>();
029
030 static {
031 primitives.put("boolean", boolean.class);
032 primitives.put("byte", byte.class);
033 primitives.put("char", char.class);
034 primitives.put("short", short.class);
035 primitives.put("int", int.class);
036 primitives.put("long", long.class);
037 primitives.put("float", float.class);
038 primitives.put("double", double.class);
039
040 primitiveWrappers.put(boolean.class, Boolean.class);
041 primitiveWrappers.put(byte.class, Byte.class);
042 primitiveWrappers.put(char.class, Character.class);
043 primitiveWrappers.put(double.class, Double.class);
044 primitiveWrappers.put(float.class, Float.class);
045 primitiveWrappers.put(int.class, Integer.class);
046 primitiveWrappers.put(long.class, Long.class);
047 primitiveWrappers.put(short.class, Short.class);
048 }
049
050 public static boolean equals(String classNameA, String classNameB) {
051 return classNameA.equals(classNameB);
052 }
053
054 public static Class forName(String string, ClassLoader classLoader) throws ClassNotFoundException {
055 int arrayDimentions = 0;
056 while (string.endsWith("[]")){
057 string = string.substring(0, string.length() - 2);
058 arrayDimentions++;
059 }
060
061 Class clazz = primitives.get(string);
062
063 if (clazz == null) clazz = Class.forName(string, true, classLoader);
064
065 if (arrayDimentions == 0){
066 return clazz;
067 }
068 return Array.newInstance(clazz, new int[arrayDimentions]).getClass();
069 }
070
071 public static String packageName(Class clazz){
072 return packageName(clazz.getName());
073 }
074
075 public static String packageName(String clazzName){
076 int i = clazzName.lastIndexOf('.');
077 if (i > 0){
078 return clazzName.substring(0, i);
079 } else {
080 return "";
081 }
082 }
083
084 public static List<String> getSimpleNames(Class... classes){
085 List<String> list = new ArrayList<String>();
086 for (Class aClass : classes) {
087 list.add(aClass.getSimpleName());
088 }
089
090 return list;
091 }
092
093 public static Class<?> deprimitivize(Class<?> fieldType) {
094 return fieldType = fieldType.isPrimitive() ? primitiveWrappers.get(fieldType): fieldType;
095 }
096 }