Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.kie.remote.services.rest.jaxb;
  
  import static org.junit.Assert.assertNotNull;
  import static org.junit.Assert.fail;
  
  import java.io.File;
  import java.io.FileWriter;
 import java.net.URI;
 import java.net.URL;
 
 
 import  org.junit.Test;
 
 public class JavaCompilerTest {
 
     @Test
     public void testMoreCompliation() throws Exception { 
         // compile class 
         Class<?> cls = getClassFromSource("NewMyType.java");
        
         // verify definition
         cls.getMethod("getNotText");
         
         // compile class with same name
         cls = getClassFromSource("MyType.java");
         
         // verify definition
         try { 
             cls.getMethod("getNotText");
             fail"The getNotText method should NOT exist here!");
         } catch(Exception e ) { 
             // ignore
         }
         cls.getMethod("getText");
     }
   
    
(runtime) Compilation of a source file (see the *.java files in src/test/resources)

Parameters:
fileName The name of the file
Returns:
A Class instance from the source in the file
Throws:
Exception
 
     public static Class getClassFromSource(String fileNamethrows Exception { 
         String source = getSource(fileName);
     
         // Save source in .java file.
         String tmpDir = System.getProperty("java.io.tmpdir");
         File root = new File(tmpDir"kie-services-remote-tests");
         File sourceFile = new File(root"org/kie/remote/services/rest/jaxb/MyType.java");
         ifsourceFile.exists() ) { 
             sourceFile.delete();
         }
         sourceFile.getParentFile().mkdirs();
         FileWriter fileWriter = null;
         try { 
             fileWriter = new FileWriter(sourceFile);
             fileWriter.append(source);
         } finally { 
             iffileWriter != null ) { 
                 fileWriter.close();
             }
         }
     
         // Compile source file.
         JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
         compiler.run(nullnullnullsourceFile.getPath());
     
         // Load and instantiate compiled class.
         URLClassLoader classLoader = URLClassLoader.newInstance(new URL[] { root.toURI().toURL() });
         Class<?> cls = Class.forName("org.kie.remote.services.rest.jaxb.MyType"trueclassLoader); 
         
         // cleanup
         sourceFile.deleteOnExit();
         root.deleteOnExit();
         
         return cls;
     }

    
Read the file into a String

Parameters:
fileName The filename (in sr/test/resources)
Returns:
The contents of the file
Throws:
Exception
 
     private static String getSource(String fileNamethrows Exception {
         URL fileUrl = JavaCompilerTest.class.getResource("/" + fileName);
         assertNotNull(fileUrl);
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
         InputStreamReader input = null;
         try { 
             URI fileUri = fileUrl.toURI();
            input = new InputStreamReader(new FileInputStream(new File(fileUri)));
    
        OutputStreamWriter output = new OutputStreamWriter(baos);
        char[] buffer = new char[4096];
        int n = 0;
        try {
            while (-1 != (n = input.read(buffer))) {
                output.write(buffer, 0, n);
            }
            output.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
        } finally { 
            ifinput != null ) { 
                input.close();
            }
        }
        return baos.toString();
    }
New to GrepCode? Check out our FAQ X