I have two different classes one is with validating method bellow.
@Validator
public boolean validIdentifier() throws IllegalArgumentException, IllegalAccessException{
boolean flag=false;
Field[] fields=this.data.getClass().getDeclaredFields();
for(Field field : fields) {
if (!field.isAccessible()){
field.setAccessible(true);
}
if (field.getName().endsWith("Identifier") && field.get(data)!=null){
flag=true;
field.setAccessible(false);
break;
}
field.setAccessible(false);
}
return flag;
}
The other one has two methods that reflect on the above validating method like this
public void validate(){
AppValidator validator=new AppValidator(somebean);
doCommonValidation(validator.getClass());
}
public void doCommonValidation(Class clazz){
Method[] methods = clazz.getMethods();
for(Method method:methods) {
for(Annotation annotation:method.getAnnotations()){
try {
if (annotation instanceof Validator && !(boolean)method.invoke(clazz)){ //exception is here
String errorMessage = ((Validator)annotation).message();
String display=((Validator)annotation).displayField();
if(errorMessage != null) {
System.out.println(display+":"+errorMessage);
}
}
} catch (IllegalAccessException | IllegalArgumentException
| InvocationTargetException e) {
e.printStackTrace();
}
}
}
}
The above code throws java.lang.IllegalArgumentException: object is not an instance of declaring class.
If I change doCommonValidation and by-pass validate method like this, everything works.
public void validate() {
try {
AppValidator validator=new AppValidator(someBean);
Method[] methods = validator.getClass().getMethods();
for(Method method:methods) {
for(Annotation annotation:method.getAnnotations()){
if (annotation instanceof ValidatingMethod && !(boolean)method.invoke(validator)){
String errorMessage = ((ValidatingMethod)annotation).message();
String display=((ValidatingMethod)annotation).displayField();
if(errorMessage != null) {
System.out.println(display+":"+errorMessage);
}
}
}
}
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | SecurityException e) {
e.printStackTrace();
return new HashMap<String, String>();
}
}
The issue is that I do need to be able to plugin several different validating classes. If I explicilty instanciate them then I have to write repeating code where the only diffirence is the class that I reflect on.
I don't understand why in the first case I get the exception and in the second I don't. Any ideas?
Thank you