简体   繁体   English

遍历参数数组-Java Reflection

[英]Iterating through Parameter Array - Java Reflection

I am new to java and am not able to figure out why the second way to iterate through parameters throws an exception. 我是java的新手,无法弄清楚为什么第二种遍历参数的方法会引发异常。 Interestingly param !=null passes however i get ArrayIndexOutofBounds exception later. 有趣的是, param !=null通过,但是稍后我得到ArrayIndexOutofBounds异常。 I looked at following link. 我看了下面的链接。 but that's not helping me. 但这对我没有帮助。 Iterating through method parameters 遍历方法参数

import java.lang.reflect.*;

public class TestReflection {

    public static void main(String[] args) {
    Class<ReflectThis> c = (Class<ReflectThis>)ReflectThis.class;
    System.out.println("public class " + c.getName());
    System.out.println("{");
    Method methods[] = c.getMethods();
    for(Method m : methods)
    {
        if(m.isAccessible())
            System.out.print("    public ");
        else
            System.out.print("    private ");

            System.out.print(m.getName()+"(");
        //System.out.println("Parameteres");
        Parameter[] params = null;
        params = m.getParameters();
            //method 1 for iterating thr params
        for(Parameter p : params)
        {
            if(p ==  null)
                break;
            System.out.print(p.getType()+ " " +p.getName()+",");

        }
        params = m.getParameters();
            //method 2 for iterating thr params
    if(params != null)
        {
            for(int i=0; params[i] != null; i++)
            {
                System.out.print(params[i].getType()+ " " +params[i].getName());
                if(params[i+1] != null)
                    System.out.print(", ");
            }
        }
        System.out.println(");");
    }

    System.out.println("}");

    }

}

You're simply looping out of bounds. 您只是在循环越界。

 for(int i=0; params[i] != null; i++)
 {
      System.out.print(params[i].getType()+ " " +params[i].getName());
      if(params[i+1] != null)
           System.out.print(", ");
 }

If you have 2 parameters, ie. 如果您有2个参数,即。 i goes from 0 to 1 , then you will try to access i01 ,那么您将尝试访问

params[1]
params[2] // out of bounds

This will basically always go out of bounds because you try to access one more than the number of elements in the array. 基本上,这总是会超出范围,因为您尝试访问的数组数要多于元素数。

Here's how you can join elements with , correctly: 这里是你如何加入的元素,正确:

Ok, the mistake lies in: 好的,错误在于:

for(int i = 0; params[i].....

This throws an exception if params is empty. 如果params为空,则抛出异常。 Example: params has in elements. 示例:params具有in元素。 Trying to get the 0th element will be out on bounds. 试图获得第0个元素将是无穷无尽的。

To ensure this doesn't happen: 为确保不会发生这种情况:

for(int i = 0; i < params.length && params[i]...

In this line you get an Exception if i is bigger than the last index: 在这一行中,如果我大于最后一个索引,则会得到异常:

for(int i=0; params[i] != null; i++)

you have to change to: 您必须更改为:

for(int i=0; i<params.length; i++)

Looks like intention is to loop on the array and append "," except for last entry. 看起来意图是在数组上循环并附加“,”(最后一项除外)。 Below should work Need to check for boundary condition as well 下面应该工作需要检查边界条件

        for(int i=0; i<params.length; i++)
        {
            if(params[i] != null)
            {
             System.out.print(params[i].getType()+ " " +params[i].getName());

             if(i != params.length-1)
                 System.out.print(", ");
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM