List of usage examples for org.apache.commons.jci.compilers CompilationResult getErrors
public CompilationProblem[] getErrors()
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 w w. ja v a 2 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.TestJavaRegexTokenizer.java
@Test public void test01WithExecute() throws ParsingException, AnalysisException, OutputException, MalformedURLException, ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException { generateLanguageFile(expandedArgs);//www . j a v a2 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 cr : result.getErrors()) { System.err.println(cr); } } Assert.assertEquals(result.getErrors().length, 0, "Syntax errors found trying to execute"); 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 getExpr = parser.getClass().getMethod("getExpr"); //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 = getExpr.invoke(parser); Assert.assertTrue(o instanceof String); String s = (String) o; Assert.assertEquals(s, "EABCDFGIA", "string does not match"); }
From source file:me.jaimegarza.syntax.test.java.TestLalr.java
@Test public void test01WithExecute() throws ParsingException, AnalysisException, OutputException, MalformedURLException, ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException { generateLanguageFile(expandedArgs);/*w w w . j ava 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 cr : result.getErrors()) { System.err.println(cr); } } Assert.assertEquals(result.getErrors().length, 0, "Syntax errors found trying to execute"); 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 getExpr = parser.getClass().getMethod("getExpr"); 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 = getExpr.invoke(parser); Assert.assertTrue(o instanceof String); String s = (String) o; Assert.assertEquals(s, "(abc)d", "string does not match"); }
From source file:me.jaimegarza.syntax.test.java.TestJavaExpandedParser.java
@Test public void test02Compile() throws ParsingException, AnalysisException, OutputException { generateLanguageFile(expandedArgs);//from w ww .jav 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.TestJavaPackedParser.java
@Test public void test03Runtime() throws ParsingException, AnalysisException, OutputException, MalformedURLException, ClassNotFoundException, InstantiationException, IllegalAccessException, SecurityException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException { generateLanguageFile(packedArgs);//from w w w . ja va 2s .co m File source = new File(tmpLanguageFile); File sourceDir = source.getParentFile(); CompilationResult result = compileJavaFile(source, sourceDir); Assert.assertEquals(result.getErrors().length, 0, "Syntax errors found trying to execute"); 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 getTotal = parser.getClass().getMethod("getTotal"); setVerbose.invoke(parser, true); parse.invoke(parser); Object o = getTotal.invoke(parser); Assert.assertTrue(o instanceof Integer); Integer i = (Integer) o; Assert.assertEquals((int) i, -17, "total does not match"); }
From source file:me.jaimegarza.syntax.test.java.TestJavaExpandedParser.java
@Test public void test03Runtime() throws ParsingException, AnalysisException, OutputException, MalformedURLException, ClassNotFoundException, InstantiationException, IllegalAccessException, SecurityException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException { generateLanguageFile(expandedArgs);//from www . jav a 2s. c o m File source = new File(tmpLanguageFile); File sourceDir = source.getParentFile(); CompilationResult result = compileJavaFile(source, sourceDir); Assert.assertEquals(result.getErrors().length, 0, "Syntax errors found trying to execute"); 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 getTotal = parser.getClass().getMethod("getTotal"); setVerbose.invoke(parser, true); parse.invoke(parser); Object o = getTotal.invoke(parser); Assert.assertTrue(o instanceof Integer); Integer i = (Integer) o; Assert.assertEquals((int) i, -17, "total does not match"); }
From source file:me.jaimegarza.syntax.test.java.TestJavaExpandedScanner.java
@Test public void test02Compile() throws ParsingException, AnalysisException, OutputException { generateLanguageFile(packedArgs);//from w ww.j a va 2s . c om 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 test03Runtime() throws ParsingException, AnalysisException, OutputException, MalformedURLException, ClassNotFoundException, InstantiationException, IllegalAccessException, SecurityException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException { generateLanguageFile(packedArgs);/*from w w w . j a v a 2 s.c o m*/ File source = new File(tmpLanguageFile); File sourceDir = source.getParentFile(); CompilationResult result = compileJavaFile(source, sourceDir); Assert.assertEquals(result.getErrors().length, 0, "Syntax errors found trying to execute"); 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); String lexicalClassName = className + "$StackElement"; Class<?> lexicalClazz = classLoader.loadClass(lexicalClassName); Object parser = clazz.newInstance(); Method setVerbose = parser.getClass().getMethod("setVerbose", boolean.class); Method init = parser.getClass().getMethod("init"); Method parse = parser.getClass().getMethod("parse", Integer.TYPE, lexicalClazz); Method getValidTokens = parser.getClass().getMethod("getValidTokens"); Method getTotal = parser.getClass().getMethod("getTotal"); setVerbose.invoke(parser, true); init.invoke(parser); for (Parameter p : parameters) { int[] tokens = (int[]) getValidTokens.invoke(parser); Assert.assertTrue(arrayContains(tokens, p.token), "Token " + p.token + " ain't there"); Object lexicalValue = lexicalClazz.newInstance(); Method setNumber = lexicalClazz.getMethod("setNumber", Integer.TYPE); setNumber.invoke(lexicalValue, p.value); parse.invoke(parser, p.token, lexicalValue); Object t = getTotal.invoke(parser); Assert.assertEquals(((Integer) t).intValue(), p.result, "Result is not " + p.result); } Object o = getTotal.invoke(parser); Assert.assertTrue(o instanceof Integer); Integer i = (Integer) o; Assert.assertEquals((int) i, -17, "total does not match"); }
From source file:nl.tue.gale.common.code.JavaCodeUtil.java
private static byte[] compile(final String code) { final List<byte[]> result = new LinkedList<byte[]>(); CompilationResult cr = compiler.compile(new String[] { "Main.java" }, new ResourceReader() { public byte[] getBytes(String arg0) { try { if ("Main.java".equals(arg0)) return code.getBytes("UTF-8"); else return null; } catch (UnsupportedEncodingException e) { return null; }/*ww w.j a v a2s . c o m*/ } public boolean isAvailable(String arg0) { return true; } }, new ResourceStore() { public byte[] read(String arg0) { return null; } public void remove(String arg0) { } public void write(String arg0, byte[] arg1) { result.add(arg1); } }); if (cr.getErrors().length > 0) throw new IllegalArgumentException("unable to compile code: " + cr.getErrors()[0].getMessage()); return result.get(0); }
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 va2 s. c om 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"); }