BAEL-4399: corrected the examples as per review

This commit is contained in:
Amitabh Tiwari
2020-10-24 18:59:11 +05:30
parent 71daa1a7c1
commit dcc5d77c1b
5 changed files with 49 additions and 32 deletions
@@ -1,6 +1,6 @@
package main.java.oldclass;
package com.baeldung.exceptions.nosuchfielderror;
public class Dependent {
//This needed to be commented post compilation of NoSuchFielDError and Compile
// This needed to be commented post compilation of NoSuchFielDError and Compile
public static String message = "Hello Baeldung!!";
}
@@ -1,7 +1,21 @@
package main.java.oldclass;
package com.baeldung.exceptions.nosuchfielderror;
import java.lang.reflect.Field;
public class NoSuchFieldError {
public static void main(String[] args) {
System.out.println(Dependent.message);
// String message = "Hello Baeldung!!!";
public static String getMessage() throws ClassNotFoundException, NoSuchFieldException, SecurityException,
IllegalArgumentException, IllegalAccessException {
NoSuchFieldError clss = new NoSuchFieldError();
Class<?> aClass = Class.forName(clss.getClass().getName());
Field field = aClass.getDeclaredField("message");
field.setAccessible(true);
String msgStr = (String) field.get(clss);
return msgStr;
}
public static String getDependentMessage() {
return Dependent.message;
}
}
@@ -1,20 +0,0 @@
package main.java.reflection;
import java.lang.reflect.Field;
public class NoSuchFieldError2 {
//String message = "Hello Baeldung!!!";
public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException {
print();
}
public static void print(){
ValidateNoSuchFieldError clss = new ValidateNoSuchFieldError();
Class<?> aClass = Class.forName(clss.getClass().getName());
Field field = aClass.getDeclaredField("message");
field.setAccessible(true);
String msgStr = (String)field.get(clss);
System.out.println(msgStr);
}
}