1 package com.zmicer.utils;
2
3 import org.apache.log4j.Logger;
4 import sun.reflect.Reflection;
5
6 import java.util.HashMap;
7 import java.util.Map;
8 import java.util.StringTokenizer;
9 import java.util.regex.Pattern;
10
11
12
13
14
15
16
17
18 public class ReflexionUtils
19 {
20
21
22
23 final public static Logger LOG = Logger.getLogger(ZmicerUtilsConstants.LoggingCategory.REFLEXION.toString());
24
25
26
27
28 final public static Pattern PATTERN = Pattern.compile("\\.([a-zA-Z0-9_]*)\\(.*\r\n.*invoke0");
29
30
31
32
33
34
35
36
37
38
39
40
41 public static Object reflectObject(final String className, boolean throwIfNotInitialized)
42 {
43 InputArgumentUtils.checkStrings(true, className);
44 try
45 {
46 return ReflexionUtils.class.getClassLoader().loadClass(className).newInstance();
47 }
48 catch (Throwable t)
49 {
50 final String message = "Can not instantiate the class instance using the provided " +
51 "name: [" + className + "]; reason: "+t.getMessage()+"";
52 LoggingUtils.logException(LOG, t, message, ReflexionUtils.class);
53 if (throwIfNotInitialized)
54 {
55 throw new IllegalStateException(message);
56 }
57 }
58 return null;
59 }
60
61
62
63
64
65
66
67
68
69
70
71
72
73 public static Map<String, Object> reflectObjects(final Map<String, String> info, final boolean throwIfNotInitialized)
74 {
75 InputArgumentUtils.checkObjects(info);
76 final Map<String, Object> result = new HashMap<String, Object>();
77 for (String key : info.keySet())
78 {
79 try
80 {
81 if (org.apache.commons.lang.StringUtils.isBlank(key) || org.apache.commons.lang.StringUtils.isBlank(info.get(key)))
82 {
83 final String message = "The map provided can not contains blank key and values. key [" + key + "], value [" +
84 info.get(key) + "]";
85 LoggingUtils.error(LOG, ReflexionUtils.class, message);
86 throw new IllegalArgumentException(message);
87 }
88 result.put(key, ReflexionUtils.class.getClassLoader().loadClass(info.get(key)).newInstance());
89 }
90 catch (Throwable t)
91 {
92 final String message = "Can not instantiate the class instance using the provided name: [" + info.get(key) + "]";
93 LoggingUtils.logException(LOG, t, message, ReflexionUtils.class);
94 if (throwIfNotInitialized)
95 {
96 throw new IllegalStateException(message);
97 }
98 result.put(key, null);
99 }
100 }
101 return result;
102 }
103
104
105
106
107
108
109
110
111 public static String getBaseName(final Class claz)
112 {
113 if (null == claz || null == claz.getName())
114 {
115 throw new IllegalArgumentException("The class and class name can not be null.");
116 }
117 final StringTokenizer tokenizer = new StringTokenizer(claz.getName(), ".");
118 String result = null;
119 while (tokenizer.hasMoreTokens())
120 {
121 result = tokenizer.nextToken();
122 }
123 return result;
124 }
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141 public static String getClassNameOfArrayMembers(final Class claz)
142 {
143 InputArgumentUtils.checkObjects(claz);
144 final String prefix = "[L";
145 final String postfix = ";";
146 if (!claz.getName().startsWith(prefix) || !claz.getName().endsWith(postfix))
147 {
148 LoggingUtils.debug(LOG, ReflexionUtils.class, "Incorrect name of the class corresponding to the array of objects was " +
149 "provided: class name [" + claz.getName() + "]");
150 throw new IllegalArgumentException("Please provide the correct class corresponding to the array of objects.");
151 }
152 return claz.getName().substring(prefix.length(), claz.getName().length() - postfix.length());
153 }
154
155
156
157
158
159
160 public static String getCallerFullClassName()
161 {
162 return Reflection.getCallerClass(MagicNumbers.THREE).getName();
163 }
164
165
166
167
168
169
170
171
172
173
174 public static boolean instanceOf(final Object obj, Class<?> baseClass)
175 {
176 InputArgumentUtils.checkObjects(obj, baseClass);
177 return baseClass.isAssignableFrom(obj.getClass());
178 }
179
180 }