/** * If it can it returns the value of the given field. If not it throws an * exception. * * @param target * the target object * @param name * the name * @return the field value * @throws ReflectiveOperationException * the reflective operation exception */ public static Object getFieldValue(final Object target, final String name) throws ReflectiveOperationException { Field field = ReflectionHelper.getField(target.getClass(), name); return field.get(target); }
private static Method getMethodFromSuperclass(final Class<?> cl, final String name, NoSuchMethodException origException, final Class<?>... parameterTypes) throws NoSuchMethodException { try { return findMethod(cl, name, parameterTypes); } catch (NoSuchMethodException nsme) { Class<?> superclass = cl.getSuperclass(); if (superclass == null) { LOG.debug("No method '{}' and no superclass for {} found:", name, cl, nsme); throw origException; } else { return getMethod(superclass, name, parameterTypes); } } }
/** * Gets the classpath from. * * @param cloader * the cloader * @return the classpath from */ public Object getClasspathFrom(final ClassLoader cloader) { try { return ReflectionHelper.getFieldValue(cloader, this.fieldname); } catch (ReflectiveOperationException ex) { throw new IllegalArgumentException(cloader + ": cannot access field '" + this.fieldname + "'", ex); } }
/** * Checks if the given class has something which looks like an id attribute. * * @param clazz * the clazz * @return true, if successful */ public static boolean hasId(final Class<?> clazz) { if (getIdField(clazz) != null) { return true; } return getIdGetter(clazz) != null; }
/** * Creates the target name depending on the given object. If the given * object has an id this id will be part of the generated name. * * @param createdObject * the created object * @return the string */ public static String createTargetName(final Object createdObject) { if (createdObject instanceof Class<?>) { Class<?> clazz = (Class<?>) createdObject; return ":" + clazz.getSimpleName(); } String name = ""; if (ReflectionHelper.hasId(createdObject)) { name += ReflectionHelper.getId(createdObject); } return name + ":" + createdObject.getClass().getSimpleName(); }
/** * Gets the method. * * @param cl * the cl * @param name * the name * @param args * the args * @return the method * @throws NoSuchMethodException * the no such method exception */ public static Method getMethod(final Class<?> cl, final String name, final Object... args) throws NoSuchMethodException { Class<?> parameterTypes[] = new Class<?>[args.length]; for (int i = 0; i < args.length; i++) { parameterTypes[i] = args[i].getClass(); } return getMethod(cl, name, parameterTypes); }
/** * Invoke method. This could be also a protected or private method. * * @param target * the target * @param name * the method name * @param args * the args for the method * @return the result of the method. */ public static Object invokeMethod(final Object target, final String name, final Object... args) { try { Method method = getMethod(target.getClass(), name, args); method.setAccessible(true); return method.invoke(target, args); } catch (NoSuchMethodException e) { throw new IllegalArgumentException( "no method \"" + name + "(" + toParamString(args) + ")\" in " + target.getClass(), e); } catch (IllegalAccessException iae) { throw new IllegalStateException("can't access method \"" + name + "\" in " + target.getClass(), iae); } catch (InvocationTargetException ite) { throw new IllegalStateException("exception in method \"" + name + "\" of " + target.getClass(), ite.getTargetException()); } }
/** * To get all uninitialized field you can call this method. * * @param obj the object where to get the fields from * * @return a collecion of unitialized fields (can be empty) */ public static Collection<Field> getUninitializedNonStaticFields(Object obj) { Collection<Field> unitializedFields = new ArrayList<Field>(); Field[] fields = obj.getClass().getDeclaredFields(); for (int i = 0; i < fields.length; i++) { try { fields[i].setAccessible(true); if ((fields[i].get(obj) == null) && !isStatic(fields[i])) { unitializedFields.add(fields[i]); } } catch (IllegalArgumentException e) { log.info(e + " => " + fields[i] + " ignored"); } catch (IllegalAccessException e) { log.debug("can't access " + fields[i] + " => ignored"); } } return unitializedFields; }
/** * To short string. * * @param fields * the fields * * @return the string */ public static String toShortString(final Collection<Field> fields) { StringBuilder sbuf = new StringBuilder(); for (Iterator<Field> iterator = fields.iterator(); iterator.hasNext();) { Field field = iterator.next(); sbuf.append(", "); sbuf.append(toShortString(field)); } return sbuf.substring(2); }
/** * Gets the method. * * @param cl * e.g. class java.lang.ClassLoader * @param name * e.g. "getPackages" * @param parameterTypes * the parameter types * @return a Method object * @throws NoSuchMethodException * the no such method exception */ public static Method getMethod(final Class<?> cl, final String name, final Class<?>... parameterTypes) throws NoSuchMethodException { try { return cl.getDeclaredMethod(name, parameterTypes); } catch (NoSuchMethodException origException) { try { return getMethodFromSuperclass(cl, name, origException, parameterTypes); } catch (NoSuchMethodException derived) { LOG.debug("Method '{}' also not found in superclass:", name, derived); throw origException; } } }
@MayReturnNull private static Field getIdField(final Class<?> clazz) { Field[] fields = clazz.getDeclaredFields(); for (int i = 0; i < fields.length; i++) { if ("id".equalsIgnoreCase(fields[i].getName())) { return fields[i]; } } Class<?> superclass = clazz.getSuperclass(); return superclass == null ? null : getIdField(superclass); }
@MayReturnNull private static Method getIdGetter(final Class<?> clazz) { Method[] methods = clazz.getDeclaredMethods(); for (int i = 0; i < methods.length; i++) { if ("getid".equalsIgnoreCase(methods[i].getName())) { return methods[i]; } } Class<?> superclass = clazz.getSuperclass(); return superclass == null ? null : getIdGetter(superclass); }
/** * Gets the method. * * @param cl e.g. class java.lang.ClassLoader * @param name e.g. "getPackages" * * @return a Method object * * @throws NoSuchMethodException the no such method exception */ public static Method getMethod(Class<?> cl, String name) throws NoSuchMethodException { try { return cl.getDeclaredMethod(name); } catch (NoSuchMethodException e) { Class<?> superclass = cl.getSuperclass(); if (superclass == null) { throw e; } else { return getMethod(superclass, name); } } }
/** * To get all uninitialized field you can call this method. * * @param obj * the object where to get the fields from * * @return a collecion of unitialized fields (can be empty) */ public static Collection<Field> getUninitializedNonStaticFields(final Object obj) { Collection<Field> unitializedFields = new ArrayList<>(); Field[] fields = obj.getClass().getDeclaredFields(); for (int i = 0; i < fields.length; i++) { try { fields[i].setAccessible(true); if ((fields[i].get(obj) == null) && !isStatic(fields[i])) { unitializedFields.add(fields[i]); } } catch (IllegalArgumentException e) { LOG.info(e + " => " + fields[i] + " ignored"); } catch (IllegalAccessException e) { LOG.debug("Cannot access " + fields[i] + " => ignored: ", e); } } return unitializedFields; }
/** * Gets the id of the given object. * * @param obj * the obj * @return the id * @since 1.6 (01.06.2015) */ public static Object getId(final Object obj) { Field idField = getIdField(obj.getClass()); try { if (idField != null) { idField.setAccessible(true); return idField.get(obj); } Method getter = getIdGetter(obj.getClass()); if (getter == null) { throw new IllegalArgumentException("no getter for '" + obj + "' available"); } getter.setAccessible(true); return getter.invoke(obj); } catch (ReflectiveOperationException ex) { throw new IllegalArgumentException("cannot get id of " + obj, ex); } }
/** * To short string. * * @param fields the fields * * @return the string */ public static String toShortString(Collection<Field> fields) { StringBuffer sbuf = new StringBuffer(); for (Iterator<Field> iterator = fields.iterator(); iterator.hasNext();) { Field field = iterator.next(); sbuf.append(", "); sbuf.append(toShortString(field)); } return sbuf.substring(2); }
/** * Gets the serial version uid. * * @param clazz the clazz * * @return the serial version uid * * @throws IllegalAccessException the illegal access exception */ public Long getSerialVersionUID(final Class<?> clazz) throws IllegalAccessException { try { Field field = ReflectionHelper.getField(clazz, "serialVersionUID"); return (Long) field.get(null); } catch (NoSuchFieldException e) { log.debug(clazz + " has no serialVersionUID"); return null; } }
/** * Returns the packages which were loaded by the classloader. <br/> * Unfortunately ClassLoader.getPackages() is protected - ok, let's do the * hard way using reflexion. * * @return array with the loaded packages */ public Package[] getLoadedPackageArray() { Method method; Class<?> cloaderClass = classLoader.getClass(); try { method = ReflectionHelper.getMethod(cloaderClass, "getPackages"); } catch (NoSuchMethodException nsme) { log.warn(cloaderClass + "#getPackages() not found", nsme); return new Package[0]; } catch (SecurityException e) { log.warn("can't get method " + cloaderClass + "#getPackages()", e); return new Package[0]; } try { method.setAccessible(true); return (Package[]) method.invoke(classLoader); } catch (Exception e) { log.warn("can't access " + method, e); return new Package[0]; } }
for (int i = 0; i < fields.length; i++) { fields[i].setAccessible(true); if (ReflectionHelper.isStatic(fields[i])) { continue;
/** * If it can it returns the value of the given field. If not it throws an * exception. * * @param obj the obj * @param name the name * * @return the field value * * @throws NoSuchFieldException the no such field exception * @throws IllegalAccessException the illegal access exception * @throws IllegalArgumentException the illegal argument exception */ public static Object getFieldValue(Object obj, String name) throws NoSuchFieldException, IllegalArgumentException, IllegalAccessException { Field field = ReflectionHelper.getField(obj.getClass(), name); return field.get(obj); }