I am new to java and am not able to figure out why the second way to iterate through parameters throws an exception.
Interestingly param !=null
passes however i get ArrayIndexOutofBounds
exception later.
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("}");
}
}