Example usage for org.apache.commons.jci.problems CompilationProblem isError

List of usage examples for org.apache.commons.jci.problems CompilationProblem isError

Introduction

In this page you can find the example usage for org.apache.commons.jci.problems CompilationProblem isError.

Prototype

boolean isError();

Source Link

Document

is the problem an error and compilation cannot continue or just a warning and compilation can proceed

Usage

From source file:me.jaimegarza.syntax.test.java.TestJavaLexerModes.java

@Test
public void testJavaLexerModes() throws ParsingException, AnalysisException, OutputException,
        MalformedURLException, ClassNotFoundException, InstantiationException, IllegalAccessException,
        NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException {
    generateLanguageFile(args);/* w  ww. j  a v  a2 s .  co  m*/
    File source = new File(tmpLanguageFile);
    File sourceDir = source.getParentFile();
    CompilationResult result = compileJavaFile(source, sourceDir);

    if (result.getErrors().length > 0) {
        for (CompilationProblem problemo : result.getErrors()) {
            if (problemo.isError()) {
                System.err.println(problemo.toString());
            }
        }
        Assert.fail("Errors during the compilation of the output java file");
    }

    URL urls[] = new URL[1];
    urls[0] = sourceDir.toURI().toURL();
    URLClassLoader classLoader = URLClassLoader.newInstance(urls, this.getClass().getClassLoader());
    String className = FilenameUtils.getBaseName(tmpLanguageFile);
    Class<?> clazz = classLoader.loadClass(className);
    Object parser = clazz.newInstance();
    //Method setVerbose = parser.getClass().getMethod("setVerbose", boolean.class);
    Method parse = parser.getClass().getMethod("parse");
    Method getOutput = parser.getClass().getMethod("getOutput");
    //setVerbose.invoke(parser, true);
    Object o = parse.invoke(parser);
    Assert.assertTrue(o instanceof Integer);
    int rc = (Integer) o;
    Assert.assertEquals(rc, 1, "Parse did not succeed");
    o = getOutput.invoke(parser);
    Assert.assertTrue(o instanceof String);
    String s = (String) o;
    Assert.assertEquals(s, "bacaab", "string does not match");
}

From source file:me.jaimegarza.syntax.test.java.TestJavaExpandedParser.java

@Test
public void test02Compile() throws ParsingException, AnalysisException, OutputException {
    generateLanguageFile(expandedArgs);/*  ww w  . j av a  2 s .c o m*/

    File source = new File(tmpLanguageFile);
    File sourceDir = source.getParentFile();
    CompilationResult result = compileJavaFile(source, sourceDir);

    if (result.getErrors().length > 0) {
        for (CompilationProblem problemo : result.getErrors()) {
            if (problemo.isError()) {
                System.err.println(problemo.toString());
            }
        }
        Assert.fail("Errors during the compilation of the output java file");
    }
}

From source file:me.jaimegarza.syntax.test.java.TestJavaExpandedScanner.java

@Test
public void test02Compile() throws ParsingException, AnalysisException, OutputException {
    generateLanguageFile(packedArgs);// w w  w.  java2s . co m

    File source = new File(tmpLanguageFile);
    File sourceDir = source.getParentFile();
    CompilationResult result = compileJavaFile(source, sourceDir);

    if (result.getErrors().length > 0) {
        for (CompilationProblem problemo : result.getErrors()) {
            if (problemo.isError()) {
                System.err.println(problemo.toString());
            }
        }
        Assert.fail("Errors during the compilation of the output java file");
    }
}

From source file:org.apache.commons.jci.examples.commandline.CommandlineCompiler.java

public static void main(String[] args) throws Exception {

    final Options options = new Options();

    options.addOption(OptionBuilder.withArgName("a.jar:b.jar").hasArg().withValueSeparator(':')
            .withDescription("Specify where to find user class files").create("classpath"));

    options.addOption(OptionBuilder.withArgName("release").hasArg()
            .withDescription("Provide source compatibility with specified release").create("source"));

    options.addOption(OptionBuilder.withArgName("release").hasArg()
            .withDescription("Generate class files for specific VM version").create("target"));

    options.addOption(OptionBuilder.withArgName("path").hasArg()
            .withDescription("Specify where to find input source files").create("sourcepath"));

    options.addOption(OptionBuilder.withArgName("directory").hasArg()
            .withDescription("Specify where to place generated class files").create("d"));

    options.addOption(OptionBuilder.withArgName("num").hasArg()
            .withDescription("Stop compilation after these number of errors").create("Xmaxerrs"));

    options.addOption(OptionBuilder.withArgName("num").hasArg()
            .withDescription("Stop compilation after these number of warning").create("Xmaxwarns"));

    options.addOption(OptionBuilder.withDescription("Generate no warnings").create("nowarn"));

    //        final HelpFormatter formatter = new HelpFormatter();
    //        formatter.printHelp("jci", options);

    final CommandLineParser parser = new GnuParser();
    final CommandLine cmd = parser.parse(options, args, true);

    ClassLoader classloader = CommandlineCompiler.class.getClassLoader();
    File sourcepath = new File(".");
    File targetpath = new File(".");
    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();
            }/*from w w  w .j a  v a  2s  . c o m*/
            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");

}