| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
package org.apache.camel.util; |
| 18 |
|
|
| 19 |
|
import org.apache.camel.RuntimeCamelException; |
| 20 |
|
import org.apache.camel.converter.ObjectConverter; |
| 21 |
|
import org.apache.commons.logging.Log; |
| 22 |
|
import org.apache.commons.logging.LogFactory; |
| 23 |
|
|
| 24 |
|
import java.lang.annotation.Annotation; |
| 25 |
|
import java.lang.reflect.InvocationTargetException; |
| 26 |
|
import java.lang.reflect.Method; |
| 27 |
|
import java.nio.charset.Charset; |
| 28 |
|
import java.util.ArrayList; |
| 29 |
|
import java.util.Collection; |
| 30 |
|
import java.util.Iterator; |
| 31 |
|
import java.util.List; |
| 32 |
|
|
| 33 |
|
|
| 34 |
|
|
| 35 |
|
|
| 36 |
|
|
| 37 |
|
|
| 38 |
|
public class ObjectHelper { |
| 39 |
3 |
private static final transient Log LOG = LogFactory.getLog(ObjectHelper.class); |
| 40 |
|
|
| 41 |
|
|
| 42 |
|
|
| 43 |
|
|
| 44 |
0 |
private ObjectHelper() { |
| 45 |
0 |
} |
| 46 |
|
|
| 47 |
|
|
| 48 |
|
|
| 49 |
|
|
| 50 |
|
public static boolean equals(Object a, Object b) { |
| 51 |
717 |
if (a == b) { |
| 52 |
594 |
return true; |
| 53 |
|
} |
| 54 |
123 |
return a != null && b != null && a.equals(b); |
| 55 |
|
} |
| 56 |
|
|
| 57 |
|
|
| 58 |
|
|
| 59 |
|
|
| 60 |
|
|
| 61 |
|
|
| 62 |
|
|
| 63 |
|
|
| 64 |
|
|
| 65 |
|
public static boolean isEqualToAny(Object object, Object... values) { |
| 66 |
48 |
for (Object value : values) { |
| 67 |
36 |
if (equals(object, value)) { |
| 68 |
9 |
return true; |
| 69 |
|
} |
| 70 |
|
} |
| 71 |
12 |
return false; |
| 72 |
|
} |
| 73 |
|
|
| 74 |
|
|
| 75 |
|
|
| 76 |
|
|
| 77 |
|
|
| 78 |
|
public static int compare(Object a, Object b) { |
| 79 |
69 |
if (a == b) { |
| 80 |
0 |
return 0; |
| 81 |
|
} |
| 82 |
69 |
if (a == null) { |
| 83 |
0 |
return -1; |
| 84 |
|
} |
| 85 |
69 |
if (b == null) { |
| 86 |
0 |
return 1; |
| 87 |
|
} |
| 88 |
69 |
if (a instanceof Comparable) { |
| 89 |
69 |
Comparable comparable = (Comparable)a; |
| 90 |
69 |
return comparable.compareTo(b); |
| 91 |
|
} else { |
| 92 |
0 |
int answer = a.getClass().getName().compareTo(b.getClass().getName()); |
| 93 |
0 |
if (answer == 0) { |
| 94 |
0 |
answer = a.hashCode() - b.hashCode(); |
| 95 |
|
} |
| 96 |
0 |
return answer; |
| 97 |
|
} |
| 98 |
|
} |
| 99 |
|
|
| 100 |
|
public static void notNull(Object value, String name) { |
| 101 |
1086 |
if (value == null) { |
| 102 |
0 |
throw new IllegalArgumentException("No " + name + " specified"); |
| 103 |
|
} |
| 104 |
1086 |
} |
| 105 |
|
|
| 106 |
|
public static String[] splitOnCharacter(String value, String needle, int count) { |
| 107 |
588 |
String rc[] = new String[count]; |
| 108 |
588 |
rc[0] = value; |
| 109 |
1170 |
for (int i = 1; i < count; i++) { |
| 110 |
588 |
String v = rc[i - 1]; |
| 111 |
588 |
int p = v.indexOf(needle); |
| 112 |
588 |
if (p < 0) { |
| 113 |
6 |
return rc; |
| 114 |
|
} |
| 115 |
582 |
rc[i - 1] = v.substring(0, p); |
| 116 |
582 |
rc[i] = v.substring(p + 1); |
| 117 |
|
} |
| 118 |
582 |
return rc; |
| 119 |
|
} |
| 120 |
|
|
| 121 |
|
|
| 122 |
|
|
| 123 |
|
|
| 124 |
|
|
| 125 |
|
|
| 126 |
|
|
| 127 |
|
|
| 128 |
|
|
| 129 |
|
public static String removeStartingCharacters(String text, char ch) { |
| 130 |
9 |
int idx = 0; |
| 131 |
18 |
while (text.charAt(idx) == ch) { |
| 132 |
9 |
idx++; |
| 133 |
9 |
} |
| 134 |
9 |
if (idx > 0) { |
| 135 |
6 |
return text.substring(idx); |
| 136 |
|
} |
| 137 |
3 |
return text; |
| 138 |
|
} |
| 139 |
|
|
| 140 |
|
|
| 141 |
|
|
| 142 |
|
|
| 143 |
|
public static boolean contains(Object collectionOrArray, Object value) { |
| 144 |
9 |
if (collectionOrArray instanceof Collection) { |
| 145 |
9 |
Collection collection = (Collection)collectionOrArray; |
| 146 |
9 |
return collection.contains(value); |
| 147 |
|
} else { |
| 148 |
0 |
Iterator iter = ObjectConverter.iterator(value); |
| 149 |
0 |
while (iter.hasNext()) { |
| 150 |
0 |
if (equals(value, iter.next())) { |
| 151 |
0 |
return true; |
| 152 |
|
} |
| 153 |
|
} |
| 154 |
0 |
return false; |
| 155 |
|
} |
| 156 |
|
} |
| 157 |
|
|
| 158 |
|
|
| 159 |
|
|
| 160 |
|
|
| 161 |
|
|
| 162 |
|
|
| 163 |
|
|
| 164 |
|
|
| 165 |
|
|
| 166 |
|
public static boolean matches(List list) { |
| 167 |
0 |
if (!list.isEmpty()) { |
| 168 |
0 |
Object value = list.get(0); |
| 169 |
0 |
if (value instanceof Boolean) { |
| 170 |
0 |
Boolean flag = (Boolean)value; |
| 171 |
0 |
return flag.booleanValue(); |
| 172 |
|
} else { |
| 173 |
|
|
| 174 |
0 |
return true; |
| 175 |
|
} |
| 176 |
|
} |
| 177 |
0 |
return false; |
| 178 |
|
} |
| 179 |
|
|
| 180 |
|
public static boolean isNotNullAndNonEmpty(String text) { |
| 181 |
0 |
return text != null && text.trim().length() > 0; |
| 182 |
|
} |
| 183 |
|
|
| 184 |
|
public static boolean isNullOrBlank(String text) { |
| 185 |
12 |
return text == null || text.trim().length() <= 0; |
| 186 |
|
} |
| 187 |
|
|
| 188 |
|
|
| 189 |
|
|
| 190 |
|
|
| 191 |
|
|
| 192 |
|
|
| 193 |
|
|
| 194 |
|
|
| 195 |
|
|
| 196 |
|
|
| 197 |
|
|
| 198 |
|
public static String getSystemProperty(String name, String defaultValue) { |
| 199 |
|
try { |
| 200 |
3 |
return System.getProperty(name, defaultValue); |
| 201 |
0 |
} catch (Exception e) { |
| 202 |
0 |
if (LOG.isDebugEnabled()) { |
| 203 |
0 |
LOG.debug("Caught security exception accessing system property: " + name + ". Reason: " + e, |
| 204 |
|
e); |
| 205 |
|
} |
| 206 |
0 |
return defaultValue; |
| 207 |
|
} |
| 208 |
|
} |
| 209 |
|
|
| 210 |
|
|
| 211 |
|
|
| 212 |
|
|
| 213 |
|
|
| 214 |
|
public static String name(Class type) { |
| 215 |
0 |
return type != null ? type.getName() : null; |
| 216 |
|
} |
| 217 |
|
|
| 218 |
|
|
| 219 |
|
|
| 220 |
|
|
| 221 |
|
public static String className(Object value) { |
| 222 |
0 |
return name(value != null ? value.getClass() : null); |
| 223 |
|
} |
| 224 |
|
|
| 225 |
|
|
| 226 |
|
|
| 227 |
|
|
| 228 |
|
|
| 229 |
|
|
| 230 |
|
|
| 231 |
|
|
| 232 |
|
public static Class<?> loadClass(String name) { |
| 233 |
222 |
return loadClass(name, ObjectHelper.class.getClassLoader()); |
| 234 |
|
} |
| 235 |
|
|
| 236 |
|
|
| 237 |
|
|
| 238 |
|
|
| 239 |
|
|
| 240 |
|
|
| 241 |
|
|
| 242 |
|
|
| 243 |
|
|
| 244 |
|
|
| 245 |
|
public static Class<?> loadClass(String name, ClassLoader loader) { |
| 246 |
222 |
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); |
| 247 |
222 |
if (contextClassLoader != null) { |
| 248 |
|
try { |
| 249 |
222 |
return contextClassLoader.loadClass(name); |
| 250 |
0 |
} catch (ClassNotFoundException e) { |
| 251 |
|
try { |
| 252 |
0 |
return loader.loadClass(name); |
| 253 |
0 |
} catch (ClassNotFoundException e1) { |
| 254 |
0 |
LOG.debug("Could not find class: " + name + ". Reason: " + e); |
| 255 |
|
} |
| 256 |
|
} |
| 257 |
|
} |
| 258 |
0 |
return null; |
| 259 |
|
} |
| 260 |
|
|
| 261 |
|
|
| 262 |
|
|
| 263 |
|
|
| 264 |
|
|
| 265 |
|
|
| 266 |
|
|
| 267 |
|
|
| 268 |
|
|
| 269 |
|
|
| 270 |
|
public static Object invokeMethod(Method method, Object instance, Object... parameters) { |
| 271 |
|
try { |
| 272 |
168 |
return method.invoke(instance, parameters); |
| 273 |
0 |
} catch (IllegalAccessException e) { |
| 274 |
0 |
throw new RuntimeCamelException(e); |
| 275 |
0 |
} catch (InvocationTargetException e) { |
| 276 |
0 |
throw new RuntimeCamelException(e.getCause()); |
| 277 |
|
} |
| 278 |
|
} |
| 279 |
|
|
| 280 |
|
|
| 281 |
|
|
| 282 |
|
|
| 283 |
|
|
| 284 |
|
|
| 285 |
|
|
| 286 |
|
|
| 287 |
|
public static List<Method> findMethodsWithAnnotation(Class<?> type, |
| 288 |
|
Class<? extends Annotation> annotationType) { |
| 289 |
0 |
List<Method> answer = new ArrayList<Method>(); |
| 290 |
|
do { |
| 291 |
0 |
Method[] methods = type.getDeclaredMethods(); |
| 292 |
0 |
for (Method method : methods) { |
| 293 |
0 |
if (method.getAnnotation(annotationType) != null) { |
| 294 |
0 |
answer.add(method); |
| 295 |
|
} |
| 296 |
|
} |
| 297 |
0 |
type = type.getSuperclass(); |
| 298 |
0 |
} while (type != null); |
| 299 |
0 |
return answer; |
| 300 |
|
} |
| 301 |
|
|
| 302 |
|
|
| 303 |
|
|
| 304 |
|
|
| 305 |
|
|
| 306 |
|
|
| 307 |
|
|
| 308 |
|
public static String asString(Object[] objects) { |
| 309 |
0 |
if (objects == null) { |
| 310 |
0 |
return "null"; |
| 311 |
|
} else { |
| 312 |
0 |
StringBuffer buffer = new StringBuffer("{"); |
| 313 |
0 |
int counter = 0; |
| 314 |
0 |
for (Object object : objects) { |
| 315 |
0 |
if (counter++ > 0) { |
| 316 |
0 |
buffer.append(", "); |
| 317 |
|
} |
| 318 |
0 |
String text = (object == null) ? "null" : object.toString(); |
| 319 |
0 |
buffer.append(text); |
| 320 |
|
} |
| 321 |
0 |
buffer.append("}"); |
| 322 |
0 |
return buffer.toString(); |
| 323 |
|
} |
| 324 |
|
} |
| 325 |
|
|
| 326 |
|
|
| 327 |
|
|
| 328 |
|
|
| 329 |
|
|
| 330 |
|
|
| 331 |
|
|
| 332 |
|
public static boolean isAssignableFrom(Class a, Class b) { |
| 333 |
0 |
a = convertPrimitiveTypeToWrapperType(a); |
| 334 |
0 |
b = convertPrimitiveTypeToWrapperType(b); |
| 335 |
0 |
return a.isAssignableFrom(b); |
| 336 |
|
} |
| 337 |
|
|
| 338 |
|
|
| 339 |
|
|
| 340 |
|
|
| 341 |
|
|
| 342 |
|
public static Class convertPrimitiveTypeToWrapperType(Class type) { |
| 343 |
9 |
Class rc = type; |
| 344 |
9 |
if (type.isPrimitive()) { |
| 345 |
9 |
if (type == int.class) { |
| 346 |
3 |
rc = Integer.class; |
| 347 |
3 |
} else if (type == long.class) { |
| 348 |
0 |
rc = Long.class; |
| 349 |
0 |
} else if (type == double.class) { |
| 350 |
0 |
rc = Double.class; |
| 351 |
0 |
} else if (type == float.class) { |
| 352 |
0 |
rc = Float.class; |
| 353 |
0 |
} else if (type == short.class) { |
| 354 |
0 |
rc = Short.class; |
| 355 |
0 |
} else if (type == byte.class) { |
| 356 |
0 |
rc = Byte.class; |
| 357 |
|
} |
| 358 |
|
} |
| 359 |
9 |
return rc; |
| 360 |
|
} |
| 361 |
|
|
| 362 |
|
|
| 363 |
|
|
| 364 |
|
|
| 365 |
|
public static String getDefaultCharacterSet() { |
| 366 |
0 |
return Charset.defaultCharset().name(); |
| 367 |
|
} |
| 368 |
|
} |