Example usage for java.io File getClass

List of usage examples for java.io File getClass

Introduction

In this page you can find the example usage for java.io File getClass.

Prototype

@HotSpotIntrinsicCandidate
public final native Class<?> getClass();

Source Link

Document

Returns the runtime class of this Object .

Usage

From source file:com.varaneckas.hawkscope.util.OSUtils.java

/**
 * Tells if file is executable/*w w w . j  a  v a  2  s. c  o  m*/
 * 
 * @param file
 * @return
 */
public static boolean isExecutable(final File file) {
    final String filePath = file.getAbsolutePath().toLowerCase();
    switch (CURRENT_OS) {
    case WIN:
        if (filePath.endsWith(".exe") || filePath.endsWith(".bat") || filePath.endsWith(".msi")) {

            return true;
        } else {
            return false;
        }
    default:
        if (System.getProperty("java.version").compareTo("1.6") >= 0) {
            //we can check if it's executable
            try {
                Method m = file.getClass().getMethod("canExecute", new Class[] {});
                if (m != null) {
                    final boolean result = (Boolean) m.invoke(file, new Object[] {});
                    m = null;
                    return result;
                }
            } catch (final Exception e) {
                log.warn("Failed dynamically calling File.canExecute", e);
                return false;
            }
        }
        return filePath.contains("/bin/");
    }
}

From source file:net.sf.jasperreports.data.hibernate.HibernateDataAdapterService.java

@Override
public void contributeParameters(Map<String, Object> parameters) throws JRException {
    HibernateDataAdapter hbmDA = getHibernateDataAdapter();
    if (hbmDA != null) {
        ClassLoader oldThreadClassLoader = Thread.currentThread().getContextClassLoader();

        try {/* w w w.j a  v a 2  s  .c  o  m*/
            Thread.currentThread().setContextClassLoader(getClassLoader(oldThreadClassLoader));

            Class<?> clazz = null;
            if (!hbmDA.isUseAnnotation()) {
                clazz = JRClassLoader.loadClassForRealName("org.hibernate.cfg.Configuration");
            } else {
                clazz = JRClassLoader.loadClassForRealName("org.hibernate.cfg.AnnotationConfiguration");
            }
            if (clazz != null) {
                Object configure = clazz.getDeclaredConstructor().newInstance();
                if (configure != null) {
                    String xmlFileName = hbmDA.getXMLFileName();
                    if (xmlFileName != null && !xmlFileName.isEmpty()) {
                        File file = new File(xmlFileName);
                        clazz.getMethod("configure", file.getClass()).invoke(configure, file);
                    } else {
                        clazz.getMethod("configure", new Class[] {}).invoke(configure, new Object[] {});
                    }
                    String pFileName = hbmDA.getPropertiesFileName();
                    if (pFileName != null && !pFileName.isEmpty()) {
                        Properties propHibernate = new Properties();
                        propHibernate.load(new FileInputStream(pFileName));

                        clazz.getMethod("setProperties", propHibernate.getClass()).invoke(configure,
                                propHibernate);
                    }

                    Object bsf = clazz.getMethod("buildSessionFactory", new Class[] {}).invoke(configure,
                            new Object[] {});
                    session = bsf.getClass().getMethod("openSession", new Class[] {}).invoke(bsf,
                            new Object[] {});
                    session.getClass().getMethod("beginTransaction", new Class[] {}).invoke(session,
                            new Object[] {});
                    parameters.put(JRHibernateQueryExecuterFactory.PARAMETER_HIBERNATE_SESSION, session);
                }
            }
        } catch (IOException | ClassNotFoundException | InstantiationException | IllegalAccessException
                | IllegalArgumentException | SecurityException | InvocationTargetException
                | NoSuchMethodException e) {
            throw new JRException(e);
        } finally {
            Thread.currentThread().setContextClassLoader(oldThreadClassLoader);
        }
    }
}

From source file:FileErrorManager.java

/**
 * Performs the initialization for this object.
 *///  w ww.ja v  a 2  s .  com
private void init() {
    if (next == null) {
        throw new NullPointerException(ErrorManager.class.getName());
    }

    File dir = this.emailStore;
    if (dir.getClass() != File.class) { //For security reasons.
        throw new IllegalArgumentException(dir.getClass().getName());
    }

    if (!dir.isDirectory()) {
        throw new IllegalArgumentException("File must be a directory.");
    }

    if (!dir.canWrite()) { //Can throw under a security manager.
        super.error(dir.getAbsolutePath(), new SecurityException("write"), ErrorManager.OPEN_FAILURE);
    }

    //For now, only absolute paths are allowed.
    if (!dir.isAbsolute()) {
        throw new IllegalArgumentException("Only absolute paths are allowed.");
    }

    if (!dir.canRead()) { //Can throw under a security manager.
        super.error(dir.getAbsolutePath(), new SecurityException("read"), ErrorManager.OPEN_FAILURE);
    }
}

From source file:com.github.maven_nar.NarUtil.java

public static int copyDirectoryStructure(final File sourceDirectory, final File destinationDirectory,
        final String includes, final String excludes) throws IOException {
    if (!sourceDirectory.exists()) {
        throw new IOException("Source directory doesn't exists (" + sourceDirectory.getAbsolutePath() + ").");
    }/*w  w w .j  a  v a  2s . c o  m*/

    final List files = FileUtils.getFiles(sourceDirectory, includes, excludes);
    final String sourcePath = sourceDirectory.getAbsolutePath();

    int copied = 0;
    for (final Object file1 : files) {
        final File file = (File) file1;
        String dest = file.getAbsolutePath();
        dest = dest.substring(sourcePath.length() + 1);
        final File destination = new File(destinationDirectory, dest);
        if (file.isFile()) {
            // destination = destination.getParentFile();
            // use FileUtils from commons-io, because it preserves timestamps
            org.apache.commons.io.FileUtils.copyFile(file, destination);
            copied++;

            // copy executable bit
            try {
                // 1.6 only so coded using introspection
                // destination.setExecutable( file.canExecute(), false );
                final Method canExecute = file.getClass().getDeclaredMethod("canExecute");
                final Method setExecutable = destination.getClass().getDeclaredMethod("setExecutable",
                        boolean.class, boolean.class);
                setExecutable.invoke(destination, canExecute.invoke(file), Boolean.FALSE);
            } catch (final SecurityException | InvocationTargetException | IllegalAccessException
                    | IllegalArgumentException | NoSuchMethodException e) {
                // ignored
            }
        } else if (file.isDirectory()) {
            if (!destination.exists() && !destination.mkdirs()) {
                throw new IOException(
                        "Could not create destination directory '" + destination.getAbsolutePath() + "'.");
            }
            copied += copyDirectoryStructure(file, destination, includes, excludes);
        } else {
            throw new IOException("Unknown file type: " + file.getAbsolutePath());
        }
    }
    return copied;
}

From source file:org.broadinstitute.gatk.utils.io.IOUtils.java

private static File replacePath(File file, String path) {
    if (file instanceof FileExtension)
        return ((FileExtension) file).withPath(path);
    if (!File.class.equals(file.getClass()))
        throw new GATKException("Sub classes of java.io.File must also implement FileExtension");
    return new File(path);
}

From source file:org.broadinstitute.sting.utils.io.IOUtils.java

private static File replacePath(File file, String path) {
    if (file instanceof FileExtension)
        return ((FileExtension) file).withPath(path);
    if (!File.class.equals(file.getClass()))
        throw new StingException("Sub classes of java.io.File must also implement FileExtension");
    return new File(path);
}

From source file:org.jenkinsci.plugins.pipeline.maven.dao.PipelineMavenPluginH2Dao.java

public PipelineMavenPluginH2Dao(File rootDir) {
    rootDir.getClass(); // check non null

    File databaseFile = new File(rootDir, "jenkins-jobs");
    String jdbcUrl = "jdbc:h2:file:" + databaseFile.getAbsolutePath() + ";AUTO_SERVER=TRUE;MULTI_THREADED=1";
    jdbcConnectionPool = JdbcConnectionPool.create(jdbcUrl, "sa", "sa");
    LOGGER.log(Level.FINE, "Open database {0}", jdbcUrl);

    initializeDatabase();/*from  ww w  .  ja  v a  2s  .c  o  m*/
    testDatabase();
}

From source file:org.rioproject.impl.util.DownloadManager.java

private static void setFileAccess(File f, String setter, boolean allow, boolean ownerOnly) {
    try {// ww  w.  j  a v  a 2 s .  c  o  m
        Method m = f.getClass().getMethod(setter, boolean.class, boolean.class);
        m.invoke(f, allow, ownerOnly);
    } catch (Exception e) {
        e.printStackTrace();
    }

}