List of usage examples for java.lang Object getClass
@HotSpotIntrinsicCandidate public final native Class<?> getClass();
From source file:Main.java
public static <T> T getT(Object o, int i) { try {/*from w ww. j a v a 2 s. c o m*/ return ((Class<T>) ((ParameterizedType) (o.getClass().getGenericSuperclass())) .getActualTypeArguments()[i]).newInstance(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (ClassCastException e) { e.printStackTrace(); } return null; }
From source file:Main.java
public static boolean setProperty(Object object, String fieldName, Object fieldValue) { Class<?> clazz = object.getClass(); String mname = "set" + fieldName; try {/*from w w w.ja va 2 s . c o m*/ for (Method m : clazz.getMethods()) { if (m.getName().equalsIgnoreCase(mname)) { m.invoke(object, fieldValue); return true; } } } catch (Exception e) { throw new IllegalStateException(e); } return false; }
From source file:Main.java
public static <T> Collection<T> getObjectsOfType(Collection<?> input, Class<T> type, Collection<T> output) { if (output == null) { output = createEmpty(input);// w w w . ja v a 2s. c o m if (output == null) output = new ArrayList<T>(); } for (Object o : input) { if (type.isAssignableFrom(o.getClass())) output.add((T) o); } return output; }
From source file:Main.java
public static void putInHashMapByGivenAttribute(HashMap map, Object obj, String attribName) { try {/*from ww w. j a v a 2s. c o m*/ Class class1 = obj.getClass(); String methodName = "get" + Character.toUpperCase(attribName.charAt(0)) + attribName.substring(1); Method method = class1.getMethod(methodName, new Class[0]); Object attributeValue = method.invoke(obj, new Object[0]); map.put(attributeValue, obj); } catch (Exception e) { e.printStackTrace(); } }
From source file:Main.java
public static int indexOfFirst(Object[] array, Class<?> type) { if (!isEmpty(array)) { int N = -1; for (Object one : array) { N++;//from w ww. j a v a 2s. c o m if (one != null && type == one.getClass()) { return N; } } } return -1; }
From source file:Main.java
@SuppressWarnings({ "rawtypes" }) public static Method hasProperty(Object o, String propertyName) { Class c = o.getClass(); Method method = methodOrNull(c, propertyName); if (method != null) { return method; }//w ww . j a va 2s. c o m method = methodOrNull(c, "get" + captitalize(propertyName)); if (method != null) { return method; } method = methodOrNull(c, "is" + captitalize(propertyName)); return method; /* * for (Method m : methods) { String methodName = m.getName(); if * (methodName.equals(propertyName) || * methodName.equals("is"+captitalize(propertyName)) || * methodName.equals("get"+captitalize(propertyName))) { return m; } } */ }
From source file:Main.java
public static Object arrayExpandAddElements(Object array, Object[] elementsToAdd) { Class cl = array.getClass(); if (!cl.isArray()) return null; int length = Array.getLength(array); int newLength = length + elementsToAdd.length; Class componentType = array.getClass().getComponentType(); Object newArray = Array.newInstance(componentType, newLength); System.arraycopy(array, 0, newArray, 0, length); for (int i = 0; i < elementsToAdd.length; i++) { Array.set(newArray, length + i, elementsToAdd[i]); }// w w w . ja va2 s . c o m return newArray; }
From source file:Main.java
/** * Set the fields' value.// ww w . j ava 2 s . c o m * * @param bean * @param valMap */ public static void setFieldValues(Object bean, Map<String, Object> valMap) { Class<?> cls = bean.getClass(); //Get all fields. Field[] fields = cls.getDeclaredFields(); for (Field field : fields) { if (valMap.containsKey(field.getName())) { field.setAccessible(true); try { field.set(bean, valMap.get(field.getName())); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } } }
From source file:Main.java
private static Field findField(Object instance, String name) throws NoSuchFieldException { for (Class<?> clazz = instance.getClass(); clazz != null; clazz = clazz.getSuperclass()) { try {//from www.j a va 2s . co m Field f = clazz.getDeclaredField(name); f.setAccessible(true); return f; } catch (NoSuchFieldException e) { } } throw new NoSuchFieldException("Unable to find field " + name + " in " + instance.getClass()); }
From source file:Main.java
public static String ObjToXml(Object object) throws JAXBException, IOException { return ObjToXml(object, object.getClass()); }