| 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.util.ArrayList; |
| 28 |
|
import java.util.Collection; |
| 29 |
|
import java.util.Iterator; |
| 30 |
|
import java.util.List; |
| 31 |
|
|
| 32 |
|
|
| 33 |
|
|
| 34 |
|
|
| 35 |
0 |
public class ObjectHelper { |
| 36 |
1 |
private static final transient Log log = LogFactory.getLog(ObjectHelper.class); |
| 37 |
|
|
| 38 |
|
|
| 39 |
|
|
| 40 |
|
|
| 41 |
|
public static boolean equals(Object a, Object b) { |
| 42 |
146 |
if (a == b) { |
| 43 |
127 |
return true; |
| 44 |
|
} |
| 45 |
19 |
return a != null && b != null && a.equals(b); |
| 46 |
|
} |
| 47 |
|
|
| 48 |
|
|
| 49 |
|
|
| 50 |
|
|
| 51 |
|
|
| 52 |
|
|
| 53 |
|
public static int compare(Object a, Object b) { |
| 54 |
7 |
if (a == b) { |
| 55 |
0 |
return 0; |
| 56 |
|
} |
| 57 |
7 |
if (a == null) { |
| 58 |
0 |
return -1; |
| 59 |
|
} |
| 60 |
7 |
if (b == null) { |
| 61 |
0 |
return 1; |
| 62 |
|
} |
| 63 |
7 |
if (a instanceof Comparable) { |
| 64 |
7 |
Comparable comparable = (Comparable) a; |
| 65 |
7 |
return comparable.compareTo(b); |
| 66 |
|
} |
| 67 |
|
else { |
| 68 |
0 |
int answer = a.getClass().getName().compareTo(b.getClass().getName()); |
| 69 |
0 |
if (answer == 0) { |
| 70 |
0 |
answer = a.hashCode() - b.hashCode(); |
| 71 |
|
} |
| 72 |
0 |
return answer; |
| 73 |
|
} |
| 74 |
|
} |
| 75 |
|
|
| 76 |
|
public static void notNull(Object value, String name) { |
| 77 |
250 |
if (value == null) { |
| 78 |
0 |
throw new IllegalArgumentException("No " + name + " specified"); |
| 79 |
|
} |
| 80 |
250 |
} |
| 81 |
|
|
| 82 |
|
public static String[] splitOnCharacter(String value, String needle, int count) { |
| 83 |
103 |
String rc[] = new String[count]; |
| 84 |
103 |
rc[0] = value; |
| 85 |
206 |
for (int i = 1; i < count; i++) { |
| 86 |
103 |
String v = rc[i - 1]; |
| 87 |
103 |
int p = v.indexOf(needle); |
| 88 |
103 |
if (p < 0) { |
| 89 |
0 |
return rc; |
| 90 |
|
} |
| 91 |
103 |
rc[i - 1] = v.substring(0, p); |
| 92 |
103 |
rc[i] = v.substring(p + 1); |
| 93 |
|
} |
| 94 |
103 |
return rc; |
| 95 |
|
} |
| 96 |
|
|
| 97 |
|
|
| 98 |
|
|
| 99 |
|
|
| 100 |
|
|
| 101 |
|
|
| 102 |
|
|
| 103 |
|
|
| 104 |
|
public static String removeStartingCharacters(String text, char ch) { |
| 105 |
3 |
int idx = 0; |
| 106 |
6 |
while (text.charAt(idx) == ch) { |
| 107 |
3 |
idx++; |
| 108 |
3 |
} |
| 109 |
3 |
if (idx > 0) { |
| 110 |
2 |
return text.substring(idx); |
| 111 |
|
} |
| 112 |
1 |
return text; |
| 113 |
|
} |
| 114 |
|
|
| 115 |
|
|
| 116 |
|
|
| 117 |
|
|
| 118 |
|
public static boolean contains(Object collectionOrArray, Object value) { |
| 119 |
3 |
if (collectionOrArray instanceof Collection) { |
| 120 |
3 |
Collection collection = (Collection) collectionOrArray; |
| 121 |
3 |
return collection.contains(value); |
| 122 |
|
} |
| 123 |
|
else { |
| 124 |
0 |
Iterator iter = ObjectConverter.iterator(value); |
| 125 |
0 |
while (iter.hasNext()) { |
| 126 |
0 |
if (equals(value, iter.next())) { |
| 127 |
0 |
return true; |
| 128 |
|
} |
| 129 |
|
} |
| 130 |
0 |
return false; |
| 131 |
|
} |
| 132 |
|
} |
| 133 |
|
|
| 134 |
|
|
| 135 |
|
|
| 136 |
|
|
| 137 |
|
|
| 138 |
|
|
| 139 |
|
|
| 140 |
|
|
| 141 |
|
public static boolean matches(List list) { |
| 142 |
0 |
if (!list.isEmpty()) { |
| 143 |
0 |
Object value = list.get(0); |
| 144 |
0 |
if (value instanceof Boolean) { |
| 145 |
0 |
Boolean flag = (Boolean) value; |
| 146 |
0 |
return flag.booleanValue(); |
| 147 |
|
} |
| 148 |
|
else { |
| 149 |
|
|
| 150 |
0 |
return true; |
| 151 |
|
} |
| 152 |
|
} |
| 153 |
0 |
return false; |
| 154 |
|
} |
| 155 |
|
|
| 156 |
|
public static boolean isNotNullOrBlank(String text) { |
| 157 |
0 |
return text != null && text.trim().length() > 0; |
| 158 |
|
} |
| 159 |
|
|
| 160 |
|
|
| 161 |
|
|
| 162 |
|
|
| 163 |
|
|
| 164 |
|
|
| 165 |
|
|
| 166 |
|
|
| 167 |
|
public static String getSystemProperty(String name, String defaultValue) { |
| 168 |
|
try { |
| 169 |
1 |
return System.getProperty(name, defaultValue); |
| 170 |
|
} |
| 171 |
0 |
catch (Exception e) { |
| 172 |
0 |
if (log.isDebugEnabled()) { |
| 173 |
0 |
log.debug("Caught security exception accessing system property: " + name + ". Reason: " + e, e); |
| 174 |
|
} |
| 175 |
0 |
return defaultValue; |
| 176 |
|
} |
| 177 |
|
} |
| 178 |
|
|
| 179 |
|
|
| 180 |
|
|
| 181 |
|
|
| 182 |
|
public static String name(Class type) { |
| 183 |
0 |
return type != null ? type.getName() : null; |
| 184 |
|
} |
| 185 |
|
|
| 186 |
|
|
| 187 |
|
|
| 188 |
|
|
| 189 |
|
public static String className(Object value) { |
| 190 |
0 |
return name(value != null ? value.getClass() : null); |
| 191 |
|
} |
| 192 |
|
|
| 193 |
|
|
| 194 |
|
|
| 195 |
|
|
| 196 |
|
|
| 197 |
|
|
| 198 |
|
|
| 199 |
|
|
| 200 |
|
public static Class<?> loadClass(String name) { |
| 201 |
0 |
return loadClass(name, ObjectHelper.class.getClassLoader()); |
| 202 |
|
} |
| 203 |
|
|
| 204 |
|
|
| 205 |
|
|
| 206 |
|
|
| 207 |
|
|
| 208 |
|
|
| 209 |
|
|
| 210 |
|
|
| 211 |
|
public static Class<?> loadClass(String name, ClassLoader loader) { |
| 212 |
0 |
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); |
| 213 |
0 |
if (contextClassLoader != null) { |
| 214 |
|
try { |
| 215 |
0 |
return contextClassLoader.loadClass(name); |
| 216 |
|
} |
| 217 |
0 |
catch (ClassNotFoundException e) { |
| 218 |
|
try { |
| 219 |
0 |
return loader.loadClass(name); |
| 220 |
|
} |
| 221 |
0 |
catch (ClassNotFoundException e1) { |
| 222 |
0 |
log.debug("Could not find class: " + name + ". Reason: " + e); |
| 223 |
|
} |
| 224 |
|
} |
| 225 |
|
} |
| 226 |
0 |
return null; |
| 227 |
|
} |
| 228 |
|
|
| 229 |
|
|
| 230 |
|
|
| 231 |
|
|
| 232 |
|
|
| 233 |
|
|
| 234 |
|
|
| 235 |
|
|
| 236 |
|
|
| 237 |
|
|
| 238 |
|
public static Object invokeMethod(Method method, Object instance, Object... parameters) { |
| 239 |
|
try { |
| 240 |
37 |
return method.invoke(instance, parameters); |
| 241 |
|
} |
| 242 |
0 |
catch (IllegalAccessException e) { |
| 243 |
0 |
throw new RuntimeCamelException(e); |
| 244 |
|
} |
| 245 |
0 |
catch (InvocationTargetException e) { |
| 246 |
0 |
throw new RuntimeCamelException(e.getCause()); |
| 247 |
|
} |
| 248 |
|
} |
| 249 |
|
|
| 250 |
|
|
| 251 |
|
|
| 252 |
|
|
| 253 |
|
|
| 254 |
|
|
| 255 |
|
|
| 256 |
|
|
| 257 |
|
public static List<Method> findMethodsWithAnnotation(Class<?> type, Class<? extends Annotation> annotationType) { |
| 258 |
0 |
List<Method> answer = new ArrayList<Method>(); |
| 259 |
|
do { |
| 260 |
0 |
Method[] methods = type.getDeclaredMethods(); |
| 261 |
0 |
for (Method method : methods) { |
| 262 |
0 |
if (method.getAnnotation(annotationType) != null) { |
| 263 |
0 |
answer.add(method); |
| 264 |
|
} |
| 265 |
|
} |
| 266 |
0 |
type = type.getSuperclass(); |
| 267 |
|
} |
| 268 |
0 |
while (type != null); |
| 269 |
0 |
return answer; |
| 270 |
|
} |
| 271 |
|
|
| 272 |
|
|
| 273 |
|
|
| 274 |
|
|
| 275 |
|
|
| 276 |
|
|
| 277 |
|
|
| 278 |
|
public static String asString(Object[] objects) { |
| 279 |
0 |
if (objects == null) { |
| 280 |
0 |
return "null"; |
| 281 |
|
} |
| 282 |
|
else { |
| 283 |
0 |
StringBuffer buffer = new StringBuffer("{"); |
| 284 |
0 |
int counter = 0; |
| 285 |
0 |
for (Object object : objects) { |
| 286 |
0 |
if (counter++ > 0) { |
| 287 |
0 |
buffer.append(", "); |
| 288 |
|
} |
| 289 |
0 |
String text = (object == null) ? "null" : object.toString(); |
| 290 |
0 |
buffer.append(text); |
| 291 |
|
} |
| 292 |
0 |
buffer.append("}"); |
| 293 |
0 |
return buffer.toString(); |
| 294 |
|
} |
| 295 |
|
} |
| 296 |
|
} |