/** * Returns JAR archive structure. * * @param jarClass any class within the JAR * @param allowedExtensions list of extension filters * @param allowedPackgages list of allowed packages * @param listener jar download listener * @return JAR archive structure */ public static JarStructure getJarStructure( final Class jarClass, final List<String> allowedExtensions, final List<String> allowedPackgages, final FileDownloadListener listener) { try { final CodeSource src = jarClass.getProtectionDomain().getCodeSource(); if (src != null) { // Creating structure // Source url final URL jarUrl = src.getLocation(); final URI uri = jarUrl.toURI(); // Source file final File jarFile; final String scheme = uri.getScheme(); if (scheme != null && scheme.equalsIgnoreCase("file")) { // Local jar-file jarFile = new File(uri); } else { // Remote jar-file jarFile = FileUtils.downloadFile( jarUrl.toString(), File.createTempFile("jar_file", ".tmp"), listener); } // Creating final JarEntry jarEntry = new JarEntry(JarEntryType.jarEntry, jarFile.getName()); final JarStructure jarStructure = new JarStructure(jarEntry); jarStructure.setJarLocation(jarFile.getAbsolutePath()); // Reading all entries and parsing them into structure final ZipInputStream zip = new ZipInputStream(jarUrl.openStream()); ZipEntry zipEntry; while ((zipEntry = zip.getNextEntry()) != null) { final String entryName = zipEntry.getName(); if (isAllowedPackage(entryName, allowedPackgages) && (zipEntry.isDirectory() || isAllowedExtension(entryName, allowedExtensions))) { parseElement(jarEntry, entryName, zipEntry); } } zip.close(); return jarStructure; } } catch (final IOException e) { FlatLafLogger.error(ReflectUtils.class, e); } catch (final URISyntaxException e) { FlatLafLogger.error(ReflectUtils.class, e); } return null; }
/** * Returns specified class field's type. This method will also look for the field in super-classes * if any exist. * * @param classType type of the class where field can be located * @param fieldName field name * @return specified class field's type */ public static Class<?> getFieldTypeSafely(final Class classType, final String fieldName) { try { return getFieldType(classType, fieldName); } catch (final NoSuchFieldException e) { if (safeMethodsLoggingEnabled) { FlatLafLogger.warn("ReflectionUtils method failed: getFieldTypeSafely", e); } return null; } }
/** * Returns newly created class instance. * * @param theClass class to process * @param arguments class constructor arguments * @return newly created class instance */ public static <T> T createInstanceSafely(final Class theClass, final Object... arguments) { try { return createInstance(theClass, arguments); } catch (final Throwable e) { if (safeMethodsLoggingEnabled) { FlatLafLogger.warn("ReflectionUtils method failed: createInstanceSafely", e); } return null; } }
/** * Returns class for the specified canonical name. * * @param canonicalName class canonical name * @return class for the specified canonical name */ public static Class getClassSafely(final String canonicalName) { try { return Class.forName(canonicalName); } catch (final ClassNotFoundException e) { if (safeMethodsLoggingEnabled) { FlatLafLogger.warn("ReflectionUtils method failed: getClassSafely", e); } return null; } }
/** * Returns static field value from the specified class. * * @param classType class type * @param fieldName class field name * @return static field value from the specified class */ public static <T> T getStaticFieldValueSafely(final Class classType, final String fieldName) { try { return getStaticFieldValue(classType, fieldName); } catch (final Throwable e) { if (safeMethodsLoggingEnabled) { FlatLafLogger.warn("ReflectionUtils method failed: getStaticFieldValueSafely", e); } return null; } }
/** * Returns object field value. This method allows to access even private object fields. * * @param object object instance * @param field object field * @param <T> field value type * @return object field value */ public static <T> T getFieldValueSafely(final Object object, final String field) { try { return getFieldValue(object, field); } catch (final Throwable e) { if (safeMethodsLoggingEnabled) { FlatLafLogger.warn("ReflectionUtils method failed: getFieldValueSafely", e); } return null; } }
/** * Returns result given by called method. * * @param object object instance * @param methodName method name * @param arguments method arguments * @return result given by called method */ public static <T> T callMethodSafely( final Object object, final String methodName, final Object... arguments) { try { return callMethod(object, methodName, arguments); } catch (final Throwable e) { if (safeMethodsLoggingEnabled) { FlatLafLogger.warn("ReflectionUtils method failed: callMethodSafely", e); } return null; } }
/** * Returns object's method with the specified name and arguments. If method is not found in the * object class all superclasses will be searched for that method. * * @param aClass object class * @param methodName method name * @param arguments method arguments * @return object's method with the specified name and arguments */ public static Method getMethodSafely( final Class aClass, final String methodName, final Object... arguments) { try { return getMethod(aClass, methodName, arguments); } catch (final Throwable e) { if (safeMethodsLoggingEnabled) { FlatLafLogger.warn("ReflectionUtils method failed: getMethodSafely", e); } return null; } }
/** * Applies specified value to object field. This method allows to access and modify even private * object fields. * * @param object object instance * @param field object field * @param value field value * @return true if value was applied successfully, false otherwise */ public static boolean setFieldValueSafely( final Object object, final String field, final Object value) { try { setFieldValue(object, field, value); return true; } catch (final Throwable e) { if (safeMethodsLoggingEnabled) { FlatLafLogger.warn("ReflectionUtils method failed: setFieldValueSafely", e); } return false; } }
/** * Returns JAR location File for the specified class. * * @param jarClass any class from that JAR * @return JAR location File */ public static File getJarLocationFile(final Class jarClass) { try { final CodeSource src = jarClass.getProtectionDomain().getCodeSource(); if (src != null) { final URL jarUrl = src.getLocation(); final URI uri = jarUrl.toURI(); final String scheme = uri.getScheme(); if (scheme != null && scheme.equalsIgnoreCase("file")) { return new File(uri); } } } catch (final URISyntaxException e) { FlatLafLogger.error(ReflectUtils.class, e); } return null; }