创建包装器 class 以捕获方法执行时间
Creating a wrapper class to capture method execution time
对于我的作业,我必须创建一个程序,将 class 包装到另一个 class 文件 (clsB.java) 中。我为它提供了我的基本 class (cls.java) 的名称,它以一种方式包装了所有方法,使它们计算工作时已经过去的时间。它应该是这样的:
int get(int arg) {
long startTime = System.currentTimeMillis();
parent::get(...) =
System.out.println("Time passed = " + System.currentTimeMillis() - startTime);
}
我尝试使用 Class 对象并提取 cls.java 代码的所有部分,以便将它们简单地写入我创建的文件中,但我找不到任何方法来添加具有的方法任何论据。有什么办法可以按照我的方式去做,还是我应该寻找其他想法?提前感谢您的所有回答:)
我将以String
class为例。
包装方法
public static void getClassToWrap(Class<?> cls) {
try {
Object obj = cls.newInstance();
obj = "helloWorld";
Method reflectedMethod = cls.getDeclaredMethod("hashCode");
System.out.println(reflectedMethod.invoke(obj));
} catch (Exception ex) {
System.out.println("stack trace");
ex.printStackTrace();
}
}
计时器方法
public static long getMethodTimes(Class c) {
long startTime = currentTimeMillis();
getClassToWrap(c);
System.out.println("Time passed = " + (currentTimeMillis() - startTime));
System.out.println(startTime);
return startTime;
}
使用 getMethodTimes(String.class);
在 main method()
中执行
编辑以响应 OP
How can I dynamically create a new .java file from a Java program?
You cannot create new classes or methods using the reflection APIs.
They are not designed for this. (The Class and Method APIs are for
performing operations on object instances in a dynamic fashion.)
If you want to create new code on the fly, there are two basic
approaches to doing this:
Generate Java source code, write it to a file, use the Java compiler
to compile it to a bytecode file, and then load the bytecodes. (There
are standard APIs for running the Java compiler within the JVM of a
running application.)
Use BCEL or equivalent to construct a bytecode file from scratch, and
then load the bytecodes.
Both approaches are tricky and computationally expensive. The BCEL
approach is particularly tricky because you need to understand a lot
about the JVM to do the job.
只需在原始 class 上调用 super
。
class Original
{
public int add(int a, int b)
{
return a+b;
}
}
class Wrapper extends Original
{
public int add(int a, int b)
{
long startTime = System.currentTimeMillis();
int r = super.add(a,a);
System.out.println("Time passed = " + System.currentTimeMillis() - startTime);
return r;
}
}
对于我的作业,我必须创建一个程序,将 class 包装到另一个 class 文件 (clsB.java) 中。我为它提供了我的基本 class (cls.java) 的名称,它以一种方式包装了所有方法,使它们计算工作时已经过去的时间。它应该是这样的:
int get(int arg) {
long startTime = System.currentTimeMillis();
parent::get(...) =
System.out.println("Time passed = " + System.currentTimeMillis() - startTime);
}
我尝试使用 Class 对象并提取 cls.java 代码的所有部分,以便将它们简单地写入我创建的文件中,但我找不到任何方法来添加具有的方法任何论据。有什么办法可以按照我的方式去做,还是我应该寻找其他想法?提前感谢您的所有回答:)
我将以String
class为例。
包装方法
public static void getClassToWrap(Class<?> cls) {
try {
Object obj = cls.newInstance();
obj = "helloWorld";
Method reflectedMethod = cls.getDeclaredMethod("hashCode");
System.out.println(reflectedMethod.invoke(obj));
} catch (Exception ex) {
System.out.println("stack trace");
ex.printStackTrace();
}
}
计时器方法
public static long getMethodTimes(Class c) {
long startTime = currentTimeMillis();
getClassToWrap(c);
System.out.println("Time passed = " + (currentTimeMillis() - startTime));
System.out.println(startTime);
return startTime;
}
使用 getMethodTimes(String.class);
main method()
中执行
编辑以响应 OP
How can I dynamically create a new .java file from a Java program?
You cannot create new classes or methods using the reflection APIs. They are not designed for this. (The Class and Method APIs are for performing operations on object instances in a dynamic fashion.)
If you want to create new code on the fly, there are two basic approaches to doing this:
Generate Java source code, write it to a file, use the Java compiler to compile it to a bytecode file, and then load the bytecodes. (There are standard APIs for running the Java compiler within the JVM of a running application.)
Use BCEL or equivalent to construct a bytecode file from scratch, and then load the bytecodes.
Both approaches are tricky and computationally expensive. The BCEL approach is particularly tricky because you need to understand a lot about the JVM to do the job.
只需在原始 class 上调用 super
。
class Original
{
public int add(int a, int b)
{
return a+b;
}
}
class Wrapper extends Original
{
public int add(int a, int b)
{
long startTime = System.currentTimeMillis();
int r = super.add(a,a);
System.out.println("Time passed = " + System.currentTimeMillis() - startTime);
return r;
}
}