public void call(GroovyClass gclass) throws CompilationFailedException { String name = gclass.getName().replace('.', File.separatorChar) + ".class"; File path = new File(configuration.getTargetDirectory(), name); // // Ensure the path is ready for the file // File directory = path.getParentFile(); if (directory != null && !directory.exists()) { directory.mkdirs(); } // // Create the file and write out the data // byte[] bytes = gclass.getBytes(); try (FileOutputStream stream = new FileOutputStream(path)) { stream.write(bytes, 0, bytes.length); } catch (IOException e) { getErrorCollector().addError(Message.create(e.getMessage(), CompilationUnit.this)); } } };
generatedClasses.add(new GroovyClass(classNode.getName(), bytes));
log.debug(" {}", ((GroovyClass)iter.next()).getName());
/** * A helper method that can be used to evaluate expressions as found in annotation * parameters. For example, it will evaluate a constant, be it referenced directly as * an integer or as a reference to a field. * <p> * If this method throws an exception, then the expression cannot be evaluated on its own. * * @param expr the expression to be evaluated * @param config the compiler configuration * @return the result of the expression */ public static Object evaluateExpression(Expression expr, CompilerConfiguration config) { String className = "Expression$" + UUID.randomUUID().toString().replace('-', '$'); ClassNode node = new ClassNode(className, Opcodes.ACC_PUBLIC, OBJECT_TYPE); ReturnStatement code = new ReturnStatement(expr); addGeneratedMethod(node, "eval", Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC, OBJECT_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, code); CompilerConfiguration copyConf = new CompilerConfiguration(config); CompilationUnit cu = new CompilationUnit(copyConf); cu.addClassNode(node); cu.compile(Phases.CLASS_GENERATION); @SuppressWarnings("unchecked") List<GroovyClass> classes = (List<GroovyClass>) cu.getClasses(); Class aClass = cu.getClassLoader().defineClass(className, classes.get(0).getBytes()); try { return aClass.getMethod("eval").invoke(null); } catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e) { throw new GroovyBugError(e); } }
log.debug(" {}", ((GroovyClass)iter.next()).getName());
public byte[] compileGroovyScript(final String className, final String script) { byte[] compiledScriptBytes = null; CompilationUnit compileUnit = new CompilationUnit(); compileUnit.addSource(className, script); compileUnit.compile(Phases.CLASS_GENERATION); List classes = compileUnit.getClasses(); GroovyClass firstClass = (GroovyClass)classes.get(0); compiledScriptBytes = firstClass.getBytes(); return compiledScriptBytes; }
log.debug(" {}", ((GroovyClass)iter.next()).getName());
protected GroovyClass[] loadGroovyClasses(InputStream jarStream) throws IOException{ //System.out.println("Loading classes from "+f); ArrayList<GroovyClass> out = new ArrayList<GroovyClass>(); byte[] buf = new byte[8192]; JarInputStream jar = new JarInputStream(jarStream); try{ JarEntry entry = null;//Enumeration<JarEntry> entries = jar.entries(); while((entry=jar.getNextJarEntry())!=null){ if(!entry.isDirectory()){ //System.out.println("Reading from jar file: "+entry.getName()); int index = entry.getName().indexOf(".class"); if(index > 0){ String name = entry.getName().substring(0,index).replaceAll("/", "."); ByteArrayOutputStream baos = new ByteArrayOutputStream(); int c; while((c=jar.read(buf))!=-1){ baos.write(buf, 0, c); } out.add(new GroovyClass(name,baos.toByteArray())); //System.out.println("Loaded groovy class "+name); } } } } finally{ jar.close(); } return out.toArray(new GroovyClass[0]); }
log.debug(" {}", ((GroovyClass)iter.next()).getName());
protected GroovyClass[] loadGroovyClasses(InputStream jarStream) throws IOException{ //System.out.println("Loading classes from "+f); ArrayList<GroovyClass> out = new ArrayList<GroovyClass>(); byte[] buf = new byte[8192]; JarInputStream jar = new JarInputStream(jarStream); try{ JarEntry entry = null;//Enumeration<JarEntry> entries = jar.entries(); while((entry=jar.getNextJarEntry())!=null){ if(!entry.isDirectory()){ //System.out.println("Reading from jar file: "+entry.getName()); int index = entry.getName().indexOf(".class"); if(index > 0){ String name = entry.getName().substring(0,index).replaceAll("/", "."); ByteArrayOutputStream baos = new ByteArrayOutputStream(); int c; while((c=jar.read(buf))!=-1){ baos.write(buf, 0, c); } out.add(new GroovyClass(name,baos.toByteArray())); //System.out.println("Loaded groovy class "+name); } } } } finally{ jar.close(); } return out.toArray(new GroovyClass[0]); }
public byte[] compileGroovyScript(final String className, final String script) { byte[] compiledScriptBytes = null; CompilationUnit compileUnit = new CompilationUnit(); compileUnit.addSource(className, script); compileUnit.compile(Phases.CLASS_GENERATION); for (Object compileClass : compileUnit.getClasses()) { GroovyClass groovyClass = (GroovyClass) compileClass; if(className.equals(groovyClass.getName())) { compiledScriptBytes = groovyClass.getBytes(); break; } } return compiledScriptBytes; }
log.debug(" {}", ((GroovyClass)iter.next()).getName());
generatedClasses.add(new GroovyClass(classNode.getName(), bytes));
generatedClasses.add(new GroovyClass(classNode.getName(), bytes));
generatedClasses.add(new GroovyClass(classNode.getName(), bytes));