Archive

Archive for April, 2012

I am afraid

It’s more than two months since the last post named I Am Sad. I am still not happy for the same thing. I decide to study harder, not only the time and the method. I admit that I have nothing to progress in the past two years, especially the past half year.
I left QD seven months ago, I am afraid to leave again, even more for the save reason.
I’am better now.

Categories: Uncategorized

java动态代理要用到的反射知识备忘

备忘
import java.io.File;
import java.io.FileWriter;
import java.lang.reflect.Constructor;
import java.net.URL;
import java.net.URLClassLoader;

import javax.tools.JavaCompiler;
import javax.tools.JavaCompiler.CompilationTask;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;

public class App {
public static void main(String[] args) throws Exception {
System.out.println("start");

String srcString = "public class xxoo { public void f(){ System.out.println(\"I am C\"); } }";

String dir = System.getProperty("user.dir");

String fileName = dir + "\\src\\xxoo.java";
File file = new File(fileName);
FileWriter fWriter = new FileWriter(file);
fWriter.write(srcString);
fWriter.flush();
fWriter.close();

JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
System.out.println(compiler.getClass().getName());
StandardJavaFileManager fileManager =
compiler.getStandardFileManager(null, null, null);
Iterable units = fileManager.getJavaFileObjects(fileName);
CompilationTask task = compiler.getTask(null, fileManager, null, null, null, units);
task.call();

URL[] ss = new URL[] { new URL("file://" + dir + "\\src")};
URLClassLoader classLoader = new URLClassLoader(ss);
Class c = classLoader.loadClass("App");

Constructor constructor = c.getConstructor(null);
App app = (App)constructor.newInstance(null);

app.p("shitttt");

java.lang.reflect.Method[] ms = c.getMethods();
for (java.lang.reflect.Method method : ms) {
p(method.getName());
}

System.out.println("over");
}

public static void p(Object o){
System.out.println(o);
}
}

Categories: Code