2013-07-29 72 views
1

我需要将代码发送到另一个方法,因为类中的方法数量可能会发生变化,所以我使用反射。但问题是我不能枚举它们在循环中benchmarkMethods.get(i).invoke(set, null);,因为它有可能给我匿名类只能传递最终变量。我在这种情况下做什么?是否有可能将非最终变量传递给匿名类的方法?

public void RunAllBenchmarks(final Object set, boolean randomOrder) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { 
    final List<Method> benchmarkMethods = new ArrayList<Method >(); 
    final Class benchClass = set.getClass(); 
    Method[] methods = benchClass.getDeclaredMethods(); 
    for (int i = 0; i < methods.length; i++){ 
     Annotation[] annotations = methods[i].getAnnotations(); 
     if (annotations.length != 0){ 
      for (int j = 0; j < annotations.length; j++) 
      if (annotations[j].annotationType().toString().contentEquals("interface Benchmark")) 
       benchmarkMethods.add(methods[i]); 
     } 
    } 
    for (int i = 0; i < benchmarkMethods.size(); i++){ 
     String name = null; 
     Method method = benchmarkMethods.listIterator().next(); 
     MeasureAndRecord(name, new IFunc() { 
      @Override 
      public Object onEvent() throws InvocationTargetException, IllegalAccessException, NoSuchMethodException { 

       return benchmarkMethods.get(i).invoke(set, null); 
      } 
     }); 

    } 

    PrintWriter writer = new PrintWriter(System.out); 
    PrintResults(writer); 
} 
+2

看起来您正在解决您正在处理的实际问题的解决方案。你能否尝试用应用反射描述你试图解决的问题,而不是应用反射的问题? – dasblinkenlight

+0

我正在写一个基准测试,有可能没有反射,但使用它我可以在一个单独的类中添加新的方法。 –

+0

我刚刚开始学习Java,我想知道你是否可以在这里想出一些东西。例如。 Net,它使用委托可以通过,并且一个局部变量 –

回答

0

一种方法可以避免匿名内部类。即有一类MethodIFunc implements IFunc调用该方法在其onEvent()

for (...) { 
    Method method = ... 

    // this class calls method.invoke(..) inside its onEvent() method 
    MethodIFunc mi = new MethodIFunc(method, set); 
    MeasureAndRecord(name, mi); 
} 

另一种方法(顺便说一句:你在第二for-loop双迭代我认为这是不实际的代码)可以用for-each声明允许变量被声明为最终的:

for (final Method method : benchmarkMethods) { 
    ... 
    MeasureAndRecord(name, new IFunc() { 
     @Override 
     public Object onEvent() throws InvocationTargetException, IllegalAccessException, NoSuchMethodException { 
      return method.invoke(set, (Object[]) null); 
     } 
    }); 

} 
0

你可以使用一个可变的int包装,例如

final AtomicInteger ai = new AtomicInteger(); 
for (int i = 0; i < benchmarkMethods.size(); i++){ 
    String name = null; 
    Method method = benchmarkMethods.listIterator().next(); 
    ai.set(i); 
    MeasureAndRecord(name, new IFunc() { 
     @Override 
     public Object onEvent() throws InvocationTargetException, IllegalAccessException, NoSuchMethodException { 
      return benchmarkMethods.get(ai.get()).invoke(set, null); 
     } 
    }); 
} 
1

我想我会补充说,有一个技巧,我用非最终变量传递到匿名类。以上面你写的一段代码为例,我做了一些小改动来说明我是如何做到的。

我在这里measureAndRecord假设是一个方法(我decapitalised的第一个字符以表示)和IFunc是要创建一个匿名扩展的类。我还假设Method method ...应该是你想传递给你的匿名类的方法(因为在你的代码中它什么都不做)。

这里的诀窍是添加一个名为init的新方法,您可以将变量传递给(最终或非最终)并返回this(在本例中为匿名类)。会发生什么情况是匿名类被调用,立即调用init方法,然后返回并使用该对象(根据需要是一个IFunc)。

必须呼吁建设(分配之前)init方法,你不能链方法,以这种方式在一起。原因是一旦该对象从第一个方法调用返回,它就是您创建的匿名子类的一个IFunc,而不是,因此Java将不再识别除此之外的任何重写方法。

无论如何,玩一玩。我发现它很非常方便,如果有点作弊。即使Java 8出现并允许几乎最终的变量被传递,这仍然可以用于变量变量。

... 

for (int i = 0; i < benchmarkMethods.size(); i++) { 
    String name = null; 
    Method method = benchmarkMethods.get(i); // Assumption on my part 

    measureAndRecord(name, new IFunc() { 
     // Anonymous class variables matching what we want to pass 
     Method method; 
     int i; 

     // Cheating "Constructor" 
     public IFunc init (Method method, int i) { 
      this.method = method; 
      this.i = i; 
      return this; // Must return this for assignment 
     } 

     @Override 
     public Object onEvent() throws InvocationTargetException,  
      IllegalAccessException, NoSuchMethodException { 
       return method.invoke(set, null); 
      } 
     }.init(method, i)); // Note that init is called BEFORE assignment 
    } 
} 

... 
相关问题