|
| 1 | +// MODIFIED FOR THE MSGPACK PROJECT |
| 2 | +// Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | +// contributor license agreements. See the NOTICE file distributed with |
| 4 | +// this work for additional information regarding copyright ownership. |
| 5 | +// The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | +// (the "License"); you may not use this file except in compliance with |
| 7 | +// the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, software |
| 12 | +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | +// License for the specific language governing permissions and limitations under |
| 15 | +// the License. |
| 16 | +// |
| 17 | + |
| 18 | +package org.apache.harmony.beans; |
| 19 | + |
| 20 | +import java.lang.reflect.Method; |
| 21 | +import java.util.Arrays; |
| 22 | + |
| 23 | +public class BeansUtils { |
| 24 | + |
| 25 | + public static final Object[] EMPTY_OBJECT_ARRAY = new Object[0]; |
| 26 | + |
| 27 | + public static final String NEW = "new"; //$NON-NLS-1$ |
| 28 | + |
| 29 | + public static final String NEWINSTANCE = "newInstance"; //$NON-NLS-1$ |
| 30 | + |
| 31 | + public static final String NEWARRAY = "newArray"; //$NON-NLS-1$ |
| 32 | + |
| 33 | + public static final String FORNAME = "forName"; //$NON-NLS-1$ |
| 34 | + |
| 35 | + public static final String GET = "get"; //$NON-NLS-1$ |
| 36 | + |
| 37 | + public static final String IS = "is"; //$NON-NLS-1$ |
| 38 | + |
| 39 | + public static final String SET = "set"; //$NON-NLS-1$ |
| 40 | + |
| 41 | + public static final String ADD = "add"; //$NON-NLS-1$ |
| 42 | + |
| 43 | + public static final String PUT = "put"; //$NON-NLS-1$ |
| 44 | + |
| 45 | + public static final String NULL = "null"; //$NON-NLS-1$ |
| 46 | + |
| 47 | + public static final String QUOTE = "\"\""; //$NON-NLS-1$ |
| 48 | + |
| 49 | + public static final int getHashCode(Object obj) { |
| 50 | + return obj != null ? obj.hashCode() : 0; |
| 51 | + } |
| 52 | + |
| 53 | + public static final int getHashCode(boolean bool) { |
| 54 | + return bool ? 1 : 0; |
| 55 | + } |
| 56 | + |
| 57 | + public static String toASCIILowerCase(String string) { |
| 58 | + char[] charArray = string.toCharArray(); |
| 59 | + StringBuilder sb = new StringBuilder(charArray.length); |
| 60 | + for (int index = 0; index < charArray.length; index++) { |
| 61 | + if ('A' <= charArray[index] && charArray[index] <= 'Z') { |
| 62 | + sb.append((char) (charArray[index] + ('a' - 'A'))); |
| 63 | + } else { |
| 64 | + sb.append(charArray[index]); |
| 65 | + } |
| 66 | + } |
| 67 | + return sb.toString(); |
| 68 | + } |
| 69 | + |
| 70 | + public static String toASCIIUpperCase(String string) { |
| 71 | + char[] charArray = string.toCharArray(); |
| 72 | + StringBuilder sb = new StringBuilder(charArray.length); |
| 73 | + for (int index = 0; index < charArray.length; index++) { |
| 74 | + if ('a' <= charArray[index] && charArray[index] <= 'z') { |
| 75 | + sb.append((char) (charArray[index] - ('a' - 'A'))); |
| 76 | + } else { |
| 77 | + sb.append(charArray[index]); |
| 78 | + } |
| 79 | + } |
| 80 | + return sb.toString(); |
| 81 | + } |
| 82 | + |
| 83 | + public static boolean isPrimitiveWrapper(Class<?> wrapper, Class<?> base) { |
| 84 | + return (base == boolean.class) && (wrapper == Boolean.class) |
| 85 | + || (base == byte.class) && (wrapper == Byte.class) |
| 86 | + || (base == char.class) && (wrapper == Character.class) |
| 87 | + || (base == short.class) && (wrapper == Short.class) |
| 88 | + || (base == int.class) && (wrapper == Integer.class) |
| 89 | + || (base == long.class) && (wrapper == Long.class) |
| 90 | + || (base == float.class) && (wrapper == Float.class) |
| 91 | + || (base == double.class) && (wrapper == Double.class); |
| 92 | + } |
| 93 | + |
| 94 | + private static final String EQUALS_METHOD = "equals"; |
| 95 | + |
| 96 | + private static final Class<?>[] EQUALS_PARAMETERS = new Class<?>[] { Object.class }; |
| 97 | + |
| 98 | + public static boolean declaredEquals(Class<?> clazz) { |
| 99 | + for (Method declaredMethod : clazz.getDeclaredMethods()) { |
| 100 | + if (EQUALS_METHOD.equals(declaredMethod.getName()) |
| 101 | + && Arrays.equals(declaredMethod.getParameterTypes(), |
| 102 | + EQUALS_PARAMETERS)) { |
| 103 | + return true; |
| 104 | + } |
| 105 | + } |
| 106 | + return false; |
| 107 | + } |
| 108 | + |
| 109 | + public static String idOfClass(Class<?> clazz) { |
| 110 | + Class<?> theClass = clazz; |
| 111 | + StringBuilder sb = new StringBuilder(); |
| 112 | + if (theClass.isArray()) { |
| 113 | + do { |
| 114 | + sb.append("Array"); //$NON-NLS-1$ |
| 115 | + theClass = theClass.getComponentType(); |
| 116 | + } while (theClass.isArray()); |
| 117 | + } |
| 118 | + String clazzName = theClass.getName(); |
| 119 | + clazzName = clazzName.substring(clazzName.lastIndexOf('.') + 1); |
| 120 | + return clazzName + sb.toString(); |
| 121 | + } |
| 122 | +} |
0 commit comments