Package org.apache.commons.jci.compilers

Examples of org.apache.commons.jci.compilers.JavaCompiler


        String targetResource = (((packageName.length() != 0) ? (packageName + ".") : "")
                    + ctxt.getServletClassName()).replace('.', '/') + ".java";
        String[] resources = new String[] {targetResource};

        JavaCompiler javaCompiler = (new JavaCompilerFactory()).createCompiler(options.getCompiler().substring(4));
        FileResourceReader reader = new FileResourceReader(ctxt.getOptions().getScratchDir());
        FileResourceStore store = new FileResourceStore(ctxt.getOptions().getScratchDir());
        JavaCompilerSettings settings = javaCompiler.createDefaultSettings();
        if (settings == null) {
            settings = new JavaCompilerSettings();
        }
        settings.setDeprecations(false);
        if (ctxt.getOptions().getJavaEncoding() != null) {
            settings.setSourceEncoding(ctxt.getOptions().getJavaEncoding());
        }
        if (ctxt.getOptions().getClassDebugInfo()) {
            // No support
        }
        // Source JVM
        if (ctxt.getOptions().getCompilerSourceVM() != null) {
            settings.setSourceVersion(ctxt.getOptions().getCompilerSourceVM());
        } else {
            // Default to 1.5
            settings.setSourceVersion("1.5");
        }
        // Target JVM
        if (ctxt.getOptions().getCompilerTargetVM() != null) {
            settings.setTargetVersion(ctxt.getOptions().getCompilerTargetVM());
        } else {
            // Default to 1.5
            settings.setTargetVersion("1.5");
        }

        CompilationResult result = javaCompiler.compile(resources, reader, store, classLoader, settings);
       
        ArrayList<JavacErrorDetail> problemList = new ArrayList<JavacErrorDetail>();
        CompilationProblem[] problems = result.getErrors();
        if (problems != null) {
            try {
View Full Code Here


        int maxerrs = 10;
        int maxwarns = 10;
        final boolean nowarn = cmd.hasOption("nowarn");


        final JavaCompiler compiler = new JavaCompilerFactory().createCompiler("eclipse");
        final JavaCompilerSettings settings = compiler.createDefaultSettings();


        for (Iterator it = cmd.iterator(); it.hasNext();) {
            final Option option = (Option) it.next();
            if ("classpath".equals(option.getOpt())) {
                final String[] values = option.getValues();
                final URL[] urls = new URL[values.length];
                for (int i = 0; i < urls.length; i++) {
                    urls[i] = new File(values[i]).toURL();
                }
                classloader = new URLClassLoader(urls);
            } else if ("source".equals(option.getOpt())) {
                settings.setSourceVersion(option.getValue());
            } else if ("target".equals(option.getOpt())) {
                settings.setTargetVersion(option.getValue());
            } else if ("sourcepath".equals(option.getOpt())) {
                sourcepath = new File(option.getValue());
            } else if ("d".equals(option.getOpt())) {
                targetpath = new File(option.getValue());
            } else if ("Xmaxerrs".equals(option.getOpt())) {
                maxerrs = Integer.parseInt(option.getValue());
            } else if ("Xmaxwarns".equals(option.getOpt())) {
                maxwarns = Integer.parseInt(option.getValue());
            }
        }

        final ResourceReader reader = new FileResourceReader(sourcepath);
        final ResourceStore store = new FileResourceStore(targetpath);
       
        final int maxErrors = maxerrs;
        final int maxWarnings = maxwarns;
        compiler.setCompilationProblemHandler(new CompilationProblemHandler() {
            int errors = 0;
            int warnings = 0;
            public boolean handle(final CompilationProblem pProblem) {

                if (pProblem.isError()) {
                    System.err.println(pProblem);

                    errors++;

                    if (errors >= maxErrors) {
                        return false;
                    }
                } else {
                    if (!nowarn) {
                        System.err.println(pProblem);
                    }

                    warnings++;

                    if (warnings >= maxWarnings) {
                        return false;
                    }
                }

                return true;
            }
        });
       
        final String[] resource = cmd.getArgs();
       
        for (int i = 0; i < resource.length; i++) {
            System.out.println("compiling " + resource[i]);
        }
       
        final CompilationResult result = compiler.compile(resource, reader, store, classloader);
       
        System.out.println( result.getErrors().length + " errors");
        System.out.println( result.getWarnings().length + " warnings");

    }
View Full Code Here

        int maxerrs = 10;
        int maxwarns = 10;
        final boolean nowarn = cmd.hasOption("nowarn");


        final JavaCompiler compiler = new JavaCompilerFactory().createCompiler("eclipse");
        final JavaCompilerSettings settings = compiler.createDefaultSettings();


        for (Iterator it = cmd.iterator(); it.hasNext();) {
            final Option option = (Option) it.next();

            if ("classpath".equals(option.getOpt())) {
                final String[] values = option.getValues();
                final URL[] urls = new URL[values.length];
                for (int i = 0; i < urls.length; i++) {
                    urls[i] = new File(values[i]).toURL();
                }
                classloader = new URLClassLoader(urls);
            } else if ("source".equals(option.getOpt())) {
                settings.setSourceVersion(option.getValue());
            } else if ("target".equals(option.getOpt())) {
                settings.setTargetVersion(option.getValue());
            } else if ("sourcepath".equals(option.getOpt())) {
                sourcepath = new File(option.getValue());
            } else if ("d".equals(option.getOpt())) {
                targetpath = new File(option.getValue());
            } else if ("Xmaxerrs".equals(option.getOpt())) {
                maxerrs = Integer.parseInt(option.getValue());
            } else if ("Xmaxwarns".equals(option.getOpt())) {
                maxwarns = Integer.parseInt(option.getValue());
            }
        }

        final ResourceReader reader = new FileResourceReader(sourcepath);
        final ResourceStore store = new FileResourceStore(targetpath);
       
        final int maxErrors = maxerrs;
        final int maxWarnings = maxwarns;
        compiler.setCompilationProblemHandler(new CompilationProblemHandler() {
            int errors = 0;
            int warnings = 0;
            public boolean handle(final CompilationProblem pProblem) {

                if (pProblem.isError()) {
                    System.err.println(pProblem);

                    errors++;

                    if (errors >= maxErrors) {
                        return false;
                    }
                } else {
                    if (!nowarn) {
                        System.err.println(pProblem);
                    }

                    warnings++;

                    if (warnings >= maxWarnings) {
                        return false;
                    }
                }

                return true;
            }
        });
       
        final String[] resource = cmd.getArgs();
       
        for (int i = 0; i < resource.length; i++) {
            System.out.println("compiling " + resource[i]);
        }
       
        final CompilationResult result = compiler.compile(resource, reader, store, classloader);
       
        System.out.println( result.getErrors().length + " errors");
        System.out.println( result.getWarnings().length + " warnings");

    }
View Full Code Here

        String targetResource = (((packageName.length() != 0) ? (packageName + ".") : "")
                    + ctxt.getServletClassName()).replace('.', '/') + ".java";
        String[] resources = new String[] {targetResource};

        JavaCompiler javaCompiler = (new JavaCompilerFactory()).createCompiler(options.getCompiler().substring(4));
        FileResourceReader reader = new FileResourceReader(ctxt.getOptions().getScratchDir());
        FileResourceStore store = new FileResourceStore(ctxt.getOptions().getScratchDir());
        JavaCompilerSettings settings = javaCompiler.createDefaultSettings();
        if (settings == null) {
            settings = new JavaCompilerSettings();
        }
        settings.setDeprecations(false);
        if (ctxt.getOptions().getJavaEncoding() != null) {
            settings.setSourceEncoding(ctxt.getOptions().getJavaEncoding());
        }
        if (ctxt.getOptions().getClassDebugInfo()) {
            // No support
        }
        // Source JVM
        if (ctxt.getOptions().getCompilerSourceVM() != null) {
            settings.setSourceVersion(ctxt.getOptions().getCompilerSourceVM());
        } else {
            // Default to 1.5
            settings.setSourceVersion("1.5");
        }
        // Target JVM
        if (ctxt.getOptions().getCompilerTargetVM() != null) {
            settings.setTargetVersion(ctxt.getOptions().getCompilerTargetVM());
        } else {
            // Default to 1.5
            settings.setTargetVersion("1.5");
        }

        CompilationResult result = javaCompiler.compile(resources, reader, store, classLoader, settings);
       
        ArrayList<JavacErrorDetail> problemList = new ArrayList<JavacErrorDetail>();
        CompilationProblem[] problems = result.getErrors();
        if (problems != null) {
            try {
View Full Code Here

TOP

Related Classes of org.apache.commons.jci.compilers.JavaCompiler

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.