I've been having a play with something called Janino which is a pure java, java compiler, which will let you compile java from within other code.
However, it's not very nice, so is probably fairly useless for most needs.
Just in case someone's actually interested in seeing how:
Code:import org.codehaus.janino.*;
import java.lang.reflect.*;
Object a;
String code="import processing.core.*; public class Foo { PApplet p; public Foo(PApplet _p) { p=_p; } public void doodle() { p.g.rect(p.random(0,p.g.width),p.random(0,p.g.height),5,5); } }";
void setup()
{
size(200,200);
try
{
Scanner scanner = new Scanner(null, new StringReader(code));
ClassLoader CL = getClass().getClassLoader();
SimpleCompiler compiler = new SimpleCompiler(scanner, CL);
ClassLoader loader = compiler.getClassLoader();
Class theclass = loader.loadClass("Foo");
Constructor[] c=theclass.getConstructors();
a=c[0].newInstance(new Object[]{this});
}
catch(Exception e)
{
println(e);
}
}
void draw()
{
try
{
a.getClass().getMethod("doodle",null).invoke(a,null);
}
catch(Exception e)
{
println(e);
}
}
Edit: You'll need to get the janino.jar and put it in the code folder for it to work of course...