List of usage examples for javax.tools JavaFileManager getClassLoader
ClassLoader getClassLoader(Location location);
From source file:net.nicholaswilliams.java.licensing.encryption.TestRSAKeyPairGenerator.java
@Test public void testGenerateJavaCode01() throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException { String code = this.generator.generateJavaCode(null, "TestGenerateJavaCode01", null, null, "public final String testMethod01(String concatenate)", "concatenate + \" some other cool text.\""); assertNotNull("The code should not be null.", code); assertTrue("The code should have length.", code.length() > 0); assertEquals("The code is not correct.", "public final class TestGenerateJavaCode01\r\n" + "{\r\n" + "\tpublic final String testMethod01(String concatenate)\r\n" + "\t{\r\n" + "\t\treturn concatenate + \" some other cool text.\";\r\n" + "\t}\r\n" + "}", code);// w ww. j a v a 2 s. c o m JavaFileManager compiled = this.compileClass("TestGenerateJavaCode01", code); Class<?> classObject = compiled.getClassLoader(null).loadClass("TestGenerateJavaCode01"); System.out.println("Successfully compiled " + classObject.toString() + "."); Object instance = classObject.newInstance(); Method method = classObject.getMethod("testMethod01", String.class); String value = (String) method.invoke(instance, "Test text and"); assertEquals("The returned value is not correct (1).", "Test text and some other cool text.", value); value = (String) method.invoke(instance, "More text plus"); assertEquals("The returned value is not correct (2).", "More text plus some other cool text.", value); }
From source file:net.nicholaswilliams.java.licensing.encryption.TestRSAKeyPairGenerator.java
@Test public void testGenerateJavaCode02() throws ClassNotFoundException, IllegalAccessException, InstantiationException, InterruptedException { String code = this.generator.generateJavaCode("com.nicholaswilliams.java.mock", "TestGenerateJavaCode02", "TestRSAKeyPairGenerator.TestDynamicCompileInterface", new String[] { "net.nicholaswilliams.java.licensing.encryption.TestRSAKeyPairGenerator" }, "public long getSystemTimeInSeconds()", "System.currentTimeMillis() / 1000L"); assertNotNull("The code should not be null.", code); assertTrue("The code should have length.", code.length() > 0); assertEquals("The code is not correct.", "package com.nicholaswilliams.java.mock;\r\n" + "\r\n" + "import net.nicholaswilliams.java.licensing.encryption.TestRSAKeyPairGenerator;\r\n" + "\r\n" + "public final class TestGenerateJavaCode02 implements TestRSAKeyPairGenerator.TestDynamicCompileInterface\r\n" + "{\r\n" + "\t@Override\r\n" + "\tpublic long getSystemTimeInSeconds()\r\n" + "\t{\r\n" + "\t\treturn System.currentTimeMillis() / 1000L;\r\n" + "\t}\r\n" + "}", code); JavaFileManager compiled = this.compileClass("com.nicholaswilliams.java.mock.TestGenerateJavaCode02", code); Class<?> classObject = compiled.getClassLoader(null) .loadClass("com.nicholaswilliams.java.mock.TestGenerateJavaCode02"); System.out.println("Successfully compiled " + classObject.toString() + "."); TestDynamicCompileInterface instance = (TestDynamicCompileInterface) classObject.newInstance(); long seconds = System.currentTimeMillis() / 1000L; assertTrue("The return value is not correct (1).", instance.getSystemTimeInSeconds() >= seconds); Thread.sleep(1000);// w w w. ja v a2 s. com assertTrue("The return value is not correct (2).", instance.getSystemTimeInSeconds() >= (seconds + 1)); }
From source file:com.cisco.oss.foundation.logging.structured.AbstractFoundationLoggingMarker.java
private static Class<FoundationLoggingMarkerFormatter> generateMarkerClass(String sourceCode, String newClassName) {/* w w w . j a va 2 s . com*/ try { // We get an instance of JavaCompiler. Then // we create a file manager // (our custom implementation of it) JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); JavaFileManager fileManager = new ClassFileManager(compiler.getStandardFileManager(null, null, null)); // Dynamic compiling requires specifying // a list of "files" to compile. In our case // this is a list containing one "file" which is in our case // our own implementation (see details below) List<JavaFileObject> jfiles = new ArrayList<JavaFileObject>(); jfiles.add(new DynamicJavaSourceCodeObject(newClassName, sourceCode)); List<String> optionList = new ArrayList<String>(); // set compiler's classpath to be same as the runtime's optionList.addAll(Arrays.asList("-classpath", System.getProperty("java.class.path"))); // We specify a task to the compiler. Compiler should use our file // manager and our list of "files". // Then we run the compilation with call() compiler.getTask(null, fileManager, null, optionList, null, jfiles).call(); // Creating an instance of our compiled class and // running its toString() method Class<FoundationLoggingMarkerFormatter> clazz = (Class<FoundationLoggingMarkerFormatter>) fileManager .getClassLoader(null).loadClass(newClassName); return clazz; } catch (Exception e) { throw new UnsupportedOperationException("can't create class of: " + newClassName, e); } }